Hamiltonian-Path-Finder/hampath.py

50 lines
968 B
Python
Raw Normal View History

2016-03-15 16:17:58 +00:00
#!/usr/bin/python
2016-03-15 16:35:02 +00:00
"""
using: ./hampath.py input_file
input file - connection matrix for a graph
numbers with ; as separator
ex.
0;1;2
1;0;3
2;3;0
"""
2016-03-15 16:17:58 +00:00
import sys
2016-03-15 16:27:36 +00:00
#Initial node for search \ start element in result chain.
start = 0;
2016-03-15 16:17:58 +00:00
def deep (path):
2016-03-15 16:27:36 +00:00
for i in range(0, len(matrix[0])): # len(matrix[0]) - number of iterations
if matrix[path[-1]][i] != 0: # path[-1] - last element in chain
if i not in path:
newPath = list(path) # a = b not equal a = copy(b) >_<
2016-03-15 16:17:58 +00:00
newPath.append(i)
2016-03-15 16:27:36 +00:00
if len(newPath) == len(matrix[0]):
if matrix[newPath[0]][newPath[-1]] != 0:
2016-03-15 16:17:58 +00:00
print newPath
return 1
fin = deep ( newPath )
2016-03-15 16:27:36 +00:00
if fin == 1: # exit if path found
2016-03-15 16:17:58 +00:00
return 1
return 0
f = open (sys.argv[1])
matrix = [ map(int,line.split(';')) for line in f ]
while True:
my_path = [ start ]
res = deep( my_path )
if res == 0 and len(matrix[0]) > start + 1:
start += 1
else:
break