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 π» = π, πΉ, π₯ , 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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