Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Disjoint Sets CS2 -- 9/8/2010. Disjoint Sets A disjoint set contains a set of sets such that in each set, an element is designated as a marker for the.
Advertisements

Union-Find structure Mikko Malinen School of Computing University of Eastern Finland.
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.
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.
Andreas Klappenecker [Based on slides by Prof. Welch]
Disjoint-Set Operation
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
CPSC 411, Fall 2008: Set 7 1 CPSC 411 Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Jennifer Welch Fall 2008.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Disjoint Sets Prof. Jennifer Welch Fall 2009.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
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 Week 9 Spanning Trees We will now consider another famous problem in graphs. Recall the bridges problem again. The purpose of the bridge.
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
Data Structure (III) GagGuy.
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
CS 473Lecture X1 CS473-Algorithms I Lecture X1 Properties of Ranks.
Disjoint Sets Data Structure (Chap. 21) A disjoint-set is a collection  ={S 1, S 2,…, S k } of distinct dynamic sets. Each set is identified by a member.
Lecture X Disjoint Set Operations
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.
CSCE 411H Design and Analysis of Algorithms Set 7: Disjoint Sets Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 7 1 * Slides adapted from.
Disjoint-Set Operation. p2. Disjoint Set Operations : MAKE-SET(x) : Create new set {x} with representative x. UNION(x,y) : x and y are elements of two.
1 Today’s Material The dynamic equivalence problem –a.k.a. Disjoint Sets/Union-Find ADT –Covered in Chapter 8 of the textbook.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
Disjoint-sets Abstract Data Type (ADT) that contains nonempty pairwise disjoint sets: For all sets X and Y in the container, – X != ϴ and Y != ϴ – And.
MA/CSSE 473 Days Answers to student questions Prim's Algorithm details and data structures Kruskal details.
Chapter 21 Data Structures for Disjoint Sets Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Prof. Tsai, Shi-Chun as well as various.
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
MA/CSSE 473 Day 37 Student Questions Kruskal Data Structures and detailed algorithm Disjoint Set ADT 6,8:15.
CSE 373: Data Structures and Algorithms
Data Structures: Disjoint Sets, Segment Trees, Fenwick Trees
MA/CSSE 473 Day 36 Student Questions More on Minimal Spanning Trees
Disjoint Sets Data Structure
MA/CSSE 473 Days Answers to student questions
Disjoint Sets Chapter 8.
MA/CSSE 473 Day 36 Kruskal proof recap
Data Structures: Disjoint Sets
Data Structures and Algorithms
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
Data Structures and Algorithms
CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find Linda Shapiro Spring 2016.
Disjoint Sets with Arrays
B-Tree Insertions, Intro to Heaps
CS200: Algorithm Analysis
CSE 373 Data Structures and Algorithms
CSCE 411 Design and Analysis of Algorithms
CSE 332: Data Abstractions Union/Find II
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 9: Self Balancing Trees
Union-Find Partition Structures
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Lecture 18: Implementing Graphs
Lecture 21: Disjoint Sets with Arrays
Lecture 9: Intro to Trees
Lecture 21 Amortized Analysis
Lecture 22: Implementing Dijkstra’s
Lecture 24: Disjoint Sets
CSE 373: Data Structures and Algorithms
Lecture 16: Midterm recap + Heaps ii
Lecture 27: Array Disjoint Sets
Lecture 26: Array Disjoint Sets
Presentation transcript:

Data Structures and Algorithms Disjoint Sets Data Structures and Algorithms CSE 373 SP 18 - Kasey Champion

Warm Up Finding a MST using Kruskal’s algorithm 8 7 b c d 9 4 2 4 14 e 11 i 6 a 10 7 8 h g f 1 2 CSE 373 SP 18 - Kasey Champion

Warm Up Finding a MST using Kruskal’s algorithm 8 7 b c d 9 4 2 4 14 e 11 i 6 a 10 7 8 h g f 1 2 CSE 373 SP 18 - Kasey Champion

New ADT Set ADT state behavior Disjoint-Set ADT state behavior B A D C create(x) - creates a new set with a single member, x Count of Elements state behavior Set of elements Elements must be unique! No required order add(x) - adds x into set if it is unique, otherwise add is ignored remove(x) – removes x from set size() – returns current number of elements in set Disjoint-Set ADT makeSet(x) – creates a new set within the disjoint set where the only member is x. Picks representative for set Count of Sets state behavior Set of Sets Disjoint: Elements must be unique across sets No required order Each set has representative findSet(x) – looks up the set containing element x, returns representative of that set union(x, y) – looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set B A C G H D F D A C B CSE 373 SP 18 - Kasey Champion

Example new() makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e) findSet(a) findSet(d) union(a, c) b Rep: 1 a Rep: 0 c Rep: 2 e Rep: 4 d Rep: 3 CSE 373 WI 18 – Michael Lee

