Algorithm for finding a Hamiltonian Path in a DAG
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
A Hamiltonian path in a graph is a path that visits each vertex exactly once. Determining the existence of a Hamiltonian path is a well-known NP-complete problem for arbitrary graphs. However, in the case of Directed Acyclic Graphs (DAGs), there are efficient algorithms for finding such a path due to their structural properties. This article will delve into the algorithmic approach to finding a Hamiltonian path in a DAG, supported with examples and a summary table.
Background
A Directed Acyclic Graph (DAG) is a directed graph with no directed cycles. This property of DAGs guarantees a partial ordering of its vertices, which can be leveraged to find a Hamiltonian path efficiently.
Overview of the Algorithm
Steps to Find a Hamiltonian Path in a DAG
- Topological Sorting: Begin by topologically sorting the DAG. Topological sorting provides a linear ordering of vertices where for every directed edge , vertex comes before . This property is crucial for checking the possibility of a Hamiltonian path efficiently.
- Checking the Path: Once the DAG is topologically sorted, a Hamiltonian path exists if and only if there is a directed edge between every pair of consecutive vertices in the topological order.
Technical Explanation
- Topological Sort: Utilize depth-first search (DFS) or a Kahn's algorithm to perform a topological sort. This step operates in time complexity, where is the number of vertices and is the number of edges in the DAG.
- Evaluating Path Existence: After obtaining a topological ordering, traverse the list of sorted vertices, checking for consecutive edges. If for every consecutive pair in the sorted list, the edge exists, then a Hamiltonian path is confirmed, otherwise, it does not exist.
Pseudocode
- There is an edge from `A` to `B`.
- There is an edge from `B` to `C`.
- There is an edge from `C` to `D`.
- Uniqueness: A Hamiltonian path in a DAG is not guaranteed to be unique. Multiple topological orderings can exist, with potentially different sequences forming a Hamiltonian path.
- Usage in Applications: Hamiltonian paths have vast applications in scheduling problems, genome sequencing, and network routing within the realm of DAGs.
Related reading
- Algorithm for finding all paths in a NxN grid
- Algorithm for finding distinct paths from A to B in weighted, directed, cyclic graph
- Algorithm for finding multiset permutation given lexicographic index
- Algorithm for finding nearby points?
- Algorithm for finding path combinations?
- Algorithm for Finding Redundant Edges in a Graph or Tree
- Algorithm for finding nearest object on 2D grid
- Algorithm for finding similar images

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.