1、清华大学算法分析与设计 课件 第08讲shortestpathsLecture 8. shortest-paths Background To find a shortest route from one city to another To find a least time route from one place to the others 工作流中的最短完成时间? Any more?清华大学 1Problem and its variants Single-Source shortest-path Single-Destination shortest-path Single-pair
2、 shortest-path All-pairs shortest-paths清华大学 3Optimal substructure of a shortest path Shortest-paths algorithms typically rely on the property that a shortest path between two vertices contains other shortest paths within it. Conditions for applying the Dynamic programming and greedy method: Optimal
3、substructure.清华大学 4Lemma 24.1. Subpaths of shortest paths are shortest paths Proof. Using method of cut and patse清华大学 5Negative weight edges Most of the instance of the problems are all of the edges are nonnegative weight Some of the instance of the problems have negative weight edges Negative cycle
4、s: the negative infinitive length清华大学 63 10 5 11 清华大学 7Cycles Can a shortest path contains cycles: Case 1. Positive Cycles: never occurs Case 2. Negative Cycles: - Case 3. zero Cycles: removable清华大学 9Representing shortest paths Using predecessor label can get the result. Let G =(V, E) be a graph : V
5、V null Shortest-paths tree rooted at s is a directed subgraph G = (V, E) of G. V is the set of vertices reachable from s in G G forms a rooted tree with root s and For all v V , the unique simple path from s to v in Gis a shortest path from s to v in G清华大学 10Relaxation We using the technique of rela
6、xation: Let dv be a shortest-path of estimate, it must be great than or equal to its real distance of v from s, the source. Initialize-Single-Source(G,s)1. For each vertex v VG1. dv = 2. v=null2. ds=0清华大学 14Some comments on relaxation 松弛什么? Relax the constrain of triangle inequality清华大学 16Relax Rela
7、x(u,v,w)1. If dvdu+w(u,v)1. dvdu+w(u,v)2. vu清华大学 17Properties and relaxation1. Triangle inequality: for any (u,v)E we have(s,v)(s,u)+w(u,v)2. Upper bound property: we always have dv(s,v) for all vertices v V, and once dv achieves the value (s,v), it never changes3. No path property: if there is no p
8、ath from s to v, we always have dv=(s,v) =清华大学 18Properties4. Convergence property: if p= is a shortest path in G for some u,v V, and if du= (s,u) at any time prior to relaxing edge (u,v), then dv=(s,v) at all time afterward5. Path relaxation property: if p= is a shortest path from v0 =s to vk, and
9、the edges in p are relaxed in the order (v0,v1), (v1,v2),(vk-1,vk), then dvk= (s,vk). This property holds regardless of any other relaxation steps that occurs.清华大学 19Properties:6. Predecessor-subgraph property: Once dv=(s,v) for all v in V, the predecessor subgraph is a shortest-paths tree rooted at
10、 s.清华大学 20Bellman-Ford algorithm Bellman-Ford(G,w,s)1. Initialize-Single-Source(G,s)2. For i1 to |VG|-11. For each edge (u,v)EG1. Relax(u,v,w)3. For each edge (u,v)EG1. If dvdu+w(u,v) return false4. Return true清华大学 21The correctness of Bellman-Ford algorithm1. 如果没有由s可达的负圈:1. 则再结束时,验证通过,且距离正确。2. 如果有由
11、s可达的负圈:1. 设此圈为,若所有三角不等式成立,则与此负圈矛盾。【】清华大学 29Si dingle-source shortest paths in a rected acyclic graph Dag-shortest-paths(G,w,s)1. Topological sort the vertices of G2. Initialize-Single-Source(G,w,s)3. For each vertex u, taken in topological order1. For each v in Adju1. Relax(u,v,w)清华大学 30Dags Shortes
12、t-paths0清华大学 31Dijkstras Algorithm It solves the problem of nonnegative weighted graphs shortest-paths Its main trick is to maintain a set S of vertices whose final shortest-path weights from the source s have already been determined.清华大学 32 Dijkstra(G,w,s)1. Initialize-SingleSource(G,s)2. S3. QVG4.
13、 While Q != 1. uQ.extractMin()2. S S U u3. For each vertex v in Adju1. Relax(u,v,w)清华大学 33Complex analysisThe Maintaining of Minimum Queue by calling:1.Insert2.Extract3.Decrease key4.Using aggregate analysis, we have O(V+E)lg V)5.By using Fibonacci heap, the running time is O(V lgV + E), by reducing
14、 the amortized cost ofdecreasing key operations清华大学 38Application of Shortest paths 图像缝合问题: Linear programming with difference constraints can be equivalently converted to a problem of shortest path of a graph.【可行解问题】清华大学 39The linear programming problem Ob j ect: ni=1 cixi subject to xj-xi bk for i
15、,j in 1,2,n Converting to a graph! 如何找到可行解! n vertices represent the n variables plus an artificial vertex v0, xj-xi bk means to add an edge (i,j) with weight bk Constraints convert to the Edges between the vertices and plus artificial edges (v0,v1), (v0,vn) X=(d(v0,v1), d(v0,v2), , d(v0,vn),) is a
16、feasiblesolution of the linear programming problem清华大学 40所有点之间的最短路径 动态规划方法: 最优解结构: 设l(k)ij为最多包含k条边的从顶点i到顶点j的最短路径的距离,如果没有这样的路径,则距离定义为,于是 l(0) 0 if ij, otherwiseij l(1) w(i,j), what is w?ij l(m)ijmin(l(m-1)ij, p(m)ij) p(m)ijmink=1,n(l(m-1)ik + w(k,j) ) l(m) min (l(m-1) + w(k,j) ) Why?ij k=1,n ik清华大学 4
17、1(k)ij每个顶点每条路径的前驱 (k)ij=?如何计算 与上面计算相关!清华大学 42问题的解 什么是(i,j), We claim: (i,j)=l(n-1)ij =l(n)ij =l(n+1) (n+2)ij =l ij 为什么?清华大学 43算法11. LW;2. For i 1,n1. LExtended-Shortest-Paths(L,W)清华大学 45Floyd-Warshall algorithm 动态规划方法: 最优解结构: 设d(k)ij为最多包含k个中间点的顶点i到顶点j的最短路径的距离,如果没有这样的路径,则距离定义为,于是 d(0)ijw(i,j), (0)ij
18、= null if there is no edge (i,j), else it is i. d(m)ijmin(d(m-1)ij, d(m-1)im+d(m-1)mj) Why? 如何计算(k) ,从i 到j最多包含k个中间点的最短路径中j的ij父节点? 由(n) 计算最短路径。ij 复杂度:O(V3)清华大学 48Johnson算法 针对稀疏矩阵,Johnson提出一种更为有效的算法。 引理25.1 改变加权函数不改变最短路径: 设:h: ER 定义: w ( u , v ) = w ( u , v ) + h ( u ) - h ( v ) 设p是从v0到vk的任意路径, 则p是从v0
19、到vk的在权W下的最短路径的充要条件是: 从v0到vk的在权 w 的最短路径,G在权W下有负回路的充要条件是G在权 w 有负回路。清华大学 49W下W下Johnson(G)1.计算G1. VG=VG U s2.2. EG=EG U (s,v):v in VG, 且 w(s,v)=0如果Bellman-Ford(G,w,s) = flase1. Print 图有负回路,return null;For each (u,v) in EG1. w(u,v)= w(u,v)+(s,u)-(s,v)For each u in VG1. Dijkstra(G,w,u) 得到 (u,v) ,对所有v 属于VG2. 对所有 v 属于 VG3.4.1. duv = (u,v)- (s,u)+(s,v)5. Return D = (duv)nn清华大学 52复杂性分析1. 计算G: O(V+E)2. Bellman-Ford算法:O(VE)3. O(E)4. O(V * Dijkstra算法复杂性)O(V * (V lg V+ E) = O(V2 lg V +VE)清华大学 53总结 单源最短路径问题 方法:贪婪:Dijkstra;Bellman Ford 关键点:松弛 应用:最优化问题、图像处理、 多源最短路径问题 动态规划问题:不同子空间设立 群论及其应用 问题转换清华大学 54
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1