Skip to content

Commit 0d0cd33

Browse files
authored
Merge pull request #410 from Manasi2001/issue-409
Hamiltonian Cycle (Python)
2 parents a2d4f83 + 56fffdf commit 0d0cd33

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

Backtracking/Hamiltonian_Cycle.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'''
2+
Hamiltonian Path: a Hamiltonian path (or traceable path) is a path
3+
in an undirected or directed graph that visits each vertex exactly
4+
once. A Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian
5+
path that is a cycle.
6+
Aim: To determine the existance of a Hamiltonian Cycle in the provided
7+
undirected graph and return the path if such path exist.
8+
The nodes are numbered from 1 to N.
9+
10+
'''
11+
12+
from collections import defaultdict
13+
14+
15+
def Move_next_node(n, graph, pos, ans):
16+
# Base Case: If all the node is visited:
17+
if pos == n:
18+
19+
# Check wether the last node and the first node are
20+
# connected in order to form a Cycle
21+
if ans[0] in graph[ans[-1]]:
22+
return ans + [ans[0]]
23+
24+
return False
25+
26+
current = ans[-1]
27+
28+
# Check for each of the adjoining vertices
29+
for i in graph[current]:
30+
31+
# Check wether the node is already visited or not
32+
if i not in ans:
33+
ans += [i]
34+
temp = Move_next_node(n, graph, pos + 1, ans)
35+
if temp:
36+
return temp
37+
38+
ans.pop()
39+
40+
return False
41+
42+
43+
def Hamiltonial_Cycle(n, graph):
44+
# To keep a track of already visited node and the answer
45+
# We will start exploring the graph from Vertex 1
46+
answer = [1]
47+
48+
# Start Exploring adjoining node/vertex
49+
return Move_next_node(n, graph, 1, answer)
50+
51+
52+
# ------------------------DRIVER CODE ------------------------
53+
if __name__ == "__main__":
54+
n, m = map(int, input("Enter the number of vertex and edges: ").split())
55+
print("Enter the edges: ")
56+
graph = defaultdict(list)
57+
for i in range(m):
58+
a, b = map(int, input().split())
59+
graph[a] += [b]
60+
graph[b] += [a]
61+
ans = Hamiltonial_Cycle(n, graph)
62+
if not ans:
63+
print("Hamiltonian Cycle is not possible in the Given graph")
64+
else:
65+
print("Hamiltonian Cycle is possible in the graph")
66+
print("The path is : ", *ans)
67+
68+
'''
69+
70+
Sample Working:
71+
72+
5----1------2
73+
/\ \ /
74+
/ \ \ /
75+
/ \ \/
76+
3------6-----4
77+
Enter the number of vertex and edges: 6 8
78+
Enter the edges
79+
5 1
80+
1 2
81+
1 4
82+
5 6
83+
6 4
84+
4 2
85+
3 6
86+
5 3
87+
Hamiltonian Cycle is possible in the graph
88+
The path is : 1 5 3 6 4 2 1
89+
5----1------2
90+
\ \
91+
\ \
92+
\ \
93+
3------6-----4
94+
Enter the number of vertex and edges: 6 6
95+
Enter the edges:
96+
5 1
97+
3 6
98+
6 4
99+
1 2
100+
5 6
101+
1 4
102+
Hamiltonian Cycle is not possible in the Given graph
103+
104+
COMPLEXITY:
105+
106+
Time Complexity -> O(2^N)
107+
Space Complexity -> O(N)
108+
109+
'''

0 commit comments

Comments
 (0)