Challenge 4: 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

The input is a directed graph.

Output

It 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

3

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