CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

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.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
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.
MA/CSSE 473 Day 36 Kruskal proof recap Prim Data Structures and detailed algorithm.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
D ESIGN & A NALYSIS OF A LGORITHM 06 – D ISJOINT S ETS Informatics Department Parahyangan Catholic University.
Homework remarking requests BEFORE submitting a remarking request: a)read and understand our solution set (which is posted on the course web site) b)read.
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.
Minimum Spanning Trees Featuring Disjoint Sets HKOI Training 2006 Liu Chi Man (cx) 25 Mar 2006.
Union-Find A data structure for maintaining a collection of disjoint sets Course: Data Structures Lecturers: Haim Kaplan and Uri Zwick January 2014.
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
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.
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 and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Disjoint Sets Data Structure
MA/CSSE 473 Days Answers to student questions
Minimum Spanning Trees
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.
Data Structures and Algorithms
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
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Sorting and recurrence analysis techniques
Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Lecture 19: Minimum Spanning Trees
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Lecture 18: Implementing Graphs
Lecture 9: Intro to Trees
Lecture 23: Minimum Spanning Trees
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:

CSE 373: Data Structures and Algorithms Disjoint Sets Autumn 2018 Shrirang (Shri) Mare shri@cs.washington.edu Thanks to Kasey Champion, Ben Jones, Adam Blank, Michael Lee, Evan McCarty, Robbie Weber, Whitaker Brand, Zora Fung, Stuart Reges, Justin Hsia, Ruth Anderson, and many others for sample slides and materials ...

Announcements Final exam info (logistics, topics covered, and practice material) is on course website. Homework 6 is due this Friday at noon. Homework 7 will be posted this Friday evening. Fill out the team sign up form by tomorrow 5pm to get the repo in time. Fill out the partner pool form by tomorrow 5pm to get assigned to a partner. CSE 373 AU 18

Today Trees and Forests Kruskal’s algorithm Disjoint Set ADT CSE 373 AU 18

Trees and Forests A tree is an undirected, connected, and acyclic graph. A graph, however, can have unconnected vertices, or multiple trees. Collection of trees is a called a forest. A forest is any undirected and acyclic graph. By definition a tree is a forest CSE 373 AU 18

Worksheet question 1 CSE 373 AU 18

Worksheet question 2a Step Components Edge Include? 1 CSE 373 AU 18

Worksheet question 2b CSE 373 AU 18

Kruskal’s Algorithm CSE 373 AU 18

Minimum spanning forest Given a forest, find a set of minimum spanning trees for each tree in the given forest. Question: Which algorithm (Prims or Kruskals) would you use to find a minimum spanning forest? CSE 373 AU 18

Disjoint Set ADT CSE 373 AU 18

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 AU 18

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 AU 18

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 AU 18

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 AU 18

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 AU 18

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 AU 18

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 AU 18 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 AU 18 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 AU 18 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 AU 18 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 AU 18

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 AU 18

Worksheet question 3 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 AU 18

Worksheet question 3 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(4, 12) union(2, 8) Does this improve the worst case runtimes? findSet is more likely to be O(log(n)) than O(n) CSE 373 AU 18

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 11 6 9 10 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 AU 18