Friday, 20 October 2006

Finding all paths on undirected graph

There is an easy way to partition the set of s-t paths in a graph G. Fix an edge tt in G. Let P1 be the set of paths from s to t which use the edge tt, and let P2 be the set of paths from s to t in Gtt. Then P1capP2=emptyset and the set of s-t paths P=P1cupP2. Moreover, there is a one to one correspondence between the set of paths P1 and the set of s-t paths in the graph Gt.



Thus, we get an easy recursive algorithm to find the set of paths s-t paths in a graph G. Pick an edge tt incident the vertex t and recursively calculate the sets P1 and P2. With a small amount of pre-processing, we can ensure that the runtime is O(m(p+1)) where m is the number of edges and p is the number of s-t paths.



To make the recurrence relation on the runtime work, consider the following. We can test in time O(m) if a given graph G and pair of vertices s and t if G has 0, exactly one, or at least two distinct s-t paths. To see this, simply find the block decomposition of the graph and, and check if there is any non-trivial block between s and t in the tree.



We can push this slightly farther. Given an instance of the problem G and vertices s and t, we can reduce the problem in time O(m) to a graph barG and vertices x and y such that for all edges xx incident to x, we have that



  1. xx is in some y-x path,

  2. there exists a y-x path in barGx.

To see this, again using the block decomposition, we contract any bridge in the graph and delete edges not contained in any s-t path. As above, this can be done in O(m) time.



We give an O(m(p1)) time algorithm to find the set of s-t paths in a given graph G with at least two s-t paths.



  1. We may assume, as above, that every edge tt incident to t is contained in some s-t path and that there exists at least one s-t path in Gt.

  2. Check if the number of s-t paths in Gtt is at least two, and if not, let P1 be the set of the unique s-t path in Gtt. If there are at least two such paths, we recursively find the set of all such paths. Let p1=|P1|. By choice of tt, p1ge1.

  3. Check if the number of s-t paths in Gt is at least two, and if not let P2 be the set of the unique s-t path in Gt. Otherwise, we recursively find the set P2 of s-t path in Gt. Let p2=|P2|, and as above, we again have p2ge1.

Step 1 can be performed in cm operations for some constant c. The initial check in steps 2 and 3 can be performed in c(m1) steps. If we must recursively run the algorithm, this will require another c(m1)(pi1) operations for i=1,2 in Steps 2 and 3, respectively. As pige1, we can bound the work in each of Steps 2 and 3 by cm+cm(pi1). Thus, the total number of operations is at most 3cm+c(m)(p1+p22)lecm(p1) if we choose cge3c.

No comments:

Post a Comment