1 / 15

Discussion #35 Dijkstra’s Algorithm

Discussion #35 Dijkstra’s Algorithm. Topics. Shortest path Greedy algorithms Dijkstra’s algorithm. b. c. a. e. d. Shortest Path. Minimum length path from one node to another (e.g. from a to e , the shortest path is 2) Algorithms Breadth-First-Search, O( n 2 ) in the worst case

frisco
Download Presentation

Discussion #35 Dijkstra’s Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discussion #35Dijkstra’s Algorithm Chapter 7, Section 5.5

  2. Topics • Shortest path • Greedy algorithms • Dijkstra’s algorithm Chapter 7, Section 5.5

  3. b c a e d Shortest Path • Minimum length path from one node to another (e.g. from a to e, the shortest path is 2) • Algorithms • Breadth-First-Search, O(n2) in the worst case • Floyd’s, O(n3); but finds all • BFS, starting at each node, O(nm), which beats Floyd’s for sparse graphs Chapter 7, Section 5.5

  4. b c a e d 5 2 2 10 1 1 1 Shortest Path in Weighted Graphs • Weights on edges: positive numbers • Algorithms • Simple BFS no longer works (e.g. the shortest path from a to b is not the edge that connects them) • Floyd’s, O(n3); finds all • Is there an algorithm that beats Floyd’s? • for the single best path? • for all paths? Chapter 7, Section 5.5

  5. b c a e d 5 2 2 10 1 1 1 Dijkstra’s Algorithm • Find the shortest weighted path between two given nodes. (Easier to find the minimum from one to all.) • Intuitively, special BFS: choose smallest accumulated path length • Algorithm • Initialize D (distance) table. • Until all nodes are settled, • 2a. find smallest distance & settle node. • 2b. adjust distances in D for unsettled nodes. Chapter 7, Section 5.5

  6. Important Aside: Greedy Algorithms • A greedy algorithm always takes the best immediate or local solution while finding an answer. • Greedy algorithms find optimal solutions for some optimization problems, but may find (far) less-than-optimal solutions for other optimization problems. • Dijkstra’s algorithm is greedy. • There is no greedy algorithm for the traveling salesman problem (find the shortest path to visit all nodes). • When greedy algorithms work, they are usually best. Chapter 7, Section 5.5

  7. 5 2 2 10 1 1 d e a c b 1 iteration distance settled a b c d e 0 0 5 2 1  a adjust w/d 1 5 2 1  a,d 2 adjust w/c 2 5 2 2 a,d,c 4 adjust w/e 3 4 2 a,d,c,e 4 4 a,d,c,e,b Trace of Dijkstra’s Algorithm 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node. -adjust distances in D for unsettled nodes. all nodes settled Circled numbers: shortest path lengths from a. Chapter 7, Section 5.5

  8. 6 2 4 10 1 1 d e a c b 1 iteration distance settled a b c d e 0 0 6 4 1  a adjust w/d 1 6 4 1  a,d 2 adjust w/e 2 6 4 2 a,d,e 3 5 adjust w/c 3 6 3 a,d,e,c 4 5 a,d,e,c,b Trace with Different Weights 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node. -adjust distances in D for unsettled nodes. all nodes settled Circled numbers: shortest path lengths from a. Chapter 7, Section 5.5

  9. Proof that Dijkstra’s Algorithm Works • Loop Invariant: For each settled node x, the minimum distance from the start node s to x is D(x). • Additional observations (which help us in the proof) are also loop invariants. • (a) The shortest path from s to any settled node v consists only of settled nodes. • (b) The shortest path from s to any unsettled node y is at least the largest settled difference so far. Chapter 7, Section 5.5

  10. Proof by Induction(on the number of iterations) • Basis: 0 iterations: x = s, D(s) = 0 (which is the minimum since all weights are positive) • For (a): initially s = v, the only settled node • For (b): initially all nodes except s are unsettled, and the shortest path to each is at least 0, indeed > 0. • Induction: Assume that the main loop invariant and the loop invariants (a) and (b) hold for k iterations and show that they hold for k+1 iterations. Chapter 7, Section 5.5

  11. k settled k+1 settled s . . . v  x s . . . v  x s . . . v  x . . . w  y Proof of Induction Part (Sketch) Assume Dijkstra’s algorithm selects x to be settled. Assume that the main loop invariant does not hold for the k+1st iteration, then there is a shorter path from s to x. By the induction hypotheses, since Dijkstra’s algorithm chooses x, the path from s to x is the shortest using settled nodes, and the path from s to y is no shorter than s to x. Since all weights are positive, the path from y to x is at least 1, so that the assumed shorter path cannot exist. Further (a) and (b) continue to hold: (a) After settling x, the path from s to x consists of only settled nodes. (b) The adjustments are only for paths from x to any unsettled nodes. Since the distance from s to any unsettled node y is at least D(x), and D(x) is the largest settled distance so far. Chapter 7, Section 5.5

  12. b c a e d Unroll the loop: n + k1 + n + k2 + … + n + k(n-1) = (n1)n = O(n2) = 2(m  # of edges in the initialization) = O(m) 5 2 2 10 1 1 1 Big-Oh for Dijkstra’s Algorithm Adjacency List: • O(n)  visit at most all edges for a node  can’t be more than n • O(n)  each node needs to be settled 2a. O(n)  look through D(x) list to find smallest 2b. O(k)  for chosen node, go through list of length k  1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. { O(n2) dominates Over all iterations, the k’s add up to 2(m  # of edges in the initialization). Chapter 7, Section 5.5

  13. d b c a e 5 2 2 10 1 1 1 Big-Oh for Dijkstra’s Algorithm Adjacency List: Clever using POTs: • O(nlogn)  POT (Partially Ordered Tree) construction dominates • O(n)  each node needs to be settled 2a. O(logn)  swap and bubble down 2b. O(klogn)  for chosen node, go through list of length k  1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. { O(mlogn) dominates Over all iterations, the k’s add up to 2(m  # of edges in the initialization). Chapter 7, Section 5.5

  14. b c a e d 5 2 2 10 1 1 1 POT Construction/Manipulation POT construction (start = a) (b,5) (c,2) (c,2) (d,1) (d,1) (b,5)      (c,2) (b,5) (b,5) (d,1) (b,5) (c,2) (b,5) (c,2) (e,) Find smallest  always on top; then swap and bubble down no bubbling needed swap bubble down adjust (d,1) (e,) (c,2) (c,2) (c,2) …       (e,) (b,5) (c,2) (b,5) (b,5) (e,2) (b,5) (c,2) (b,5) (e,2) (e,) (d,1) Adjust distances  POT doubly linked to adjacency list nodes  bubble up and down, as needed, when going through list of chosen node to adjust values Chapter 7, Section 5.5

  15. Dijkstra’s Algorithm vs. Floyd’s • Dijkstra’s algorithm is O(mlogn) = O(n2logn) in the worst case. • Floyd’s algorithm is O(n3), but computes all shortest paths. • Dijkstra’s algorithm can compute all shortest paths by starting it n times, once for each node. Thus, to compute all paths, Dijkstra’s algorithm is O(nmlogn) = O(n3logn) in the worst case. • Which is better depends on the application. • Dijkstra’s algorithm is better if only a path from a single node to another or to all others is needed. • For all paths, Dijkstra’s algorithm is better for sparse graphs, and Floyd’s algorithm is better for dense graphs. Chapter 7, Section 5.5

More Related