Title: Reachability as Transitive Closure
1 Reachability as Transitive Closure
- Algorithm Design Analysis
- 17
2In the last class
- Undirected and Symmetric Digraph
- UDF Search Skeleton
- Biconnected Components
- Articulation Points and Biconnectedness
- Biconnected Component Algorithm
- Analysis of the Algorithm
3Reachability as Transitive Closure
- Transitive Closure by DFS
- Transitive Closure by Shortcuts
- Washalls Algorithm for Transitive Closure
- All-Pair Shortest Paths
4Fundamental 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.
5Computing the Reachability by DFS
- With each vertex vi, constructing a DFS tree
rooted at vi. - For each vertex vj encountered, Rij 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
Rij, but also Rkj can be set)
6Computing 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. (?(nm)) - Computing reachability for G?.
- Expand the reachability for G? to that for G.
(O(n2))
7Transitive Closure by Shortcuts
- The idea if there are edges sisk, sksj, then an
edge sisj, the shortcut is inserted.
Pass two
Pass one
Note the triple (1,5,3) is considered more than
once
8Transitive Closure by Shortcuts the algorithm
- Input A, an n?n boolean matrix that represents a
binary relation - Output R, the boolean matrix for the transitive
closure of A - Procedure
- 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 (i1 i?n i)
- for (j1 j?n j)
- for (k1 k?n k)
- rijrij?(rik?rkj)
The order of (i,j,k) matters
9Change the order Washalls 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 (k1 k?n k)
- for (i1 i?n i)
- for (j1 j?n j)
- rijrij?(rik?rkj)
k varys in the outmost loop
Note false to true can not be reversed
10Highest-numbered intermediate vertex
The highest intermediate vertex in the intervals
(sisk), (sksj) are both less than sk
sj
sk
si
A specific order is assumed for all vertices
Vertical position of vertices reflect their
vertex numbers
11Correctness of Washalls Algorithm
- Notation
- The value of rij changes during the execution of
the body of the for k loop - After initializations rij(0)
- After the kth time of execution rij(k)
12Correctness of Washalls Algorithm
- If there is a simple path from si to sj(i?j) 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 sisj?E
- Hypothesis the conclusion holds for hltk(h?0)
- Induction the simple sisj-path can be looked as
sisk-pathsksj-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
13Correctness of Washalls Algorithm
- If there is no path from si to sj, then
rijfalse. - Proof
- If rijtrue, then only two cases
- rij is set by initialization, then sisj?E
- Otherwise, 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)
14All-pairs Shortest Path
- Non-negative weighted graph
- Shortest 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.
15Computing the Distance Matrix
- Basic formula
- D(0)ijwij
- D(k)ijmin(D(k-1)ij, D(k-1)ik
D(k-1)ij) - Basic property
- D(k)ij?dij(k)
- where dij(k) is the weight of a shortest path
from vi to vj with highest numbered vertex vk.
16All-Pairs Shortest Paths
- Floyd algorithm
- Only slight changes on Washalls algorithm.
- Routing table