Skip to content

Hamiltonian Cycle #1930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 2, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update hamiltonian_cycle.py
  • Loading branch information
cclauss authored May 2, 2020
commit edc9e7503e2b889f954efd730880435bf1be9d05
7 changes: 2 additions & 5 deletions backtracking/hamiltonian_cycle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import List

"""
A Hamiltonian cycle (Hamiltonian circuit) is a graph cycle
through a graph that visits each node exactly once.
Expand All @@ -8,6 +6,7 @@

Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
"""
from typing import List


def valid_connection(
Expand Down Expand Up @@ -173,6 +172,4 @@ def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
# initialize start and end of path with starting index
path[0] = path[-1] = start_index
# evaluate and if we find answer return path either return empty array
if util_hamilton_cycle(graph, path, 1):
return path
return []
return path if util_hamilton_cycle(graph, path, 1) else []