Skip to content

[Model] DirectedHamiltonianPath #813

@isPANN

Description

@isPANN

Motivation

DIRECTED HAMILTONIAN PATH (P1) from Garey & Johnson, Chapter 3, p.60. A fundamental NP-complete graph problem: given a directed graph G = (V, A), does there exist a Hamiltonian path, i.e., an ordering of all vertices such that consecutive vertices in the ordering are connected by arcs? This is the directed counterpart of the undirected Hamiltonian Path problem, obtained by replacing each edge {u,v} with two arcs (u,v) and (v,u). NP-completeness follows directly from the undirected version.

Associated rules:

  • R145: DIRECTED HAMILTONIAN PATH -> NO-WAIT FLOW-SHOP SCHEDULING (establishes NP-completeness of no-wait flow-shop)
  • R194: DIRECTED HAMILTONIAN PATH -> SQUARE-TILING (establishes NP-completeness of bounded Wang tiling)

Definition

Name: DirectedHamiltonianPath

Canonical name: Directed Hamiltonian Path; also: Hamiltonian Path in Digraphs, D-HAM-PATH
Reference: Garey & Johnson, Computers and Intractability, Chapter 3, p.60

Mathematical definition:

INSTANCE: A directed graph G = (V, A), where V is a set of vertices and A is a set of ordered pairs of vertices (arcs).
QUESTION: Does G contain a Hamiltonian path, i.e., an ordering of V as <v_1, v_2, ..., v_n>, where n = |V|, such that (v_i, v_{i+1}) in A for 1 <= i < n?

This is a feasibility (decision) problem: the answer is yes or no.

Variables

  • Count: n = |V| (one variable per vertex, representing its position in the path)
  • Per-variable domain: {0, 1, ..., n-1} -- the position of vertex v in the Hamiltonian path ordering
  • Meaning: pi(v) in {0, ..., n-1} assigns each vertex a unique position in the path. The path is valid iff for every consecutive pair (v at position i, w at position i+1), the arc (v, w) exists in A.

ILP alternative: n^2 binary variables x_{v,k} where x_{v,k} = 1 if vertex v occupies position k. Constraints: each vertex at exactly one position, each position has exactly one vertex, and consecutive positions must correspond to arcs in A.

Schema (data type)

Type name: DirectedHamiltonianPath
Variants: none (operates on a general directed graph)

Field Type Description
graph DirectedGraph The directed graph G = (V, A)

Notes:

  • This is a feasibility (decision) problem: Value = Or (satisfiability-style).
  • Key getter methods: num_vertices() (= |V|), num_arcs() (= |A|), delegated from the DirectedGraph field.
  • The undirected HamiltonianPath is a special case: each undirected edge {u, v} corresponds to two arcs (u, v) and (v, u).

Complexity

  • Decision complexity: NP-complete. The undirected Hamiltonian Path problem can be reduced to the directed version by replacing each edge {u,v} with arcs (u,v) and (v,u). NP-completeness follows from the undirected case (Karp, 1972 via Hamiltonian Circuit, then to path).
  • Best known exact algorithm: O(n^2 * 2^n) time and O(n * 2^n) space via Held-Karp dynamic programming (Bellman, 1962; Held & Karp, 1962). The algorithm computes, for each subset S of vertices and endpoint v in S, whether there is a directed path visiting exactly the vertices in S and ending at v. Unlike the undirected case, no sub-2^n algorithm is known for directed Hamiltonian path.
  • References:
    • R. Bellman (1962). "Dynamic programming treatment of the travelling salesman problem." J. ACM 9(1), pp. 61-63.
    • M. Held, R.M. Karp (1962). "A dynamic programming approach to sequencing problems." J. SIAM 10(1), pp. 196-210.

Specialization

  • Generalization of: Hamiltonian Path (undirected) -- the undirected version is a special case where arcs come in symmetric pairs.
  • Special case of: Directed Hamiltonian Circuit -- a Hamiltonian path is a Hamiltonian circuit minus one arc; standard reductions exist between the two (add a new vertex connected to all others).
  • Known tractable cases: Tournaments (complete directed graphs) always have a Hamiltonian path (Ore, 1960); DAGs have a Hamiltonian path iff they have a unique topological ordering.

