Finding all paths between a set of vertices in a DAG

Deadly 提交于 2019-12-24 04:21:01

问题


Given a graph G= (V, E) that is:

  1. directed,
  2. acyclic,
  3. non-weighted,
  4. may have more than one edge between two vertices (thus, source and destination are not enough to determine an edge).

And given a set of vertices, let's call them vSet; that contains a vertex vRoot; I need to find ALL paths pSet between vSet elements respecting the following:

  1. any vertex that appears as a source for some path in pSet must be reachable from vRoot.
  2. any path in pSet must has its source and destination from vSet, and must not contain any other vertex of vSet.

I've developed an algorithm similar to BFS, that starts from vRoot (according to 1 above), grow each of the current paths with one edge per an iteration until it reaches another vertex v1 of vSet; then store this reaching path and start growing new set of paths staring from v1.

Here is a pseudo code

output = ∅;
maxLengthPaths = ∅;
1. add all edges that vRoot is there source to maxLengthPaths
2. while size(maxlengthpaths) != ∅ do
  (a) paths := ∅;
  (b) extendedPaths := ∅;
  (c) foreach path p in maxLengthPaths do
    i. if (destination of p in vSet)
      1. add p to output 
      2. for each edge e that destination of p is its source
        A. add e to extendedPaths
    ii. else
      1. add p to paths
    iii. for path p1 in paths 
      1. for each edge that destination of p1  is its source
        A. extend p1 by a edge and add it to extendedPaths
  (d) maxLengthPaths = extendedPaths

Here are my questions: 1. Is this the best way to achieve my task? 2. I was trying to figure out its time complexity; I found that its exponential of pow(maxNumberOfOutGoingEdgesFormAVertex, maxLengthPath). Is this really the complexity?

来源:https://stackoverflow.com/questions/29995965/finding-all-paths-between-a-set-of-vertices-in-a-dag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!