From d3d010f80435d194f0d23de1db9f3127513ce486 Mon Sep 17 00:00:00 2001 From: raisaaajose Date: Wed, 22 Oct 2025 20:02:48 +0530 Subject: [PATCH 1/6] TSP under Graphs --- graphs/travelling_salesman.py | 206 ++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 graphs/travelling_salesman.py diff --git a/graphs/travelling_salesman.py b/graphs/travelling_salesman.py new file mode 100644 index 000000000000..6439bff3b3a3 --- /dev/null +++ b/graphs/travelling_salesman.py @@ -0,0 +1,206 @@ +import heapq + +def tsp(cost): + """ + https://www.geeksforgeeks.org/dsa/approximate-solution-for-travelling-salesman-problem-using-mst/ + + Problem definition: + Given a 2d matrix cost[][] of size n where cost[i][j] denotes the cost of moving from city i to city j. + The task is to complete a tour from city 0 to all other towns such that we visit each city exactly once + and then return to city 0 at minimum cost. + + Both the Naive and Dynamic Programming solutions for this problem are infeasible. + In fact, there is no polynomial time solution available for this problem as it is a known NP-Hard problem. + + There are approximate algorithms to solve the problem though; for example, the Minimum Spanning Tree (MST) based + approximation algorithm defined below which gives a solution that is at most twice the cost of the optimal solution. + + Assumptions: + 1. The graph is complete. + + 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j + directly from i, rather than through some other vertex k) + + 3. The cost matrix is symmetric, i.e., cost[i][j] = cost[j][i] + + Time complexity: O(n ^ 3), the time complexity of triangleInequality() function is O(n ^ 3) as we are using 3 nested loops. + Space Complexity: O(n ^ 2), to store the adjacency list, and creating MST. + + """ + # create the adjacency list + adj = createList(cost) + + #check for triangle inequality violations + if triangleInequality(adj): + print("Triangle Inequality Violation") + return -1 + + # construct the travelling salesman tour + tspTour = approximateTSP(adj) + + # calculate the cost of the tour + tspCost = tourCost(tspTour) + + return tspCost + +# function to implement approximate TSP +def approximateTSP(adj): + n = len(adj) + + # to store the cost of minimum spanning tree + mstCost = [0] + + # stores edges of minimum spanning tree + mstEdges = findMST(adj, mstCost) + + # to mark the visited nodes + visited = [False] * n + + # create adjacency list for mst + mstAdj = [[] for _ in range(n)] + for e in mstEdges: + mstAdj[e[0]].append([e[1], e[2]]) + mstAdj[e[1]].append([e[0], e[2]]) + + # to store the eulerian tour + tour = [] + eulerianCircuit(mstAdj, 0, tour, visited, -1) + + # add the starting node to the tour + tour.append(0) + + # to store the final tour path + tourPath = [] + + for i in range(len(tour) - 1): + u = tour[i] + v = tour[i + 1] + weight = 0 + + # find the weight of the edge u -> v + for neighbor in adj[u]: + if neighbor[0] == v: + weight = neighbor[1] + break + + # add the edge to the tour path + tourPath.append([u, v, weight]) + + return tourPath + +def tourCost(tour): + cost = 0 + for edge in tour: + cost += edge[2] + return cost + + +def eulerianCircuit(adj, u, tour, visited, parent): + visited[u] = True + tour.append(u) + + for neighbor in adj[u]: + v = neighbor[0] + if v == parent: + continue + + if visited[v] == False: + eulerianCircuit(adj, v, tour, visited, u) + +# function to find the minimum spanning tree +def findMST(adj, mstCost): + n = len(adj) + + # to marks the visited nodes + visited = [False] * n + + # stores edges of minimum spanning tree + mstEdges = [] + + pq = [] + heapq.heappush(pq, [0, 0, -1]) + + while pq: + current = heapq.heappop(pq) + + u = current[1] + weight = current[0] + parent = current[2] + + if visited[u]: + continue + + mstCost[0] += weight + visited[u] = True + + if parent != -1: + mstEdges.append([u, parent, weight]) + + for neighbor in adj[u]: + v = neighbor[0] + if v == parent: + continue + w = neighbor[1] + + if not visited[v]: + heapq.heappush(pq, [w, v, u]) + return mstEdges + + + +# function to calculate if the +# triangle inequality is violated +def triangleInequality(adj): + n = len(adj) + + # Sort each adjacency list based + # on the weight of the edges + for i in range(n): + adj[i].sort(key=lambda a: a[1]) + + # check triangle inequality for each + # triplet of nodes (u, v, w) + for u in range(n): + for x in adj[u]: + v = x[0] + costUV = x[1] + for y in adj[v]: + w = y[0] + costVW = y[1] + for z in adj[u]: + if z[0] == w: + costUW = z[1] + if (costUV + costVW < costUW) and (u < w): + return True + # no violations found + return False + +# function to create the adjacency list +def createList(cost): + n = len(cost) + + # to store the adjacency list + adj = [[] for _ in range(n)] + + for u in range(n): + for v in range(n): + # if there is no edge between u and v + if cost[u][v] == 0: + continue + # add the edge to the adjacency list + adj[u].append([v, cost[u][v]]) + + return adj + + + +if __name__ == "__main__": + #test + cost = [ + [0, 1000, 5000], + [5000, 0, 1000], + [1000, 5000, 0] + ] + + print(tsp(cost)) + \ No newline at end of file From 831a50b4d9880a58e611a81fa859841b497c6573 Mon Sep 17 00:00:00 2001 From: raisaaajose Date: Wed, 22 Oct 2025 20:06:38 +0530 Subject: [PATCH 2/6] Revert "TSP under Graphs" This reverts commit d3d010f80435d194f0d23de1db9f3127513ce486. --- graphs/travelling_salesman.py | 206 ---------------------------------- 1 file changed, 206 deletions(-) delete mode 100644 graphs/travelling_salesman.py diff --git a/graphs/travelling_salesman.py b/graphs/travelling_salesman.py deleted file mode 100644 index 6439bff3b3a3..000000000000 --- a/graphs/travelling_salesman.py +++ /dev/null @@ -1,206 +0,0 @@ -import heapq - -def tsp(cost): - """ - https://www.geeksforgeeks.org/dsa/approximate-solution-for-travelling-salesman-problem-using-mst/ - - Problem definition: - Given a 2d matrix cost[][] of size n where cost[i][j] denotes the cost of moving from city i to city j. - The task is to complete a tour from city 0 to all other towns such that we visit each city exactly once - and then return to city 0 at minimum cost. - - Both the Naive and Dynamic Programming solutions for this problem are infeasible. - In fact, there is no polynomial time solution available for this problem as it is a known NP-Hard problem. - - There are approximate algorithms to solve the problem though; for example, the Minimum Spanning Tree (MST) based - approximation algorithm defined below which gives a solution that is at most twice the cost of the optimal solution. - - Assumptions: - 1. The graph is complete. - - 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j - directly from i, rather than through some other vertex k) - - 3. The cost matrix is symmetric, i.e., cost[i][j] = cost[j][i] - - Time complexity: O(n ^ 3), the time complexity of triangleInequality() function is O(n ^ 3) as we are using 3 nested loops. - Space Complexity: O(n ^ 2), to store the adjacency list, and creating MST. - - """ - # create the adjacency list - adj = createList(cost) - - #check for triangle inequality violations - if triangleInequality(adj): - print("Triangle Inequality Violation") - return -1 - - # construct the travelling salesman tour - tspTour = approximateTSP(adj) - - # calculate the cost of the tour - tspCost = tourCost(tspTour) - - return tspCost - -# function to implement approximate TSP -def approximateTSP(adj): - n = len(adj) - - # to store the cost of minimum spanning tree - mstCost = [0] - - # stores edges of minimum spanning tree - mstEdges = findMST(adj, mstCost) - - # to mark the visited nodes - visited = [False] * n - - # create adjacency list for mst - mstAdj = [[] for _ in range(n)] - for e in mstEdges: - mstAdj[e[0]].append([e[1], e[2]]) - mstAdj[e[1]].append([e[0], e[2]]) - - # to store the eulerian tour - tour = [] - eulerianCircuit(mstAdj, 0, tour, visited, -1) - - # add the starting node to the tour - tour.append(0) - - # to store the final tour path - tourPath = [] - - for i in range(len(tour) - 1): - u = tour[i] - v = tour[i + 1] - weight = 0 - - # find the weight of the edge u -> v - for neighbor in adj[u]: - if neighbor[0] == v: - weight = neighbor[1] - break - - # add the edge to the tour path - tourPath.append([u, v, weight]) - - return tourPath - -def tourCost(tour): - cost = 0 - for edge in tour: - cost += edge[2] - return cost - - -def eulerianCircuit(adj, u, tour, visited, parent): - visited[u] = True - tour.append(u) - - for neighbor in adj[u]: - v = neighbor[0] - if v == parent: - continue - - if visited[v] == False: - eulerianCircuit(adj, v, tour, visited, u) - -# function to find the minimum spanning tree -def findMST(adj, mstCost): - n = len(adj) - - # to marks the visited nodes - visited = [False] * n - - # stores edges of minimum spanning tree - mstEdges = [] - - pq = [] - heapq.heappush(pq, [0, 0, -1]) - - while pq: - current = heapq.heappop(pq) - - u = current[1] - weight = current[0] - parent = current[2] - - if visited[u]: - continue - - mstCost[0] += weight - visited[u] = True - - if parent != -1: - mstEdges.append([u, parent, weight]) - - for neighbor in adj[u]: - v = neighbor[0] - if v == parent: - continue - w = neighbor[1] - - if not visited[v]: - heapq.heappush(pq, [w, v, u]) - return mstEdges - - - -# function to calculate if the -# triangle inequality is violated -def triangleInequality(adj): - n = len(adj) - - # Sort each adjacency list based - # on the weight of the edges - for i in range(n): - adj[i].sort(key=lambda a: a[1]) - - # check triangle inequality for each - # triplet of nodes (u, v, w) - for u in range(n): - for x in adj[u]: - v = x[0] - costUV = x[1] - for y in adj[v]: - w = y[0] - costVW = y[1] - for z in adj[u]: - if z[0] == w: - costUW = z[1] - if (costUV + costVW < costUW) and (u < w): - return True - # no violations found - return False - -# function to create the adjacency list -def createList(cost): - n = len(cost) - - # to store the adjacency list - adj = [[] for _ in range(n)] - - for u in range(n): - for v in range(n): - # if there is no edge between u and v - if cost[u][v] == 0: - continue - # add the edge to the adjacency list - adj[u].append([v, cost[u][v]]) - - return adj - - - -if __name__ == "__main__": - #test - cost = [ - [0, 1000, 5000], - [5000, 0, 1000], - [1000, 5000, 0] - ] - - print(tsp(cost)) - \ No newline at end of file From 1452f5d93b2ba0987c861b18da7d1b800e248e08 Mon Sep 17 00:00:00 2001 From: raisaaajose Date: Wed, 22 Oct 2025 20:10:08 +0530 Subject: [PATCH 3/6] TSP under Graphs --- graphs/travelling_salesman.py | 206 ++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 graphs/travelling_salesman.py diff --git a/graphs/travelling_salesman.py b/graphs/travelling_salesman.py new file mode 100644 index 000000000000..6439bff3b3a3 --- /dev/null +++ b/graphs/travelling_salesman.py @@ -0,0 +1,206 @@ +import heapq + +def tsp(cost): + """ + https://www.geeksforgeeks.org/dsa/approximate-solution-for-travelling-salesman-problem-using-mst/ + + Problem definition: + Given a 2d matrix cost[][] of size n where cost[i][j] denotes the cost of moving from city i to city j. + The task is to complete a tour from city 0 to all other towns such that we visit each city exactly once + and then return to city 0 at minimum cost. + + Both the Naive and Dynamic Programming solutions for this problem are infeasible. + In fact, there is no polynomial time solution available for this problem as it is a known NP-Hard problem. + + There are approximate algorithms to solve the problem though; for example, the Minimum Spanning Tree (MST) based + approximation algorithm defined below which gives a solution that is at most twice the cost of the optimal solution. + + Assumptions: + 1. The graph is complete. + + 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j + directly from i, rather than through some other vertex k) + + 3. The cost matrix is symmetric, i.e., cost[i][j] = cost[j][i] + + Time complexity: O(n ^ 3), the time complexity of triangleInequality() function is O(n ^ 3) as we are using 3 nested loops. + Space Complexity: O(n ^ 2), to store the adjacency list, and creating MST. + + """ + # create the adjacency list + adj = createList(cost) + + #check for triangle inequality violations + if triangleInequality(adj): + print("Triangle Inequality Violation") + return -1 + + # construct the travelling salesman tour + tspTour = approximateTSP(adj) + + # calculate the cost of the tour + tspCost = tourCost(tspTour) + + return tspCost + +# function to implement approximate TSP +def approximateTSP(adj): + n = len(adj) + + # to store the cost of minimum spanning tree + mstCost = [0] + + # stores edges of minimum spanning tree + mstEdges = findMST(adj, mstCost) + + # to mark the visited nodes + visited = [False] * n + + # create adjacency list for mst + mstAdj = [[] for _ in range(n)] + for e in mstEdges: + mstAdj[e[0]].append([e[1], e[2]]) + mstAdj[e[1]].append([e[0], e[2]]) + + # to store the eulerian tour + tour = [] + eulerianCircuit(mstAdj, 0, tour, visited, -1) + + # add the starting node to the tour + tour.append(0) + + # to store the final tour path + tourPath = [] + + for i in range(len(tour) - 1): + u = tour[i] + v = tour[i + 1] + weight = 0 + + # find the weight of the edge u -> v + for neighbor in adj[u]: + if neighbor[0] == v: + weight = neighbor[1] + break + + # add the edge to the tour path + tourPath.append([u, v, weight]) + + return tourPath + +def tourCost(tour): + cost = 0 + for edge in tour: + cost += edge[2] + return cost + + +def eulerianCircuit(adj, u, tour, visited, parent): + visited[u] = True + tour.append(u) + + for neighbor in adj[u]: + v = neighbor[0] + if v == parent: + continue + + if visited[v] == False: + eulerianCircuit(adj, v, tour, visited, u) + +# function to find the minimum spanning tree +def findMST(adj, mstCost): + n = len(adj) + + # to marks the visited nodes + visited = [False] * n + + # stores edges of minimum spanning tree + mstEdges = [] + + pq = [] + heapq.heappush(pq, [0, 0, -1]) + + while pq: + current = heapq.heappop(pq) + + u = current[1] + weight = current[0] + parent = current[2] + + if visited[u]: + continue + + mstCost[0] += weight + visited[u] = True + + if parent != -1: + mstEdges.append([u, parent, weight]) + + for neighbor in adj[u]: + v = neighbor[0] + if v == parent: + continue + w = neighbor[1] + + if not visited[v]: + heapq.heappush(pq, [w, v, u]) + return mstEdges + + + +# function to calculate if the +# triangle inequality is violated +def triangleInequality(adj): + n = len(adj) + + # Sort each adjacency list based + # on the weight of the edges + for i in range(n): + adj[i].sort(key=lambda a: a[1]) + + # check triangle inequality for each + # triplet of nodes (u, v, w) + for u in range(n): + for x in adj[u]: + v = x[0] + costUV = x[1] + for y in adj[v]: + w = y[0] + costVW = y[1] + for z in adj[u]: + if z[0] == w: + costUW = z[1] + if (costUV + costVW < costUW) and (u < w): + return True + # no violations found + return False + +# function to create the adjacency list +def createList(cost): + n = len(cost) + + # to store the adjacency list + adj = [[] for _ in range(n)] + + for u in range(n): + for v in range(n): + # if there is no edge between u and v + if cost[u][v] == 0: + continue + # add the edge to the adjacency list + adj[u].append([v, cost[u][v]]) + + return adj + + + +if __name__ == "__main__": + #test + cost = [ + [0, 1000, 5000], + [5000, 0, 1000], + [1000, 5000, 0] + ] + + print(tsp(cost)) + \ No newline at end of file From c1b0edd48381251521c3419488c29794be63cc86 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 14:47:01 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/travelling_salesman.py | 102 +++++++++++++++++----------------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/graphs/travelling_salesman.py b/graphs/travelling_salesman.py index 6439bff3b3a3..db603ba802af 100644 --- a/graphs/travelling_salesman.py +++ b/graphs/travelling_salesman.py @@ -1,5 +1,6 @@ import heapq + def tsp(cost): """ https://www.geeksforgeeks.org/dsa/approximate-solution-for-travelling-salesman-problem-using-mst/ @@ -18,8 +19,8 @@ def tsp(cost): Assumptions: 1. The graph is complete. - 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j - directly from i, rather than through some other vertex k) + 2. The problem instance satisfies Triangle-Inequality.(The least distant path to reach a vertex j from i is always to reach j + directly from i, rather than through some other vertex k) 3. The cost matrix is symmetric, i.e., cost[i][j] = cost[j][i] @@ -29,65 +30,67 @@ def tsp(cost): """ # create the adjacency list adj = createList(cost) - - #check for triangle inequality violations + + # check for triangle inequality violations if triangleInequality(adj): print("Triangle Inequality Violation") return -1 - + # construct the travelling salesman tour tspTour = approximateTSP(adj) - + # calculate the cost of the tour tspCost = tourCost(tspTour) - + return tspCost + # function to implement approximate TSP def approximateTSP(adj): n = len(adj) - + # to store the cost of minimum spanning tree mstCost = [0] - + # stores edges of minimum spanning tree mstEdges = findMST(adj, mstCost) - + # to mark the visited nodes visited = [False] * n - + # create adjacency list for mst mstAdj = [[] for _ in range(n)] for e in mstEdges: mstAdj[e[0]].append([e[1], e[2]]) mstAdj[e[1]].append([e[0], e[2]]) - + # to store the eulerian tour tour = [] eulerianCircuit(mstAdj, 0, tour, visited, -1) - + # add the starting node to the tour tour.append(0) - + # to store the final tour path tourPath = [] - + for i in range(len(tour) - 1): u = tour[i] v = tour[i + 1] weight = 0 - + # find the weight of the edge u -> v for neighbor in adj[u]: if neighbor[0] == v: weight = neighbor[1] break - + # add the edge to the tour path tourPath.append([u, v, weight]) - + return tourPath + def tourCost(tour): cost = 0 for edge in tour: @@ -98,67 +101,67 @@ def tourCost(tour): def eulerianCircuit(adj, u, tour, visited, parent): visited[u] = True tour.append(u) - + for neighbor in adj[u]: v = neighbor[0] if v == parent: continue - + if visited[v] == False: eulerianCircuit(adj, v, tour, visited, u) - + + # function to find the minimum spanning tree def findMST(adj, mstCost): n = len(adj) - + # to marks the visited nodes visited = [False] * n - + # stores edges of minimum spanning tree mstEdges = [] - + pq = [] heapq.heappush(pq, [0, 0, -1]) - + while pq: current = heapq.heappop(pq) - + u = current[1] weight = current[0] parent = current[2] - + if visited[u]: continue - + mstCost[0] += weight visited[u] = True - + if parent != -1: mstEdges.append([u, parent, weight]) - + for neighbor in adj[u]: v = neighbor[0] if v == parent: continue w = neighbor[1] - + if not visited[v]: heapq.heappush(pq, [w, v, u]) return mstEdges - - -# function to calculate if the + +# function to calculate if the # triangle inequality is violated def triangleInequality(adj): n = len(adj) - - # Sort each adjacency list based + + # Sort each adjacency list based # on the weight of the edges for i in range(n): adj[i].sort(key=lambda a: a[1]) - - # check triangle inequality for each + + # check triangle inequality for each # triplet of nodes (u, v, w) for u in range(n): for x in adj[u]: @@ -174,14 +177,15 @@ def triangleInequality(adj): return True # no violations found return False - + + # function to create the adjacency list def createList(cost): n = len(cost) - + # to store the adjacency list adj = [[] for _ in range(n)] - + for u in range(n): for v in range(n): # if there is no edge between u and v @@ -189,18 +193,12 @@ def createList(cost): continue # add the edge to the adjacency list adj[u].append([v, cost[u][v]]) - + return adj - - + if __name__ == "__main__": - #test - cost = [ - [0, 1000, 5000], - [5000, 0, 1000], - [1000, 5000, 0] - ] - + # test + cost = [[0, 1000, 5000], [5000, 0, 1000], [1000, 5000, 0]] + print(tsp(cost)) - \ No newline at end of file From 038739f3bd2155765a108c82a97c93654ddb2c3c Mon Sep 17 00:00:00 2001 From: raisaaajose Date: Wed, 22 Oct 2025 20:30:10 +0530 Subject: [PATCH 5/6] updated to snake_case --- graphs/travelling_salesman.py | 61 ++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/graphs/travelling_salesman.py b/graphs/travelling_salesman.py index 6439bff3b3a3..245c954cd192 100644 --- a/graphs/travelling_salesman.py +++ b/graphs/travelling_salesman.py @@ -28,49 +28,50 @@ def tsp(cost): """ # create the adjacency list - adj = createList(cost) + adj = create_list(cost) #check for triangle inequality violations - if triangleInequality(adj): + if triangle_inequality(adj): print("Triangle Inequality Violation") return -1 # construct the travelling salesman tour - tspTour = approximateTSP(adj) + tsp_tour = approximate_tsp(adj) # calculate the cost of the tour - tspCost = tourCost(tspTour) + tsp_cost = tour_cost(tsp_tour) - return tspCost + return tsp_cost # function to implement approximate TSP -def approximateTSP(adj): +def approximate_tsp(adj): n = len(adj) # to store the cost of minimum spanning tree - mstCost = [0] + mst_cost = [0] # stores edges of minimum spanning tree - mstEdges = findMST(adj, mstCost) + mst_edges = find_mst(adj, mst_cost) # to mark the visited nodes visited = [False] * n # create adjacency list for mst - mstAdj = [[] for _ in range(n)] - for e in mstEdges: - mstAdj[e[0]].append([e[1], e[2]]) - mstAdj[e[1]].append([e[0], e[2]]) + mst_adj = [[] for _ in range(n)] + mst_edges = find_mst(adj, mst_cost) + for e in mst_edges: + mst_adj[e[0]].append([e[1], e[2]]) + mst_adj[e[1]].append([e[0], e[2]]) # to store the eulerian tour tour = [] - eulerianCircuit(mstAdj, 0, tour, visited, -1) + eulerian_circuit(mst_adj, 0, tour, visited, -1) # add the starting node to the tour tour.append(0) # to store the final tour path - tourPath = [] + tour_path = [] for i in range(len(tour) - 1): u = tour[i] @@ -84,18 +85,18 @@ def approximateTSP(adj): break # add the edge to the tour path - tourPath.append([u, v, weight]) + tour_path.append([u, v, weight]) - return tourPath + return tour_path -def tourCost(tour): +def tour_cost(tour): cost = 0 for edge in tour: cost += edge[2] return cost -def eulerianCircuit(adj, u, tour, visited, parent): +def eulerian_circuit(adj, u, tour, visited, parent): visited[u] = True tour.append(u) @@ -105,17 +106,17 @@ def eulerianCircuit(adj, u, tour, visited, parent): continue if visited[v] == False: - eulerianCircuit(adj, v, tour, visited, u) + eulerian_circuit(adj, v, tour, visited, u) # function to find the minimum spanning tree -def findMST(adj, mstCost): +def find_mst(adj, mst_cost): n = len(adj) # to marks the visited nodes visited = [False] * n # stores edges of minimum spanning tree - mstEdges = [] + mst_edges = [] pq = [] heapq.heappush(pq, [0, 0, -1]) @@ -130,11 +131,11 @@ def findMST(adj, mstCost): if visited[u]: continue - mstCost[0] += weight + mst_cost[0] += weight visited[u] = True if parent != -1: - mstEdges.append([u, parent, weight]) + mst_edges.append([u, parent, weight]) for neighbor in adj[u]: v = neighbor[0] @@ -144,13 +145,13 @@ def findMST(adj, mstCost): if not visited[v]: heapq.heappush(pq, [w, v, u]) - return mstEdges + return mst_edges # function to calculate if the # triangle inequality is violated -def triangleInequality(adj): +def triangle_inequality(adj): n = len(adj) # Sort each adjacency list based @@ -163,20 +164,20 @@ def triangleInequality(adj): for u in range(n): for x in adj[u]: v = x[0] - costUV = x[1] + cost_UV = x[1] for y in adj[v]: w = y[0] - costVW = y[1] + cost_VW = y[1] for z in adj[u]: if z[0] == w: - costUW = z[1] - if (costUV + costVW < costUW) and (u < w): + cost_UW = z[1] + if (cost_UV + cost_VW < cost_UW) and (u < w): return True # no violations found return False # function to create the adjacency list -def createList(cost): +def create_list(cost): n = len(cost) # to store the adjacency list From c5c777a8515678d31d18c26041fe7fcf363a14c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 22 Oct 2025 15:03:36 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/travelling_salesman.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/graphs/travelling_salesman.py b/graphs/travelling_salesman.py index c6efd9781042..73452b8b2474 100644 --- a/graphs/travelling_salesman.py +++ b/graphs/travelling_salesman.py @@ -30,18 +30,18 @@ def tsp(cost): """ # create the adjacency list adj = create_list(cost) - - #check for triangle inequality violations + + # check for triangle inequality violations if triangle_inequality(adj): print("Triangle Inequality Violation") return -1 # construct the travelling salesman tour tsp_tour = approximate_tsp(adj) - + # calculate the cost of the tour tsp_cost = tour_cost(tsp_tour) - + return tsp_cost @@ -51,10 +51,10 @@ def approximate_tsp(adj): # to store the cost of minimum spanning tree mst_cost = [0] - + # stores edges of minimum spanning tree mst_edges = find_mst(adj, mst_cost) - + # to mark the visited nodes visited = [False] * n @@ -64,17 +64,17 @@ def approximate_tsp(adj): for e in mst_edges: mst_adj[e[0]].append([e[1], e[2]]) mst_adj[e[1]].append([e[0], e[2]]) - + # to store the eulerian tour tour = [] eulerian_circuit(mst_adj, 0, tour, visited, -1) - + # add the starting node to the tour tour.append(0) # to store the final tour path tour_path = [] - + for i in range(len(tour) - 1): u = tour[i] v = tour[i + 1] @@ -88,9 +88,10 @@ def approximate_tsp(adj): # add the edge to the tour path tour_path.append([u, v, weight]) - + return tour_path + def tour_cost(tour): cost = 0 for edge in tour: @@ -109,7 +110,8 @@ def eulerian_circuit(adj, u, tour, visited, parent): if visited[v] == False: eulerian_circuit(adj, v, tour, visited, u) - + + # function to find the minimum spanning tree def find_mst(adj, mst_cost): n = len(adj) @@ -119,7 +121,7 @@ def find_mst(adj, mst_cost): # stores edges of minimum spanning tree mst_edges = [] - + pq = [] heapq.heappush(pq, [0, 0, -1]) @@ -132,13 +134,13 @@ def find_mst(adj, mst_cost): if visited[u]: continue - + mst_cost[0] += weight visited[u] = True if parent != -1: mst_edges.append([u, parent, weight]) - + for neighbor in adj[u]: v = neighbor[0] if v == parent: @@ -148,7 +150,6 @@ def find_mst(adj, mst_cost): if not visited[v]: heapq.heappush(pq, [w, v, u]) return mst_edges - # function to calculate if the