Find Weakly Connected Components in a Directed Graph
Weakly Connected Graph:
A directed graph ‘G = (V, E)’ is weakly connected if the underlying undirected graph Ĝ is connected.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
The underlying undirected graph is the graph Ĝ = (V, Ê) where Ê represents the set of undirected edges that is obtained by removing the arrowheads from the directed edges and making them bidirectional in G.
The directed graph G above is weakly connected since its underlying undirected graph Ĝ is connected.
Weakly Connected Component:
Given a directed graph, a weakly connected component (WCC) is a subgraph of the original graph where all vertices are connected to each other by some path, ignoring the direction of edges.
In the above directed graph, there are two weakly connected components:
- [0, 1, 2, 3]
- [4, 5]
Algorithm to find Weakly Connected Component:
It uses the algorithm to find connected components of an undirected graph.
- Construct the underlying undirected graph of the given directed graph.
- Find all the connected components of the undirected graph.
- The connected components of the undirected graph will be the weakly connected components of the directed graph.
Below is the code of Weakly Connected Component which takes a directed graph DG as input and returns all the weakly connected components WCC of the input graph.
Component 1: 0 1 3 2 Component 2: 4 5
Time complexity: O(V+E)