VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-a-mother-vertex-in-a-graph/

⇱ Mother Vertex in a Graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Mother Vertex in a Graph

Last Updated : 5 May, 2026

Given a directed graph with V vertices labeled from 0 to V-1 and a list of edges edges[][], where each edge is represented as [u, v] indicating a directed edge from vertex u to vertex v, find a Mother Vertex of the graph.

A Mother Vertex is a vertex from which all other vertices can be reached.

  • If multiple such vertices exist, return the one with the smallest value.
  • If no such vertex exists, return -1.

Example: 

👁 Mother-Vertex-in-a-Graph

Input: V = 7, edges[][] = [[0, 1], [0, 2], [1, 3], [4, 1], [6, 4], [5, 6], [5, 2], [6, 0]]
Output: 5
Explanation: As shown in the above graph.

Input: V = 5, edges[][] = [[0, 2], [0, 3], [1, 0], [2, 1], [3, 4]]
Output: 0
Explanation: Vertices 0, 1, and 2 can each reach all other vertices in the graph. Among them, 0 is the smallest, so the output is 0.

[Naive Approach] - Using BFS(Breadth First Search) - O(V * (E + V)) Time and O(V) Space

A straightforward (brute-force) approach is to run BFS (or DFS) from every vertex and check whether all other vertices are reachable from it. If such a vertex exists, it is a mother vertex. However, this approach is inefficient, as it requires performing a traversal for each vertex.


[Expected Approach] - Using DFS (Last Finished Vertex Technique) - O(V + E) Time and O(V) Space

The approach is that if a mother vertex exists in a directed graph, it will be the last vertex to finish in a DFS traversal. This is because DFS may start from multiple unvisited vertices, forming a DFS forest. A mother vertex can reach all other vertices, so when DFS eventually starts from it, it will traverse the entire remaining graph in one go and finish last. Any earlier DFS starting points cannot reach all nodes (otherwise they would also be mother vertices). Therefore, the last finished vertex is the only possible candidate, and we finally verify it by checking whether it can reach all vertices.

Consider the following dry run:

V = 7, Edges = {{0, 1}, {0, 2}, {1, 3}, {4, 1}, {6, 4}, {5, 6}, {5, 2}, {6, 0}}

  • Step 1: Build Adjacency List
    0 --> {1, 2}, 1 --> {3}, 4 --> {1}, 5 --> {2, 6}, 6 --> {0, 4}
  • Step 2: Perform DFS on all vertices
    For v = 0 --> DFS: 0 -> 1 -> 3 -> 2 --> visited = {0, 1, 2, 3} --> candidate = 0
    For v = 1, 2, 3 --> already visited --> skip
    For v = 4 --> DFS: 4 -> 1 (already visited) --> visited = {0, 1, 2, 3, 4} --> candidate = 4
    For v = 5 --> DFS: 5 -> 6 -> 0 (already visited), 6 -> 4(already visited) -->
    visited = {0, 1, 2, 3, 4, 5, 6} --> candidate = 5
    For v = 6 --> already visited --> skip
  • Step 3: Verify the candidate
    Start DFS from vertex 5: visit 5 -> 6 -> 4 -> 1 -> 3 and 6 -> 0 -> 2 ---> visited = {0, 1, 2, 3, 4, 5, 6}
    All vertices are reachable from 5 and hence, 5 is a mother vertex.

Final answer : 5


Output
5
Comment
Article Tags: