Hamiltonian paths and cycles #
A graph, given by a list of vertices and a list of edges, is read as a directed graph through its
arcs (u, v) ∈ G.2, or as an undirected graph through Cslib.Complexity.Graph.toSimpleGraph
restricted to the declared vertices (SimpleGraph.induce). In both readings the vertices are the
declared ones: repeated vertex entries count once, and edges with an undeclared endpoint are never
used.
DirHamPathis the set of triples(G, s, t)such that the directed graphGhas a Hamiltonian path fromstotSipser, Section 7.3. Mathlib'sDigraphhas no walks, so the path is a list of vertices (Graph.IsDirHamPath).DirHamCycleis the set of directed graphs with a Hamiltonian cycle (Graph.IsDirHamCycle): a nonempty list of the vertices whose consecutive vertices, read cyclically (Cycle.Chain), are joined by arcs. A single vertex therefore needs a self-loop, and two vertices need arcs both ways.HamPathandHamCycleare the undirected versions, stated with Mathlib'sSimpleGraph.Walk.IsHamiltonianandSimpleGraph.Walk.IsHamiltonianCycle. A Hamiltonian cycle is a cycle, so it has at least three vertices; unlikeSimpleGraph.IsHamiltonian, no convention makes the one-vertex graph Hamiltonian.mem_HamPath_iffandmem_HamCycle_iffrestate them with lists of vertices.
All four problems are in NP. A certificate names an ordering of the distinct declared vertices:
for a vertex list of length m, it is read as one block of m bits for each distinct declared
vertex, and each block names the entry of the vertex list whose position is its number of set bits
(Graph.decodeVertexList). The verifiers check that the decoded list contains every declared
vertex exactly once and nothing else (Graph.verifyOrdering), and then the endpoint, length and
edge conditions. Every ordering has a certificate of length n ^ 2 for instances of encoded
length n. Path instances (G, s, t) are encoded by encPair encGraph (encPair encNat encNat),
cycle instances by encGraph.
Main definitions #
Cslib.Complexity.Graph.IsDirHamPath,Cslib.Complexity.DirHamPath.Cslib.Complexity.Graph.IsDirHamCycle,Cslib.Complexity.DirHamCycle.Cslib.Complexity.HamPath,Cslib.Complexity.HamCycle.Cslib.Complexity.Graph.arcBool,Cslib.Complexity.Graph.chainBool,Cslib.Complexity.Graph.cycleChainBool: executable tests on lists of vertices.Cslib.Complexity.DirHamPath.verifier,Cslib.Complexity.DirHamCycle.verifier,Cslib.Complexity.HamPath.verifier,Cslib.Complexity.HamCycle.verifier.
Main results #
Cslib.Complexity.mem_HamPath_iff: undirected Hamiltonian paths as lists of vertices.Cslib.Complexity.mem_HamCycle_iff: undirected Hamiltonian cycles as lists of vertices.Cslib.Complexity.Graph.mem_DirHamCycle_cons_iff: closing a Hamiltonian path with an arc back to its start gives a Hamiltonian cycle, when no other arc enters the start.Cslib.Complexity.DirHamPath.isVerifier,Cslib.Complexity.DirHamPath.mem_NP:DirHamPath ∈ NP (encPair encGraph (encPair encNat encNat)).Cslib.Complexity.DirHamCycle.isVerifier,Cslib.Complexity.DirHamCycle.mem_NP:DirHamCycle ∈ NP encGraph.Cslib.Complexity.HamPath.isVerifier,Cslib.Complexity.HamPath.mem_NP:HamPath ∈ NP (encPair encGraph (encPair encNat encNat)).Cslib.Complexity.HamCycle.isVerifier,Cslib.Complexity.HamCycle.mem_NP:HamCycle ∈ NP encGraph.
References #
The directed graphs with a Hamiltonian path from s to t
Sipser, Section 7.3.
Instances For
The directed graphs with a Hamiltonian cycle [Kar72].
Instances For
The graphs with a Hamiltonian path from s to t through their declared vertices
Sipser, Theorem 7.55.
Instances For
The graphs with a Hamiltonian cycle through their declared vertices [Kar72].
Instances For
Lists of vertices #
A list is a closed chain exactly when appending its first element gives a chain.
Closing a path into a cycle #
Closing a Hamiltonian path with an arc back to its start. If no arc of G enters the declared
vertex s, then adding the arc (t, s) creates a Hamiltonian cycle exactly when G has a
Hamiltonian path from s to t.
Verifying lists of vertices #
Executable arc test of a directed graph: (u, v) is an edge of G.
Instances For
Testing for an arc in an encoded graph is polynomial-time.
cycleChainBool r decides Cycle.Chain R when r decides R: the empty list passes, and a
one-entry list [a] passes exactly when R a a.
Polynomial-time verification #
Testing whether r holds between consecutive entries of a computed list is polynomial-time,
for an input-dependent polynomial-time test r.
Testing whether r holds between cyclically consecutive entries of a computed list is
polynomial-time, for an input-dependent polynomial-time test r.
Membership in NP #
The verifier for DirHamPath: decode a list of vertices from the certificate
(Graph.decodeVertexList) and check that it lists every declared vertex exactly once, starts at
s, ends at t, and that consecutive vertices are joined by arcs.
Instances For
DirHamPath.verifier verifies DirHamPath with certificates of length n ^ 2 for instances
of encoded length n.
The DirHamPath verifier is polynomial-time.
The verifier for DirHamCycle: decode a list of vertices from the certificate
(Graph.decodeVertexList) and check that it is nonempty, lists every declared vertex exactly
once, and that cyclically consecutive vertices are joined by arcs.
Instances For
DirHamCycle.verifier verifies DirHamCycle with certificates of length n ^ 2 for graphs
of encoded length n.
DirHamCycle belongs to NP.
The verifier for HamPath: decode a list of vertices from the certificate
(Graph.decodeVertexList) and check that it lists every declared vertex exactly once, starts at
s, ends at t, and that consecutive vertices are adjacent.
Instances For
HamPath.verifier verifies HamPath with certificates of length n ^ 2 for instances of
encoded length n.
The HamPath verifier is polynomial-time.
The verifier for HamCycle: decode a list of vertices from the certificate
(Graph.decodeVertexList) and check that it has at least three vertices, lists every declared
vertex exactly once, and that cyclically consecutive vertices are adjacent.
Instances For
HamCycle.verifier verifies HamCycle with certificates of length n ^ 2 for graphs of
encoded length n.