11"""
2- A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
2+ A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
33 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
55 is the 'Hamiltonian path problem', which is NP-complete.
6-
6+
77 Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
88"""
99from typing import List
@@ -18,7 +18,7 @@ def valid_connection(
1818 2. Next vertex should not be in path
1919 If both validations succeeds we return true saying that it is possible to connect this vertices
2020 either we return false
21-
21+
2222 Case 1:Use exact graph as in main function, with initialized values
2323 >>> graph = [[0, 1, 0, 1, 0],
2424 ... [1, 0, 1, 1, 1],
@@ -56,11 +56,11 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
5656 Recursive Step:
5757 2. Iterate over each vertex
5858 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
6060 2.2 Do recursive call and check if going to this vertex solves problem
6161 2.3 if next vertex leads to solution return True
6262 2.4 else backtrack, delete remembered vertex
63-
63+
6464 Case 1: Use exact graph as in main function, with initialized values
6565 >>> graph = [[0, 1, 0, 1, 0],
6666 ... [1, 0, 1, 1, 1],
@@ -111,12 +111,12 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
111111 Wrapper function to call subroutine called util_hamilton_cycle,
112112 which will either return array of vertices indicating hamiltonian cycle
113113 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.
116116 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:
118118 (0)->(1)->(2)->(4)->(3)->(0)
119-
119+
120120 (0)---(1)---(2)
121121 | / \ |
122122 | / \ |
@@ -130,10 +130,10 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
130130 ... [0, 1, 1, 1, 0]]
131131 >>> hamilton_cycle(graph)
132132 [0, 1, 2, 4, 3, 0]
133-
134- Case 2:
133+
134+ Case 2:
135135 Same Graph as it was in Case 1, changed starting index from default to 3
136-
136+
137137 (0)---(1)---(2)
138138 | / \ |
139139 | / \ |
@@ -147,11 +147,11 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
147147 ... [0, 1, 1, 1, 0]]
148148 >>> hamilton_cycle(graph, 3)
149149 [3, 0, 1, 2, 4, 3]
150-
150+
151151 Case 3:
152152 Following Graph is exactly what it was before, but edge 3-4 is removed.
153153 Result is that there is no Hamiltonian Cycle anymore.
154-
154+
155155 (0)---(1)---(2)
156156 | / \ |
157157 | / \ |
0 commit comments