Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions backtracking/hamiltonian_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,17 @@ def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
path[0] = path[-1] = start_index
# evaluate and if we find answer return path either return empty array
return path if util_hamilton_cycle(graph, path, 1) else []


if __name__ == "__main__":
import doctest

doctest.testmod()
graph = [
[0, 1, 0, 1, 0],
[1, 0, 1, 1, 1],
[0, 1, 0, 0, 1],
[1, 1, 0, 0, 1],
[0, 1, 1, 1, 0],
]
print(hamilton_cycle(graph))