简体   繁体   中英

How to find mother vertex in a directed graph in O(n+m)?

A mother vertex in a directed graph G = (V,E) is a vertex v such that all other vertices G can be reached by a directed path from v Give an O(n+m) algorithm to test whether graph G contains a mother vertex.

(c) from Skiena manual

Found only O(n(n+m)) way

When googling I actually found the answer here . If this is homework, you should think twice before peeking :)

Algorithm::

a) Do DFS/BFS of the graph and keep track of the last finished vertex 'x' .

b) If there exist any mother vertex, then 'x' is one of them. Check if 'x' is a mother vertex by doing DFS/BFS from vertex 'x'.

Time Complexity O(n+m) + O(n+m) = O(n+m)

step1 . Do topological sorting of vertices of directed graph.

step2 . Now check whether we can reach all vertices from first vertex of topologically sorted vertices in step 1.

To perform a step 2, again initialize array discovered[i] to false and do dfs startin from first node of topologically sorted vertices.

If all vertices can be reached, then graph has mother vertex, and mother vertex will be the former of topologically sorted vertices.

time complexity: step1 takes O(n + m) , step 2 takes O(n + m) so total O(n+m) + O(n+m) = O(n+m)

I saw the solution. I dont think we need to find SCC. Just do a DFS from a random vertex and then do the DFS from the vertex with last finish time. If there is a mother vertex then it has to be this.

  1. Do topological sorting on the graph. Example : ACDEB
  2. Find if there exists a path from the first node in the topological order to all other nodes 2.a. Initialize the distance from A to all the nodes as infinite and distance from A to A as 0. 2.b. For all the nodes in topological order, Update shortest distance for all adjacent nodes from A.
  3. Loop over all the nodes to see if there is still some infinite distance. If there is an infinite distance, there's no path from A to that node and return false.
  4. If loop over all the nodes is successful, return true.

we can find the mother vertex in O(m+n) using KOSARAJU's algorithm

  1. First find the DFS or BFS from any vertex and track the visited vertex and push it in STACK.
  2. Top element is mother vertex if it can visit the all vertex , so apply again DFS or BFS.

See this link for DFS using recursion and stack to track the visiting time of all vertices

  1. so start from stack top and visit till the stack is not empty . If there is a single vertex that is not visited then there doesn't exis a mother vertex.

Here is the algorithm fo finding the mother vertex in a Graph , G = (VE) :

  1. Do DFS traversal of the given graph. While doing traversal keep track of last finished vertex 'v'. This step takes O(V+E) time.

  2. If there exist mother vertex/vertices, then 'v' must be one (or one of them). Check if v is a mother vertex by doing DFS/BFS from v. This step also takes O(V+E) time.

geeksforgeeks.com的这篇文章是一个很好的起点/整体答案: 在图形中查找母顶点

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM