[英]Find all vertices reachable from a vertex of a directed graph in time O(|E| + |V|)
Where |E| | E | denotes the number of edges, |V|
表示边数,| V | the number of vertices.
顶点数。
My idea is to use depth-first search on the given vertex to find all vertices reachable from it. 我的想法是在给定的顶点上使用深度优先搜索来找到所有可到达的顶点。 However as far as my understanding goes, performing depth-first search from only one vertex requires O(1 + out-degree(u)) time, where u is the vertex in question.
但是据我所知,仅从一个顶点执行深度优先搜索需要O(1 + out-degree(u))时间,其中u是所讨论的顶点。
Assuming that depth-first search is the answer, why would I have to perform a full O(|V| + |E|) search? 假设深度优先搜索是答案,为什么我必须执行完整的O(| V | + | E |)搜索?
If you perform only one step of the DFS then it is O(1 + outdegree(v)), nevertheless, that will only get you the vertices that are reachable from v at one step, but not all the vertices that you may reach. 但是,如果仅执行DFS的一个步骤,则它是O(1 + outdegree(v)),这只会使您一步就可以从v获得顶点,但不能获得您可能到达的所有顶点。
Think of the recursion, in order to retrieve all reachable vertices you should perform another recursive DFS search for each reached vertex. 考虑一下递归,为了检索所有可到达的顶点,您应该对每个到达的顶点执行另一个递归DFS搜索。 In the worst case you will reach all vertices and thus you will have the sum of O(1+outdegree(v)) for each vertex, so you will traverse all the edges of the graph and get O(|V|+|E|).
在最坏的情况下,您将到达所有顶点,因此每个顶点的总和为O(1 + outdegree(v)),因此您将遍历图形的所有边并获得O(| V | + | E |)。
Because 因为
(1) you must perform a depth-first search not only in the initial vertex, but also in all vertices that are directly connected to it, and in all vertices those vertices are connected to, and so on. (1)您不仅必须在初始顶点中,而且在与其直接连接的所有顶点以及与这些顶点相连的所有顶点中,都执行深度优先搜索。
(2) in the worst case, all the vertices will be reachable from the initial one, and it will be equivalent to perform a full DFS. (2)在最坏的情况下,所有顶点从初始顶点都是可到达的,并且等效于执行完整的DFS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.