Class DagHamiltonianPath<V,E>

java.lang.Object
org.jgrapht.alg.tour.HamiltonianPathAlgorithmBase<V,E>
org.jgrapht.alg.tour.DagHamiltonianPath<V,E>
Type Parameters:
V - the graph vertex type
E - the graph edge type
All Implemented Interfaces:
HamiltonianPathAlgorithm<V,E>

public class DagHamiltonianPath<V,E> extends HamiltonianPathAlgorithmBase<V,E>
Polynomial-time Hamiltonian path algorithm for directed acyclic graphs.

Hamiltonian path existence in a directed acyclic graph (DAG) reduces to deciding whether the longest directed path covers all vertices. This implementation computes a topological order, runs the standard longest-path-in-DAG dynamic program over that order, and reconstructs a Hamiltonian path when the longest path's length equals the vertex count.

The longest-path-in-DAG DP is a textbook reduction; see for example CLRS, "Introduction to Algorithms" (4th ed.), section on single-source shortest paths in DAGs, which dualises to longest-path by negating edge weights, and the MIT 6.s078 lecture notes (lecture 17 on dynamic programming, MIT 6.s078, Spring 2022) for the Hamiltonian-path framing.

Total complexity is O(|V| + |E|) time and O(|V|) space.

This class only accepts directed acyclic graphs. Passing a directed graph that contains a cycle causes an IllegalArgumentException wrapping the NotDirectedAcyclicGraphException thrown by TopologicalOrderIterator when the topological pass discovers the cycle; passing an undirected graph or null also fails with an IllegalArgumentException (or NullPointerException respectively). To solve the Hamiltonian path problem on cyclic directed graphs, use BacktrackingHamiltonianPath or HeldKarpHamiltonianPath.

In multigraphs, parallel edges between the same vertex pair do not change the result. The returned path uses an arbitrary representative edge selected via Graph.getEdge(V, V) and is not weight-optimised across parallel edges.

Author:
seilat