11import heapq
22
3+
34def tsp (cost ):
45 """
56 https://www.geeksforgeeks.org/dsa/approximate-solution-for-travelling-salesman-problem-using-mst/
@@ -18,8 +19,8 @@ def tsp(cost):
1819 Assumptions:
1920 1. The graph is complete.
2021
21- 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j
22- directly from i, rather than through some other vertex k)
22+ 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j
23+ directly from i, rather than through some other vertex k)
2324
2425 3. The cost matrix is symmetric, i.e., cost[i][j] = cost[j][i]
2526
@@ -34,7 +35,7 @@ def tsp(cost):
3435 if triangle_inequality (adj ):
3536 print ("Triangle Inequality Violation" )
3637 return - 1
37-
38+
3839 # construct the travelling salesman tour
3940 tsp_tour = approximate_tsp (adj )
4041
@@ -43,10 +44,11 @@ def tsp(cost):
4344
4445 return tsp_cost
4546
47+
4648# function to implement approximate TSP
4749def approximate_tsp (adj ):
4850 n = len (adj )
49-
51+
5052 # to store the cost of minimum spanning tree
5153 mst_cost = [0 ]
5254
@@ -55,7 +57,7 @@ def approximate_tsp(adj):
5557
5658 # to mark the visited nodes
5759 visited = [False ] * n
58-
60+
5961 # create adjacency list for mst
6062 mst_adj = [[] for _ in range (n )]
6163 mst_edges = find_mst (adj , mst_cost )
@@ -69,21 +71,21 @@ def approximate_tsp(adj):
6971
7072 # add the starting node to the tour
7173 tour .append (0 )
72-
74+
7375 # to store the final tour path
7476 tour_path = []
7577
7678 for i in range (len (tour ) - 1 ):
7779 u = tour [i ]
7880 v = tour [i + 1 ]
7981 weight = 0
80-
82+
8183 # find the weight of the edge u -> v
8284 for neighbor in adj [u ]:
8385 if neighbor [0 ] == v :
8486 weight = neighbor [1 ]
8587 break
86-
88+
8789 # add the edge to the tour path
8890 tour_path .append ([u , v , weight ])
8991
@@ -99,41 +101,41 @@ def tour_cost(tour):
99101def eulerian_circuit (adj , u , tour , visited , parent ):
100102 visited [u ] = True
101103 tour .append (u )
102-
104+
103105 for neighbor in adj [u ]:
104106 v = neighbor [0 ]
105107 if v == parent :
106108 continue
107-
109+
108110 if visited [v ] == False :
109111 eulerian_circuit (adj , v , tour , visited , u )
110112
111113# function to find the minimum spanning tree
112114def find_mst (adj , mst_cost ):
113115 n = len (adj )
114-
116+
115117 # to marks the visited nodes
116118 visited = [False ] * n
117-
119+
118120 # stores edges of minimum spanning tree
119121 mst_edges = []
120122
121123 pq = []
122124 heapq .heappush (pq , [0 , 0 , - 1 ])
123-
125+
124126 while pq :
125127 current = heapq .heappop (pq )
126-
128+
127129 u = current [1 ]
128130 weight = current [0 ]
129131 parent = current [2 ]
130-
132+
131133 if visited [u ]:
132134 continue
133135
134136 mst_cost [0 ] += weight
135137 visited [u ] = True
136-
138+
137139 if parent != - 1 :
138140 mst_edges .append ([u , parent , weight ])
139141
@@ -142,24 +144,24 @@ def find_mst(adj, mst_cost):
142144 if v == parent :
143145 continue
144146 w = neighbor [1 ]
145-
147+
146148 if not visited [v ]:
147149 heapq .heappush (pq , [w , v , u ])
148150 return mst_edges
149151
150152
151-
152- # function to calculate if the
153+
154+ # function to calculate if the
153155# triangle inequality is violated
154156def triangle_inequality (adj ):
155157 n = len (adj )
156-
157- # Sort each adjacency list based
158+
159+ # Sort each adjacency list based
158160 # on the weight of the edges
159161 for i in range (n ):
160162 adj [i ].sort (key = lambda a : a [1 ])
161-
162- # check triangle inequality for each
163+
164+ # check triangle inequality for each
163165 # triplet of nodes (u, v, w)
164166 for u in range (n ):
165167 for x in adj [u ]:
@@ -175,33 +177,28 @@ def triangle_inequality(adj):
175177 return True
176178 # no violations found
177179 return False
178-
180+
181+
179182# function to create the adjacency list
180183def create_list (cost ):
181184 n = len (cost )
182-
185+
183186 # to store the adjacency list
184187 adj = [[] for _ in range (n )]
185-
188+
186189 for u in range (n ):
187190 for v in range (n ):
188191 # if there is no edge between u and v
189192 if cost [u ][v ] == 0 :
190193 continue
191194 # add the edge to the adjacency list
192195 adj [u ].append ([v , cost [u ][v ]])
193-
196+
194197 return adj
195-
196198
197-
199+
198200if __name__ == "__main__" :
199- #test
200- cost = [
201- [0 , 1000 , 5000 ],
202- [5000 , 0 , 1000 ],
203- [1000 , 5000 , 0 ]
204- ]
205-
201+ # test
202+ cost = [[0 , 1000 , 5000 ], [5000 , 0 , 1000 ], [1000 , 5000 , 0 ]]
203+
206204 print (tsp (cost ))
207-
0 commit comments