1
1
"""
2
- A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
2
+ A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
3
3
through a graph that visits each node exactly once.
4
- Determining whether such paths and cycles exist in graphs
4
+ Determining whether such paths and cycles exist in graphs
5
5
is the 'Hamiltonian path problem', which is NP-complete.
6
-
6
+
7
7
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
8
8
"""
9
9
from typing import List
@@ -18,7 +18,7 @@ def valid_connection(
18
18
2. Next vertex should not be in path
19
19
If both validations succeeds we return true saying that it is possible to connect this vertices
20
20
either we return false
21
-
21
+
22
22
Case 1:Use exact graph as in main function, with initialized values
23
23
>>> graph = [[0, 1, 0, 1, 0],
24
24
... [1, 0, 1, 1, 1],
@@ -56,11 +56,11 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
56
56
Recursive Step:
57
57
2. Iterate over each vertex
58
58
Check if next vertex is valid for transiting from current vertex
59
- 2.1 Remember next vertex as next transition
59
+ 2.1 Remember next vertex as next transition
60
60
2.2 Do recursive call and check if going to this vertex solves problem
61
61
2.3 if next vertex leads to solution return True
62
62
2.4 else backtrack, delete remembered vertex
63
-
63
+
64
64
Case 1: Use exact graph as in main function, with initialized values
65
65
>>> graph = [[0, 1, 0, 1, 0],
66
66
... [1, 0, 1, 1, 1],
@@ -111,12 +111,12 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
111
111
Wrapper function to call subroutine called util_hamilton_cycle,
112
112
which will either return array of vertices indicating hamiltonian cycle
113
113
or an empty list indicating that hamiltonian cycle was not found.
114
- Case 1:
115
- Following graph consists of 5 edges.
114
+ Case 1:
115
+ Following graph consists of 5 edges.
116
116
If we look closely, we can see that there are multiple Hamiltonian cycles.
117
- For example one result is when we iterate like:
117
+ For example one result is when we iterate like:
118
118
(0)->(1)->(2)->(4)->(3)->(0)
119
-
119
+
120
120
(0)---(1)---(2)
121
121
| / \ |
122
122
| / \ |
@@ -130,10 +130,10 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
130
130
... [0, 1, 1, 1, 0]]
131
131
>>> hamilton_cycle(graph)
132
132
[0, 1, 2, 4, 3, 0]
133
-
134
- Case 2:
133
+
134
+ Case 2:
135
135
Same Graph as it was in Case 1, changed starting index from default to 3
136
-
136
+
137
137
(0)---(1)---(2)
138
138
| / \ |
139
139
| / \ |
@@ -147,11 +147,11 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
147
147
... [0, 1, 1, 1, 0]]
148
148
>>> hamilton_cycle(graph, 3)
149
149
[3, 0, 1, 2, 4, 3]
150
-
150
+
151
151
Case 3:
152
152
Following Graph is exactly what it was before, but edge 3-4 is removed.
153
153
Result is that there is no Hamiltonian Cycle anymore.
154
-
154
+
155
155
(0)---(1)---(2)
156
156
| / \ |
157
157
| / \ |
0 commit comments