MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

Disjoint Sets CS2 -- 9/8/2010. Disjoint Sets A disjoint set contains a set of sets such that in each set, an element is designated as a marker for the.
Union-Find structure Mikko Malinen School of Computing University of Eastern Finland.
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
Disjoint Sets Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of union.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
CSE 326: Data Structures Disjoint Union/Find Ben Lerner Summer 2007.
Minimum Spanning Tree Algorithms
CSE 326: Data Structures Disjoint Union/Find. Equivalence Relations Relation R : For every pair of elements (a, b) in a set S, a R b is either true or.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set. {1}, {2}, …, {n} An intermixed sequence of union.
CHAPTER 3 Data Structures. Algorithm Initializing a Stack This algorithm initializes a stack to empty. An empty stack has t = -1. Input Parameters:
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
Minimum Spanning Trees (MST)
CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Lecture 9 Disjoint Set ADT. Preliminary Definitions A set is a collection of objects. Set A is a subset of set B if all elements of A are in B. Subsets.
Lecture 16: Union and Find for Disjoint Data Sets Shang-Hua Teng.
CSE 373, Copyright S. Tanimoto, 2002 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Spring 2015 Lecture 11: Minimum Spanning Trees
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different set.  {1}, {2}, …, {n} An intermixed sequence of.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Disjoint Sets Data Structure (Chap. 21) A disjoint-set is a collection  ={S 1, S 2,…, S k } of distinct dynamic sets. Each set is identified by a member.
Lecture X Disjoint Set Operations
Disjoint Sets Data Structure. Disjoint Sets Some applications require maintaining a collection of disjoint sets. A Disjoint set S is a collection of sets.
Union-find Algorithm Presented by Michael Cassarino.
Union Find ADT Data type for disjoint sets: makeSet(x): Given an element x create a singleton set that contains only this element. Return a locator/handle.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Nicki Dell Spring 2014.
CSE373: Data Structures & Algorithms Lecture 10: Implementing Union-Find Dan Grossman Fall 2013.
CSCE 411H Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 7 1 * Slides adapted from.
Union & Find Problem 황승원 Fall 2010 CSE, POSTECH 2 2 Union-Find Problem Given a set {1, 2, …, n} of n elements. Initially each element is in a different.
Disjoint-Set Operation. p2. Disjoint Set Operations : MAKE-SET(x) : Create new set {x} with representative x. UNION(x,y) : x and y are elements of two.
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And.
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
0 Union-Find data structure. 1 Disjoint set ADT (also Dynamic Equivalence) The universe consists of n elements, named 1, 2, …, n n The ADT is a collection.
CHAPTER 8 THE DISJOINT SET ADT §1 Equivalence Relations 【 Definition 】 A relation R is defined on a set S if for every pair of elements (a, b), a, b 
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 373, Copyright S. Tanimoto, 2001 Up-trees - 1 Up-Trees Review of the UNION-FIND ADT Straight implementation with Up-Trees Path compression Worst-case.
How many edges does a tree of n nodes have? 1.2n-4 2.n(n-1)/2 3.n-1 4.n/2 5.Between n and 2n.
MA/CSSE 473 Day 37 Student Questions Kruskal data structures
CSE 373: Data Structures and Algorithms
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Disjoint Sets Data Structure
CSE 373, Copyright S. Tanimoto, 2001 Up-trees -
MA/CSSE 473 Days Answers to student questions
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
MA/CSSE 473 Day 36 Kruskal proof recap
An application of trees: Union-find problem
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Lecture 20: Disjoint Sets
Running Time Analysis Union is clearly a constant time operation.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Disjoint Sets Data Structure (Chap. 21)
An application of trees: Union-find problem
CSE 373: Data Structures and Algorithms
Presentation transcript:

MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15

