Friday, 20 October 2006

Finding all paths on undirected graph

There is an easy way to partition the set of ss-tt paths in a graph GG. Fix an edge tttt in GG. Let P1P1 be the set of paths from ss to tt which use the edge tttt, and let P2P2 be the set of paths from ss to tt in GttGtt. Then P1capP2=emptysetP1capP2=emptyset and the set of ss-tt paths P=P1cupP2P=P1cupP2. Moreover, there is a one to one correspondence between the set of paths P1P1 and the set of ss-tt paths in the graph GtGt.



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



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



We can push this slightly farther. Given an instance of the problem GG and vertices ss and tt, we can reduce the problem in time O(m)O(m) to a graph barGbarG and vertices xx and yy such that for all edges xxxx incident to xx, we have that



  1. xxxx is in some yy-xx path,

  2. there exists a yy-xx path in barGxbarGx.

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



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



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

  2. Check if the number of ss-tt paths in GttGtt is at least two, and if not, let P1P1 be the set of the unique ss-tt path in GttGtt. 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