Data Structures & Algorithms Union-Find Example Richard Newman.

Slides:



Advertisements
Similar presentations
CSE 326: Data Structures Part 7: The Dynamic (Equivalence) Duo: Weighted Union & Path Compression Henry Kautz Autumn Quarter 2002 Whack!! ZING POW BAM!
Advertisements

Introduction to Algorithms Quicksort
Introduction to Computer Science Theory
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.
Inpainting Assigment – Tips and Hints Outline how to design a good test plan selection of dimensions to test along selection of values for each dimension.
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
EECS 311: Chapter 8 Notes Chris Riesbeck EECS Northwestern.
Algorithm and data structure. Overview Programming and problem solving, with applications Algorithm: method for solving a problem Data structure: method.
Advanced Topics in Algorithms and Data Structures Lecture pg 1 Recursion.
CPSC 322, Lecture 9Slide 1 Search: Advanced Topics Computer Science cpsc322, Lecture 9 (Textbook Chpt 3.6) January, 23, 2009.
CSE 326: Data Structures Disjoint Union/Find Ben Lerner Summer 2007.
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.
CS2420: Lecture 43 Vladimir Kulyukin Computer Science Department Utah State University.
DAST, Spring © L. Joskowicz 1 Data Structures – LECTURE 1 Introduction Motivation: algorithms and abstract data types Easy problems, hard problems.
Hash Tables1 Part E Hash Tables  
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
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.
CS2420: Lecture 42 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
Slides from Kevin Wayne on Union- Find and Percolotion.
© Ronaldo Menezes, Florida Tech Fundamentals of Algorithmic Problem Solving  Algorithms are not answers to problems  They are specific instructions for.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Trees Dale Roberts, Lecturer
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Lauren Milne Spring 2015.
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.
Algorithms, 4 th Edition∙Robert Sedgewick and Kevin Wayne∙Copyright © 2002–2010∙ March 16, :10:29 PM ‣ dynamic connectivity ‣ quick find ‣ quick.
CSC 211 Data Structures Lecture 13
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,
Example Algorithms CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Union-find Algorithm Presented by Michael Cassarino.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Recursion and Trees Dale Roberts, Lecturer
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.
CS 146: Data Structures and Algorithms July 16 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
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 
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
CSE373: Data Structures & Algorithms Lecture 9: Disjoint Sets and the Union-Find ADT Lauren Milne Summer 2015.
WEEK 5 The Disjoint Set Class Ch CE222 Dr. Senem Kumova Metin
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
1 Resolving Collision Although collisions should be avoided as much as possible, they are inevitable Need a strategy for resolving collisions. We look.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
Theory of Computational Complexity Probability and Computing Chapter Hikaru Inada Iwama and Ito lab M1.
Parent Pointer Implementation
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Chapter 8 Disjoint Sets and Dynamic Equivalence
Disjoint Sets Chapter 8.
Compsci 201, Union-Find Algorithms
CSE373: Data Structures & Algorithms Lecture 10: Disjoint Sets and the Union-Find ADT Linda Shapiro Spring 2016.
Data Structures & Algorithms Union-Find Example
Data Structures & Algorithms Union-Find Example
Lecture 2- Query Processing (continued)
Union-find algorithms
Union-Find.
5.4 T-joins and Postman Problems
Disjoint Sets DS.S.1 Chapter 8 Overview Dynamic Equivalence Classes
Disjoint Sets Textbook Chapter 8
Disjoint Set Operations: “UNION-FIND” Method
Presentation transcript:

Data Structures & Algorithms Union-Find Example Richard Newman

Steps to Develop an Algorithm  Define the problem – model it  Determine constraints  Find or create an algorithm to solve it  Evaluate algorithm – speed, space, etc.  If algorithm isn’t satisfactory, why not?  Try to fix algorithm  Iterate until solution found (or give up)

Dynamic Connectivity Problem Given a set of N elements Support two operations: Connect two elements Given two elements, is there a path between them?

Example Connect (4, 3) Connect (3, 8) Connect (6, 5) Connect (9, 4) Connect (2, 1) Are 0 and 7 connected (No) Are 8 and 9 connected (Yes)

Example (con’t) Connect (5, 0) Connect (7, 2) Connect (6, 1) Connect (1, 0) Are 0 and 7 connected (Yes) Now consider a problem with 10,000 elements and 15,000 connections…

Modeling the Elements Various interpretations of the elements:  Pixels in a digital photo  Computers in a network  Socket pins on a PC board  Transistors in a VLSI design  Variable names in a C++ program  Locations on a map  Friends in a social network …… Convenient to just number 0 to N-1 Use as array index, suppress details

