]> Skullheadx's Git Forge - The-Traveling-Salesman-Problem.git/commitdiff
christofides upd
authorSkullheadx <704277@pdsb.net>
Sun, 8 Jan 2023 02:04:38 +0000 (21:04 -0500)
committerSkullheadx <704277@pdsb.net>
Sun, 8 Jan 2023 02:04:38 +0000 (21:04 -0500)
Christofides.py
main.py

index 352b1b9b734ec9f4c1bc2c0a985bfaabb02ff1ba..1dbe91a36f494828beef805f01eeb559fb00e573 100644 (file)
@@ -1,9 +1,53 @@
-from graph import distance, find_MST, calculate_route, linker
+from graph import distance, find_MST, calculate_route
 from queue import Queue
 
 
+def linker2(points):
+    p = points[:]
+    direct = [p[0][0]]
+    head = p[0][0]
+    current = p[0]
+
+    graph = dict()
+
+    for pair in p:
+        start, end = pair
+        if start in graph:
+            graph[start].append(end)
+        else:
+            graph[start] = [end]
+        if end in graph:
+            graph[end].append(start)
+        else:
+            graph[end] = [start]
+    seen = set()
+    seen.add(head)
+    while True:
+        start, end = current
+        direct.append(end)
+        if len(graph[end]) > 2:
+            a = start
+            for ind, i in enumerate(graph[end]):
+                if i != a and ((len(direct) == len(points) - 1 and i == head) or i not in seen):
+                    b = i
+                    seen.add(i)
+                    break
+        else:
+            a, b = graph[end]
+            if b in seen:
+                b = head
+            seen.add(b)
+        if a == start:
+            current = (end, b)
+        else:
+            current = (end, a)
+
+        if end == head:
+            break
+
+    return direct
+
 def christofides(graph: list):
-    route = []
     _, mst = find_MST(graph)
     g = {town: [] for town in graph}
     for i in mst:
@@ -47,21 +91,18 @@ def christofides(graph: list):
                         q.put(c)
 
 
-    multigraph = mst + perfect_matching
-    print(f"{odd_degree_vertices=}")
-
-    print(f"{mst=}")
-    print()
-    print(f"{perfect_matching=}")
-    print()
-    print(f"{multigraph=}")
+    multigraph = perfect_matching + mst
+    # print(f"{odd_degree_vertices=}")
+    # print(f"{mst=}")
+    # print(f"{perfect_matching=}")
+    # print(f"{multigraph=}")
 
-    eulerian_tour = linker(multigraph)
+    eulerian_tour = linker2(multigraph)
 
     print(eulerian_tour)
 
-    print(g)
-    return mst
+    print(g)
+    return eulerian_tour
 
 """
 
@@ -72,3 +113,4 @@ def christofides(graph: list):
 ((300, 438), (24, 97)), 
 ((396, 559), (141, 520)), 3
 ((24, 97), (490, 227))"""
+
diff --git a/main.py b/main.py
index c2eb29426d559f6becd270b9d9a0bc2eff8b03c9..7fabde636483132d39ae9b1494887180d36a6c20 100644 (file)
--- a/main.py
+++ b/main.py
@@ -8,7 +8,7 @@ from time import perf_counter
 import os
 
 GRAPH_PATH = "graphs/"
-CREATE_NEW_GRAPHS = False
+CREATE_NEW_GRAPHS = True
 
 
 def main():
@@ -23,7 +23,7 @@ def main():
                     print("The file does not exist")
 
     if CREATE_NEW_GRAPHS:
-        graph, filename = create(GRAPH_PATH, 640, 640, 5)
+        graph, filename = create(GRAPH_PATH, 640, 640, 10)
     else:
         filename = "graph1.txt"
         graph = read(GRAPH_PATH, filename)
@@ -45,9 +45,9 @@ def main():
     one_tree_time_end = perf_counter()
 
     print_info(route, route_time_end - route_time_start, "Christofides Algorithm", lower_bound,
-               one_tree_time_end - one_tree_time_start, r=3000, mode="points")
+               one_tree_time_end - one_tree_time_start, r=3000, mode="direct")
 
-    display = Display(os.path.join(GRAPH_PATH, filename), [], mst=MST, one_tree=route,
+    display = Display(os.path.join(GRAPH_PATH, filename), route, mst=MST, one_tree=None,
                       removed_vertex=removed_vertex, mode="direct")
     display.show()