lecture24: Disjoint Sets

Similar documents
lecture14: Tree Traversals

CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015

lecture17: AVL Trees

lecture29: Shortest Path Algorithms

lecture09: Linked Lists

CS 225. April 6 Disjoint Sets Implementation. Data Structures. Wade Fagen-Ulmschneider

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

CSE 100 Disjoint Set, Union Find

Today s Outline. Motivation. Disjoint Sets. Disjoint Sets and Dynamic Equivalence Relations. Announcements. Today s Topics:

CMSC 341 Lecture 20 Disjointed Sets

Union-Find: A Data Structure for Disjoint Set Operations

Priority Queues. 04/10/03 Lecture 22 1

CMSC 341 Lecture 20 Disjointed Sets

CS 225. Nov. 13 Introduction to Graphs. Data Structures. Wade Fagen-Ulmschneider

Advanced Set Representation Methods

Disjoint Sets and the Union/Find Problem

CMSC 341 Lecture 20 Disjointed Sets

Announcements. mp3.1 extra credit due tomorrow lab quacks due Saturday night (6/29) mp3 due Monday (7/1)

Disjoint Sets. The obvious data structure for disjoint sets looks like this.

lecture23: Hash Tables

lecture05: The Big Three

CSE 100: GRAPH ALGORITHMS

Disjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50

CSC263 Week 11. Larry Zhang.

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets

CSE 373 Autumn 2010: Midterm #2 (closed book, closed notes, NO calculators allowed)

Algorithms. Algorithms 1.5 UNION FIND. union find data type quick-find quick-union improvements applications ROBERT SEDGEWICK KEVIN WAYNE

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

COMP 250 Midterm #2 March 11 th 2013

Disjoint Sets. Based on slides from previous iterations of this course.

Second Examination Solution

COP 4531 Complexity & Analysis of Data Structures & Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

CS 315 Data Structures mid-term 2

Outline Purpose Disjoint Sets Maze Generation. Disjoint Sets. Seth Long. March 24, 2010

Union-Find: A Data Structure for Disjoint Set Operations

Balanced Search Trees

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Disjoint set (Union-Find)

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445

What is an algorithm?

Solutions to Exam Data structures (X and NV)

Binary Relations Part One

Data Structure Lecture#14: Non-Binary Trees (Chapter 6) U Kang Seoul National University

Unit 10: Data Structures CS 101, Fall 2018

Greedy Algorithms Part Three

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Binary Search Trees Part Two

CS 310 Advanced Data Structures and Algorithms

What is an algorithm?

Memory, Arrays, and Parameters

Algorithms. Algorithms 1.5 UNION-FIND. dynamic connectivity quick find quick union improvements applications ROBERT SEDGEWICK KEVIN WAYNE

CSCI-1200 Data Structures Fall 2017 Lecture 7 Order Notation & Basic Recursion

N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label

Exercises: Disjoint Sets/ Union-find [updated Feb. 3]

B+ Tree Review. CSE332: Data Abstractions Lecture 10: More B Trees; Hashing. Can do a little better with insert. Adoption for insert

Search Lesson Outline

lecture04: Constructors and Destructors

13 A: External Algorithms II; Disjoint Sets; Java API Support

Binary Relations McGraw-Hill Education

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

lecture27: Graph Traversals

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Data Structures in Java. Session 18 Instructor: Bert Huang

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Data Structures in Java

Data Structures for Disjoint Sets

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

Queues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1

Dynamic Data Structures. John Ross Wallrabenstein

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

CMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

ECE 122. Engineering Problem Solving Using Java

CSE332: Data Abstractions Lecture 7: B Trees. James Fogarty Winter 2012

Fibonacci Heaps Priority Queues. John Ross Wallrabenstein

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Union-find algorithms. (1) How do you determine whether a vertex x is connected to a vertex y in a graph?

Public-Service Announcements

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Priority Queues Heaps Heapsort

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Instructions PLEASE READ (notice bold and underlined phrases)

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

lecture30: Beyond CS 225

Randomized Algorithms: Element Distinctness

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Data Structures in Java. Session 18 Instructor: Bert Huang

1. AVL Trees (10 Points)

Trees, Part 1: Unbalanced Trees

Priority Queues Heaps Heapsort

EECS 311 Data Structures Midterm Exam Don t Panic!

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Transcription:

lecture24: Largely based on slides by Cinda Heeren CS 225 UIUC 22nd July, 2013

Announcements mp6 due tonight mp7 out soon! mt3 tomorrow night (7/23) Optional review instead of lab tomorrow

Code Challenge Friday Night! 6pm in the lab

