-
Notifications
You must be signed in to change notification settings - Fork 0
/
HamiltonianPath.py
44 lines (35 loc) · 1.14 KB
/
HamiltonianPath.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
import networkx as nx
import sys
def HamiltonianPath(file):
path = open(file)
n = int(path.readline())
connections = path.read().split("\n")
connections = [i.split(" ") for i in connections]
while([""] in connections):
connections.remove([""])
graph = nx.Graph()
for j in range(0, n):
for k in range(1, len(connections[j])):
graph.add_edge(connections[j][0], connections[j][k])
def HamWalk(G, p):
if(len(p) == G.number_of_nodes()):
return p
else:
for vertex in sorted(G.neighbors(p[-1])):
if not (vertex in p):
return HamWalk(G, p+[vertex])
return False
def HamMain(G):
path = False
for vertex in sorted(G.nodes()):
path = HamWalk(G, [vertex])
if path != False:
return path
return path
path = HamMain(graph)
if path == False:
print('No Hamiltonian Path')
else:
print('Hamiltonian Path: ' + ' '.join(path))
file = str(sys.argv[1])
HamiltonianPath(file)