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)
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
"""
import os
GRAPH_PATH = "graphs/"
-CREATE_NEW_GRAPHS = True
+CREATE_NEW_GRAPHS = False
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)
# 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()