Strongly connected directed-graphs

• Definition: Strongly connected directed graphs

• Definition:

• A directed graph G is strongly connected if:

• For any two vertices u and v:

 There is a directed path u → v,     and      There is a directed path v → u

• Example:

• Naive algorithm to determine strong connectedness in a di-graph G

• The naive algorithm tests if we can reach all nodes starting at a node x

We do this for every node x

• Naive algorithm:

 ``` for ( each node "n" ∈ G ) { Find all nodes x that can be reached by a traveral starting in "n"; if ( # nodes reached != N ) return( G is not strongly connected ); } return( G is strongly connected ); ```

• Running time of the naive algorithm:

 Each graph traversal is O(N2) The naive algorithm performs N graph traversals           Running time of naive algorithm = O(N3)

• A more efficient strong connectedness detection algorithm

• Reverse Path graph:

• The reverse path graph of a directed graph is a graph where the direction of the edges are reverse

• Example:

• Traversing the forward path graph and the reverse path graph:

Observation:

• A node y that is reached by a traversal in the forward path graph starting at node x   <==>   there is a path x → y

(This is pretty obvious)

• A node y that is reached by a traversal in the reverse path graph starting at node x   <==>   there is a path y → x

You can see this fact when you reverse the directions of the edges in the reverse path graph:

Note:

 Node 2 can be reached from node 1 in the reverse path graph If we reverse the direction of the edge, you find the path from 2 → 1

• How to detect a strongly connected directed graph:

• Recall that:

• A directed graph G is strongly connected if:

• For any two vertices u and v:

 There is a directed path u → v,     and      There is a directed path v → u

• A graph G is strongly connected if and only if:

 Every node can be reached from a node u in the forward path graph,      and Every node can be reached from a node u in the reverse path graph

Proof:

• If G is strongly connected then:

• There is a directed path uv, for any node v

Therefore:

 Every node can be reached from a node u in the forward path graph

• There is a directed path vu Therefore:

 Every node can be reached from a node u in the reverse path graph

• The reverse of the statement (claim) is very trivial

• Algorithm to detect strongly connected di-graphs:

 ``` Select an arbitrary node x; A = set of node reached by a DFS/BFS in the forward path graph starting at x; if ( A ≠ all nodes in graph ) { return ( not strongly connected ); } B = set of node reached by a DFS/BFS in the reverse path graph starting at x; if ( B ≠ all nodes in graph ) { return ( not strongly connected ); } return ( strongly connected ); ```