Challenge: Find a "Mother Vertex" in a Directed Graph

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

Problem statement

You have to implement the find_mother_vertex() function which will take a directed graph as an input and find out which vertex is the mother vertex in the graph.

By definition, the mother vertex is a vertex in a graph such that all other vertices in a graph can be reached by following a path from that vertex. A graph can have multiple mother vertices, but you only need to find one.

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

3

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