1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.

Slides:



Advertisements
Similar presentations
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Advertisements

Minimum Spanning Tree CSE 331 Section 2 James Daly.
Minimum Spanning Tree (MST) form a tree that connects all the vertices (spanning tree). minimize the total edge weight of the spanning tree. Problem Select.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Design and Analysis of Algorithms Minimum Spanning trees
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
Analysis of Algorithms
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
David Luebke 1 9/15/2015 CS 332: Algorithms Topological Sort Minimum Spanning Trees.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Finding Minimum Spanning Trees Algorithm Design and Analysis Week 4 Bibliography: [CLRS]- Chap 23 – Minimum.
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 2/23/2016 ITCS 6114 Topological Sort Minimum Spanning Trees.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms. Zhengjin,Central South University2 Review: Dynamic Programming Summary of the basic idea: Optimal substructure: optimal solution to.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
David Luebke 1 11/21/2016 CS 332: Algorithms Minimum Spanning Tree Shortest Paths.
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible locally optimal irrevocable.
Introduction to Algorithms
Topological Sort Minimum Spanning Tree
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
CISC 235: Topic 10 Graph Algorithms.
Many slides here are based on E. Demaine , D. Luebke slides
Many slides here are based on D. Luebke slides
Minimum Spanning Trees
CS 332: Algorithms Dijkstra’s Algorithm Continued Disjoint-Set Union
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Total running time is O(E lg E).
Finding Minimum Spanning Trees
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms

2  Giving credit where credit is due: »Most of slides for this lecture are based on slides created by Dr. David Luebke, University of Virginia. »Some examples and slides are based on lecture notes created by Dr. Chuck Cusack, Hope College, Dr. Jim Cohoon, University of Virginia, and Dr. Ben Choi, Louisiana Technical University. »I have modified them and added new slides

3 Greedy Algorithms  Main Concept: Make the best or greedy choice at any given step.  Choices are made in sequence such that »Each individual choice is best according to some limited “short- term” criterion, that is not too expensive to evaluate »Once a choice is made, it cannot be undone! v Even if it becomes evident later that it was a poor choice v Sometimes life is like that   The goal is to »take a sequence of locally optimal actions, hoping to yield a globally optimal solution.

4 When to be Greedy  Greedy algorithms apply to problems with »Optimal substructures: optimal solutions contain optimal sub- solutions. »The greedy choice property: an optimal solution can be obtained by making the greedy choice at each step. »Many optimal solutions, but we only need one such solution.

5 Minimum Spanning Tree (MST)  A spanning tree for a connected, undirected graph, G=(V, E) is 1. a connected subgraph of G that forms an 2. undirected tree incident with each vertex.  In a weighted graph G=(V, E, W), »the weight of a subgraph is the sum of the weights of the edges in the subgraph.  A minimum spanning tree (MST) for a weighted graph is »a spanning tree with the minimum weight.

6 Minimum Spanning Tree  Problem: given a connected, undirected, weighted graph: find a spanning tree using edges that minimize the total weight

7 Minimum Spanning Tree  Which edges form the minimum spanning tree (MST) of the graph? HBC GED F A

8 Minimum Spanning Tree  Answer: HBC GED F A

9 Another Example  Given a weighted graph G=(V, E,W), find a MST of G

10 Finding a MST  MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees  Principal greedy methods: algorithms by Prim and Kruskal  Prim »Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree v Intermediary solution is a subtree  Kruskal »Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far v Intermediary solution is a spanning forest

11 Prime’s Algorithm (High-Level Pseudocode)  Prime(G) //Input: A weighted connected graph G = //Output: E T --- the set of edges composing MST of G V T = {v 0 } E T =  for i = 1 to |V| - 1 do find a minimum-weight edge e* = (u*, v*) among all the edges (u, v) such that u is in V T and v is in V-V T V T = V T  {v*} E T = E T  {e*} return E T

12 Prime’s Algorithm (High-Level Pseudocode)  Prime(G) //Input: A weighted connected graph G = //Output: E T --- the set of edges composing MST of G V T = {v 0 } E T =  for i = 1 to |V| - 1 do find a minimum-weight edge e* = (u*, v*) among all the edges (u, v) such that u is in V T and v is in V-V T V T = V T  {v*} E T = E T  {e*} return E T HBC GED F A

