Find a vertex that all other vertices can be reached from in a digraph

前端 未结 4 994
傲寒
傲寒 2021-02-15 18:03

Given a directed graph, how can we determine whether or not there exists a vertex v, from which all other vertices are reachable. the algorithm should be as efficient as possibl

4条回答
  •  你的背包
    2021-02-15 18:29

    The solution can be found by taking the concept from Kosaraju's algorithm for Strongly Connected Components. The idea is based on the following fact:

    If there exists a vertex (or vertices), from which all other vertices are reachable, then this vertex would have the maximum finish time in a DFS traversal. So, the solution would look like:

    // Utility function to find mother vertex 
    //(vertex from which all other vertices are reachable)
    
    public void findMotherVertex() {
        int motherVertex=0;
    
        for (int i=0;i

    The above algorithm takes O(V+E) time to solve the problem.

提交回复
热议问题