Modeling the Connections Assume “is connected to” is an equivalence relation  Reflexive: a is connected to a  Symmetric: if a is connected to b, then b is connected to a  Transitive: if a is connected to b, and b is connected to c, then a is connected to c

Connected Components  A connected component is a maximal set of elements that are mutually connected (i.e., an equivalence set) {0} {1,2} {3,4,8,9} {5,6} {7}

Implementing the Operations Recall – connect two elements, and answer if two elements have a path between them  Find: in which component is element a?  Union: replace components containging elements a and b with their union  Connected: are elements a and b in the same component?

Example Union(1,6) {0} {1,2} {3,4,8,9} {5,6} {7} {0} {1,2,5,6} {3,4,8,9} {7} Components?

Union-Find Data Type Goal: Design an efficient data structure for union-find  Number of elements can be huge  Number of operations can be huge  Union and find operations can be intermixed public class UF UF int(N); voidunion(int a, int b); intfind(int a); booleanconnected(int a, int b); ;;

Dynamic Connectivity Client  Read in number of elements N from stdin  Repeat: –Read in pair of integers from stdin –If not yet connected, connect them and print out pair read input int N while stdin is not empty read in pair of ints a and b if not connected (a, b) union(a, b) print out a and b ;;

Quick-Find  Data Structure –Integer array id[] of length N –Interpretation: id[a] is the id of the component containing a i: id[i]:

Quick-Find  Data Structure –Integer array id[] of length N –Interpretation: id[a] is the id of the component containing a  Find: what is the id of a?  Connected: do a and b have the same id?  Union: Change all the entries in id that have the same id as a to be the id of b.

Quick-Find Union(1,6) i: id: i: id: It works – so is there a problem? Well, there may be many values to change, and many to search!

Quick-Find  Quick-Find operation times –Initialization takes time O(N) –Union takes time O(N) –Find takes time O(1) –Connected takes time O(1)  Union is too slow – it takes O(N 2 ) array accesses to process N union operations on N elements

Quadratic Algos Do Not Scale!  Rough Standards (for now) –10 9 operations per second –10 9 words of memory –Touch all words in 1 second (+/- truism since 1950!)  Huge problem for Quick-Find:  10 9 union commands on 10 9 elements  Takes more than operations  This is 30+ years of computer time! 

Quadratic Algos Do Not Scale!  They do not keep pace with technology  New computer may be 10x as fast  But it has 10x as much memory  Want to solve problems 10x as big  With quadratic algorithm, it takes… … 10 x as long!!! 

Quick-Union  Data Structure –Integer array id[] of length N –Interpretation: id[a] is the parent of a –Component is root of a = id[id[…id[a]…]] (fixed point) i: id[i]:

Quick-Union  Data Structure – What is root of tree of a? – Do a and b have the same root? – Set id of root of b’s tree to be root of a’s tree i: id[i]:  –Find: –Connected: –Union:

Quick-Union i: id[i]: –Find 9 –Connected 8, 9: –Union 7,5 5 Only ONE value changes! = FAST

Quick-Union  Quick-Union operation times (worst case) –Initialization takes time O(N) –Union takes time O(N) (must find two roots) –Find takes time O(N) –Connected takes time O(N)  Now union AND find are too slow – it takes O(N 2 ) array accesses to process N operations on N elements

Quick-Find/Quick-Union  Observations:  Problem with Quick-Find is unions –May take N array accesses –Trees are flat, but too expensive to keep them flat!  Problem with Quick-Union –Trees may get tall –Find (and hence, connected and union) may take N array accesses

Weighted Quick-Union  Make Quick-Union trees stay short!  Keep track of tree size  Join smaller tree into larger tree –May alternatively do union by height/rank –Need to keep track of “weight” b a a b Quick-Union may do this But we always want this

Weighted Quick-Union  Weighted Quick-Union operation times –Initialization takes time O(N) –Union takes time O(1) (given roots) –Find takes time O(depth of a) –Connected takes time O(max {depth of a, b})  Proposition: Depth of any node x is at most lg N Pf: What causes depth of x to increase?

Weighted Quick-Union  Proposition: Depth of any node x is at most lg N Pf: What causes depth of x to increase? Only union! And if x is in smaller tree. So x’s tree must at least double in size each time union increases x’s depth Which can happen at most lg N times. (Why?)

Next – Lecture 3  Read Chapter 2  Empirical analysis  Asymptotic analysis of algorithms  Basic recurrences