CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms Professor Henry Carter Fall 2016
Recap Greedy algorithms iterate locally optimal choices to construct a globally optimal solution Minimum spanning trees are subgraphs that connect all vertices with the cheapest edge weights Prim s algorithm illustrates greedy design on adding vertices 2
Minimum Spanning Tree Given: a weighted, undirected, connected graph Find: a subgraph that connects all vertices with the cheapest edge weights Applications: network infrastructure, data set clustering, approximation algorithms 3
Greedy Approaches How could we greedily add vertices to the MST? How could we greedily add edges to the MST? 4
Kruskal s Algorithm Greedy approach to edges Maintains one set: edges in the MST (and counters for number of edges considered and number of edges in the MST) Add the minimum cost edge that does NOT form a cycle 5
Example b 1 d 3 4 4 6 a 5 c 5 e 6 1 8 f 6
Kruskal s Algorithm Kruskal(G) input : A weighted connected graph G = hv,ei. output: E T, the set of edges composing a minimum spanning tree of G. Sort E in nondecreasing order of the edge weights w(e i1 ) apple apple w(e i E ) E T ;; ecounter 0 k 0 while ecounter < V 1 do k k +1 if E T [ {e ik } is acyclic then E T E T [ {e ik }; ecounter ecounter +1 end end return E T 7
Finding Cycles Consider the starting vertices as disjoint sets When an edge is added, we take the union of the sets containing the endpoint vertices To check for cycles, ensure that the two endpoints are in disjoint subsets (i.e. disjoint subtrees) Three operations: Makeset(x) Find(x) Union(S 1, S 2 ) 8
Analysis Worst case: E iterations Testing for cycles (i.e., searching for disconnected components): O( E log V ) Union Find Sorting edges by weight: O( E log E ) 9
Practice b 1 d 5 3 4 6 a 6 c 2 e Find the MST using Kruskal s algorithm: 10
Shortest-Path Finding Recall: Floyd s algorithm Given a single source, find the shortest path to: a single destination all other vertices Single-source shortest-path problem 11
Dijkstra s Algorithm The shortest path from s to b is the shortest of the paths to all of b s neighbors plus the edge connecting b Considering the source, update the shortest path to all adjacent vertices Iterate for the next closest vertex u, saving the shortest distance to all of u s neighbors that passes through u 12
Example b 4 d 3 2 5 6 a 7 c 1 e 13
Reconstructing the shortest path b 4 d 3 2 5 6 a 7 c 1 e A 0 - B 3 A C 5 B E 6 C D 7 B 14
Dijkstra s Algorithm Dijkstra(G, s) input : A weighted connected graph G = hv,ei with nonnegative weights and a source vertex s. output: The length d v of a shortest path from s to v and its penultimate vertex p v for every vertex in V. Initialize(Q) for every vertex v 2 V do d v 1; p v null Insert(Q, v, d v ) end d s 0; Decrease(Q, s, d s ) V T ; for i 0 to V 1 do u DeleteMin(Q) V T V T [ {u } for every vertex u in V V T that is adjacent to u do if d u + w(u,u) <d u then d u d u + w(u,u); p u u Decrease(Q, u, d u ) end end end return V T 15
Analysis Similar analysis to Prim s algorithm Weight matrix and unordered priority queue: V 2 Min-heap: E log V Can be improved in theory with a Fibonacci heap 16
Practice b 4 d 3 2 5 6 a 7 c 4 e 17
Recap Greedy approach to graph problems iteratively searches for the locally optimal solution Single-source shortest-path problem Dijkstra s algorithm assumes the min distance to b is the min distance to b s neighbor plus the edge connecting b 18
Next Time... Levitin Chapter 9.4, 10.1 Remember, you need to read it BEFORE you come to class! Homework: 9.2: 1, 2, 4, 5 9.3: 2, 3, 4, 7 19