Find a Mother vertex in a Graph using Bit Masking
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
Time Complexity: O(V ^ 2), where V is the total number of vertices in the graph.
Auxiliary Space: O(V)
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.