13 Prime’s Algorithm (High-Level Pseudocode)  Prime(G) //Input: A weighted connected graph G = //Output: E T --- the set of edges composing MST of G V T = {v 0 } E T =  for i = 1 to |V| - 1 do find a minimum-weight edge e* = (u*, v*) among all the edges (u, v) such that u is in V T and v is in V-V T V T = V T  {v*} E T = E T  {e*} return E T HBC GED F A

14 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree Intermediary solution is a subtree

15 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); Run on example graph

16 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);     Run on example graph

17 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0    Pick a start vertex r r

18 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0    Red vertices have been removed from Q u

19 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  0  3  Red arrows indicate parent pointers u

20 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  0  3  u

21 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  0  3  u

22 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 14  08  3  u

23 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10  08  3  u

24 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 10  08  3  u

25 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102  08  3  u

26 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102   u

27 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 102   u

28 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);  u

29 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

30 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

31 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

32 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

33 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

34 Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u

35 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What is the hidden cost in this code?

36 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); delete the smallest element from the min-heap decrease an element’s value in the min-heap (outline an efficient algorithm for it)

37 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; DecreaseKey(v, w(u,v)); How often is ExtractMin() called? How often is DecreaseKey() called?

38 Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] =  ; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); What will be the running time? There are n=|V| ExtractMin calls and m=|E| DecreaseKey calls. The priority Q implementation has a large impact on performance. E.g., O((n+m)lg n) = O(m lg n) using min-heap for Q (for connected graph, n – 1  m)

39 Finding a MST  Principal greedy methods: algorithms by Prim and Kruskal  Prim »Grow a single tree by repeatedly adding the least cost edge that connects a vertex in the existing tree to a vertex not in the existing tree v Intermediary solution is a subtree  Kruskal »Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far v Intermediary solution is a spanning forest

40 MST Applications? HBC GED F A

41 MST Applications  Network design »telephone, electrical power, hydraulic, TV cable, computer, road  MST gives a minimum-cost network connecting all sites  MST: the most economical construction of a network

42 Kruskal’s Algorithm (High-Level Pseudocode)  Kruskal(G) //Input: A weighted connected graph G = //Output: E T --- the set of edges composing MST of G Sort E in nondecreasing order of the edge weight E T =  ; encounter = 0 //initialize the set of tree edges and its size k = 0 //initialize the number of processed edges while encounter < |V| - 1 k = k+1 if E T  {e k } is acyclic E T = E T  {e k }; encounter = encounter + 1 return E T

43 Kruskal’s Algorithm (High-Level Pseudocode)  Kruskal(G) //Input: A weighted connected graph G = //Output: E T --- the set of edges composing MST of G Sort E in nondecreasing order of the edge weight E T =  ; encounter = 0 //initialize the set of tree edges and its size k = 0 //initialize the number of processed edges while encounter < |V| - 1 k = k+1 if E T  {e k } is acyclic E T = E T  {e k }; encounter = encounter + 1 return E T HBC GED F A

44 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Grow a tree by repeatedly adding the least cost edge that does not introduce a cycle among the edges included so far Intermediary solution is a spanning forest

45 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

46 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

47 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

48 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

49 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

50 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2? Run the algorithm:

51 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

52 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

53 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

54 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? 21 Run the algorithm:

55 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

56 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

57 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

58 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

59 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

60 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? 8 21 Run the algorithm:

61 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

62 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

63 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } 2 19? Run the algorithm:

64 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

65 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } ? Run the algorithm:

66 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

67 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } Run the algorithm:

68 Kruskal’s Algorithm Kruskal() { T =  ; for each v  V MakeSet(v); sort E by increasing edge weight w for each (u,v)  E (in sorted order) if FindSet(u)  FindSet(v) T = T U {{u,v}}; Union(FindSet(u), FindSet(v)); } What will affect the running time? Let n=|V| and m=|E| O(n) MakeSet() calls 1 Sort O(m) FindSet() calls O(n) Union() calls

69 Kruskal’s Algorithm: Running Time  To summarize: »Sort edges: O(m lg m) »O(n) MakeSet()’s »O(m) FindSet()’s »O(n) Union()’s  Upshot: »Best disjoint subsets union-find algorithm makes above 3 operations only slightly worse than linear »Refer to the textbook for a discussion on the algorithms »Overall thus O(m lg m)

70 In-Class Exercise (9.1.7)

71 In-Class Exercise  Applying Kruskal’s algorithm to (b)