**Introduction**

The problem states that we are given a graph G, and there are E edges and V vertices. Mother Vertex is defined as that vertex v, from which all other vertices of the graph are reachable. There can be zero, one, or many mother vertex in the given graph, we need to find all the mother vertices.

Letâ€™s Discuss the sample example to understand the problem better.

**Sample Examples **

**Example 1:**

**Input: **Given graph

**Output: **0 10 40

**Explanation: **You can check from the given graph, that all nodes are reachable from nodes 0, 10, and 40.

**Example 2:**

**Input: **Given graph** **

**Output: **1 2 3 4 5

**Explanation: **You can reach any node, from any node, so all are mother vertices.

**Naive approach **

The idea is simple, for every node, either use BFS and __DFS Algorithm__ to check whether all other nodes are reachable from a given node or not.

### Implementation in C++

```
// recursive function to find the sum of depths of all subtree
#include<bits/stdc++.h>
using namespace std;
// calling the dfs function for every node
void dfs(int sv, vector<vector<int>>&adj, vector<bool>&vis){
vis[sv] = true;
for(auto it : adj[sv]){
if(!vis[it]) dfs(it, adj, vis);
}
return;
}
// checking if all the nodes are visited are not
bool allVisited(vector<bool>&vis){
for(int i=0;i<vis.size();i++){
if(!vis[i]) return false;
}
return true;
}
int main(){
int v = 5;
vector<vector<int>>adj(v);
// graph given in sample case 2
adj[0].push_back(1);
adj[1].push_back(2);
adj[2].push_back(3);
adj[3].push_back(4);
adj[4].push_back(0);
// array to store the mother vertices
vector<int>motherV;
for(int i=0;i<v;i++){
// intiliaze the visited vector
// to keep check if the node is visited or not
vector<bool>vis(v, false);
// calling the dfs function
dfs(i, adj, vis);
// checking if all the nodes are visited or not
if(allVisited(vis)) motherV.push_back(i);
}
// finally printing the number of mother vertices
cout << "Total Number of mother vertices are : " << motherV.size() << endl;
// printing the mother vertices
for(auto it : motherV) cout << it << " ";
cout << endl;
}
```

**Output: **

```
Total Number of mother vertices are: 5
0 1 2 3 4
```

#### Complexity Analysis

**Time Complexity: O(V(V*E))**

**Explanation:** You need to traverse the complete graph for every vertex V.

**Space Complexity: O(V)**

Explanation: Recursive stack space will be equal to a number of vertices V.