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.
We'll cover the following...
Solution #1: Naive solution
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
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 visited. If the graph is disconnected, the visited array will still have some vertices, which haven’t been set to ...