Extra Remark

Full book text:

All three Hamiltonian problems mentioned so far also remain NP-complete if we replace the undirected graph G by a directed graph and replace the undirected Hamiltonian circuit or path by a directed Hamiltonian circuit or path. Recall that a directed graph G = (V, A) consists of a vertex set V and a set of ordered pairs of vertices called arcs. A Hamiltonian path in a directed graph G = (V, A) is an ordering of V as <v_1, v_2, . . . , v_n>, where n = |V|, such that (v_i, v_{i+1}) ∈ A for 1 ≤ i < n. A Hamiltonian circuit has the additional requirement that (v_n, v_1) ∈ A. Each of the three undirected Hamiltonian problems can be transformed to its directed counterpart simply by replacing each edge {u, v} in the given undirected graph by the two arcs (u, v) and (v, u). In essence, the undirected versions are merely special cases of their directed counterparts.

How to solve

  • It can be solved by (existing) bruteforce. (Enumerate all n! permutations of vertices; check if each consecutive pair has an arc.)
  • It can be solved by reducing to integer programming. (ILP with binary variables x_{v,k} = 1 if vertex v is at position k; constraints: each vertex at exactly one position, each position has exactly one vertex, and for each consecutive position pair (k, k+1), the selected vertices must have an arc between them. Companion rule issue to be filed separately.)
  • Other: Held-Karp DP in O(n^2 * 2^n).

Example Instance

Input:
G = (V, A) with V = {0, 1, 2, 3, 4, 5} (n = 6 vertices)
A = {(0,1), (0,3), (1,3), (1,4), (2,0), (2,4), (3,2), (3,5), (4,5), (5,1)}

Hamiltonian path: <0, 1, 3, 2, 4, 5>
Check: (0,1) in A, (1,3) in A, (3,2) in A, (2,4) in A, (4,5) in A. All 6 vertices visited exactly once.
Answer: YES.

Note: The path ordering is non-trivial -- vertex 3 must come before vertex 2 despite 2 < 3 in natural order. A greedy approach following (0,1), (1,4), ... would reach 4 -> 5 -> 1 and revisit vertex 1, failing. The correct path requires backtracking from vertex 1 to vertex 3 rather than vertex 4.

Negative example:
Remove arcs (3,2) and (5,1) from the positive example.
G' = (V, A') with A' = {(0,1), (0,3), (1,3), (1,4), (2,0), (2,4), (3,5), (4,5)}.

Vertex 2 has no incoming arcs in G', so any Hamiltonian path must start at vertex 2. From vertex 2, the only outgoing arcs lead to 0 or 4:

  • 2→0→1→3→5→? (must reach 4, but no arc from 5 to 4)
  • 2→0→1→4→5→? (must reach 3, but no arc from 5 to 3)
  • 2→0→3→5→? (must reach 1 and 4, but no outgoing arcs from 5 to either)
  • 2→4→5→? (no outgoing arcs from 5 to unvisited vertices)
    Exhaustive search confirms no directed Hamiltonian path exists. Answer: NO.
Python validation script
from itertools import permutations

def find_ham_paths(n, arcs):
    arc_set = set(arcs)
    paths = []
    for perm in permutations(range(n)):
        if all((perm[i], perm[i+1]) in arc_set for i in range(n-1)):
            paths.append(list(perm))
    return paths

# Positive example
arcs_pos = [(0,1),(0,3),(1,3),(1,4),(2,0),(2,4),(3,2),(3,5),(4,5),(5,1)]
paths_pos = find_ham_paths(6, arcs_pos)
assert [0,1,3,2,4,5] in paths_pos
assert len(paths_pos) == 5
print(f"Positive: {len(paths_pos)} Hamiltonian paths")

# Negative example
arcs_neg = [(0,1),(0,3),(1,3),(1,4),(2,0),(2,4),(3,5),(4,5)]
paths_neg = find_ham_paths(6, arcs_neg)
assert len(paths_neg) == 0
print("Negative: No Hamiltonian path (correct)")

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoodAn issue passed all checks.modelA model problem to be implemented.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions