A mother vertex in a Graph G = (V, E) is a vertex v such that a path from v can reach all other vertices in G by a path from v.
We can solve this problem using Depth First Search approach. To further optimize our approach, we will use an efficient solution.
The below solution is also using Depth First Search to solve this problem, but it executes Depth First Search Cycle only once, and as soon as it finds the mother vertex, it stops the execution.
- While executing the Depth First Search, we have a Bitmask Array representing a bitmask for each vertex. This Bitmask Array is passed to all vertices during execution.
- Each vertex modifies their dedicated bitmask in such a way the all set bits representing the vertices can be visited from this vertex, including the current vertex.
- At each iteration, we check if all the vertices can be visited from this vertex by checking the current vertex bitmask value (If bits representing to all vertices are set). If all vertices can be visited from this vertex, then it breaks the execution and finds the mother vertex as the current vertex.
- If Vertex-2 has been visited from Vertex-1, and Vertex-2 has already been visited earlier, then the Vertex-1 updates it’s bitmask by doing OR operation with the Vertex-2 bitmask.
Time complexity: O(V+E)
How does the above idea work?
Let this approach support a graph having a maximum of 32 vertices. This method can be augmented to support more number of vertices as well, but here it is dealt with a maximum of 32 vertices.
- A bitmask array of 32 bit-masks is created. The 0th index of array denotes the Bitmask for Vertex-0, while the 1st index of the array denotes the Bitmask for Vertex-1, and so on.
- All the vertices of the graph are visited using the Depth First Search algorithm, and the same bitmask array is passed to all vertices. The Bitmask values are set accordingly to represent all vertices that could be visited from this vertex.
- Bit Index is set corresponding to the neighbour vertices, including own bit index. Neighbour vertices will also repeat the same for their bitmask and return the same to Vertex-1.
- Vertex-1 will keep on performing the OR operation on the return value of all neighbours to its bitmask to represent all the vertices that could be visited from vertex-1.
- Do note that if the Vertex-2 is a neighbour of the Vertex-1, and it is already visited from another neighbour vertex, then it will not visit its neighbour’s vertex again and returns its bitmask to the first Node.
- Each iteration will check if bitmask corresponding to current vertex has all bits set to 1. If all bits are set to 1 for current vertex means, it means all vertices can be visited from the current vertex, and the current vertex is the mother vertex of the graph.
Below is the implementation of our above approach:
There is an edge from 0 to 2 There is an edge from 0 to 1 There is an edge from 1 to 3 There is an edge from 4 to 1 There is an edge from 5 to 6 There is an edge from 5 to 2 There is an edge from 6 to 4 There is an edge from 6 to 0 Mother vertex is: 5
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find a Mother Vertex in a Graph
- Find the Degree of a Particular vertex in a Graph
- Find dependencies of each Vertex in a Directed Graph
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- All vertex pairs connected with exactly k edges in a graph
- Add and Remove vertex in Adjacency Matrix representation of Graph
- k'th heaviest adjacent node in a graph where each vertex has weight
- Topological Sort of a graph using departure time of vertex
- Add and Remove vertex in Adjacency List representation of Graph
- Finding minimum vertex cover size of a graph using binary search
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices
- Powers of 2 to required sum using Bit Masking
- Find k-cores of an undirected graph
- Find the maximum value permutation of a graph
- Find if an undirected graph contains an independent set of a given size
- Find all cliques of size K in an undirected graph
- Find if there is a path between two vertices in a directed graph
- Find if there is a path between two vertices in a directed graph | Set 2
- Find if there is a path between two vertices in an undirected graph
- Program to find the number of region in Planar Graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.