breadth first search dijkstra s algorithm for shortest
play

Breadth First Search, Dijkstras Algorithm for Shortest Paths - PowerPoint PPT Presentation

Algorithms & Models of Computation CS/ECE 374, Fall 2017 Breadth First Search, Dijkstras Algorithm for Shortest Paths Lecture 17 Tuesday, October 31, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 42 Part I Breadth First


  1. Shortest Path Problems 3 Fall 2017 18 CS374 Sariel Har-Peled (UIUC) Many applications! Find shortest paths for all pairs of nodes. Given node s fjnd shortest path from s to all other nodes. Shortest Path Problems 2 1 18 / 42 Input A (undirected or directed) graph G = ( V , E ) with edge lengths (or costs). For edge e = ( u , v ) , ℓ ( e ) = ℓ ( u , v ) is its length. Given nodes s , t fjnd shortest path from s to t .

  2. Shortest Path Problems 3 Fall 2017 18 CS374 Sariel Har-Peled (UIUC) Many applications! Find shortest paths for all pairs of nodes. Given node s fjnd shortest path from s to all other nodes. Shortest Path Problems 2 1 18 / 42 Input A (undirected or directed) graph G = ( V , E ) with edge lengths (or costs). For edge e = ( u , v ) , ℓ ( e ) = ℓ ( u , v ) is its length. Given nodes s , t fjnd shortest path from s to t .

  3. Single-Source Shortest Paths: Restrict attention to directed graphs Fall 2017 19 CS374 Sariel Har-Peled (UIUC) Exercise: show reduction works. Relies on non-negativity! 3 2 1 problem - how? Undirected graph problem can be reduced to directed graph Non-Negative Edge Lengths 2 1 2 Given node s fjnd shortest path from s to all other nodes. 3 2 1 Single-Source Shortest Path Problems 1 19 / 42 Input: A (undirected or directed) graph G = ( V , E ) with non-negative edge lengths. For edge e = ( u , v ) , ℓ ( e ) = ℓ ( u , v ) is its length. Given nodes s , t fjnd shortest path from s to t . Given undirected graph G , create a new directed graph G ′ by replacing each edge { u , v } in G by ( u , v ) and ( v , u ) in G ′ . set ℓ ( u , v ) = ℓ ( v , u ) = ℓ ( { u , v } )

  4. Single-Source Shortest Paths: Restrict attention to directed graphs Fall 2017 19 CS374 Sariel Har-Peled (UIUC) Exercise: show reduction works. Relies on non-negativity! 3 2 1 problem - how? Undirected graph problem can be reduced to directed graph Non-Negative Edge Lengths 2 1 2 Given node s fjnd shortest path from s to all other nodes. 3 2 1 Single-Source Shortest Path Problems 1 19 / 42 Input: A (undirected or directed) graph G = ( V , E ) with non-negative edge lengths. For edge e = ( u , v ) , ℓ ( e ) = ℓ ( u , v ) is its length. Given nodes s , t fjnd shortest path from s to t . Given undirected graph G , create a new directed graph G ′ by replacing each edge { u , v } in G by ( u , v ) and ( v , u ) in G ′ . set ℓ ( u , v ) = ℓ ( v , u ) = ℓ ( { u , v } )

  5. Single-Source Shortest Paths: Restrict attention to directed graphs Fall 2017 19 CS374 Sariel Har-Peled (UIUC) Exercise: show reduction works. Relies on non-negativity! 3 2 1 problem - how? Undirected graph problem can be reduced to directed graph Non-Negative Edge Lengths 2 1 2 Given node s fjnd shortest path from s to all other nodes. 3 2 1 Single-Source Shortest Path Problems 1 19 / 42 Input: A (undirected or directed) graph G = ( V , E ) with non-negative edge lengths. For edge e = ( u , v ) , ℓ ( e ) = ℓ ( u , v ) is its length. Given nodes s , t fjnd shortest path from s to t . Given undirected graph G , create a new directed graph G ′ by replacing each edge { u , v } in G by ( u , v ) and ( v , u ) in G ′ . set ℓ ( u , v ) = ℓ ( v , u ) = ℓ ( { u , v } )

  6. Single-Source Shortest Paths via BFS 1 Fall 2017 20 CS374 Sariel Har-Peled (UIUC) if L is large. 3 20 / 42 2 2 nodes. Run BFS ( s ) to get shortest path distances from s to all other 1 Special case: All edge lengths are 1 . O ( m + n ) time algorithm. Special case: Suppose ℓ ( e ) is an integer for all e ? Can we use BFS ? Reduce to unit edge-length problem by placing ℓ ( e ) − 1 dummy nodes on e . Let L = max e ℓ ( e ) . New graph has O ( mL ) edges and O ( mL + n ) nodes. BFS takes O ( mL + n ) time. Not effjcient

  7. Single-Source Shortest Paths via BFS 1 Fall 2017 20 CS374 Sariel Har-Peled (UIUC) if L is large. 3 20 / 42 2 2 nodes. Run BFS ( s ) to get shortest path distances from s to all other 1 Special case: All edge lengths are 1 . O ( m + n ) time algorithm. Special case: Suppose ℓ ( e ) is an integer for all e ? Can we use BFS ? Reduce to unit edge-length problem by placing ℓ ( e ) − 1 dummy nodes on e . Let L = max e ℓ ( e ) . New graph has O ( mL ) edges and O ( mL + n ) nodes. BFS takes O ( mL + n ) time. Not effjcient

  8. Single-Source Shortest Paths via BFS 1 Fall 2017 20 CS374 Sariel Har-Peled (UIUC) if L is large. 3 20 / 42 2 2 nodes. Run BFS ( s ) to get shortest path distances from s to all other 1 Special case: All edge lengths are 1 . O ( m + n ) time algorithm. Special case: Suppose ℓ ( e ) is an integer for all e ? Can we use BFS ? Reduce to unit edge-length problem by placing ℓ ( e ) − 1 dummy nodes on e . Let L = max e ℓ ( e ) . New graph has O ( mL ) edges and O ( mL + n ) nodes. BFS takes O ( mL + n ) time. Not effjcient

  9. Single-Source Shortest Paths via BFS 1 Fall 2017 20 CS374 Sariel Har-Peled (UIUC) if L is large. 3 20 / 42 2 2 nodes. Run BFS ( s ) to get shortest path distances from s to all other 1 Special case: All edge lengths are 1 . O ( m + n ) time algorithm. Special case: Suppose ℓ ( e ) is an integer for all e ? Can we use BFS ? Reduce to unit edge-length problem by placing ℓ ( e ) − 1 dummy nodes on e . Let L = max e ℓ ( e ) . New graph has O ( mL ) edges and O ( mL + n ) nodes. BFS takes O ( mL + n ) time. Not effjcient

  10. Single-Source Shortest Paths via BFS 1 Fall 2017 20 CS374 Sariel Har-Peled (UIUC) if L is large. 3 20 / 42 2 2 nodes. Run BFS ( s ) to get shortest path distances from s to all other 1 Special case: All edge lengths are 1 . O ( m + n ) time algorithm. Special case: Suppose ℓ ( e ) is an integer for all e ? Can we use BFS ? Reduce to unit edge-length problem by placing ℓ ( e ) − 1 dummy nodes on e . Let L = max e ℓ ( e ) . New graph has O ( mL ) edges and O ( mL + n ) nodes. BFS takes O ( mL + n ) time. Not effjcient

  11. Towards an algorithm 2 Fall 2017 21 CS374 Sariel Har-Peled (UIUC) observe that edge lengths are non-negative. Proof. Why does BFS work? 21 / 42 1 Let G be a directed graph with non-negative edge lengths. Let Lemma BFS (s) explores nodes in increasing distance from s dist ( s , v ) denote the shortest path length from s to v . If s = v 0 → v 1 → v 2 → . . . → v k shortest path from s to v k then for 1 ≤ i < k : s = v 0 → v 1 → v 2 → . . . → v i is shortest path from s to v i dist ( s , v i ) ≤ dist ( s , v k ) . Relies on non-neg edge lengths. Suppose not. Then for some i < k there is a path P ′ from s to v i of length strictly less than that of s = v 0 → v 1 → . . . → v i . Then P ′ concatenated with v i → v i + 1 . . . → v k contains a strictly shorter path to v k than s = v 0 → v 1 . . . → v k . For the second part,

  12. Towards an algorithm 2 Fall 2017 21 CS374 Sariel Har-Peled (UIUC) observe that edge lengths are non-negative. Proof. Why does BFS work? 21 / 42 1 Let G be a directed graph with non-negative edge lengths. Let Lemma BFS (s) explores nodes in increasing distance from s dist ( s , v ) denote the shortest path length from s to v . If s = v 0 → v 1 → v 2 → . . . → v k shortest path from s to v k then for 1 ≤ i < k : s = v 0 → v 1 → v 2 → . . . → v i is shortest path from s to v i dist ( s , v i ) ≤ dist ( s , v k ) . Relies on non-neg edge lengths. Suppose not. Then for some i < k there is a path P ′ from s to v i of length strictly less than that of s = v 0 → v 1 → . . . → v i . Then P ′ concatenated with v i → v i + 1 . . . → v k contains a strictly shorter path to v k than s = v 0 → v 1 . . . → v k . For the second part,

  13. Towards an algorithm 2 Fall 2017 21 CS374 Sariel Har-Peled (UIUC) observe that edge lengths are non-negative. Proof. Why does BFS work? 21 / 42 1 Let G be a directed graph with non-negative edge lengths. Let Lemma BFS (s) explores nodes in increasing distance from s dist ( s , v ) denote the shortest path length from s to v . If s = v 0 → v 1 → v 2 → . . . → v k shortest path from s to v k then for 1 ≤ i < k : s = v 0 → v 1 → v 2 → . . . → v i is shortest path from s to v i dist ( s , v i ) ≤ dist ( s , v k ) . Relies on non-neg edge lengths. Suppose not. Then for some i < k there is a path P ′ from s to v i of length strictly less than that of s = v 0 → v 1 → . . . → v i . Then P ′ concatenated with v i → v i + 1 . . . → v k contains a strictly shorter path to v k than s = v 0 → v 1 . . . → v k . For the second part,

  14. Towards an algorithm Lemma Fall 2017 21 CS374 Sariel Har-Peled (UIUC) observe that edge lengths are non-negative. Proof. 2 1 Let G be a directed graph with non-negative edge lengths. Let 21 / 42 dist ( s , v ) denote the shortest path length from s to v . If s = v 0 → v 1 → v 2 → . . . → v k shortest path from s to v k then for 1 ≤ i < k : s = v 0 → v 1 → v 2 → . . . → v i is shortest path from s to v i dist ( s , v i ) ≤ dist ( s , v k ) . Relies on non-neg edge lengths. Suppose not. Then for some i < k there is a path P ′ from s to v i of length strictly less than that of s = v 0 → v 1 → . . . → v i . Then P ′ concatenated with v i → v i + 1 . . . → v k contains a strictly shorter path to v k than s = v 0 → v 1 . . . → v k . For the second part,

  15. A proof by picture Sariel Har-Peled (UIUC) CS374 22 Fall 2017 22 / 42 s = v 0 v 6 v 2 v 5 v 4 v 1 v 3 Shortest path from v 0 to v 6

  16. A proof by picture Sariel Har-Peled (UIUC) Fall 2017 22 CS374 22 / 42 Shorter path from v 0 to v 4 s = v 0 v 6 v 2 v 5 v 4 v 1 v 3 Shortest path from v 0 to v 6

  17. A proof by picture Sariel Har-Peled (UIUC) Fall 2017 22 CS374 22 / 42 A shorter path from v 0 to v 6 . A contradiction. s = v 0 v 6 v 2 v 5 v 4 v 1 v 3 Shortest path from v 0 to v 6

  18. A Basic Strategy Explore vertices in increasing order of distance from s : Fall 2017 23 CS374 Sariel Har-Peled (UIUC) How can we implement the step in the for loop? i 'th closest to s and that no edge has zero length) 23 / 42 (For simplicity assume that nodes are at different distances from s Initialize for each node v , dist ( s , v ) = ∞ Initialize X = { s } , for i = 2 to | V | do (* Invariant: X contains the i − 1 closest nodes to s *) Among nodes in V − X , find the node v that is the Update dist ( s , v ) X = X ∪ { v }

  19. A Basic Strategy Explore vertices in increasing order of distance from s : Fall 2017 23 CS374 Sariel Har-Peled (UIUC) How can we implement the step in the for loop? i 'th closest to s and that no edge has zero length) 23 / 42 (For simplicity assume that nodes are at different distances from s Initialize for each node v , dist ( s , v ) = ∞ Initialize X = { s } , for i = 2 to | V | do (* Invariant: X contains the i − 1 closest nodes to s *) Among nodes in V − X , find the node v that is the Update dist ( s , v ) X = X ∪ { v }

  20. Finding the i th closest node 1 Fall 2017 24 CS374 Sariel Har-Peled (UIUC) than v . Implies v is not the i ’th closest node to s - recall that X If P had an intermediate node u not in X then u will be closer to s Proof. Let P be a shortest path from s to v where v is the i th closest node. Claim 2 24 / 42 X contains the i − 1 closest nodes to s Want to fjnd the i th closest node from V − X . What do we know about the i th closest node? Then, all intermediate nodes in P belong to X . already has the i − 1 closest nodes.

  21. Finding the i th closest node 1 Fall 2017 24 CS374 Sariel Har-Peled (UIUC) than v . Implies v is not the i ’th closest node to s - recall that X If P had an intermediate node u not in X then u will be closer to s Proof. Let P be a shortest path from s to v where v is the i th closest node. Claim 2 24 / 42 X contains the i − 1 closest nodes to s Want to fjnd the i th closest node from V − X . What do we know about the i th closest node? Then, all intermediate nodes in P belong to X . already has the i − 1 closest nodes.

  22. Finding the i th closest node 1 Fall 2017 24 CS374 Sariel Har-Peled (UIUC) than v . Implies v is not the i ’th closest node to s - recall that X If P had an intermediate node u not in X then u will be closer to s Proof. Let P be a shortest path from s to v where v is the i th closest node. Claim 2 24 / 42 X contains the i − 1 closest nodes to s Want to fjnd the i th closest node from V − X . What do we know about the i th closest node? Then, all intermediate nodes in P belong to X . already has the i − 1 closest nodes.

  23. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 10 9 a 18 6 0 6 6 11 30 13 19 20 6 8 16 25

  24. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 a 18 6 0 c g 6 6 11 30 13 19 20 6 8 16 d 25 e h

  25. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 a 18 6 0 c g 6 6 6 11 30 13 19 20 6 8 16 d 25 e h

  26. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 9 a 18 6 0 c g 6 6 6 11 30 13 19 20 6 8 16 d 25 e h

  27. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 9 a 18 6 0 c g 6 6 6 11 30 13 19 20 6 8 16 d 25 e 13 h

  28. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 19 9 a 18 6 0 c g 6 6 6 11 30 13 19 20 6 8 16 d 25 e 13 h

  29. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 19 9 a 18 6 0 c g 6 6 6 11 30 13 25 19 20 6 8 16 d 25 e 13 h

  30. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 19 9 a 18 6 0 c g 6 6 6 36 11 30 13 25 19 20 6 8 16 d 25 e 13 h

  31. Finding the i th closest node repeatedly An example Fall 2017 25 CS374 Sariel Har-Peled (UIUC) 25 / 42 b f 10 9 19 9 a 18 6 0 c g 6 6 6 36 11 30 13 25 19 20 6 8 16 d 25 e 38 13 h

  32. Finding the i th closest node Corollary Fall 2017 26 CS374 Sariel Har-Peled (UIUC) The i th closest node is adjacent to X . 26 / 42 10 9 a 18 6 0 6 6 30 11 13 19 20 6 8 16 25

  33. Finding the i th closest node 1 Fall 2017 27 CS374 Sariel Har-Peled (UIUC) Lemma 2 1 2 to u using only nodes in X as intermediate vertices. 1 2 27 / 42 X contains the i − 1 closest nodes to s Want to fjnd the i th closest node from V − X . For each u ∈ V − X let P ( s , u , X ) be a shortest path from s Let d ′ ( s , u ) be the length of P ( s , u , X ) Observations: for each u ∈ V − X , dist ( s , u ) ≤ d ′ ( s , u ) since we are constraining the paths d ′ ( s , u ) = min t ∈ X ( dist ( s , t ) + ℓ ( t , u )) - Why? If v is the i th closest node to s , then d ′ ( s , v ) = dist ( s , v ) .

  34. Finding the i th closest node 1 Fall 2017 27 CS374 Sariel Har-Peled (UIUC) Lemma 2 1 2 to u using only nodes in X as intermediate vertices. 1 2 27 / 42 X contains the i − 1 closest nodes to s Want to fjnd the i th closest node from V − X . For each u ∈ V − X let P ( s , u , X ) be a shortest path from s Let d ′ ( s , u ) be the length of P ( s , u , X ) Observations: for each u ∈ V − X , dist ( s , u ) ≤ d ′ ( s , u ) since we are constraining the paths d ′ ( s , u ) = min t ∈ X ( dist ( s , t ) + ℓ ( t , u )) - Why? If v is the i th closest node to s , then d ′ ( s , v ) = dist ( s , v ) .

  35. Finding the i th closest node 1 Fall 2017 27 CS374 Sariel Har-Peled (UIUC) Lemma 2 1 2 to u using only nodes in X as intermediate vertices. 1 2 27 / 42 X contains the i − 1 closest nodes to s Want to fjnd the i th closest node from V − X . For each u ∈ V − X let P ( s , u , X ) be a shortest path from s Let d ′ ( s , u ) be the length of P ( s , u , X ) Observations: for each u ∈ V − X , dist ( s , u ) ≤ d ′ ( s , u ) since we are constraining the paths d ′ ( s , u ) = min t ∈ X ( dist ( s , t ) + ℓ ( t , u )) - Why? If v is the i th closest node to s , then d ′ ( s , v ) = dist ( s , v ) .

  36. Finding the i th closest node Let v be the i th closest node to s . Then there is a shortest path P Fall 2017 28 CS374 Sariel Har-Peled (UIUC) from s to v that contains only nodes in X as intermediate nodes (see Proof. Lemma 2 1 Given: 28 / 42 X : Set of i − 1 closest nodes to s . d ′ ( s , u ) = min t ∈ X ( dist ( s , t ) + ℓ ( t , u )) If v is an i th closest node to s , then d ′ ( s , v ) = dist ( s , v ) . previous claim). Therefore d ′ ( s , v ) = dist ( s , v ) .

  37. Finding the i th closest node Lemma Corollary Proof. Sariel Har-Peled (UIUC) CS374 29 Fall 2017 29 / 42 If v is an i th closest node to s , then d ′ ( s , v ) = dist ( s , v ) . The i th closest node to s is the node v ∈ V − X such that d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) . For every node u ∈ V − X , dist ( s , u ) ≤ d ′ ( s , u ) and for the i th closest node v , dist ( s , v ) = d ′ ( s , v ) . Moreover, dist ( s , u ) ≥ dist ( s , v ) for each u ∈ V − X .

  38. Algorithm Correctness: By induction on i using previous lemmas. Fall 2017 30 CS374 Sariel Har-Peled (UIUC) 1 30 / 42 using only X as intermediate nodes*) Initialize for each node v : dist ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do (* Invariant: X contains the i − 1 closest nodes to s *) (* Invariant: d ′ ( s , u ) is shortest path distance from u to s Let v be such that d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } for each node u in V − X do � � d ′ ( s , u ) = min t ∈ X dist ( s , t ) + ℓ ( t , u ) Running time: O ( n · ( n + m )) time. n outer iterations. In each iteration, d ′ ( s , u ) for each u by scanning all edges out of nodes in X ; O ( m + n ) time/iteration.

  39. Algorithm Correctness: By induction on i using previous lemmas. Fall 2017 30 CS374 Sariel Har-Peled (UIUC) 1 30 / 42 using only X as intermediate nodes*) Initialize for each node v : dist ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do (* Invariant: X contains the i − 1 closest nodes to s *) (* Invariant: d ′ ( s , u ) is shortest path distance from u to s Let v be such that d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } for each node u in V − X do � � d ′ ( s , u ) = min t ∈ X dist ( s , t ) + ℓ ( t , u ) Running time: O ( n · ( n + m )) time. n outer iterations. In each iteration, d ′ ( s , u ) for each u by scanning all edges out of nodes in X ; O ( m + n ) time/iteration.

  40. Algorithm Correctness: By induction on i using previous lemmas. Fall 2017 30 CS374 Sariel Har-Peled (UIUC) 1 30 / 42 using only X as intermediate nodes*) Initialize for each node v : dist ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do (* Invariant: X contains the i − 1 closest nodes to s *) (* Invariant: d ′ ( s , u ) is shortest path distance from u to s Let v be such that d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } for each node u in V − X do � � d ′ ( s , u ) = min t ∈ X dist ( s , t ) + ℓ ( t , u ) Running time: O ( n · ( n + m )) time. n outer iterations. In each iteration, d ′ ( s , u ) for each u by scanning all edges out of nodes in X ; O ( m + n ) time/iteration.

  41. Algorithm Correctness: By induction on i using previous lemmas. Fall 2017 30 CS374 Sariel Har-Peled (UIUC) 1 30 / 42 using only X as intermediate nodes*) Initialize for each node v : dist ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do (* Invariant: X contains the i − 1 closest nodes to s *) (* Invariant: d ′ ( s , u ) is shortest path distance from u to s Let v be such that d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } for each node u in V − X do � � d ′ ( s , u ) = min t ∈ X dist ( s , t ) + ℓ ( t , u ) Running time: O ( n · ( n + m )) time. n outer iterations. In each iteration, d ′ ( s , u ) for each u by scanning all edges out of nodes in X ; O ( m + n ) time/iteration.

  42. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 18 6 0 6 6 30 11 13 19 20 6 8 16 25

  43. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 S 10 a 9 18 6 0 0 6 6 30 11 13 19 20 6 8 16 25

  44. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 S 10 a 9 18 6 0 0 6 6 30 11 13 19 20 6 8 16 25

  45. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 S 10 a 9 9 18 6 0 0 6 6 6 30 11 13 19 20 6 8 16 25 13

  46. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 S 10 a 9 9 18 6 0 0 6 6 6 6 30 11 13 19 20 6 8 16 25 13

  47. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 S 10 a 9 9 18 6 0 0 6 6 6 6 30 11 13 19 20 6 8 16 25 13

  48. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 S 10 a 9 24 9 18 6 0 0 6 6 6 6 30 11 13 36 19 20 6 8 16 25 13

  49. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 24 9 18 6 0 0 6 6 6 6 30 11 13 36 19 20 6 8 16 25 13

  50. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 24 9 18 6 0 0 6 6 6 6 30 11 13 36 19 20 6 8 16 25 13

  51. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 24 9 18 6 0 0 6 6 6 6 30 11 13 36 19 20 6 8 16 25 13 13

  52. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 24 9 18 6 0 0 6 6 6 6 30 11 13 36 33 19 20 6 8 16 25 38 13 13

  53. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 19 24 9 18 6 0 0 6 6 6 6 30 11 13 36 33 19 20 6 8 16 25 38 13 13

  54. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 19 24 9 18 6 0 0 6 6 6 6 30 11 13 33 36 36 25 19 20 6 8 16 25 38 13 13

  55. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 19 24 9 18 6 0 0 6 6 6 6 30 11 13 36 36 33 25 25 19 20 6 8 16 25 38 13 13

  56. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 19 24 9 18 6 0 0 6 6 6 6 36 30 11 13 36 36 33 25 25 19 20 6 8 16 25 38 13 13

  57. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 24 19 9 18 6 0 0 6 6 6 6 36 36 36 30 11 13 25 25 36 36 33 19 20 6 8 16 25 38 13 13

  58. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 24 19 9 18 6 0 0 6 6 6 6 36 36 36 30 11 13 25 25 36 36 33 19 20 6 8 16 25 38 13 13

  59. Example: Dijkstra algorithm in action Sariel Har-Peled (UIUC) Fall 2017 31 CS374 31 / 42 10 a 9 9 19 24 19 9 18 6 0 0 6 6 6 6 36 36 36 30 11 13 25 25 36 36 33 19 20 6 8 16 25 38 13 38 13

  60. Improved Algorithm 1 Fall 2017 32 CS374 Sariel Har-Peled (UIUC) time values is O n s u Finding v from d 3 2 n outer iterations and in each iteration following steps 1 32 / 42 2 // Main work is to compute the d ′ ( s , u ) values in each iteration d ′ ( s , u ) changes from iteration i to i + 1 only because of the node v that is added to X in iteration i . Initialize for each node v , dist ( s , v ) = d ′ ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do // X contains the i − 1 closest nodes to s , and the values of d ′ ( s , u ) are current Let v be node realizing d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } Update d ′ ( s , u ) for each u in V − X as follows: � � d ′ ( s , u ) = min d ′ ( s , u ) , dist ( s , v ) + ℓ ( v , u ) Running time: O ( m + n 2 ) time. updating d ′ ( s , u ) after v is added takes O ( deg ( v )) time so total work is O ( m ) since a node enters X only once

  61. Improved Algorithm 1 Fall 2017 32 CS374 Sariel Har-Peled (UIUC) time values is O n s u Finding v from d 3 2 n outer iterations and in each iteration following steps 1 32 / 42 2 // Main work is to compute the d ′ ( s , u ) values in each iteration d ′ ( s , u ) changes from iteration i to i + 1 only because of the node v that is added to X in iteration i . Initialize for each node v , dist ( s , v ) = d ′ ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do // X contains the i − 1 closest nodes to s , and the values of d ′ ( s , u ) are current Let v be node realizing d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } Update d ′ ( s , u ) for each u in V − X as follows: � � d ′ ( s , u ) = min d ′ ( s , u ) , dist ( s , v ) + ℓ ( v , u ) Running time: O ( m + n 2 ) time. updating d ′ ( s , u ) after v is added takes O ( deg ( v )) time so total work is O ( m ) since a node enters X only once

  62. Improved Algorithm 1 Fall 2017 32 CS374 Sariel Har-Peled (UIUC) 3 2 n outer iterations and in each iteration following steps 32 / 42 // Initialize for each node v , dist ( s , v ) = d ′ ( s , v ) = ∞ Initialize X = ∅ , d ′ ( s , s ) = 0 for i = 1 to | V | do // X contains the i − 1 closest nodes to s , and the values of d ′ ( s , u ) are current Let v be node realizing d ′ ( s , v ) = min u ∈ V − X d ′ ( s , u ) dist ( s , v ) = d ′ ( s , v ) X = X ∪ { v } Update d ′ ( s , u ) for each u in V − X as follows: � � d ′ ( s , u ) = min d ′ ( s , u ) , dist ( s , v ) + ℓ ( v , u ) Running time: O ( m + n 2 ) time. updating d ′ ( s , u ) after v is added takes O ( deg ( v )) time so total work is O ( m ) since a node enters X only once Finding v from d ′ ( s , u ) values is O ( n ) time

  63. Dijkstra’s Algorithm 1 Fall 2017 33 CS374 Sariel Har-Peled (UIUC) 2 1 Priority Queues to maintain dist values for faster running time 33 / 42 2 update dist values after adding v by scanning edges out of v eliminate d ′ ( s , u ) and let dist ( s , u ) maintain it Initialize for each node v, dist ( s , v ) = ∞ Initialize X = ∅ , dist ( s , s ) = 0 for i = 1 to | V | do Let v be such that dist ( s , v ) = min u ∈ V − X dist ( s , u ) X = X ∪ { v } for each u in Adj ( v ) do � � dist ( s , u ) = min dist ( s , u ) , dist ( s , v ) + ℓ ( v , u ) Using heaps and standard priority queues: O (( m + n ) log n ) Using Fibonacci heaps: O ( m + n log n ) .

  64. Dijkstra’s Algorithm 1 Fall 2017 33 CS374 Sariel Har-Peled (UIUC) 2 1 Priority Queues to maintain dist values for faster running time 33 / 42 2 update dist values after adding v by scanning edges out of v eliminate d ′ ( s , u ) and let dist ( s , u ) maintain it Initialize for each node v, dist ( s , v ) = ∞ Initialize X = ∅ , dist ( s , s ) = 0 for i = 1 to | V | do Let v be such that dist ( s , v ) = min u ∈ V − X dist ( s , u ) X = X ∪ { v } for each u in Adj ( v ) do � � dist ( s , u ) = min dist ( s , u ) , dist ( s , v ) + ℓ ( v , u ) Using heaps and standard priority queues: O (( m + n ) log n ) Using Fibonacci heaps: O ( m + n log n ) .

  65. Priority Queues 5 Fall 2017 34 CS374 Sariel Har-Peled (UIUC) decreaseKey is implemented via delete and insert . meld : merge two separate priority queues into one. 7 6 Data structure to store a set S of n elements where each element delete ( v ): Remove element v from S . 4 3 fjndMin : fjnd the minimum key in S . 2 makePQ : create an empty queue. 1 following operations: 34 / 42 v ∈ S has an associated real/integer key k ( v ) such that the extractMin : Remove v ∈ S with smallest key and return it. insert ( v , k ( v ) ): Add new element v with key k ( v ) to S . decreaseKey ( v , k ′ ( v ) ): decrease key of v from k ( v ) (current key) to k ′ ( v ) (new key). Assumption: k ′ ( v ) ≤ k ( v ) . All operations can be performed in O (log n ) time.

  66. Priority Queues 5 Fall 2017 34 CS374 Sariel Har-Peled (UIUC) decreaseKey is implemented via delete and insert . meld : merge two separate priority queues into one. 7 6 Data structure to store a set S of n elements where each element delete ( v ): Remove element v from S . 4 3 fjndMin : fjnd the minimum key in S . 2 makePQ : create an empty queue. 1 following operations: 34 / 42 v ∈ S has an associated real/integer key k ( v ) such that the extractMin : Remove v ∈ S with smallest key and return it. insert ( v , k ( v ) ): Add new element v with key k ( v ) to S . decreaseKey ( v , k ′ ( v ) ): decrease key of v from k ( v ) (current key) to k ′ ( v ) (new key). Assumption: k ′ ( v ) ≤ k ( v ) . All operations can be performed in O (log n ) time.

  67. Priority Queues 5 Fall 2017 34 CS374 Sariel Har-Peled (UIUC) decreaseKey is implemented via delete and insert . meld : merge two separate priority queues into one. 7 6 Data structure to store a set S of n elements where each element delete ( v ): Remove element v from S . 4 3 fjndMin : fjnd the minimum key in S . 2 makePQ : create an empty queue. 1 following operations: 34 / 42 v ∈ S has an associated real/integer key k ( v ) such that the extractMin : Remove v ∈ S with smallest key and return it. insert ( v , k ( v ) ): Add new element v with key k ( v ) to S . decreaseKey ( v , k ′ ( v ) ): decrease key of v from k ( v ) (current key) to k ′ ( v ) (new key). Assumption: k ′ ( v ) ≤ k ( v ) . All operations can be performed in O (log n ) time.

  68. Dijkstra’s Algorithm using Priority Queues . Fall 2017 35 CS374 Sariel Har-Peled (UIUC) 3 2 1 Priority Queue operations: 35 / 42 decreaseKey Q ← makePQ () insert ( Q , ( s , 0 ) ) for each node u � = s do insert ( Q , ( u , ∞ )) X ← ∅ for i = 1 to | V | do ( v , dist ( s , v )) = extractMin ( Q ) X = X ∪ { v } for each u in Adj ( v ) do � ��� � � Q , u , min dist ( s , u ) , dist ( s , v ) + ℓ ( v , u ) O ( n ) insert operations O ( n ) extractMin operations O ( m ) decreaseKey operations

  69. Implementing Priority Queues via Heaps Using Heaps Store elements in a heap based on the key value 1 Sariel Har-Peled (UIUC) CS374 36 Fall 2017 36 / 42 All operations can be done in O (log n ) time Dijkstra’s algorithm can be implemented in O (( n + m ) log n ) time.

  70. Implementing Priority Queues via Heaps Using Heaps Store elements in a heap based on the key value 1 Sariel Har-Peled (UIUC) CS374 36 Fall 2017 36 / 42 All operations can be done in O (log n ) time Dijkstra’s algorithm can be implemented in O (( n + m ) log n ) time.

  71. Priority Queues: Fibonacci Heaps/Relaxed Heaps 2 Fall 2017 37 CS374 Sariel Har-Peled (UIUC) (European Symposium on Algorithms, September 2009!) implement, and perform well in practice. Rank-Pairing Heaps work has obtained data structures that are easier to analyze and Data structures are complicated to analyze/implement. Recent 37 / 42 Fibonacci Heaps 1 3 2 1 extractMin , insert , delete , meld in O (log n ) time decreaseKey in O ( 1 ) amortized time: ℓ decreaseKey operations for ℓ ≥ n take together O ( ℓ ) time Relaxed Heaps: decreaseKey in O ( 1 ) worst case time but at the expense of meld (not necessary for Dijkstra’s algorithm) Dijkstra’s algorithm can be implemented in O ( n log n + m ) time. If m = Ω( n log n ) , running time is linear in input size.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend