What is a Mother Vertex?
A mother vertex in a graph G = (V,E) is a vertex v such that all other vertices in G can be reached by a path from v.
We strongly recommend you to minimize your browser and try this yourself first.
How to find mother vertex?
- Case 1:- Undirected Connected Graph : In this case, all the vertices are mother vertices as we can reach to all the other nodes in the graph.
- Case 2:- Undirected/Directed Disconnected Graph : In this case, there is no mother vertices as we cannot reach to all the other nodes in the graph.
- Case 3:- Directed Connected Graph : In this case, we have to find a vertex -v in the graph such that we can reach to all the other nodes in the graph through a directed path.
A Naive approach :
A trivial approach will be to perform a DFS/BFS on all the vertices and find whether we can reach all the vertices from that vertex. This approach takes O(V(E+V)) time, which is very inefficient for large graphs.
Can we do better?
We can find a mother vertex in O(V+E) time. The idea is based on Kosaraju’s Strongly Connected Component Algorithm. In a graph of strongly connected components, mother vertices are always vertices of source component in component graph. The idea is based on below fact.
If there exist mother vertex (or vertices), then one of the mother vertices is the last finished vertex in DFS. (Or a mother vertex has the maximum finish time in DFS traversal).
A vertex is said to be finished in DFS if a recursive call for its DFS is over, i.e., all descendants of the vertex have been visited.
How does the above idea work?
Let the last finished vertex be v. Basically, we need to prove that there cannot be an edge from another vertex u to v if u is not another mother vertex (Or there cannot exist a non-mother vertex u such that u-→v is an edge). There can be two possibilities.
- Recursive DFS call is made for u before v. If an edge u-→v exists, then v must have finished before u because v is reachable through u and a vertex finishes after all its descendants.
- Recursive DFS call is made for v before u. In this case also, if an edge u-→v exists, then either v must finish before u (which contradicts our assumption that v is finished at the end) OR u should be reachable from v (which means u is another mother vertex).
- Do DFS traversal of the given graph. While doing traversal keep track of last finished vertex ‘v’. This step takes O(V+E) time.
- If there exist mother vertex (or vetices), then v must be one (or one of them). Check if v is a mother vertex by doing DFS/BFS from v. This step also takes O(V+E) time.
Below is implementation of above algorithm.
A mother vertex is 5
Time Complexity : O(V + E)
This article is contributed by Rachit Belwariar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Find a Mother vertex in a Graph using Bit Masking
- Check if vertex X lies in subgraph of vertex Y for the given Graph
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Find dependencies of each Vertex in a Directed Graph
- All vertex pairs connected with exactly k edges in a graph
- Print all possible paths in a DAG from vertex whose indegree is 0
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Queries to count connected components after removal of a vertex from a Tree
- Find k-cores of an undirected graph
- Find the maximum value permutation of a graph
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Find the maximum component size after addition of each edge to the graph
- Find if there is a path between two vertices in an undirected graph
- Find any simple cycle in an undirected unweighted Graph
- Depth First Search or DFS for a Graph
- Detect Cycle in a Directed Graph
- Check whether a given graph is Bipartite or not
- Bridges in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Biconnected graph
Improved By : ShubhamDixit