Data Structures for Kruskal A sorted list of edges (edge list, not adjacency list) –Edge e has fields e.v and e.w (#s of its end vertices) Disjoint subsets of vertices, representing the connected components at each stage. –Start with n subsets, each containing one vertex. –End with one subset containing all vertices. Disjoint Set ADT has 3 operations: –makeset(i): creates a singleton set containing vertex i. –findset(i): returns the "canonical" member of its subset. I.e., if i and j are elements of the same subset, findset(i) == findset(j) –union(i, j): merges the subsets containing i and j into a single subset.

Example of operations makeset (1) makeset (2) makeset (3) makeset (4) makeset (5) makeset (6) union(4, 6) union (1,3) union(4, 5) findset(2) findset(5) What are the sets after these operations?

Kruskal Algorithm Assume vertices are numbered 1...n (n = |V|) Sort edge list by weight (increasing order) for i = 1..n: makeset(i) i, count, result = 1, 0, [] while count < n-1: if findset(edgelist[i].v) != findset(edgelist[i].w): result += [edgelist[i]] count += 1 union(edgelist[i].v, edgelist[i].w) i += 1 return result What can we say about efficiency of this algorithm (in terms of n=|V| and m=|E|)?

Implement Disjoint Set ADT Each disjoint set is a tree, with the "marked" (canonical) element as its root Efficient representation of these trees: –an array called parent –parent[i] contains the index of i’s parent. –If i is a root, parent[i]=i

Using this representation makeset(i): findset(i): mergetrees(i,j): –assume that i and j are the marked elements from different sets. union(i,j): –assume that i and j are elements from different sets def makeset1(i): parent[i] = i def findset1(i): while i != parent[i]: i = parent[i] return i def mergetrees1(i,j): parent[i] = j def union1(i,j): mergetrees1(findset1(i), findset1(j)) Write these procedures on the board

Analysis Assume that we are going to do n makeset operations followed by m union/find operations time for makeset? worst case time for findset? worst case time for union? Worst case for all m union/find operations? worst case for total? What if m < n? Write the formula to use min

def mergetrees2(i,j): if height[i] < height[j]): parent[i] = j elif height[i] > height[j]: parent[j] = i else: parent[i] = j height[j] = height[j] + 1 Can we keep the trees from growing so fast? Make the shorter tree the child of the taller one What do we need to add to the representation? rewrite makeset, mergetrees. findset & union are unchanged. What can we say about the maximum height of a k-node tree? def makeset2(i): parent[i] = i height[i] = 0

Theorem: max height of a k-node tree T produced by these algorithms is  lg k  Base case… Induction hypothesis… Induction step: –Let T be a k-node tree –T is the union of two trees: T 1 with k 1 nodes and height h 1 T 2 with k 2 nodes and height h 2 –What can we about the heights of these trees? –Case 1: h 1 ≠h 2. Height of T is –Case 2: h 1 =h 2. WLOG Assume k 1 ≥k 2. Then k 2 ≤k/2. Height of tree is 1 + h2 ≤ … Added after class because we did not get to it: 1 + h2 <= 1 +  lg k 2  <= 1 +  lg k/2  = 1 +  lg k - 1  =  lg k 

Worst-case running time Again, assume n makeset operations, followed by m union/find operations. If m > n If m < n

Speed it up a little more Path compression: Whenever we do a findset operation, change the parent pointer of each node that we pass through on the way to the root so that it now points directly to the root. Replace the height array by a rank array, since it now is only an upper bound for the height. Look at makeset, findset, mergetrees (on next slides)

Makeset This algorithm represents the set {i} as a one-node tree and initializes its rank to 0. def makeset3(i): parent[i] = i rank[i] = 0

Findset This algorithm returns the root of the tree to which i belongs and makes every node on the path from i to the root (except the root itself) a child of the root. def findset(i): root = i while root != parent[root]: root = parent[root] j = parent[i] while j != root: parent[i] = root i = j j = parent[i] return root

Mergetrees This algorithm receives as input the roots of two distinct trees and combines them by making the root of the tree of smaller rank a child of the other root. If the trees have the same rank, we arbitrarily make the root of the first tree a child of the other root. def mergetrees(i,j) : if rank[i] < rank[j]: parent[i] = j elif rank[i] > rank[j]: parent[j] = i else: parent[i] = j rank[j] = rank[j] + 1

Analysis It's complicated! R.E. Tarjan proved (1975)*: –Let t = m + n –Worst case running time is Ѳ(t α(t, n)), where α is a function with an extremely slow growth rate. –Tarjan's α: –α(t, n) ≤ 4 for all n ≤ Thus the amortized time for each operation is essentially constant time. * According to Algorithmsby R. Johnsonbaugh and M. Schaefer, 2004, Prentice-Hall, pages