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 >

Slides:



Advertisements
Similar presentations
1 Disjoint Sets Set = a collection of (distinguishable) elements Two sets are disjoint if they have no common elements Disjoint-set data structure: –maintains.
Advertisements

EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Union-Find: A Data Structure for Disjoint Set Operations
Disjoint Union / Find CSE 373 Data Structures Lecture 17.
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.
Heaps Heaps are used to efficiently implement two operations:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Chapter 9: Union-Find Algorithms The Design and Analysis of Algorithms.
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.
29-Jan-00 CPSC 212: Data Structures and Algorithms Instructor: Harry Plantinga.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structure (III) GagGuy.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
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.
MST and Max Flow CS3233. Overview Two Graph Problems Minimum Spanning Tree Maximum Flow/Minimum Cut Problem One Data Structure Disjoint Sets.
Houyang 3/25/13. USACO March Contest Congrats to Johnny Ho for scoring over 900 points in the Gold Division o 7th place in US Kudos to Jonathan Uesato.
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Aaron Bauer Winter 2014.
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,
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.
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 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Disjoint Sets.
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.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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:
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
Minimum Spanning Trees Kruskal's and Prim's algorithms.
Parent Pointer Implementation
CSE 373: Data Structures and Algorithms
Disjoint Sets Data Structure
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
CSE 332: Data Structures Disjoint Set Union/Find
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
CSE 332: Data Abstractions Union/Find II
ICS 353: Design and Analysis of Algorithms
Equivalence Relations
CSE373: Data Structures & Algorithms Implementing Union-Find
CMSC 341 Disjoint Sets.
CSE 326 Union Find Richard Anderson Text Chapter CSE 326.
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
CMSC 341 Disjoint Sets.
Set Representation S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
An application of trees: Union-find problem
Disjoint Sets Textbook Chapter 8
CSE 373: Data Structures and Algorithms
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

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 > 0; c /= 10) digsum += c % 10; int M = digsum; for(int d1 = 2; d1 * d1 <= a; ++d1) if(a % d1 == 0) M = max(M, digsum + getdp(d1) + getdp(a / d1)); return DP[a] = M; } int main() { int n; cin >> n; cout << getdp(n) << "\n"; return 0; }

Problem I have N circles and K demands. I want to color the N circles. o Demand i states that circle A_i and B_i must be colored the same color. I want to use as many colors as possible cause I'm fabulous How many different colors can I use? Solution o Union Find! o Circles that must be the same color are in the same set. o For each demand, merge A_i and B_i. o At the end, count how many sets there are.

The Union-Find Algorithm Maintain a collection of disjoint sets. o Each element belongs to a set. o Each set has a "representative" element. What you can do with Union-Find o FIND(X)  Find which set element X belongs to.  Returns representative of X's set. o UNION(X, Y)  Merge the two sets containing elements X and Y.  If X and Y are already in the same set, nothing happens. o O(inverse-ackermann(n)) amortized runtime  In other words, very fast!! (almost constant time)

How It Works Each set is stored as a tree. o The root is the representative element of its set. Every element has a parent. o The parent of a root is itself. o Initially, each element is in its own set. FIND(X) returns the root of X. if (parent[X] == X) return X; else return FIND(parent[X]); UNION(A,B) merges A and B's trees. o Put A's root under B's root. parent[FIND(A)] = FIND(B);

Example Solution int[] par; int FIND(int x) { if (par[x] == x) { return x; } return FIND(par[x]); } void UNION(int x, int y) { x = FIND(x); y = FIND(y); par[x] = y; // if x == y, this does nothing! }

Example Solution Continued for(int i = 0; i < N; ++i) par[i] = i; // initially, all circles are disjoint. for(int i = 0; i < K; ++i) { int a, b;... UNION(a, b); // a and b must be in the same set. } int cnt = 0; // number of sets is just number of roots. for(int i = 0; i < N; ++i) { if (par[i] == i) { // check if i is a root. cnt++; } return cnt;

How to make it fast Union Find needs optimizations to become fast. Optimization 1 - Path Compression o Once you find an element's root, set that root as its parent. Optimization 2 - Union By Rank o The rank of a set is the height of its tree. o When merging sets A and B, put B under A if B is shorter than A. o This keeps the trees shorter These two optimizations combined make the overall average runtime nearly constant.

UNION-FIND With Optimization int[] par, rank; int FIND(int x) { if (par[x] == x) return x; return par[x] = FIND(par[x]); } void UNION(int a, int b) { a = FIND(a); b = FIND(b); if (a == b) return; if (rank[a] > rank[b]) swap(a, b); par[a] = b; rank[b] = max(rank[b], rank[a] + 1); }

Applications Union-Find is used in Kruskal's algorithm for Minimum-Spanning Trees. Union-Find can be used in place of DFS when searching for connected components or cycles. Union-Find can be used in many other problems. o Very common in USACO Silver and Gold contests. o Also on Codeforces Path-Compression can be extended to other trees sometimes.

POTW There is a river, represented by a grid with width W o All squares (x,y) with 0 <= y < W are part of the river. In the i-th minute, Andy throws a rock into the river, which fills a unit square with coordinate (x_i, y_i). [0 <= y_i < W] Andy wants to block off flow from west to east. How many rocks will he throw before this goal is first achieved? o Water cannot flow over stones or between stones o It can, however, flow east to west if necessary

Beautiful POTW Diagram

Input Format and Constraints Input Format o Line 1: Two integers, W, T  The width of the river  The number of minutes Andy spends throwing rocks. o Lines 2...T+1: Two integers x_i, y_i. Output Format o Line 1: The number of rocks Andy throws before blocking off the flow, or - 1 if it cannot be done. Constraints o 15 points: W, T < 10. |x_i| < 10 o 35 points: W, T < |x_i| < 1000 o 50 points: W, T < , |x_i| < 10^9

Sample Case Input: Output: 4 widthwidth