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

index 1dbe91a36f494828beef805f01eeb559fb00e573..5354507aa0cac4c0517cfb9281772d7f2a7ce023 100644 (file)
@@ -1,51 +1,43 @@
 from graph import distance, find_MST, calculate_route
 from queue import Queue
-
+from collections import Counter
+from copy import deepcopy
 
 def linker2(points):
-    p = points[:]
-    direct = [p[0][0]]
-    head = p[0][0]
-    current = p[0]
-
     graph = dict()
 
-    for pair in p:
+    for pair in points:
         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]
+    print(graph)
+    q = Queue()
+    q.put([points[0][0]])
     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)
+    while not q.empty():
+        current = q.get()
+
+        if tuple(current) in seen:
+            continue
         else:
-            current = (end, a)
+            seen.add(tuple(current))
 
-        if end == head:
-            break
+        if Counter(current) == Counter(points):
+            return current
+        print(current, points)
+        for i in graph[current[-1]]:
+            temp = deepcopy(current)
+            temp.append(i)
+            q.put(temp)
 
-    return direct
+    return []
 
 def christofides(graph: list):
     _, mst = find_MST(graph)
@@ -91,18 +83,18 @@ def christofides(graph: list):
                         q.put(c)
 
 
-    multigraph = perfect_matching + mst
+    multigraph = mst + perfect_matching
     # print(f"{odd_degree_vertices=}")
     # print(f"{mst=}")
     # print(f"{perfect_matching=}")
     # print(f"{multigraph=}")
 
     eulerian_tour = linker2(multigraph)
-
+    #
     print(eulerian_tour)
-
+    #
     # print(g)
-    return eulerian_tour
+    return perfect_matching
 
 """
 
diff --git a/main.py b/main.py
index 7fabde636483132d39ae9b1494887180d36a6c20..c092c127280abc21cb5e127b96cb279b55b8eb59 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 = True
+CREATE_NEW_GRAPHS = False
 
 
 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, 10)
+        graph, filename = create(GRAPH_PATH, 640, 640, 5)
     else:
         filename = "graph1.txt"
         graph = read(GRAPH_PATH, filename)
@@ -44,10 +44,10 @@ def main():
     # lower_bound, one_tree, removed_vertex = find_lower_bound(graph)
     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="direct")
+    print_info(route, route_time_end - route_time_start, "Christofides Algorithm", lower_bound,
+               one_tree_time_end - one_tree_time_start, r=3000, mode="direct")
 
-    display = Display(os.path.join(GRAPH_PATH, filename), route, mst=MST, one_tree=None,
+    display = Display(os.path.join(GRAPH_PATH, filename), [], mst=route, one_tree=MST,
                       removed_vertex=removed_vertex, mode="direct")
     display.show()