dijkstra s algorithm shortest path problem
play

Dijkstra s Algorithm Shortest Path Problem Directed graph G = (V - PowerPoint PPT Presentation

Dijkstra s Algorithm Shortest Path Problem Directed graph G = (V , E) Source s l e = length of edge e l e 0 for all edges e Shortest path problem: (Google Maps!) find shortest path from s to all other nodes 1 a c 2 3 1 1 1 e


  1. Dijkstra’ s Algorithm

  2. Shortest Path Problem Directed graph G = (V , E) Source s l e = length of edge e l e ≥ 0 for all edges e Shortest path problem: (Google Maps!) find shortest path from s to all other nodes 1 a c 2 3 1 1 1 e f 4 b d 1

  3. Simplification For now, let’ s just find the lengths of the shortest paths to every other node We’ll show how to recover the paths later

  4. Dijkstra’ s Algorithm Idea: explore outward by distance Maintain set S of explored nodes: for u ∈ S, we know the length d(u) of the shortest path from s to u. Initialize S = { s }, d(s) = 0. Repeatedly find shortest path to any node v ∉ S that remains in S until the final edge e = (u, v) S = S ∪ {v}, d(v) = d(u) + l e 1 a c 2 3 1 S 1 1 e f 4 b d 1

  5. Dijkstra’ s Algorithm Repeatedly find shortest path to any node v ∉ S that remains in S until the final edge e = (u, v) Minimize: d’(v) = min { d(u) + l e : u ∈ S, e = (u, v) ∈ E } 1 a c 2 3 1 S 1 1 e f 4 b d 1

  6. Dijkstra’ s Algorithm Node d() 1 a c 2 3 1 1 1 e f 4 b d 1

  7. Dijkstra’ s Algorithm Node d() a 0 1 a c 2 3 1 1 1 e f 4 b d 1

  8. Dijkstra’ s Algorithm Node d() a 0 1 a c 2 b 1 3 1 1 1 e f 4 b d 1

  9. Dijkstra’ s Algorithm Node d() a 0 1 a c 2 b 1 3 1 1 1 e f c 1 4 b d 1

  10. Dijkstra’ s Algorithm Node d() a 0 1 a c 2 b 1 3 1 1 1 e f c 1 4 b d d 2 1

  11. Dijkstra’ s Algorithm Node d() a 0 1 a c 2 b 1 3 1 1 1 e f c 1 4 b d d 2 1 e 3

  12. Dijkstra’ s Algorithm Node d() a 0 1 a c 2 b 1 3 1 1 1 e f c 1 4 b d d 2 1 e 3 f 4

  13. Example Second example on board

  14. Dijkstra’ s Algorithm: Implementation Dijkstra’ s Algorithm (G, s) { � S = {s} / / S is the set of explored nodes � d(s) = 0 � / / d is the distance to the node from s � while S ≠ V { / / there are unexplored nodes select a node v ∉ S with at least one edge from S to minimize d’(v) = min { d(u) + l e : u ∈ S, e = (u, v) ∈ E } � � add v to S � � d(v) = d’(v) � } How do we recover a path with length d(v)?

  15. Dijkstra’ s Algorithm: Implementation Dijkstra’ s Algorithm (G, s) { � S = {s} / / S is the set of explored nodes � d(s) = 0 � / / d is the distance to the node from s � while S ≠ V { / / there are unexplored nodes select a node v ∉ S with at least one edge from S to minimize d’(v) = min { d(u) + l e : u ∈ S, e = (u, v) ∈ E } � � add v to S � � d(v) = d’(v) prev(v) = argmin { d(u) + l e : u ∈ S, e = (u, v) ∈ E } � } Proof of correctness: start in small groups, complete on board

  16. Dijkstra’ s Algorithm: Implementation Dijkstra’ s Algorithm (G, s) { � S = {s} / / S is the set of explored nodes � d(s) = 0 � / / d is the distance to the node from s � while S ≠ V { / / there are unexplored nodes select a node v ∉ S with at least one edge from S to minimize d’(v) = min { d(u) + l e : u ∈ S, e = (u, v) ∈ E } � � add v to S � � d(v) = d’(v) prev(v) = argmin { d(u) + l e : u ∈ S, e = (u, v) ∈ E } � } How do we implement this efficiently?

  17. Dijkstra’ s Algorithm (G, s) { � S = {s} / / S is the set of explored nodes � d’(s) = 0 � / / explicitly maintain the d’ values d’(v) = ∞ for all v ∉ S while S != V { / / there are unexplored nodes Let v be the node that minimizes d’(v) d(v) = d’(v) for each edge (v, w) where v ∈ S, w ∉ S { � � � d’(v) = min( d’(v), d(v) + l(v, w) ) � � } � } } Data structure?

  18. Running Time With heap-based priority queue, running time of Dijkstra’ s algorithm is: O(m) - traverse edges O(n log n) - n extractMin operations O(m log n) - m changeKey operations Total: O(m log n)

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