Dynamic Equivalence Problem

Slides:



Advertisements
Similar presentations
Lecture 10 Disjoint Set ADT.
Advertisements

CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
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.
Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
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.
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.
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.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structure (III) GagGuy.
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.
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):
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.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 326: Data Structures: Set ADT
Priority Queues A priority queue is an ADT where:
CSE 373: Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
Heaps, Heapsort, and Priority Queues
Disjoint Sets Data Structure
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Disjoint Sets Chapter 8.
CS200: Algorithms Analysis
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Data Structures Lecture 27 Sohail Aslam.
Data Structures: Disjoint Sets
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.
Heaps and Priority Queues
Ch. 8 Priority Queues And Heaps
CSE 332: Data Structures Disjoint Set Union/Find
Forests D. J. Foreman.
CSE 332: Data Abstractions Union/Find II
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
Equivalence Relations
Data Structures Lecture 28 Sohail Aslam.
CSE373: Data Structures & Algorithms Implementing Union-Find
Data Structures Lecture 29 Sohail Aslam.
Heap code in C++ template <class eType>
Algorithms Lecture # 29 Dr. Sohail Aslam.
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets Given a set {1, 2, …, n} of n elements.
Lecture No.02 Data Structures Dr. Sohail Aslam
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.
Algorithms Lecture # 02 Dr. Sohail Aslam.
An application of trees: Union-find problem
Lecture 21 Amortized Analysis
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.
Algorithms Lecture #42 Dr. Sohail Aslam.
Algorithms Lecture # 26 Dr. Sohail Aslam.
Algorithms Lecture # 25 Dr. Sohail Aslam.
Heaps Section 6.4, Pg. 309 (Section 9.1).
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Dynamic Equivalence Problem We will use a tree to represent each set, since each element in a tree has the same root. The root can be used to name the set. There will be a collection of trees, each tree representing one set. A collection of trees is called a forest. Start lecture 35 here.

Lecture No.35 Data Structure Dr. Sohail Aslam

Dynamic Equivalence Problem The trees we will use are not necessarily binary. To perform union of two sets, we merge the two trees by making the root of one point to the root of the other. A find(x) on element x is performed by returning the root of the tree containing x.

Dynamic Equivalence Problem 1 2 3 4 5 6 7 8 Eight elements, initially in different sets.

Dynamic Equivalence Problem 1 2 3 4 5 7 8 6 After union(5,6)

Dynamic Equivalence Problem 1 2 3 4 5 7 6 8 After union(7,8)

Dynamic Equivalence Problem 1 2 3 4 5 6 7 8 After union(5,7)

Dynamic Equivalence Problem 1 2 3 5 4 6 7 8 After union(3,4)

Dynamic Equivalence Problem 1 2 3 4 5 6 7 8 After union(4,5)

Dynamic Equivalence Problem Typical tree traversal not required, so no need for pointers to children, instead we need a pointer to parent – an up-tree Parent pointers can be stored in an array: parent[i] (set to -1 if i is root). The algorithm for find and union can thus be:

Dynamic Equivalence Problem Initialization: for (i=0; i < n; i++) parent[i] = -1; find(i): // traverse to the root (-1) for(j=i; parent[j] >= 0; j=parent[j]) ; return j;

Dynamic Equivalence Problem union(i,j): root_i = find(i); root_j = find(j); if (root_i != root_j) parent[root_j] = root_i;

Parent Array 1 2 3 4 5 6 7 8 -1 1 2 3 4 5 6 7 8 Eight elements, initially in different sets.

Parent Array 1 2 3 4 5 7 8 6 -1 5 1 2 3 4 6 7 8 After union(5,6)

Parent Array 1 2 3 4 5 7 6 8 -1 5 7 1 2 3 4 6 8 After union(7,8)

Parent Array 1 2 3 4 5 6 7 8 -1 5 7 1 2 3 4 6 8 After union(5,7)

Parent Array 1 2 3 5 4 6 7 8 -1 3 5 7 1 2 4 6 8 After union(3,4)

Parent Array 1 2 3 4 5 6 7 8 -1 3 5 7 1 2 4 6 8 After union(4,5)

Parent Array Find(8) 1 2 3 4 5 6 7 8 -1 3 5 7 1 2 4 6 8

Running Time Analysis Union is clearly a constant time operation. Running time of find(i) is proportional to the height of the tree containing node i. This can be proportional to n in the worst case (but not always) Goal: Modify union to ensure that heights stay small End of lecture 35, start of lecture 36