How to find mother vertex in a directed graph in Onm?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding the Mother Vertex in a Directed Graph
A mother vertex in a directed graph is characterized by its ability to reach every other vertex in the graph through a directed path. Identifying such a vertex can be crucial for understanding graph connectivity and structure. In this article, we will delve into a method to find a mother vertex in a directed graph using an efficient approach with a time complexity of , where is the number of vertices and is the number of edges.
Concepts and Definitions
- Directed Graph: A set of vertices connected by edges, where the direction of the edge indicates the path.
- Mother Vertex: A vertex `$v$
\is a mother vertex if there is a path from $``v$` to every other vertex in the graph.
Algorithm to find the Mother Vertex
To find a mother vertex in linear time using Depth-First Search (DFS), we can follow these steps:
- Identify a Potential Mother Vertex:
- Perform a DFS traversal of the graph while keeping track of the last finished vertex. The idea here is that a mother vertex is always among the last completed vertices in DFS of the original graph. This is because, if we start from a mother vertex, DFS should visit all vertices in the graph.
- Verify the Potential Mother Vertex:
- Conduct another DFS starting from the last finished vertex from step 1. If this DFS visits all vertices, then it is a mother vertex. Otherwise, no mother vertex exists in the graph.
Here's a detailed breakdown of the process:
Steps and Explanation
Step 1: Identify a Candidate Mother Vertex
- Initialize a boolean array `visited[]` to keep track of visited vertices.
- Perform DFS for each unvisited vertex. Track the last vertex that finishes processing in a variable, say `lastFinish`.
- 0 -> 2
- 2 -> 3
- 3 -> 1
- 1 -> 4
- Why Last Finish is Important: If any vertex can reach all others, a DFS from this vertex would finish processing last. Therefore, tracking the last finished vertex potentially reduces unnecessary checks.
- Graph Representation: The adjacency list representation is generally preferred for sparse graphs, contributing to the efficiency of DFS.
Related reading
- How to find multidimensional path of exact 0 cost with 1, 0, -1 weights
- How to find nth element from the end of a singly linked list?
- How to find out Geometric Median
- How to find out if an item is present in a stdvector?
- How to find MySQL process list and to kill those processes?
- How to find pythagorean triplets in an array faster than ON2?
- How to find optimum combination for Cutting Stock Problem using Knapsack
- How to find overall CPU usage in a multi-tenant environment?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.