CS 3343: Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Introduction to Algorithms 6.046J/18.401J L ECTURE 16 Greedy Algorithms (and Graphs) Graph representation Minimum spanning trees Optimal substructure Greedy.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
David Luebke 1 9/10/2015 CS 332: Algorithms Single-Source Shortest Path.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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.
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
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.
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.
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
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.
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Algorithms
Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: feasible locally optimal irrevocable.
Topological Sort Minimum Spanning Tree
Minimum Spanning Trees
Minimum Spanning Tree Shortest Paths
CSC 413/513: Intro to Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Many slides here are based on E. Demaine , D. Luebke slides
CS6045: Advanced Algorithms
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
Minimum Spanning Trees
Analysis of Algorithms CS 477/677
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Algorithms Searching in a Graph.
Minimum spanning tree Shortest path algorithms
Greedy Algorithms Comp 122, Spring 2004.
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Finding Minimum Spanning Trees
Greedy Algorithms (I) Greed, for lack of a better word, is good! Greed is right! Greed works! - Michael Douglas, U.S. actor in the role of Gordon Gecko,
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Presentation transcript:

CS 3343: Analysis of Algorithms Lecture 22: Minimum Spanning Tree

Outline Review of last lecture Prim’s algorithm and Kruskal’s algorithm for MST in detail

Graph Review G = (V, E) Types of graphs V: a set of vertices E: a set of edges Degree: number of edges In-degree vs out-degree Types of graphs Directed / undirected Weighted / un-weighted Dense / sparse Connected / disconnected

Graph representations Adjacency matrix A 1 2 3 4 1 2 4 3 How much storage does the adjacency matrix require? A: O(V2)

Graph representations Adjacency list 1 2 3 3 2 4 3 3 How much storage does the adjacency list require? A: O(V+E)

Graph representations Undirected graph 4 3 2 1 A 1 2 4 3 2 3 1 3 1 2 4 3

Graph representations Weighted graph 4 3 2 9 6 5 1 A 1 5 2 6 4 9 4 3 2,5 3,6 1,5 3,9 1,6 2,9 4,4 3,4

Analysis of time complexity Convention: Number of vertices: n, |V|, V Number of edges: m, |E|, E O(V+E) is the same as O(n+m) or O(|V|+|E|)

Tradeoffs between the two representations |V| = n, |E| = m Adj Matrix Adj List test (u, v)  E Θ(1) O(n) Degree(u) Θ(n) Memory Θ(n2) Θ(n+m) Edge insertion Edge deletion Graph traversal Both representations are very useful and have different properties, although adjacency lists are probably better for most problems

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph: 14 10 3 6 4 5 2 9 15 8

Minimum Spanning Tree Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 6 4 5 9 A spanning tree is a tree that connects all vertices Number of edges = ? A spanning tree has no designated root. 14 2 10 15 3 8

Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees Let T be an MST of G with an edge (u,v) in the middle Removing (u,v) partitions T into two trees T1 and T2 w(T) = w(u,v) + w(T1) + w(T2) Claim 1: T1 is an MST of G1 = (V1, E1), and T2 is an MST of G2 = (V2, E2) Proof by contradiction: if T1 is not optimal, we can replace T1 with a better spanning tree, T1’ T1’, T2 and (u, v) form a new spanning tree T’ W(T’) < W(T). Contradiction. T1 T2 u v T1’

Minimum Spanning Tree MSTs satisfy the optimal substructure property: an optimal tree is composed of optimal subtrees Let T be an MST of G with an edge (u,v) in the middle Removing (u,v) partitions T into two trees T1 and T2 w(T) = w(u,v) + w(T1) + w(T2) Claim 2: (u, v) is the lightest edge connecting G1 = (V1, E1) and G2 = (V2, E2) Proof by contradiction: if (u, v) is not the lightest edge, we can remove it, and reconnect T1 and T2 with a lighter edge (x, y) T1, T2 and (x, y) form a new spanning tree T’ W(T’) < W(T). Contradiction. T1 T2 x y u v

Algorithms Generic idea: Two of the most well-known algorithms Compute MSTs for sub-graphs Connect two MSTs for sub-graphs with the lightest edge Two of the most well-known algorithms Prim’s algorithm Kruskal’s algorithm Let’s first talk about the ideas behind the algorithms without worrying about the implementation and analysis