A New Task Given the set {C#, C + +, Java, Ruby, Python}, partition students by their favorite programming language. Each student has an integer ID (we can keep track of this mapping with a Dictionary).

What kind of operations can we perform? find(4) What does this return? find(3) == find(1)? if find(2)!= find(0), then union(2, 0)

We re modeling equivalence relations What is an equivalence relation again? A given binary relation on a set S is said to be an equivalence relation if and only if it is reflexive, symmetric and transitive. Equivalently, a, b, c S: a a (reflexive) a b b a (symmetric) a b b c a c (transitive) How does this translate to our programming language example?

Another example How could you model a social network using this framework? What is the equivalence relation? What about RGBAPixels in a PNG?

Announcements Partitioning a Social Network

Partitioning Regions in an Image

Our New ADT We need a union-find ADT:. It supports 1 addelements, to initialize the structure: initially all elements are in their own set 2 find, to return which set an element belongs to (by returning a representative element from the set) 3 union, to connect two elements together class DisjointSets { public: void addelements(int num); int find(int id); void union(int id1, int id2); private: //??? };

Our first implementation DisjointSets dsets; dsets.addelements(8); 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 dsets.union(2, 5); 0 1 2 3 4 5 6 7 0 1 2 3 4 2 6 7 dsets.union(5, 7); dsets.union(0, 1); dsets.union(0, 6); 0 1 2 3 4 5 6 7 1 6 2 3 4 2 1 2 When we create the set, each element is its own set and its own representative When unioning, choose an arbitrary element as the set representative How does find work? What if we wanted to do union(1, 2)? What is the running time of find and union?

DisjointSets via an array It seems our array implementation gives great running times for find, but union can be very inefficient: O(n) when combining large sets together. We should think of a way to get sublinear running times for our DisjointSets operations...

UpTrees: A better DS implementation We can imagine our sets as a collection of trees (Uptrees) instead of elements in an array The root of each tree is the representative of the set it is in When unioning two elements, we can just point the root at the set we re unioning with! We don t even have to use pointers for Uptrees instead, each element s field holds the index of its parent (roots can have -1 as their data) Let s draw what this looks like...

Imagining Uptrees DisjointSets dsets; dsets.addelements(8); 0 1 2 3 4 5 6 7-1 -1-1 -1-1 -1-1 -1 dsets.union(2, 5); 0 1 2 3 4 5 6 7-1 -1 5-1 -1-1 -1-1 dsets.union(5, 7); dsets.union(0, 1); dsets.union(0, 6); 0 1 2 3 4 5 6 7 1 6 5-1 -1 7-1 -1 When unioning, we need to deal with the roots to make sure all elements are unioned correctly By default, let s point the root of the first argument to the root of the second Can you draw the uptrees?

Concept Check: Draw the Uptrees Draw the forest of Uptrees if this is our array. 0 1 2 3 4 5 6 7-1 3-1 -1 6 3 0 2 Could you write code that generated this forest? How many disjoint sets are there?

Our New DS Implementation // return the root of the Uptree containing id int DisjointSets::find(int id) { } // combine the sets containing id1 and id2 void DisjointSets::union(int id1, int id2) { }

Analyzing DS The running time of DS operations using Uptrees depends on... So the worst case running time is still O(n) What does an ideal Uptree look like? If we have to union two Uptrees together, how can we do it so we don t increase the height?

Smart Unions Union by height Keeps the overall height of the tree as short as possible Union by size Increases the distance to the root for the fewest number of nodes Both of these smart union schemes guarantee a O(log n) uptree height in all the disjoint sets

Union By Size // combine the sets containing id1 and id2 void DisjointSets::union(int id1, int id2) { int root1 = find(id1); int root2 = find(id2); int newsize = _set[root1] + _set[root2]; //?? // root1 has more elements if(_set[root1] < _set[root2]) { } // root2 has more elements (or same) else { } }

Union By Height We think you should be able to write a union-by-height implementation, too (In mp7, we ll assume you write a union-by-size though) What other considerations do we have to take if doing union-by-height?

Another Optimization After computing the root in find, set the current node s parent to be that root! This makes our trees much shorter, which is great because we already know all the operations on our disjoint sets are O(h) int DisjointSets::find(int id) { }

Union-find with smart unions and path compression

New running times... log n number of times you can take the logarithm of n before you get to 1 n log n 1 0 2 1 4 2 16 3 65, 536 4 2 65,536 5 In any sequence of m union and find operations on a collection of n items, the running time is O(m log n) (effectively constant).

Using DS mp7 (how?) lab graphs: Kruskal s Algorithm lots of other applications...