algorithms and data structures
play

Algorithms and Data Structures Lecture 10 Graph Algorithms III: - PowerPoint PPT Presentation

Algorithms and Data Structures Lecture 10 Graph Algorithms III: Shortest Paths Fabian Kuhn Algorithms and Complexity Fabian Kuhn Algorithms and Complexity Shortest Paths Single Sourse Shortest Paths Problem Given: weighted graph =


  1. Algorithms and Data Structures Lecture 10 Graph Algorithms III: Shortest Paths Fabian Kuhn Algorithms and Complexity Fabian Kuhn Algorithms and Complexity

  2. Shortest Paths Single Sourse Shortest Paths Problem β€’ Given: weighted graph 𝐻 = π‘Š, 𝐹, π‘₯ , start node 𝑑 ∈ π‘Š – We denote the weight of an edge 𝑣, 𝑀 by π‘₯ 𝑣, 𝑀 – Assumption for now: βˆ€π‘“ ∈ 𝐹: π‘₯ 𝑓 β‰₯ 0 β€’ Goal: Find shortest paths / distances from 𝑑 to all nodes – Distance from 𝑑 to 𝑀 : 𝑒 𝐻 𝑑, 𝑀 (length of a shortest path) 1 𝒕 1 1 2 15 3 3 9 7 8 3 4 2 6 6 1 5 Distance from node 1 to node 7 : 10 8 6 7 9 Fabian Kuhn Algorithms and Complexity 2

  3. Optimality of Subpaths Lemma: If 𝑀 0 , 𝑀 1 , … , 𝑀 𝑙 is a shortest path from 𝑀 0 to 𝑀 𝑙 , then it holds for all 0 ≀ 𝑗 ≀ π‘˜ ≀ 𝑙 that the subpath 𝑀 𝑗 , 𝑀 𝑗+1 , … , 𝑀 π‘˜ is also a shortest path from 𝑀 𝑗 to 𝑀 π‘˜ . Shortest path from π’˜ 𝟏 to π’˜ 𝒍 : π’˜ π’Œ π’˜ 𝟏 π’˜ 𝒋 π’˜ 𝒍 π’˜ 𝟐 β€’ Subpath from 𝑀 𝑗 to 𝑀 π‘˜ is also a shortest path. – Otherwise, one could replace the path from 𝑀 𝑗 to 𝑀 π‘˜ by the shortest path from 𝑀 𝑗 to 𝑀 π‘˜ . – If by doing this, nodes are visited multiple time, one can cut out cycles and obtains an even shorter path. β€’ Lemma also holds for negative edge weights, – as long as the graph does not contain negative cycles. Fabian Kuhn Algorithms and Complexity 3

  4. Shortest-Path Tree β€’ Spanning tree that is rooted at node 𝑑 and that contains shortest paths from 𝑑 to all other nodes. – Such a tree always exists (follows from the optimality of subpaths) β€’ For unweighted graphs: BFS spanning tree β€’ Goal: Find a shortest path tree 𝒕 1 1 2 15 3 3 9 7 8 3 4 2 6 6 1 5 8 6 7 9 Fabian Kuhn Algorithms and Complexity 4

  5. Dijkstra’s Algorithm: Idea β€’ Algorithm by Edsger W. Dijkstra (published in 1959) Idea: β€’ We start at 𝑑 and build the spanning tree in a step-by-step manner. Invariant: Algorithm always has a tree rooted at 𝑑 , which is a subtree of a shortest path tree. β€’ Goal: In each step of the algorithm, add one node – Initially: subtree only consists of 𝑑 (trivially satisfies invariant...) – 1 st step: Because of the optimality of subpaths, there must be a shortest path consisting of a single edge... – Always add the remaining node at the smallest distance from 𝑑 . Fabian Kuhn Algorithms and Complexity 5

  6. Dijkstra’s Algorithm : One Step Given: A tree π‘ˆ that is rooted in 𝑑 , such that π‘ˆ is a subtree of a shortest paths tree for node 𝑑 in 𝐻 . (nodes of π‘ˆ : 𝑇 ) How can we extend π‘ˆ by a single node? 10 𝟏 𝑇 : nodes in the tree π‘ˆ 𝒕 𝟐𝟏 πŸ” 1 5 4 𝟐 𝑂 𝑇 : nodes that can be added to 2 3 𝟐𝟐 𝑻 the tree directly. 3 5 πŸ• πŸ‘ πŸ— 7 To add 𝑀 ∈ 𝑂 𝑇 it most hold that 1 𝑢(𝑻) 2 3 𝑒 𝐻 𝑑, 𝑀 = min π‘£βˆˆπ‘‡ 𝑒 𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 2 πŸ— 5 πŸ“ We will see that this always holds for πŸ— 6 𝑀 ∈ 𝑂 𝑇 with minimum distance 𝟐𝟏 𝑒 𝐻 𝑑, 𝑀 from 𝑑 . Fabian Kuhn Algorithms and Complexity 6

  7. Dijkstra’s Algorithm : One Step Given: π‘ˆ is subtree of a shortest path tree for 𝑑 in 𝐻 . Lemma: For a node 𝑀 ∈ 𝑂 𝑇 and an edge 𝑣, 𝑀 with 𝑣 ∈ 𝑇 such that 𝑒 𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 is minimized, it holds that 𝒆 𝑯 𝒕, π’˜ = 𝒆 𝑯 𝒕, 𝒗 + 𝒙 𝒗, π’˜ Consider the 𝑑 - 𝑀 path that we obtain in this way: 𝑻 𝑢(𝑻) 𝒕 𝒗 π’˜ Assume that there is a shorter path: 𝑢(𝑻) 𝑻 𝒕 π’š 𝒛 π’˜ – Because there are no negative edge weights, we therefore have 𝑒 𝐻 𝑑, 𝑦 + π‘₯ 𝑦, 𝑧 ≀ 𝑒 𝐻 (𝑑, 𝑀) < 𝑒 𝐻 𝑑, 𝑣 + π‘₯(𝑣, 𝑀) Fabian Kuhn Algorithms and Complexity 7

  8. Dijkstra’s Algorithm Invariant: Algorithm always has a tree π‘ˆ = (𝑇, 𝐡) rooted at 𝑑 , which is a subtree of a shortest path tree of 𝐻 . β€’ At the beginning, we have π‘ˆ = 𝑑 , βˆ… β€’ For each node 𝑀 βˆ‰ 𝑇 , one at all times computes πœ€ 𝑑, 𝑀 ≔ π‘£βˆˆπ‘‡βˆ©π‘‚ in 𝑀 𝑒 𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 min – as well as the incoming neighbor 𝑣 =: 𝛽 𝑀 that minimized the expression... β€’ πœ€ 𝑑, 𝑀 corresponds to an 𝑑 - 𝑀 path ⟹ πœ€ 𝑑, 𝑀 β‰₯ 𝑒 𝐻 𝑑, 𝑀 β€’ Lemma on last slide: For minimum 𝜺 𝒕, π’˜ , we have: 𝜺 𝒕, π’˜ = 𝒆 𝑯 𝒕, π’˜ Fabian Kuhn Algorithms and Complexity 8

  9. Dijkstra’s Algorithm Initialization 𝑼 = βˆ…, βˆ… β€’ πœ€ 𝑑, 𝑑 = 0 , and πœ€ 𝑑, 𝑀 = ∞ for all 𝑀 β‰  𝑑 update πœ€ 𝑑, 𝑦 β€’ 𝛽 𝑀 = NULL for all 𝑀 ∈ π‘Š π’š 𝒕 Iteration Step π’˜ β€’ Choose a node 𝑀 with smallest πœ€ 𝑑, 𝑀 ≔ π‘£βˆˆπ‘‡βˆ©π‘‚ in 𝑀 𝑒 𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 min β€’ Go through all out-neighbors 𝑦 ∈ π‘Š βˆ– 𝑇 and set πœ€ 𝑑, 𝑦 ≔ min πœ€ 𝑑, 𝑦 , πœ€ 𝑑, 𝑀 + π‘₯ 𝑀, 𝑦 – If πœ€ 𝑑, 𝑦 is decreased, set 𝛽 𝑦 = 𝑀 β€’ Add node 𝑀 and edge 𝛽 𝑀 , 𝑀 to the tree π‘ˆ . Fabian Kuhn Algorithms and Complexity 9

  10. Dijkstra’s Algorithm: Example ∞ 1 32 ∞ ∞ 3 9 10 23 4 13 ∞ ∞ 3 2 6 2 ∞ 1 ∞ 3 ∞ 17 9 19 8 2 1 20 𝟏 ∞ 18 1 ∞ Fabian Kuhn Algorithms and Complexity 10

  11. Dijkstra’s Algorithm: Example ∞ 1 32 ∞ ∞ 3 9 10 23 4 13 𝟐 ∞ 3 2 6 2 ∞ 1 πŸπŸ– 3 𝟐𝟘 17 9 19 8 2 1 20 𝟏 πŸ‘πŸ 18 1 πŸπŸ— Fabian Kuhn Algorithms and Complexity 11

  12. Dijkstra’s Algorithm: Example ∞ 1 32 πŸ“ ∞ 3 9 10 23 4 13 𝟐 πŸπŸ“ 3 2 6 2 ∞ 1 πŸ– 3 𝟐𝟘 17 9 19 8 2 1 20 𝟏 πŸ‘πŸ 18 1 πŸπŸ— Fabian Kuhn Algorithms and Complexity 12

  13. Dijkstra’s Algorithm: Example πŸ” 1 32 πŸ“ ∞ 3 9 10 23 4 13 𝟐 πŸπŸ’ 3 2 6 2 ∞ 1 πŸ– 3 𝟐𝟘 17 9 19 8 2 1 20 𝟏 πŸ‘πŸ 18 1 πŸπŸ— Fabian Kuhn Algorithms and Complexity 13

  14. Dijkstra’s Algorithm: Example πŸ” 1 32 πŸ“ πŸ’πŸ– 3 9 10 23 4 13 𝟐 πŸπŸ’ 3 2 6 2 ∞ 1 πŸ– 3 𝟐𝟘 17 9 19 8 2 1 20 𝟏 πŸ‘πŸ 18 1 πŸπŸ— Fabian Kuhn Algorithms and Complexity 14

  15. Dijkstra’s Algorithm: Example πŸ” 1 32 πŸ“ πŸ’πŸ– 3 9 10 23 4 13 𝟐 𝟘 3 2 6 2 ∞ 1 πŸ– 3 𝟐𝟘 17 9 19 8 2 1 20 𝟏 πŸ‘πŸ 18 1 πŸπŸ” Fabian Kuhn Algorithms and Complexity 15

  16. Dijkstra’s Algorithm: Example πŸ” 1 32 πŸ“ 𝟐𝟘 3 9 10 23 4 13 𝟐 𝟘 3 2 6 2 πŸπŸ‘ 1 πŸ– 3 𝟐𝟐 17 9 19 8 2 1 20 𝟏 πŸ‘πŸ 18 1 πŸπŸ” Fabian Kuhn Algorithms and Complexity 16

  17. Dijkstra’s Algorithm: Example πŸ” 1 32 πŸ“ 𝟐𝟘 3 9 10 23 4 13 𝟐 𝟘 3 2 6 2 πŸπŸ‘ 1 πŸ– 3 𝟐𝟐 17 9 19 8 2 1 20 𝟏 πŸπŸ’ 18 1 πŸπŸ‘ Fabian Kuhn Algorithms and Complexity 17

  18. Dijkstra’s Algorithm Initialization 𝑼 = βˆ…, βˆ… β€’ πœ€ 𝑑, 𝑑 = 0 , and πœ€ 𝑑, 𝑀 = ∞ for all 𝑀 β‰  𝑑 update πœ€ 𝑑, 𝑦 β€’ 𝛽 𝑀 = NULL for all 𝑀 ∈ π‘Š π’š 𝒕 Iteration Step π’˜ β€’ Choose a node 𝑀 with smallest πœ€ 𝑑, 𝑀 ≔ π‘£βˆˆπ‘‡βˆ©π‘‚ in 𝑀 𝑒 𝐻 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 min β€’ Go through all out-neighbors 𝑦 ∈ π‘Š βˆ– 𝑇 and set πœ€ 𝑑, 𝑦 ≔ min πœ€ 𝑑, 𝑦 , πœ€ 𝑑, 𝑀 + π‘₯ 𝑀, 𝑦 – If πœ€ 𝑑, 𝑦 is decreased, set 𝛽 𝑦 = 𝑀 Similar to the MST algorithm β€’ Add node 𝑀 and edge 𝛽 𝑀 , 𝑀 to the tree π‘ˆ . of Prim! Fabian Kuhn Algorithms and Complexity 18

  19. Reminder : Prim’s MST Algorithm 𝐼 = new priority queue; 𝐡 = βˆ… for all 𝑣 ∈ π‘Š βˆ– {𝑑} do 𝐼 .insert( 𝑣 , ∞ ); 𝛽(𝑣) = NULL 𝐼 .insert( 𝑑 , 0 ) while 𝐼 is not empty do 𝑣 = H.deleteMin() for all unmarked neighbors 𝑀 of 𝑣 do if π‘₯ 𝑣, 𝑀 < 𝑒(𝑀) then 𝐼 .decreaseKey( 𝑀 , π‘₯ 𝑣, 𝑀 ) 𝛽 𝑀 = 𝑣 𝑣 .marked = true if 𝑣 β‰  𝑑 then 𝐡 = 𝐡 βˆͺ 𝑣, 𝛽 𝑣 Fabian Kuhn Algorithms and Complexity 19

  20. Dijkstra’s Algorithm : Implementation 𝐼 = new priority queue; 𝐡 = βˆ… for all 𝑣 ∈ π‘Š βˆ– {𝑑} do 𝐼 .insert( 𝑣 , ∞ ); πœ€ 𝑑, 𝑣 = ∞ ; 𝛽(𝑣) = NULL 𝐼 .insert( 𝑑 , 0 ) while 𝐼 is not empty do 𝑣 = H.deleteMin() for all unmarked out-neighbors 𝑀 of 𝑣 do if πœ€ 𝑑, 𝑣 + π‘₯ 𝑣, 𝑀 < πœ€ 𝑑, 𝑀 then πœ€(𝑑, 𝑀) = πœ€ 𝑑, 𝑣 + π‘₯(𝑣, 𝑀) 𝐼 .decreaseKey( 𝑀 , πœ€ 𝑑, 𝑀 ) 𝛽 𝑀 = 𝑣 𝑣 .marked = true if 𝑣 β‰  𝑑 then 𝐡 = 𝐡 βˆͺ 𝛽 𝑣 , 𝑣 Fabian Kuhn Algorithms and Complexity 20

  21. Dijkstra’s Algorithm: Running Time β€’ Algorithm implementation is almost identical to the implementation of Prim’s MST algorithm. β€’ Number of heap operations: create: 1 , insert: π‘œ , deleteMin: π‘œ , decreaseKey: ≀ 𝑛 – Or alternatively without decrease-key: 𝑃 𝑛 insert and deleteMin Op. β€’ Running time with binary heap: 𝑷 𝒏 𝐦𝐩𝐑 𝒐 β€’ Running time with Fibonacci heap: 𝑷 𝒏 + 𝒐 𝐦𝐩𝐑 𝒐 Fabian Kuhn Algorithms and Complexity 21

Recommend


More recommend