Given G, a directed graph, is there a path (not necessarily a simple path) that goes through all the vertices in G?
I first need to examine what happens in an acyclic graph and in a strongly-connected graph, and then to find a solution for a general graph, using the graph of the strongly connected components.
So far I have figured out that for a strongly connected graph there is always a path. For an acyclic graph, if there is more than one source a path will never exist. Also, if there is a vertex for which D out is greater than 1, a path will never exist.
The problem is, I’m not sure that last one is right, and if it’s wrong, my algorithm is wrong.
The last assumption is not true, for example have a look at the graph
G = (V,E), whereE = {(v_i,v_j) | i < j }The graph is obviously a DAG. so finding the maximal strongly connected component won’t change the graph. Also – the graph has a hamiltonian path, howeverd_out(v_1) > 1[assuming|V| > 3].However – you are on the right track.
Algorithm in high level pseudo code:
resulting graph is a DAG.
Claim:
Proof of claim:
<-
if there is a hamiltonian path – then such a path is trivial, for each MSCC – the path goes through all vertices, and then to the out edge that is representing the our edge that was chosen in the hamiltonian path in the MSCC graph.
->
If such a path exists, let it be
v0->v1->...vm.Let’s denote
V_ithe maximal strongly connected component in whichv_ilays.Now, for the path in the original graph
v0->v1->...->vm, there is also a path in the MSCC graph2:V_0->V_1->...->V_m.Note that if
V_iappears twice [or more] in the above path – the two occurances are adjacent to each other, otherwise the MSCC found is not maximal because ifV_i->V_k->...->V_iis feasible path – then V_i and V_k are not maximal, since you can unite them into one bigger SCC.Now, for each
V_icollapse all occurances of it into one, and you get yourself a path – where eachV_iappears at most once [we just showed why], and exactly one [since everyv_iwas on the original path and we did not remove MSCC’s completely – just collapsed them].Thus – the generated path is a hamiltonian path in the MSCC graph.
Proof of correctness of the suggested algrotithm:
Since we showed hamiltonian path in MSCC graph exists if and only if there exists a requested path in the original graph – then:
->
The algorithm returned true -> The algorithm found a Hamiltonian path in the DAG -> There is a Hamiltonian Path in the Dag [foot-note 1] -> There is a path as requested in the original graph.
<-
The algorithm returned false -> The algorithm did not find a Hamiltonian pat in the DAG -> There is no hamiltonian path in the DAG [foot-note 1] -> There is no path as requested in the original path.
Q.E.D.
1: in a DAG, there is a unique topological sort if there exists a hamiltonian path, and if there is a hamiltonian path – it is the order of the vertices in the topological sort. Thus, in a DAG – finding a hamiltonian path is easy.
2: Actually, it is a bit modification,
V_i->V_iis not really an edge on the MSCC, graph but for now let’s denote it as one.