Prim’s algorithm Basic idea: Start from an arbitrary single node A MST for a single node has no edge Gradually build up a single larger and larger MST 6 5 Not yet discovered 7 Fully explored nodes Discovered but not fully explored nodes

Prim’s algorithm Basic idea: Start from an arbitrary single node A MST for a single node has no edge Gradually build up a single larger and larger MST 6 2 5 9 Not yet discovered 4 7 Fully explored nodes Discovered but not fully explored nodes

Prim’s algorithm Basic idea: Start from an arbitrary single node A MST for a single node has no edge Gradually build up a single larger and larger MST 2 6 5 9 4 7

Prim’s algorithm in words Randomly pick a vertex as the initial tree T Gradually expand into a MST: For each vertex that is not in T but directly connected to some nodes in T Compute its minimum distance to any vertex in T Select the vertex that is closest to T Add it to T

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Pseudocode for Prim’s algorithm Given G = (V, E). Output: a MST T. Randomly select a vertex v S = {v}; A = V \ S; T = {}. While (A is not empty) find a vertex u  A that connects to vertex v  S such that w(u, v) ≤ w(x, y), for any x  A and y  S S = S U {u}; A = A \ {u}; T = T U (u, v). End Return T

Time complexity Given G = (V, E). Output: a MST T. Randomly select a vertex v S = {v}; A = V \ S; T = {}. While (A is not empty) find a vertex u  A that connects to vertex v  S such that w(u, v) ≤ w(x, y), for any x  A and y  S S = S U {u}; A = A \ {u}; T = T U (u, v). End Return T n vertices Time complexity: n * (time spent on purple line per vertex) Naïve: test all edges => Θ(n * m) Improve: keep the list of candidates in an array => Θ(n2) Better: with priority queue => Θ(m log n)

Idea 1: naive find a vertex u  A that connects to vertex v  S such that w(u, v) ≤ w(x, y), for any x  A and y  S min_weight = infinity. For each edge (x, y)  E if x  A, y  S, and w(x, y) < min_weight u = x; v = y; min_weight = w(x, y); time spent per vertex: Θ(m) Total time complexity: Θ(n*m)

Idea 2: distance array For each v  V d[v] = infinity; p[v] = null; // For each vertex v, d[v] is the min distance from v to any node already in S // p[v] is the parent node of v in the spanning tree For each v  V d[v] = infinity; p[v] = null; Randomly select a v, d[v] = 1; // d[v]=1 just to ensure proper start S = {}; A = V; T = {}. While (A is not empty) Search d to find the smallest d[u] > 0. S = S U {u}; A = A \ {u}; T = T U (u, p[u]); d[u] = 0. For each v in adj[u] if d[v] > w(u, v) d[v] = w(u, v); p[v] = u; End

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ ∞ 1 p / /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ ∞ p / /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ 14 3 ∞ p / Discovered 14 7 15 8 c e h 3 10 Explored d Not discovered a b c d e f g h ∞ 14 3 ∞ p / /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ 14 ∞ p / /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ 8 10 ∞ p / 10 ∞ p / /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ 10 ∞ p / /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 6 10 5 ∞ p b 10 5 ∞ p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 6 10 ∞ p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 6 7 9 ∞ p b 7 9 ∞ p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 0 7 9 ∞ p b 7 9 ∞ p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 0 9 ∞ p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 0 9 15 p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 0 15 p b /

a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h 0 p b /

Time complexity? n vertices Θ(n) per vertex Overall complexity: Θ (n2) // For each vertex v, d[v] is the min distance from v to any node already in S // p[v] is the parent node of v in the spanning tree For each v  V d[v] = infinity; p[v] = null; Randomly select a v, d[v] = 1; // d[v]=1 just to ensure proper start S = {}. T = {}. A = V. While (A is not empty) Search d to find the smallest d[u] > 0. S = S U {u}; A = A \ {u}; T = T U (u, p[u]); d[u] = 0. For each v in adj[u] if d[v] > w(u, v) d[v] = w(u, v); p[v] = u; End n vertices Θ(n) per vertex O(n) per vertex if using adj list (n) per vertex if using adj matrix Overall complexity: Θ (n2)

