-
Notifications
You must be signed in to change notification settings - Fork 0
/
eulerian_path.py
65 lines (55 loc) · 1.41 KB
/
eulerian_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import random
edges = []
with open("input.txt", "r") as file:
for line in file:
edges.append(line.strip().split(" -> "))
def get_adjacent_edges(pos, edges):
choices = []
for edge in edges:
if pos == edge[0]:
choices.append(edge[1])
return choices
split_edges = []
for edge in edges:
if len(edge[1]) > 1:
split = edge[1].split(',')
for item in split:
split_edges.append([edge[0], item])
else:
split_edges.append([edge[0], edge[1]])
def check_edges(edges):
start = [x[0] for x in edges]
end = [x[1] for x in edges]
vertices = set(start).union(set(end))
in_dict = {x : 0 for x in vertices}
out_dict = {x : 0 for x in vertices}
for edge in edges:
out_dict[edge[0]] += 1
in_dict[edge[1]] += 1
for vertex in vertices:
if out_dict[vertex] > in_dict[vertex]:
x = vertex
if x:
return x
else:
return random.sample(vertices, 1)
split_edges = [list(map(int, x)) for x in split_edges]
stack = []
circuit = []
pos = check_edges(split_edges)
print(pos)
while len(split_edges) > 0:
choices = get_adjacent_edges(pos, split_edges)
while choices == []:
circuit.append(pos)
pos = stack.pop()
choices = get_adjacent_edges(pos, split_edges)
stack.append(pos)
pos = random.choice(choices)
split_edges.remove([stack[-1], pos])
stack.append(pos)
while len(stack) > 0:
circuit.append(stack.pop())
circuit = list(map(str, circuit))
output = open('output.txt', 'w')
output.write('->'.join(circuit[::-1]))