Question

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.

Was it helpful?

Solution

The last assumption is not true, for example have a look at the graph G = (V,E), where E = {(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, however d_out(v_1) > 1 [assuming |V| > 3].

However - you are on the right track.

Algorithm in high level pseudo code:

  1. find Maximal Strongly Connected Components in the graph - the resulting graph is a DAG.
  2. Use topological sort on the resulting graph1.
  3. Check if the ordered sorting creates a hamiltonian path
  4. if it is - return true, else return false

Claim:

A path with all vertices exists if and only if the DAG representing the MSCC of the graph has hamiltonian path

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_i the maximal strongly connected component in which v_i lays.
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_i appears twice [or more] in the above path - the two occurances are adjacent to each other, otherwise the MSCC found is not maximal because if V_i->V_k->...->V_i is feasible path - then V_i and V_k are not maximal, since you can unite them into one bigger SCC.
Now, for each V_i collapse all occurances of it into one, and you get yourself a path - where each V_i appears at most once [we just showed why], and exactly one [since every v_i was 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_i is not really an edge on the MSCC, graph but for now let's denote it as one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top