I am referring to Skienna's Book on Algorithms.
The problem of testing whether a graph G
contains a Hamiltonian path
is NP-hard
, where a Hamiltonian path P
is a path that visits each vertex exactly once. There does not have to be an edge in G from the ending vertex to the starting vertex of P , unlike in the Hamiltonian cycle problem.
Given a directed acyclic graph G (DAG
), give an O(n + m)
time algorithm to test whether or not it contains a Hamiltonian path.
My approach,
I am planning to use DFS
and Topological sorting
. But I didn't know how to connect the two concepts in solving the problem. How can a topological sort be used to determine the solution.
Any suggestions?