1 / 44

Single-Source Shortest Path & Minimum Spanning Trees

Single-Source Shortest Path & Minimum Spanning Trees. Dijkstra’s , Prim’s, and Kruskal’s Algorithms. Single-Source Shortest Path Defined. Given G = (V, E) Source vertex s  V Compute {SP} = set of shortest paths from s to all vertices in V. Path Weight Defined.

tekla
Download Presentation

Single-Source Shortest Path & Minimum Spanning Trees

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. Single-Source Shortest Path&Minimum Spanning Trees Dijkstra’s, Prim’s, and Kruskal’s Algorithms

  2. Single-Source Shortest Path Defined • Given • G = (V, E) • Source vertex s  V • Compute • {SP} = set of shortest paths from s to all vertices in V

  3. Path Weight Defined Weight of path p = {v1, v2, … vk} Shortest-path weight from u to v

  4. Determining Path Lengths a 1 3 s b 5 0 2 4 c

  5. Determining Path Lengths a 3 1 3 s b 5 0 5 2 4 c 2

  6. Determining Path Lengths a 3 1 3 s b 5 0 5 2 4 c d[b] <= d[c] + w(c,b) 5 <= 2 + 4 2

  7. Determining Path Lengths a d[b] > d[a] + w(a,b) 5 > 3 + 1 3 1 3 s b 5 0 5 2 4 c 2

  8. Determining Path Lengths a d[b] > d[a] + w(a,b) 5 > 3 + 1 3 1 3 s b 5 0 4 2 4 c 2 Reduce

  9. Path Lengths - FIN a All vertices are in S 3 1 3 s b 5 0 4 2 4 c 2

  10. Cycles in Paths a b -4 3 -1 3 4 6 s c d g 5 8 0 5 11 - -3 2 3 7 e f - - -6

  11. Properties of Shortest Paths • Cannot contain a negative-weight cycle • No need to contain 0-weight cycle • No need to contain positive-weight cycle • Thus contains no cycles • Will contain at most V-1 edges • Assumes no cycles (as stated above) • Maintain predecessor information • E & V

  12. Shortest Paths Not Unique t x 6 3 9 3 s 4 2 1 2 7 0 3 5 5 11 6 y z t x t x 3 9 3 9 6 6 3 3 s 4 s 4 0 0 2 1 2 7 2 1 2 7 3 3 5 11 5 11 5 5 6 6 y y z z

  13. Shortest Path Distance Is Unique x s u z v y [z] must be either x or y, but not both This is true even if d[x] + w(x,z) = d[y] + w(y,z) establishing that if s is source, d[z] is well-defined and unique

  14. Setting up the Solution • For each vertex v in V[G] • Predecessor of v ([v]) = NIL • Distance to v from source (d[v]) =  • RELAX: as we process the graph, we’ll • “Improve” [v] & d[v] by “relaxing” if (d[v] > d[u] + w(u,v)) d[v] = d[u] + w(u,v) [v] = u

  15. Init & Relax Initialize(G, s) for each v in V[G] d[v] = INFINITY [v] = NULLd[s] = 0 Relax(u, v, w) if (d[v] > d[u] + w(u,v)) d[v] = d[u] + w(u,v) [v] = u

  16. Dijkstra’s Algorithm (1959) Assume G = (V, E), source s, & weight function w Also assume all edges are non-negative Dijkstra(G, w, s) Initialize(G, s) S = {} Q = V[G] while Q != {} u = Extract_Min(Q) S.Add(u) for each vertex v in u.Adjacency() Relax(u, v, w)

  17. Example: Dijkstra’s t x 1   10 s 9 2 3 4 6 0 7   5 y 2 z

  18. Example: Dijkstra’s t x 1 10  10 s 9 2 3 4 6 0 7 5  5 y 2 z

  19. Example: Dijkstra’s t x 1 8 14 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z

  20. Example: Dijkstra’s t x 1 8 13 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z

  21. Example: Dijkstra’s t x 1 8 9 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z

  22. Dijkstra’s - FIN t x 1 8 9 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z

  23. Runtime of Dijkstra’s • O(V lgV + E lgV) • O(E lgV) • If G is connected • O(V lgV + E) • If we use a Fibonacci heap Better if V << E (i.e. sparsely connected)

  24. MST Defined

  25. Minimum Spanning Tree • Connect all nodes at minimum cost. • Can start anywhere? • Why? • One solution. • i.e. There is just one minimum • Two algorithms • Prim’s • Kruskal’s

  26. A Graph 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  27. Prim's start 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  28. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  29. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  30. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  31. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  32. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  33. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  34. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  35. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  36. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  37. Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  38. Prim's 5 4 4 2 5 2 5 6 6 44 5

  39. The Same Graph 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  40. Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  41. Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  42. Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  43. Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11

  44. Kruskal's 5 4 4 2 5 2 5 6 6 44 5

More Related