Example new() makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e) findSet(a) findSet(d) union(a, c) union(b, d) b Rep: 1 a Rep: 0 c e Rep: 4 d Rep: 3 CSE 373 WI 18 – Michael Lee

Example new() makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e) findSet(a) findSet(d) union(a, c) union(b, d) b Rep: 1 a Rep: 0 d c e Rep: 4 findSet(a) == findSet(c) findSet(a) == findSet(d) CSE 373 WI 18 – Michael Lee

TreeDisjointSet<E> Implementation TreeSet<E> TreeSet(x) state behavior SetNode overallRoot add(x) remove(x, y) getRep()-returns data of overallRoot Disjoint-Set ADT makeSet(x) – creates a new set within the disjoint set where the only member is x. Picks representative for set Count of Sets state behavior Set of Sets Disjoint: Elements must be unique across sets No required order Each set has representative findSet(x) – looks up the set containing element x, returns representative of that set union(x, y) – looks up set containing x and set containing y, combines two sets into one. Picks new representative for resulting set TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory SetNode<E> SetNode(x) state behavior E data addChild(x) removeChild(x, y) Collection<SetNode> children CSE 373 SP 18 - Kasey Champion

TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement makeSet(x) forest makeSet(0) makeSet(1) makeSet(2) makeSet(3) makeSet(4) makeSet(5) 1 2 3 4 5 1 2 3 4 5 Worst case runtime? O(1) CSE 373 SP 18 - Kasey Champion

TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) 1 2 3 4 5 1 2 3 4 5 -> CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)

TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) union(2, 1) 1 2 3 4 5 1 2 3 4 5 -> CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)

TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) union(2, 1) union(2, 5) 2 3 4 1 5 1 2 3 4 5 -> CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)

TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement union(x, y) forest union(3, 5) union(2, 1) union(2, 5) 2 4 1 3 5 1 2 3 4 5 CSE 373 SP 18 - Kasey Champion Runtime Call findSet on both x and y Figure out where to add y into x Worst case runtime? O(n)

TreeDisjointSet<E> makeSet(x)-create a new tree of size 1 and add to our forest state behavior Collection<TreeSet> forest findSet(x)-locates node with x and moves up tree to find root union(x, y)-append tree with y as a child of tree with x Dictionary<NodeValues, NodeLocations> nodeInventory Implement findSet(x) forest findSet(0) findSet(3) findSet(5) 2 4 1 3 5 1 2 3 4 5 Worst case runtime? O(n) Worst case runtime of union? CSE 373 SP 18 - Kasey Champion

Improving union Problem: Trees can be unbalanced Solution: Union-by-rank! let rank(x) be a number representing the upper bound of the height of x so rank(x) >= height(x) Keep track of rank of all trees When unioning make the tree with larger rank the root If it’s a tie, pick one randomly and increase rank by one rank = 0 rank = 0 rank = 1 rank = 0 rank = 2 2 4 4 1 3 5 CSE 373 SP 18 - Kasey Champion

Practice Given the following disjoint-set what would be the result of the following calls on union if we add the “union-by-rank” optimization. Draw the forest at each stage with corresponding ranks for each tree. rank = 2 rank = 0 rank = 2 rank = 1 6 2 8 7 4 3 9 10 11 13 1 5 12 union(2, 13) union(4, 12) union(2, 8) CSE 373 SP 18 - Kasey Champion

Practice Given the following disjoint-set what would be the result of the following calls on union if we add the “union-by-rank” optimization. Draw the forest at each stage with corresponding ranks for each tree. rank = 3 8 6 9 10 11 7 4 3 12 13 2 1 5 union(2, 13) union(12, 4) union(2, 8) Does this improve the worst case runtimes? findSet is more likely to be O(log(n)) than O(n) CSE 373 SP 18 - Kasey Champion

Improving findSet() Problem: Every time we call findSet() you must traverse all the levels of the tree to find representative Solution: Path Compression Collapse tree into fewer levels by updating parent pointer of each node you visit Whenever you call findSet() update each node you touch’s parent pointer to point directly to overallRoot rank = 3 rank = 2 findSet(5) findSet(4) 8 8 6 9 10 11 7 5 4 6 9 10 11 7 Does this improve the worst case runtimes? findSet is more likely to be O(1) than O(log(n)) 4 3 12 13 2 3 12 13 2 5 CSE 373 SP 18 - Kasey Champion

Example Using the union-by-rank and path-compression optimized implementations of disjoint-sets draw the resulting forest caused by these calls: makeSet(a) makeSet(b) makeSet(c) makeSet(d) makeSet(e) makeSet(f) makeSet(h) union(c, e) union(d, e) union(a, c) union(g, h) union(b, f) union(g, f) union(b, c) rank = 2 e a b c d f h g CSE 373 SP 18 - Kasey Champion

Array Representation Like heaps, pretend the tree exists, but use an Array for actual implementation CSE 373 SP 18 - Kasey Champion