收藏 分享(赏)

算法设计与分析16TrClosure.ppt

上传人:lxhqcj 文档编号:7281980 上传时间:2019-05-12 格式:PPT 页数:33 大小:208.50KB
下载 相关 举报
算法设计与分析16TrClosure.ppt_第1页
第1页 / 共33页
算法设计与分析16TrClosure.ppt_第2页
第2页 / 共33页
算法设计与分析16TrClosure.ppt_第3页
第3页 / 共33页
算法设计与分析16TrClosure.ppt_第4页
第4页 / 共33页
算法设计与分析16TrClosure.ppt_第5页
第5页 / 共33页
点击查看更多>>
资源描述

1、Reachability as Transitive Closure,Algorithm : Design & Analysis 16,In the last class,Optimization Problem MST Problem Prims Algortithm Kruskals Algorithm Single-Source Shortest Path Problem Dijstras Algorithm Greedy Stretegy,Reachability as Transitive Closure,Shortest Path and Transitive Closure Wa

2、shalls Algorithm for Transitive Closure All-Pair Shortest Paths Matrix for Transitive Closure Multiplying Bit Matrices - Kronrods Algorithm,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 (ref

3、lexive) transitive closure of the adjacency relation, which can be represented as a bit matrix.,Transitive Closure by Shortcuts,The idea: if there are edges sisk, sksj, then an edge sisj, the “shortcut” is inserted.,1,2,5,3,4,Pass one,Pass two,Note: the triple (1,5,3) is considered more than once,Tr

4、ans. Closure by Shortcuts: algorithm,Input: A, an nn 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;whi

5、le (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,Shortcuts in different order,Duplicated checking may be deleted by changing the order of the vertices.,Pass one,No edge is added in Pass two. End.,Check

6、 the vertices in decreasing order.,Change 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 (k=1; kn; k+)for (i=1; in; i+)for (j=1; jn; j+

7、)rij=rij(rikrkj),k varys in the outmost loop,Note: “false to true” can not be reversed,Highest-numbered intermediate vertex,sk,si,sj,A specific order is assumed for all vertices,Vertical position of vertices reflect their vertex numbers,The highest intermediate vertex in the intervals (sisk), (sksj)

8、 are both less than sk,Correctness 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),Correctness of Washalls Algorithm,If there is a simple path from si to sj(ij) for whi

9、ch the highest-numbered intermediate vertex is sk, then rij(k)=true. Proof by induction: Base case: rij(0)=true if and only if sisjE Hypothesis: the conclusion holds for hk(h0) Induction: the simple sisj-path can be looked as sisk-path+sksj-path, with the indices h1, h2 of the highest-numbered inter

10、mediate vertices of both segment strictly(simple path) less than k. So, rik(h1)=true, rkj(h2)=true, then rik(k-1)=true, rkj(k-1)=true(Remember, false to true can not be reversed). So, rij(k)=true,Correctness of Washalls Algorithm,If there is no path from si to sj, then rij=false. Proof If rij=true,

11、then only two cases:rij is set by initialization, then sisjE Otherwise, rij is set during the kth execution of (for k=1,2,) when rik(k-1)=true, rkj(k-1)=true, which, recursively, leading to the conclusion of the existence of a sisj-path. (Note: If a sisj-path exists, there exists a simple sisj-path)

12、,All-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. The regular matrix representing a graph can easily be transformed into

13、 a (minimum) distance matrix D(just replacing 1 by edge weight, 0 by infinity, and setting main diagonal elements as 0),Computing the Distance Matrix,Basic formula: D(0)ij=wij D(k)ij=min(D(k-1)ij, D(k-1)ik+ D(k-1)ij)Basic property: D(k)ijdij(k)where dij(k) is the weight of a shortest path from vi to

14、 vj with highest numbered intermediate vertex vk.,All-Pairs Shortest Paths,Floyd algorithm Only slight changes on Washalls algorithm.Routing table tracking the path,Void allPairsShortestPaths(float W, int n, float D)int i, j, k;Copy W into D;for (k=1; kn; k+)for (i=1; in; i+)for (j=1; jn; j+)Dij = m