Idea 3: priority queue (min-heap) Observation In idea 2, we need to search the distance array n times, each time we only look for the minimum element. Distance array size = n n x n = n2 Can we do better? Priority queue (heap) enables fast retrieval of min (or max) elements log (n) for most operations

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h ∞ ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d ∞ c b a d e f g h 0 ChangeKey c b a d e f g h 0 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d ∞ h b a d e f g ExctractMin h b a d e f g ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d 3 14 ∞ d b a h e f g ChangeKey d b a h e f g 3 14 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d ∞ b g a h e f 14 ExctractMin b g a h e f 14 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d 10 ∞ b e a h g f 8 Changekey b e a h g f 8 10 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d 10 ∞ e f a h g ExtractMin e f a h g 10 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d 5 10 6 ∞ f e a h g Changekey f e a h g 5 10 6 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d 6 10 ∞ a e g h ExtractMin a e g h 6 10 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d 6 7 9 ∞ a e g h Changekey a e g h 6 7 9 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d ExtractMin e h g 7 ∞ 9

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d ExtractMin g h 9 ∞

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d Changekey g h 9 15

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d ExtractMin h 15

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Complete Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u  Q key[u] = ; p[u] = null; key[r] = 0; while (Q not empty) u = ExtractMin(Q); for each v  Adj[u] if (v  Q and w(u,v) < key[v]) p[v] = u; ChangeKey(v, w(u,v)); Overall running time: Θ(m log n) Cost per ChangeKey n vertices Θ(n) times Θ(n2) times? Θ(m) times (with adj list) How often is ExtractMin() called? How often is ChangeKey() called?

Kruskal’s algorithm in words Procedure: Sort all edges into non-decreasing order Initially each node is in its own tree For each edge in the sorted list If the edge connects two separate trees, then join the two trees together with that edge

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Example a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d

Time complexity Depend on implementation Pseudocode sort all edges according to weights T = {}. tree(v) = v for all v. for each edge (u, v) if tree(u) != tree(v) T = T U (u, v); union (tree(u), tree(v)) Θ(m log m) = Θ(m log n) m edges m times (n-1) times. Overall time complexity: m log n + m t + n u Naïve: Θ(nm) Better implementation: Θ(m log n) tree union tree finding

Disjoint Set Union Use a linked list to represent tree elements, with pointers back to root: tree[u] != tree[v]: O(1) Union(tree[u],tree[v]): “Copy” elements of A into set B by adjusting elements of A to point to B Each union may take O(n) time Precisely n-1 unions O(n2)?

Disjoint Set Union Better strategy and analysis Always copy smaller list into larger list Size of combined list is at least twice the size of smaller list Each vertex copied at most logn times before a single tree emerges Total number of copy operations for n vertex is therefore O(n log n) Overall time complexity: m log n + m t + n u t: time for finding tree root is constant u: time for n union operations is at most nlog (n) m log n + m t + n u = Θ (m log n + m + n log n) = Θ (m log n) Conclusion Kruskal’s algorithm runs in Θ(m log n) for both dense and sparse graph

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b c d e f g h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d a b f c d e g h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b f a c d e g h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b f e a c d g h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b c d f e a g h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b c d f g e a h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b c d f g e a h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b c d f g e a h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b c d f g e a h

Example with disjoint set union c-d: 3 b-f: 5 b-a: 6 f-e: 7 b-d: 8 f-g: 9 d-e: 10 a-f: 12 b-c: 14 e-h: 15 a 6 12 5 9 b f g 14 7 15 8 c e h 3 10 d b c d f g e h a

Disjoint Set Union Better strategy and analysis Always copy smaller list into larger list Size of combined list is at least twice the size of smaller list Each vertex copied at most logn times before a single tree emerges Total number of copy operations for n vertex is therefore O(n log n) Overall time complexity: m log n + m t + n u t: time for finding tree root is constant u: time for n union operations is at most nlog (n) m log n + m t + n u = Θ (m log n + m + n log n) = Θ (m log n) Conclusion Kruskal’s algorithm runs in Θ(m log n) for both dense and sparse graph How about using counting sort? Θ(m + n log n)

Summary Kruskal’s algorithm Prim’s algorithm Θ(m log n) Possibly Θ(m + n log n) with counting sort Prim’s algorithm With priority queue : Θ(m log n) Assume graph represented by adj list With distance array : Θ(n^2) Adj list or adj matrix For sparse graphs priority queue wins For dense graphs distance array may be better