Solution Review: Find a "Mother Vertex" in a Graph
This review provides a detailed analysis of the different ways to solve the "Find a 'Mother Vertex' in a Graph" challenge.
Solution #1: Naive solution
Press + to interact
C#
Files
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace chapter_5{class Challenge_4_1{static int performDFS(Graph g, int source) {int num_of_vertices = g.getVertices();int vertices_reached = 0; //To store how many vertices reached starting from source//Bool Array to hold the history of visited nodes//Make a node visited whenever you push it into stackbool [] visited = new bool[num_of_vertices];//Create Stack(Implemented in previous lesson) for Depth First Traversal and Push source in itStack <int>stack = new Stack<int>();stack.Push(source);visited[source] = true;//Traverse while stack is not emptyint current_node;while (stack.Count != 0) {//Pop a vertex/node from stackcurrent_node = stack.Pop();//Get adjacent vertices to the current_node from the array,//and if only push unvisited adjacent vertices into stackLinkedList.Node temp = (g.getArray())[current_node].GetHead();while (temp != null) {if (!visited[temp.data]) {stack.Push(temp.data);visited[temp.data] = true;vertices_reached++;}temp = temp.nextElement;}} //end of whilevisited = null;return vertices_reached + 1; //+1 to include the source itself.}static int findMotherVertex(Graph g){//Traverse the Graph Array and perform DFS operation on each vertex//The vertex whose DFS Traversal results is equal to the total number//of vertices in graph is a mother vertexint num_of_vertices_reached = 0;for (int i = 0; i < g.getVertices(); i++) {num_of_vertices_reached = performDFS(g, i);if (num_of_vertices_reached == g.getVertices()){return i;}}return - 1;}static void Main(string[] args){Graph g = new Graph(4);g.addEdge(0, 1);g.addEdge(1, 2);g.addEdge(3, 0);g.addEdge(3, 1);Console.WriteLine(findMotherVertex(g));}}}
This is the brute force approach for solving this problem. Run a DFS on each vertex using DFS
, and keep track of the number of vertices visited in the search. If it is equal to g.vertices
, then that vertex can reach all the vertices and, hence, it is a mother vertex.
The algorithm would also work with a BFS using a queue.
Time complexity
Since you run a DFS on each node, the time complexity is O(V(V + E)).
Solution #2: Last finished vertex
Press + to interact
C#
Files
using System;using System.Collections.Generic;namespace chapter_5{class Challenge_4_2{//A recursive function to print DFS starting from vstatic void DFS(Graph g, int node, bool [] visited){// Mark the current node as visited and print itvisited[node] = true;// Recur for all the vertices adjacent to this vertexLinkedList.Node temp = (g.getArray())[node].GetHead();while (temp != null){if (visited[temp.data] == false)DFS(g, temp.data, visited);temp = temp.nextElement;}}static int findMotherVertex(Graph g){//visited[] is used for DFS. Initially all are// initialized as not visitedbool [] visited = new bool[g.getVertices()];//To store last finished vertex (or mother vertex)int lastV = 0;//Do a DFS traversal and find the last finished vertexfor (int i = 0; i < g.getVertices(); i++){if (visited[i] == false){DFS(g, i, visited);lastV = i;}}// If there exist mother vertex (or vetices) in given graph, then v must be one (or one of them)// Now check if v is actually a mother vertex (or graph has a mother vertex).//We basically check if every vertex is reachable from v or not.//Reset all values in visited[] as false and do//DFS beginning from v to check if all vertices are//reachable from it or not.for (int i = 0; i < g.getVertices(); i++){visited[i] = false;}DFS(g, lastV, visited);for (int i = 0; i < g.getVertices(); i++){if (visited[i] == false)return -1;}// delete[] visited;visited = null;return lastV;}static void Main(string[] args){Graph g = new Graph(4);g.addEdge(0, 1);g.addEdge(1, 2);g.addEdge(3, 0);g.addEdge(3, 1);Console.WriteLine(findMotherVertex(g));}}}
This solution is based in Kosaraju’s Strongly Connected Component Algorithm. Initially, run the DFS on the whole graph in a loop line (17). The DFS ensures that all the nodes in the graph are ...
Access this course and 1400+ top-rated courses and projects.