- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
All-Pairs Shortest Paths (26.025).ppt
All-Pairs Shortest Paths (26.0/25) HW: problem 26-1, p. 576/25-1, p. 641 Directed graph G = (V,E), weight E ? ? Goal: Create n ? n matrix of s-p distances ?(u,v) Running Bellman-Ford once from each vertex O( ) = O( ) on dense graphs Adjacency-matrix representation of graph: n ? n matrix W = (wij) of edge weights assume wii = 0 ?i, s-p to self has no edges in absence of negative cycles Dynamic Programming (26.1/25.1) dij(m) = weight of s-p from i to j with ? m edges dij(0) = 0 if i = j and dij(0) = ? if i ? j dij(m) = mink{dik(m-1) + wkj} Runtime = O( n4) because n-1 passes each computing n2 d’s in O(n) time Matrix Multiplication (26.1/25.1) Similar: C = A ? B, two n ? n matrices cij = ?k aik ? bkj O(n3) operations replacing: ‘‘ + ’’ ? ‘‘ min ’’ ‘‘ ? ’’ ? ‘‘ + ’’ gives cij= mink {aik + bkj} D(m) = D(m-1) ‘‘? ’’ W identity matrix is D(0) Cannot use Strassen’s because no subtraction Time is still O(n ? n3 ) = O(n4 ) Repeated squaring: W2n = Wn ? Wn Compute W, W2 , W4 ,..., W2k , k= log n, O(n3 log n) Floyd-Warshall Algorithm (26.2/25.2) Also dynamic programming but faster (by log n) cij(m) = weight of s-p from i to j with intermediate vertices in the set {1, 2, ..., m} ? ?(i, j)= cij(n) DP: compute cij(n) in terms of smaller cij(n-1) cij(0) = wij cij(m) = min {cij(m) , cim(m-1) + cmj(m-1) } intermediate nodes in {1, 2, ..., m} Floyd-Warshall Algorithm (26.2/25.2) Difference from previous: we do not check all possible intermediate vertices. Code: for m=1..n do for i=1..n do for j = 1..n do cij(m) = min {cij(m-1) , cim(m-1) + cmj(m-1) } Runtime O(n3 ) Transitive Closure G* of graph G: (i,j) ? G* iff ? path from i to j in G Adjacency matrix, elements on {0,1} Floyd-Warshall with ‘‘ min ’’ ? ‘‘OR’’ , ‘‘+’’ ? ‘‘ AND ’’ Runtime O(n3 ) Useful in many problems Johnson’s Algorithm (26.3/25.3) Johnson’s = B
文档评论(0)