Presentation on theme: "Reachability as Transitive Closure"— Presentation transcript:
1 Reachability as Transitive Closure Algorithm : Design & Analysis
2 In the last class… Undirected and Symmetric Digraph UDF Search SkeletonBiconnected ComponentsArticulation Points and BiconnectednessBiconnected Component AlgorithmAnalysis of the Algorithm
3 Reachability as Transitive Closure Transitive Closure by DFSTransitive Closure by ShortcutsWashall’s Algorithm for Transitive ClosureAll-Pair Shortest Paths
4 Fundamental Questions For all pair of vertices in a graph, say, u, v:Is there a path from u to v?What is the shortest path from u to v?Reachability as a (reflexive) transitive closure of the adjacency relation, which can be represented as a bit matrix.
5 Computing the Reachability by DFS With each vertex vi, constructing a DFS tree rooted at vi.For each vertex vj encountered, R[i][j] is set to true.At most, m edges are processed.It is possible to set one in more than one row during each DFS. (In fact, if vk is on the path from vi to vj, when vj is encountered, not only R[i][j], but also R[k][j] can be set)
6 Computing Reachability by Condensation Graph Note: the elements of the sub-matrix of R for a strong component are all true.The steps:Find the strong components of G, and get G, the condensation graph of G. ((n+m))Computing reachability for G.Expand the reachability for G to that for G. (O(n2))
7 Transitive Closure by Shortcuts The idea: if there are edges sisk, sksj, then an edge sisj, the “shortcut” is inserted.125341253412534Pass twoPass oneNote: the triple (1,5,3) is considered more than once
8 Transitive Closure by Shortcuts: the algorithm Input: A, an nn boolean matrix that represents a binary relationOutput: R, the boolean matrix for the transitive closure of AProcedurevoid simpleTransitiveClosure(boolean A, int n, boolean R)int i,j,k;Copy A to R;Set all main diagonal entries, rii, to true;while (any entry of R changed during one complete pass)for (i=1; in; i++)for (j=1; jn; j++)for (k=1; kn; k++)rij=rij(rikrkj)The order of (i,j,k) matters
9 Change the order: Washall’s Algorithm void simpleTransitiveClosure(boolean A, int n, boolean R)int i,j,k;Copy A to R;Set all main diagonal entries, rii, to true;while (any entry of R changed during one complete pass)for (k=1; kn; k++)for (i=1; in; i++)for (j=1; jn; j++)rij=rij(rikrkj)k varys in the outmost loopNote: “false to true” can not be reversed
10 Highest-numbered intermediate vertex The highest intermediate vertex in the intervals (sisk), (sksj) are both less than sksjsksiA specific order is assumed for all verticesVertical position of vertices reflect their vertex numbers
11 Correctness of Washall’s Algorithm Notation:The value of rij changes during the execution of the body of the “for k…” loopAfter initializations: rij(0)After the kth time of execution: rij(k)
12 Correctness of Washall’s Algorithm If there is a simple path from si to sj(ij) for which the highest-numbered intermediate vertex is sk, then rij(k)=true.Proof by induction:Base case: rij(0)=true if and only if sisjEHypothesis: the conclusion holds for h<k(h0)Induction: the simple sisj-path can be looked as sisk-path+sksj-path, with the indices h1, h2 of the highest-numbered intermediate vertices of both segment strictly(simple path) less than k. So, rij(h1)=true, rij(h2)=true, then rij(k-1)=true, rij(k-1)=true(Remember, false to true can not be reversed). So, rij(k)=true
13 Correctness of Washall’s Algorithm If there is no path from si to sj, then rij=false.ProofIf rij=true, then only two cases:rij is set by initialization, then sisjEOtherwise, rij is set during the kth execution of (for k…) when rij(k-1)=true, rij(k-1)=true, which, recursively, leads to the conclusion of the existence of a sisj-path. (Note: If a sisj-path exists, there exists a simple sisj-path)
14 All-pairs Shortest Path Non-negative weighted graphShortest path property: If a shortest path from x to z consisting of path P from x to y followed by path Q from y to z. Then P is a shortest xz-path, and Q, a shortest zy-path.
15 Computing the Distance Matrix Basic formula:D(0)[i][j]=wijD(k)[i][j]=min(D(k-1)[i][j], D(k-1)[i][k]+ D(k-1)[i][j])Basic property:D(k)[i][j]dij(k)where dij(k) is the weight of a shortest path from vi to vj with highest numbered vertex vk.