@@ -28,49 +28,50 @@ def tsp(cost):
2828
2929 """
3030 # create the adjacency list
31- adj = createList (cost )
31+ adj = create_list (cost )
3232
3333 #check for triangle inequality violations
34- if triangleInequality (adj ):
34+ if triangle_inequality (adj ):
3535 print ("Triangle Inequality Violation" )
3636 return - 1
3737
3838 # construct the travelling salesman tour
39- tspTour = approximateTSP (adj )
39+ tsp_tour = approximate_tsp (adj )
4040
4141 # calculate the cost of the tour
42- tspCost = tourCost ( tspTour )
42+ tsp_cost = tour_cost ( tsp_tour )
4343
44- return tspCost
44+ return tsp_cost
4545
4646# function to implement approximate TSP
47- def approximateTSP (adj ):
47+ def approximate_tsp (adj ):
4848 n = len (adj )
4949
5050 # to store the cost of minimum spanning tree
51- mstCost = [0 ]
51+ mst_cost = [0 ]
5252
5353 # stores edges of minimum spanning tree
54- mstEdges = findMST (adj , mstCost )
54+ mst_edges = find_mst (adj , mst_cost )
5555
5656 # to mark the visited nodes
5757 visited = [False ] * n
5858
5959 # create adjacency list for mst
60- mstAdj = [[] for _ in range (n )]
61- for e in mstEdges :
62- mstAdj [e [0 ]].append ([e [1 ], e [2 ]])
63- mstAdj [e [1 ]].append ([e [0 ], e [2 ]])
60+ mst_adj = [[] for _ in range (n )]
61+ mst_edges = find_mst (adj , mst_cost )
62+ for e in mst_edges :
63+ mst_adj [e [0 ]].append ([e [1 ], e [2 ]])
64+ mst_adj [e [1 ]].append ([e [0 ], e [2 ]])
6465
6566 # to store the eulerian tour
6667 tour = []
67- eulerianCircuit ( mstAdj , 0 , tour , visited , - 1 )
68+ eulerian_circuit ( mst_adj , 0 , tour , visited , - 1 )
6869
6970 # add the starting node to the tour
7071 tour .append (0 )
7172
7273 # to store the final tour path
73- tourPath = []
74+ tour_path = []
7475
7576 for i in range (len (tour ) - 1 ):
7677 u = tour [i ]
@@ -84,18 +85,18 @@ def approximateTSP(adj):
8485 break
8586
8687 # add the edge to the tour path
87- tourPath .append ([u , v , weight ])
88+ tour_path .append ([u , v , weight ])
8889
89- return tourPath
90+ return tour_path
9091
91- def tourCost (tour ):
92+ def tour_cost (tour ):
9293 cost = 0
9394 for edge in tour :
9495 cost += edge [2 ]
9596 return cost
9697
9798
98- def eulerianCircuit (adj , u , tour , visited , parent ):
99+ def eulerian_circuit (adj , u , tour , visited , parent ):
99100 visited [u ] = True
100101 tour .append (u )
101102
@@ -105,17 +106,17 @@ def eulerianCircuit(adj, u, tour, visited, parent):
105106 continue
106107
107108 if visited [v ] == False :
108- eulerianCircuit (adj , v , tour , visited , u )
109+ eulerian_circuit (adj , v , tour , visited , u )
109110
110111# function to find the minimum spanning tree
111- def findMST (adj , mstCost ):
112+ def find_mst (adj , mst_cost ):
112113 n = len (adj )
113114
114115 # to marks the visited nodes
115116 visited = [False ] * n
116117
117118 # stores edges of minimum spanning tree
118- mstEdges = []
119+ mst_edges = []
119120
120121 pq = []
121122 heapq .heappush (pq , [0 , 0 , - 1 ])
@@ -130,11 +131,11 @@ def findMST(adj, mstCost):
130131 if visited [u ]:
131132 continue
132133
133- mstCost [0 ] += weight
134+ mst_cost [0 ] += weight
134135 visited [u ] = True
135136
136137 if parent != - 1 :
137- mstEdges .append ([u , parent , weight ])
138+ mst_edges .append ([u , parent , weight ])
138139
139140 for neighbor in adj [u ]:
140141 v = neighbor [0 ]
@@ -144,13 +145,13 @@ def findMST(adj, mstCost):
144145
145146 if not visited [v ]:
146147 heapq .heappush (pq , [w , v , u ])
147- return mstEdges
148+ return mst_edges
148149
149150
150151
151152# function to calculate if the
152153# triangle inequality is violated
153- def triangleInequality (adj ):
154+ def triangle_inequality (adj ):
154155 n = len (adj )
155156
156157 # Sort each adjacency list based
@@ -163,20 +164,20 @@ def triangleInequality(adj):
163164 for u in range (n ):
164165 for x in adj [u ]:
165166 v = x [0 ]
166- costUV = x [1 ]
167+ cost_UV = x [1 ]
167168 for y in adj [v ]:
168169 w = y [0 ]
169- costVW = y [1 ]
170+ cost_VW = y [1 ]
170171 for z in adj [u ]:
171172 if z [0 ] == w :
172- costUW = z [1 ]
173- if (costUV + costVW < costUW ) and (u < w ):
173+ cost_UW = z [1 ]
174+ if (cost_UV + cost_VW < cost_UW ) and (u < w ):
174175 return True
175176 # no violations found
176177 return False
177178
178179# function to create the adjacency list
179- def createList (cost ):
180+ def create_list (cost ):
180181 n = len (cost )
181182
182183 # to store the adjacency list
0 commit comments