Challenge: Find a "Mother Vertex" in a Graph

Given a graph, can you find a vertex from which all the other vertices are reachable?

Problem Statement #

You have to implement the int findMotherVertex(Graph g) function which will take a graph as input and find out a mother vertex in the graph.

By definition, the mother vertex is one from which all other vertices are reachable.

Input #

A directed graph

Output #

Returns the value of the mother vertex if it exists. Otherwise, it returns -1.

Sample Input #

graph = {
		3 -> 0 
    3 -> 1
    0 -> 1
    1 -> 2

Sample Output #


Level up your interview prep. Join Educative to access 70+ hands-on prep courses.