-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTugasBB.py
More file actions
170 lines (139 loc) · 5.29 KB
/
Copy pathTugasBB.py
File metadata and controls
170 lines (139 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Python3 program to solve
# Traveling Salesman Problem using
# Branch and Bound.
import math
maxsize = float('inf')
# Function to copy temporary solution
# to the final solution
def copyToFinal(curr_path):
final_path[:N + 1] = curr_path[:]
final_path[N] = curr_path[0]
# Function to find the minimum edge cost
# having an end at the vertex i
def firstMin(adj, i):
min = maxsize
for k in range(N):
if adj[i][k] < min and i != k:
min = adj[i][k]
return min
# function to find the second minimum edge
# cost having an end at the vertex i
def secondMin(adj, i):
first, second = maxsize, maxsize
for j in range(N):
if i == j:
continue
if adj[i][j] <= first:
second = first
first = adj[i][j]
elif(adj[i][j] <= second and
adj[i][j] != first):
second = adj[i][j]
return second
# function that takes as arguments:
# curr_bound -> lower bound of the root node
# curr_weight-> stores the weight of the path so far
# level-> current level while moving
# in the search space tree
# curr_path[] -> where the solution is being stored
# which would later be copied to final_path[]
def TSPRec(adj, curr_bound, curr_weight,
level, curr_path, visited):
global final_res
# base case is when we have reached level N
# which means we have covered all the nodes once
if level == N:
# check if there is an edge from
# last vertex in path back to the first vertex
if adj[curr_path[level - 1]][curr_path[0]] != 0:
# curr_res has the total weight
# of the solution we got
curr_res = curr_weight + adj[curr_path[level - 1]]\
[curr_path[0]]
if curr_res < final_res:
copyToFinal(curr_path)
final_res = curr_res
return
# for any other level iterate for all vertices
# to build the search space tree recursively
for i in range(N):
# Consider next vertex if it is not same
# (diagonal entry in adjacency matrix and
# not visited already)
if (adj[curr_path[level-1]][i] != 0 and
visited[i] == False):
temp = curr_bound
curr_weight += adj[curr_path[level - 1]][i]
# different computation of curr_bound
# for level 2 from the other levels
if level == 1:
curr_bound -= ((firstMin(adj, curr_path[level - 1]) +
firstMin(adj, i)) / 2)
else:
curr_bound -= ((secondMin(adj, curr_path[level - 1]) +
firstMin(adj, i)) / 2)
# curr_bound + curr_weight is the actual lower bound
# for the node that we have arrived on.
# If current lower bound < final_res,
# we need to explore the node further
if curr_bound + curr_weight < final_res:
curr_path[level] = i
visited[i] = True
# call TSPRec for the next level
TSPRec(adj, curr_bound, curr_weight,
level + 1, curr_path, visited)
# Else we have to prune the node by resetting
# all changes to curr_weight and curr_bound
curr_weight -= adj[curr_path[level - 1]][i]
curr_bound = temp
# Also reset the visited array
visited = [False] * len(visited)
for j in range(level):
if curr_path[j] != -1:
visited[curr_path[j]] = True
# This function sets up final_path
def TSP(adj):
# Calculate initial lower bound for the root node
# using the formula 1/2 * (sum of first min +
# second min) for all edges. Also initialize the
# curr_path and visited array
curr_bound = 0
curr_path = [-1] * (N + 1)
visited = [False] * N
# Compute initial bound
for i in range(N):
curr_bound += (firstMin(adj, i) +
secondMin(adj, i))
# Rounding off the lower bound to an integer
curr_bound = math.ceil(curr_bound / 2)
# We start at vertex 1 so the first vertex
# in curr_path[] is 0
visited[0] = True
curr_path[0] = 0
# Call to TSPRec for curr_weight
# equal to 0 and level 1
TSPRec(adj, curr_bound, 0, 1, curr_path, visited)
# Driver code
# Adjacency matrix for the given graph
adj = [[0, 12, 8, 0, 15],
[10, 0, 15, 12, 10],
[8, 14, 0, 10, 8],
[12, 17, 10, 0, 11],
[15, 7, 0, 18, 0]
]
N = 5
# final_path[] stores the final solution
# i.e. the // path of the salesman.
final_path = [None] * (N + 1)
# visited[] keeps track of the already
# visited nodes in a particular path
visited = [False] * N
# Stores the final minimum weight
# of shortest tour.
final_res = maxsize
TSP(adj)
print("Minimum cost :", final_res)
print("Path Taken : ", end = ' ')
for i in range(N + 1):
print(final_path[i], end = ' ')
# This code is contributed by ng24_7