15、in (Dij, Dik+Dkj;,Matrix Representation,Define family of matrix A(p): aij(p)=true if and only if there is a path of length p from si to sj. A(0) is specified as identity matrix. A(1) is exactly the adjacency matrix. Note that aij(2)=true if and only if exists some sk, such that both aik(1) and akj(1

16、) are true. So, aij(2)=k=1,2,n (aik(1)akj(1), which is an entry in the Boolean matrix product.,Boolean Matrix Operations: Recalled,Boolean matrix prodect C=AB as: cij=k=1,2,n(aikbkj) Boolean matrix sum D=A+B as: dij=aikbkj R, the transitive closure matrix of A, is the sum of all Ap, p is a non-negat

17、ive integer. For a digraph with n vertices, the length of the longest simple path is no larger than n-1.,Transitive Closure by Intuition,R = A0+A1+An-1 By the straightforward method, each multiplication takes time in (n3), so, the total time is in (n4). Improvement by replacing the sum of many power

18、s by a power of a single matrix A0+A1+Ak=(I+A)k. Proof by induction: (I+A)k=(I+A)k-1(I+A)= (I+A)k-1I+(I+A)k-1A,Formula for Transitive Closure,Let A be an nn Boolean matrix representing a binary relation. Then R, the matrix for the transitive closure of A is (I+A)s for any sn-1. The cost for the comp

19、utation: Computing (I+A): in (n2) Computing the power: clg(n-1), if we use for s the least power of 2 not less than n-1, where c is the cost for each matrix multiplication. Even for integer matrix, c is not worse than (n3),Bit Matrix,A bit string of length n is a sequence of n bits occupying contigu

20、ous storage(word boundary) (usually, n is larger than the word length of a computer) If A is a bit matrix of nn, then Ai denotes the ith row of A which is a bit string of length n. aij is the jth bit of Ai. The procedure bitwiseOR(a,b,n) compute ab bitwise for n bits, leaving the result in a.,Straig

21、htforward Multiplication of Bit Matrix,Computing C=ABfor (i=1; in, i+)for (k=1; kn, k+)if (aik=true) bitwiseOR(Ci, Bk, n),In the case of aik is true, cij=aikbkj is true iff. bkj is true,Thought as a union of sets (row union), n2 unions are done at most,Union for Bk is repeated each time when the kth

22、 bit is true in a different row of A is encountered.,Reducing the Duplicates by Grouping,Multiplication of A, B, two 1212 matrices,12 rows of B are divided evenly into 3 groups, with rows 1-4 in group 1, etc.With each group, all possible unions of different rows are pre-computed. (This can be done w

23、ith 11 unions if suitable order is assumed.)When the first row of AB is computed, (B1B3B4) is used in stead of 3 different unions, and this combination is used in computing the 3rd and 7th rows as well.,Segment Length t,The Segmentation for Matrix A,1 2 t t+1 2t (j-1)t+1 jt ,bitSeg(Ai,1,t),bitSeg(Ai

24、,2,t),Ai,The nn array,bitSeg(Ai,j,t),Bit string of length t, looked as a t-bit integer,An Example,A1A2A3A4A5A6A7A8A9A10A11A12,Group 1 (1t),Group 2 (t+12t),Group 3 (2t+13t),t=4,B1B2B3B4B5B6B7B8B9B10B11B12,Group 1 (1t),Group 2 (t+12t),Group 3 (2t+13t),bitSeg(A7, 1, t) = 10112 = 11,any union combinatio

25、n of B1,B2,B3,B4, totaling 16, 11 computed,any union combination of B5,B6,B7,B8, totaling 16, 11 computed,any union combination of B9,B10,B11,B12, totaling 16, 11 computed,Where to store?,Storage of the Row Combinations,Using one large 2-dimensional array Goals keep all unions generated provide inde

26、xing for using Coding within a group One-to-one correspondence between a bit string of length t and one union for a subset of a set of t elements Establishing indexing for union required When constructing a row of AB, a segment can be notated as a integer. Use it as index.,Storage the Unions,i,j,k s

27、tands for BiBjBk,one row for one group,column indexed by bitSeg(Ai,j,t),allUnion,Array for Row Combinations,The gn array: allUnions,row j,Containing all possible row combinations, totaling 2t, within jth group of B,column i,Containing in each row the union, of which the code is exactly i, looked as

28、a t-bit binary number.,Indexed by segment coding for Matrix A,Cost as Function of Group Size,Cost for the pre-computation There are 2t different combination of rows in one group, including an empty and t singleton. Note, in a suitable order, each combination can be made using only one union. So, the

29、 total number of union is g2t-(t+1), where g=n/t is the number of group. Cost for the generation of the product In computing one of n rows of AB, at most one combination from each group is used. So, the total number of union is n(g-1),Selecting Best Group Size,The total number of union done is: g2t-

30、(t+1)+n(g-1) (n2t)/t+n2/t (Note: g=n/t ) Trying to minimize the number of union Assuming that the first term is of higher order: Then tlgn, and the least value is reached when t=lgn. Assuming that the second term is of higher order: Then tlgn, and the least value is reached when t=lgn. So, when tlgn

31、, the number of union is roughly 2n2/lgn, which is of lower order than n2. We use t=lgn,For symplicity, exact power for n is assumed,Scketch for the Procedure,t=lgn; g=n/t;for (i=1; in; i+)for (j=1; jg; j+)Ci = Ci allUnionsjbitSeg(Ai,j,t),Kronrod Algorithm,Input: A,B and n, where A and B are nn bit

32、matrices. Output: C, the Boolean matrix product. Procedure The processing order has been changed, from “row by row” to “group by group”, resulting the reduction of storage space for unions.,Complexity of Kronrod Algorithm,For computing all unions within a group, 2t-1 union operations are done. One union is bitwiseORed to n row of C So, altogether, (n/t)(2t-1+n) row unions are done. The cost of row union is n/w bitwise or operations, where w is word size of bitwise or instruction dependent constant.,Home Assignments,pp.446- 9.10 9.12 9.16 9.17,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > 数据结构与算法

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报