# 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.**Example:**

**Approach:**

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:

`// C++ code to find Mother` `// Vertex using Bitmask` ` ` `#include <bits/stdc++.h>` `#include <iostream>` ` ` `using` `namespace` `std;` ` ` `struct` `Graph {` ` ` `int` `V;` ` ` ` ` `// Store in descending order` ` ` `set<` `int` `, greater<` `int` `> >* adjList;` `};` ` ` `Graph* CreateGraph(` `int` `N)` `{` ` ` `Graph* g = ` `new` `Graph();` ` ` `g->V = N;` ` ` `g->adjList` ` ` `= ` `new` `set<` `int` `, greater<` `int` `> >[N];` ` ` `return` `g;` `}` ` ` `void` `AddEdge(` ` ` `Graph* g, ` `int` `src, ` `int` `dest)` `{` ` ` ` ` `g->adjList[src].insert(dest);` `}` ` ` `void` `PrintGraph(Graph* g)` `{` ` ` ` ` `set<` `int` `, greater<` `int` `> >::iterator it;` ` ` ` ` `for` `(` `int` `i = 0; i < g->V; i++) {` ` ` ` ` `for` `(it = g->adjList[i].begin();` ` ` `it != g->adjList[i].end();` ` ` `it++) {` ` ` `cout << ` `"There is an edge from "` ` ` `<< i << ` `" to "` ` ` `<< *it << endl;` ` ` `}` ` ` `}` `}` ` ` `bool` `IsEdge(Graph* g, ` `int` `src, ` `int` `dest)` `{` ` ` `if` `(g->adjList[src].find(dest)` ` ` `!= g->adjList[src].end()) {` ` ` ` ` `return` `true` `;` ` ` `}` ` ` `return` `false` `;` `}` ` ` `int` `MotherVertexUtil(` ` ` `Graph* g, ` `int` `index,` ` ` `int` `* mask, ` `int` `* m_vertex)` `{` ` ` ` ` `// If mother vertex is already found` ` ` `// then simply return with existing` ` ` `// mask of the vertex index` ` ` `if` `(*m_vertex != -1) {` ` ` ` ` `return` `mask[index];` ` ` `}` ` ` ` ` `// if this vertex is already visited,` ` ` `// return the bit-mask` ` ` `// value of this vertex.` ` ` `if` `(mask[index] != 0) {` ` ` `return` `mask[index];` ` ` `}` ` ` ` ` `int` `tmpmask = 0;` ` ` ` ` `// Set the bit corresponding` ` ` `// to vertex index in tmpmask` ` ` `tmpmask |= (1 << index);` ` ` ` ` `for` `(` `int` `i = 0; i < g->V; i++) {` ` ` `if` `((index != i)` ` ` `&& IsEdge(g, index, i)) {` ` ` ` ` `// Set bits corresponding to all` ` ` `// vertex which can be visite` ` ` `// by this vertex by ORing` ` ` `// the return value by util function` ` ` ` ` `// Vertex is not visited` ` ` `if` `(mask[i] == 0) {` ` ` ` ` `int` `retmask` ` ` `= MotherVertexUtil(` ` ` `g, i, mask, m_vertex);` ` ` `tmpmask |= retmask;` ` ` `}` ` ` ` ` `// Vertex is already visited` ` ` `else` `{` ` ` `tmpmask |= mask[i];` ` ` `}` ` ` ` ` `// Check if current vertex is` ` ` `// mother vertex or mother vertex` ` ` `// is already found` ` ` `if` `(tmpmask == (` `pow` `(2, g->V) - 1)) {` ` ` ` ` `// If all bits of a mask is set` ` ` `// it means current vertex` ` ` `// is mother vertex` ` ` `if` `(*m_vertex == -1) {` ` ` `*m_vertex = index;` ` ` `}` ` ` `return` `tmpmask;` ` ` `}` ` ` `}` ` ` `}` ` ` ` ` `// populate tmpmask as final` ` ` `// bit mask of the vertex` ` ` `mask[index] |= tmpmask;` ` ` ` ` `return` `mask[index];` `}` ` ` `int` `MotherVertex(Graph* g)` `{` ` ` ` ` `int` `v = g->V;` ` ` `int` `* mask = ` `new` `int` `(v);` ` ` ` ` `// Initially bit mask` ` ` `// for all vertex will be 0` ` ` `for` `(` `int` `i = 0; i < v; i++) {` ` ` `mask[i] = 0;` ` ` `}` ` ` ` ` `// DFS traversal is used to check` ` ` `// for the mother vertex` ` ` `// All set bits (of bitmask of a vertex)` ` ` `// represent the current vertex index` ` ` `// and index of vertices which could be` ` ` `// visited from the current vertex.` ` ` ` ` `/* Example:` ` ` `If a vertex index is 3` ` ` `then and vertex 5, 7 and 10` ` ` `can be visited from this vertex` ` ` `then final bit mask of this vertex` ` ` `would be` ` ` `00000000000000000000010010101000` ` ` `(bits 3, 5, 7 and 10 are set) */` ` ` ` ` `// tmpmask is used to store` ` ` `// the final bitmask of the vertex.` ` ` `int` `tmpmask = 0;` ` ` ` ` `// flag to check if` ` ` `// mother vertex is found` ` ` `int` `m_vertex = -1;` ` ` ` ` `for` `(` `int` `index = 0; index < v; index++) {` ` ` ` ` `// set the bit corresponding` ` ` `// to vertex index in tmpmask` ` ` `tmpmask = (1 << index);` ` ` ` ` `// mask for a vertex is 0` ` ` `// means it has not yet` ` ` `// visited so visit this vertex` ` ` `if` `(mask[index] == 0) {` ` ` ` ` `int` `retmask` ` ` `= MotherVertexUtil(` ` ` `g, index,` ` ` `mask, &m_vertex);` ` ` ` ` `// set bits corresponding to all` ` ` `// vertices which can be visited` ` ` `// from this vertex by ORing` ` ` `// the return value by util function` ` ` `tmpmask |= retmask;` ` ` `}` ` ` ` ` `// check if current vertex is` ` ` `// mother vertex or mother vertex` ` ` `// is already found` ` ` `// If all bits of a mask is set` ` ` `// it means current vertex` ` ` `// is mother vertex` ` ` `if` `(tmpmask == (` `pow` `(2, v) - 1)) {` ` ` ` ` `// current vertex is mother vertex` ` ` `if` `(m_vertex == -1) {` ` ` `m_vertex = index;` ` ` `}` ` ` `break` `;` ` ` `}` ` ` ` ` `// populate tmpmask as final bit` ` ` `// mask of the vertex` ` ` `mask[index] |= tmpmask;` ` ` `}` ` ` ` ` `return` `m_vertex;` `}` ` ` `// Driver code` `int` `main()` `{` ` ` ` ` `Graph* g = CreateGraph(7);` ` ` `AddEdge(g, 0, 2);` ` ` `AddEdge(g, 0, 1);` ` ` `AddEdge(g, 1, 3);` ` ` `AddEdge(g, 4, 1);` ` ` `AddEdge(g, 5, 2);` ` ` `AddEdge(g, 5, 6);` ` ` `AddEdge(g, 6, 0);` ` ` `AddEdge(g, 6, 4);` ` ` `PrintGraph(g);` ` ` ` ` `int` `m_vertex = MotherVertex(g);` ` ` `if` `(m_vertex == -1) {` ` ` ` ` `cout << ` `"Mother vertex is not"` ` ` `<< ` `" existing in this graph"` ` ` `<< endl;` ` ` `}` ` ` `else` `{` ` ` `cout << ` `"Mother vertex is: "` ` ` `<< m_vertex << endl;` ` ` `}` ` ` `return` `0;` `}` |

**Output:**

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

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****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.