-
Notifications
You must be signed in to change notification settings - Fork 0
/
eulerian.py
65 lines (46 loc) · 1.33 KB
/
eulerian.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 2 01:15:43 2019
@author: bjwil
"""
import random
import copy
sign = '->'
answer = '6->8->7->9->6->5->4->2->1->0->3->2->6'
with open ('Eulerian_Cycle.txt', 'r') as in_file:
lines = in_file.read().split('\n')
#s = lines[1]
#n = int(lines[0])
edict1 = {}
for connection in lines:
connection = connection.replace(" ", "")
edict1[connection.split('->')[0]] = [v for v in connection.split('->')[1].split(',')]
def generate_edges(graph):
edges = []
for node in graph:
for neighbour in graph[node]:
edges.append((node, neighbour))
return edges
graph = generate_edges(edict1)
def cycle(edict):
dict_copy = copy.deepcopy(edict)
path = ''
#start = start_key
start = random.choice(list(dict_copy.keys()))
path = path + (start + sign)
while start in dict_copy:
next_ = random.choice(dict_copy[start])
path = path + (next_ + sign)
dict_copy[start].remove(next_)
if dict_copy[start] == []:
dict_copy.pop(start, 'already popped')
start = next_
path = path[:-2]
return bool(dict_copy), path, dict_copy, edict
test = cycle(edict1)
while test[0]:
test = cycle(edict1)
final = test[1]
print(test[1])
with open('Output8.txt', 'w') as f:
f.write(final)