Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a graph is semi-connected or not

A directed graph G = (V, E) is said to be semi-connected if, for all pairs of vertices u, v in V we have u -> v or v-> u path. Give an efficient algorithm to determine whether or not G is semi-connected

like image 454
Dan Avatar asked Jun 04 '15 11:06

Dan


Video Answer


2 Answers

Trivial O(V^3) solution could be to use floyd warshal all-to-all shortest path, but that's an overkill (in terms of time complexity).

It can be done in O(V+E).

Claim:

A DAG is semi connected in a topological sort, for each i, there there is an edge (vi,vi+1)

Proof:

Given a DAG with topological sort v1,v2,...,vn:

If there is no edge (vi,v(i+1)) for some i, then there is also no path (v(i+1),vi) (because it's a topological sort of a DAG), and the graph is not semi connected.

If for every i there is an edge (vi,vi+1), then for each i,j (i < j) there is a path vi->vi+1->...->vj-1->vj, and the graph is semi connected.

From this we can get the algorithm:

  1. Find Maximal SCCs in the graph.
  2. Build the SCC graph G'=(U,E') such that U is a set of SCCs. E'= { (V1,V2) | there is v1 in V1 and v2 in V2 such that (v1,v2) is in E }.
  3. Do topological sort on G'.
  4. Check if for every i, there is edge Vi,V(i+1).

Correctness proof:

If the graph is semi connected, for a pair (v1,v2), such that there is a path v1->...->v2 - Let V1, V2 be their SCCs. There is a path from V1 to V2, and thus also from v1 to v2, since all nodes in V1 and V2 are strongly connected.

If the algorithm yielded true, then for any two given nodes v1, v2 - we know they are in SCC V1 and V2. There is a path from V1 to V2 (without loss of generality), and thus also from v1 to v2.


As a side note, also every semi-connected graph has a root (vertex r that leads to all vertices):

Proof:
Assume there is no root. Define #(v) = |{u | there is a path from v to u}| (number of nodes that has a path from v to them).
Choose a such that #(a) = max{#(v) | for all v}.
a is not a root, so there is some node u that has no path from a to it. Since graph is semi connected, it means there is a path u->...->a. But that means #(u) >= #(a) + 1 (all nodes reachable from a and also u).
Contradiction to maximality of #(a), thus there is a root.

like image 68
amit Avatar answered Sep 28 '22 12:09

amit


Amit's soltuin described completely the most efficient approach. I might just add that one can replace step 4 by checking whether there exists more than one topological order of G'. If yes, then the graph is not semi connected. Otherwise, the graph is semi connected. This can be easily incorporated in Kahn's algorithm for finding topological order of a graph.

Another less efficient solution that works in quadratic time is the following.

First, construct another graph G* which is the reverse of the original graph. Then for each vertex v of G, you run a DFS from v in G and consider the set of reachable nodes as R_v. If R_v != V(G), then run another DFS from v in G* and let the set of reachable nodes be R_v. If the union of R_v and R_v is not V(G) then the graph is not semi connected.

like image 40
Hassan AbouEisha Avatar answered Sep 28 '22 13:09

Hassan AbouEisha