Parent Pointer Implementation

Slides:



Advertisements
Similar presentations
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Advertisements

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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
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.
Heaps Heaps are used to efficiently implement two operations:
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
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.
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.
© 2004 Goodrich, Tamassia Union-Find1 Union-Find Partition Structures.
Data Structures & Algorithms Union-Find Example Richard Newman.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
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.
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,
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Union-find Algorithm Presented by Michael Cassarino.
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):
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.
1 The Disjoint Set ADT CS146 Chapter 8 Yan Qing Lei.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
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
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
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 
CMSC 341 Disjoint Sets. 2 Disjoint Set Definition Suppose we have N distinct items. We want to partition the items into a collection of sets such that:
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
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CSE 373: Data Structures and Algorithms
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
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.
Disjoint Set Neil Tang 02/23/2010
Disjoint Set Neil Tang 02/26/2008
Data Structures & Algorithms Union-Find Example
ICS 353: Design and Analysis of Algorithms
CSE 373 Data Structures and Algorithms
Data Structures & Algorithms Union-Find Example
ICS 353: Design and Analysis of Algorithms
Union-Find Partition Structures
Union-Find Partition Structures
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Tree.
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.
Minimum Spanning Trees
An application of trees: Union-find problem
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
General Trees A general 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.
Presentation transcript:

Parent Pointer Implementation

Equivalence Class Problem The parent pointer representation is good for answering: Are two elements in the same tree? /** Determine if nodes in different trees */ public boolean differ(int a, int b) { Integer root1 = FIND(array[a]); Integer root2 = FIND(array[b]); return root1 != root2; } Examples of equivalence classes: Connected components in graphs Point clustering

Union/Find Want to keep the depth small. /** Merge two subtrees */ public void UNION(int a, int b) { Integer root1 = FIND(a); // Find a’s root Integer root2 = FIND(b); // Find b’s root if (root1 != root2) array[root2] = root1; } public Integer FIND(Integer curr) { if (array[curr] == null) return curr; while (array[curr] != null) curr = array[curr]; return curr; Want to keep the depth small. Weighted union rule: Join the tree with fewer nodes to the tree with more nodes.

Equiv Class Processing (1) Initially, all objects are in separate sets (equivalence classes). (b) shows the result of processing equivalences (A, B), (C, H), (F, G), (D, E), and (I, F). (c) shows the result of processing equivalences (A, H) and (E, G). Note that weighted union is used.

Equiv Class Processing (2) (d) shows the result of processing equivalence (H, E).

Path Compression public Integer FIND(Integer curr) { if (array[curr] == null) return curr; array[curr] = FIND(array[curr]); return array[curr]; } Path compression is used to process equivalence (H, E).