480 likes | 677 Views
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.
E N D
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 Weight of path p = {v1, v2, … vk} Shortest-path weight from u to v
Determining Path Lengths a 1 3 s b 5 0 2 4 c
Determining Path Lengths a 3 1 3 s b 5 0 5 2 4 c 2
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
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
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
Path Lengths - FIN a All vertices are in S 3 1 3 s b 5 0 4 2 4 c 2
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
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
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
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
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
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
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)
Example: Dijkstra’s t x 1 10 s 9 2 3 4 6 0 7 5 y 2 z
Example: Dijkstra’s t x 1 10 10 s 9 2 3 4 6 0 7 5 5 y 2 z
Example: Dijkstra’s t x 1 8 14 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z
Example: Dijkstra’s t x 1 8 13 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z
Example: Dijkstra’s t x 1 8 9 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z
Dijkstra’s - FIN t x 1 8 9 10 s 9 2 3 4 6 0 7 5 7 5 y 2 z
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)
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
A Graph 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's start 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Prim's 5 4 4 2 5 2 5 6 6 44 5
The Same Graph 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Kruskal's 5 4 9 4 2 5 12 8 6 7 2 5 8 6 8 7 9 12 6 5 11
Kruskal's 5 4 4 2 5 2 5 6 6 44 5