Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here  Disjoint set union S 1  S 2 ={0,6,7,8,1,4,9}  Find(i):

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
1 Union-find. 2 Maintain a collection of disjoint sets under the following two operations S 3 = Union(S 1,S 2 ) Find(x) : returns the set containing x.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
MA/CSSE 473 Day 38 Finish Disjoint Sets Dijkstra.
Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
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.
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.
CS Data Structures Chapter 5 Trees. Forests  Definition: A forest is a set of n ≥ 0 disjoint trees.  When we remove a root from a tree, we’ll.
Heaps Heaps are used to efficiently implement two operations:
CS2420: Lecture 15 Vladimir Kulyukin Computer Science Department Utah State University.
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.
1 Chapter 8 The Disjoint Set ADT Concerns with equivalence problems Find and Union.
Chapter 5 Trees: Outline
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Binary search trees. What’s on the menu? ADT DictionaryBST & Algorithms SimulatorComplexity.
Data Structure (III) GagGuy.
Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >
General Trees CS 400/600 – Data Structures. General Trees2.
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.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets.
CMSC 341 Disjoint Sets. 8/3/2007 UMBC CMSC 341 DisjointSets 2 Disjoint Set Definition Suppose we have an application involving N distinct items. We will.
CMSC 341 Disjoint Sets Textbook Chapter 8. Equivalence Relations A relation R is defined on a set S if for every pair of elements (a, b) with a,b  S,
Binary Search Tree Qamar Abbas.
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.
Fundamental Data Structures and Algorithms Peter Lee April 24, 2003 Union-Find.
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.
1 Disjoint Set Data Structure. 2 Disjoint Sets What are Disjoint Sets? Tree Representation Basic Operations Parent Array Representation Simple Find and.
ICS 353: Design and Analysis of Algorithms Heaps and the Disjoint Sets Data Structures King Fahd University of Petroleum & Minerals Information & Computer.
CSCI 333 Data Structures Chapter 6 30 September and 2 and 4 October 2002.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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 
MST, Topological Sort and Disjoint Sets
General Trees A tree T is a finite set of one or more nodes such that there is one designated node r called the root of T, and the remaining nodes in (T-{r})
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
C Point Version July 2003 By Andrea Kohlhase, The Course Capsules Project (CCaps),CMU Look at our website CCaps for latest news!CCaps C ategorize.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
Parent Pointer Implementation
CSCE 210 Data Structures and Algorithms
Disjoint Sets Chapter 8.
Trees-1 Data Structures with C Chpater-5 Course code: 10CS35
An application of trees: Union-find problem
Course Outline Introduction and Algorithm Analysis (Ch. 2)
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.
CHAPTER 5 Trees All the programs in this file are selected from
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Union-Find Partition Structures
CSE373: Data Structures & Algorithms Implementing Union-Find
Union-Find Partition Structures
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Running Time Analysis Union is clearly a constant time operation.
Set Representation S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
Union-Find Problem Given a set {1, 2, …, n} of n elements.
Dynamic Equivalence Problem
An application of trees: Union-find problem
Presentation transcript:

Set Representation S 1 ={0, 6, 7, 8}, S 2 ={1, 4, 9}, S 3 ={2, 3, 5} Two operations considered here  Disjoint set union S 1  S 2 ={0,6,7,8,1,4,9}  Find(i): Find the set containing the element i. 3  S 3, 8  S S i  S j = 

Disjoint Set Union Possible representation for S 1 union S 2 Make one of trees a subtree of the other S1 union S2 S2 union S1 OR

*Figure 5.41:Data Representation of S 1 S 2 and S 3 (p.240)

Array Representation for Set int find1(int i) { for (; parent[i]>=0; i=parent[i]); return i; } void union1(int i, int j) { parent[i]= j; }

n-1 n-2 0  *Figure 5.43:Degenerate tree (p.242) union operation O(n) n-1 find operation O(n 2 ) union(0,1), find(0) union(1,2), find(0). union(n-2,n-1),find(0) degenerate tree

*Figure 5.44:Trees obtained using the weighting rule(p.243) weighting rule for union(i,j): if # of nodes in i < # in j then j the parent of i

Modified Union Operation void union2(int i, int j) { int temp = parent[i]+parent[j]; if (parent[i]>parent[j]) { parent[i]=j; parent[j]=temp; } else { parent[j]=i; parent[i]=temp; } If the number of nodes in tree i is less than the number in tree j, then make j the parent of i; otherwise make i the parent of j. Keep a count in the root of tree i has fewer nodes. j has fewer nodes

Figure 5.45:Trees achieving worst case bound (p.245)  log 2 8  +1

Modified Find(i) Operation int find2(int i) { int root, trail, lead; for (root=i; parent[root]>=0; root=parent[root]); for (trail=i; trail!=root; trail=lead) { lead = parent[trail]; parent[trail]= root; } return root: } If j is a node on the path from i to its root then make j a child of the root