Disjoint Sets with Arrays

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

1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 20 Prof. Erik Demaine.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 17 Union-Find on disjoint sets Motivation Linked list representation Tree representation.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
Analysis of Algorithms
Definition: Given an undirected graph G = (V, E), a spanning tree of G is any subgraph of G that is a tree Minimum Spanning Trees (Ch. 23) abc d f e gh.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
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.
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.
CSCI 3160 Design and Analysis of Algorithms Tutorial 3 Chengyu Lin.
MST – KRUSKAL UNIT IV. Disjoint-Set Union Problem Want a data structure to support disjoint sets – Collection of disjoint sets S = {S i }, S i ∩ S j =
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.
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.
David Luebke 1 3/1/2016 CS 332: Algorithms Dijkstra’s Algorithm Disjoint-Set Union.
21. Data Structures for Disjoint Sets Heejin Park College of Information and Communications Hanyang University.
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSE 373: Data Structures and Algorithms
Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
May 12th – Minimum Spanning Trees
Minimum Spanning Trees
Disjoint Sets Data Structure
CSE373: Data Structures & Algorithms Lecture 19: Spanning Trees
Minimum Spanning Trees
CMSC 341 Disjoint Sets Based on slides from previous iterations of this course.
Data Structures and Algorithms
Minimum Spanning Tree Neil Tang 3/25/2010
CS200: Algorithm Analysis
CSE 373 Data Structures and Algorithms
CSE 332: Data Abstractions Union/Find II
CSC 413/513: Intro to Algorithms
CSE 373: Data Structures and Algorithms
Minimum Spanning Tree Neil Tang 4/3/2008
CSE 373: Data Structures and Algorithms
CS 332: Algorithms Amortized Analysis Continued
Lecture 19: Minimum Spanning Trees
Lecture 20: Disjoint Sets
Kruskal’s algorithm for MST and Special Data Structures: Disjoint Sets
Lecture 18: Implementing Graphs
CSE 373: Data Structures and Algorithms
Lecture 21: Disjoint Sets with Arrays
Review of MST Algorithms Disjoint-Set Union Amortized Analysis
Lecture 23: Minimum Spanning Trees
Lecture 22: Implementing Dijkstra’s
Lecture 24: Disjoint Sets
CSE 373: Data Structures and Algorithms
Chapter 9: Graphs Spanning Trees
Lecture 27: Array Disjoint Sets
Lecture 26: Array Disjoint Sets
Presentation transcript:

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

TreeDisjointSet<E> Warm Up 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 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 Reminders: Union-by-rank: make the tree with the larger rank the new root, absorbing the other tree. If ranks are equal pick one at random, increase rank by 1 Path-compression: when running findSet() update parent pointers of all encountered nodes to point directly to overall root Union(x, y) internally calls findSet(x) and findSet(y) a b c d f h g CSE 373 SP 18 - Kasey Champion

TreeDisjointSet<E> Warm Up 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 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(g) makeSet(h) union(c, e) union(d, e) union(a, c) union(g, h) union(b, f) union(g, f) union(b, c) Reminders: Union-by-rank: make the tree with the larger rank the new root, absorbing the other tree. If ranks are equal pick one at random, increase rank by 1 Path-compression: when running findSet() update parent pointers of all encountered nodes to point directly to overall root Union(x, y) internally calls findSet(x) and findSet(y) https://courses.cs.washington.edu/courses/cse373/18sp/files/slides/disjoint_set_warmup.pdf CSE 373 SP 18 - Kasey Champion

Implementing Disjoint Sets Administrivia Monday Tuesday Wednesday Thursday Friday 5/21 Disjoint Sets 5/23 Implementing Disjoint Sets 5/24 Interview Prep 5/25 P vs NP HW 6 due HW 7 out 5/28 Memorial Day 5/30 Final Review 5/31 6/1 Tech Interview Prep HW 7 due 6/5 Final @ 8:30am Sorry, Kasey’s email is DEEP Want a meeting? Email me this week for times next week Have ANY grading questions/concerns, email Kasey by this weekend TA lead review TBA Alternative testing time TBA CSE 373 SP 18 - Kasey Champion

Optimized Disjoint Set Runtime makeSet(x) Without Optimizations With Optimizations findSet(x) union(x, y) O(1) O(1) O(n) Best case: O(1) Worst case: O(logn) O(n) Best case: O(1) Worst case: O(logn) CSE 373 SP 18 - Kasey Champion

Kruskal’s tm = time to make MSTs tf = time to find connected components tu = time to union KruskalMST(Graph G) initialize each vertex to be a connected component sort the edges by weight foreach(edge (u, v) in sorted order){ if(u and v are in different components){ add (u,v) to the MST Update u and v to be in the same component } O(V*tm) O(ElogE) / O(ElogV) O(V*tu+E*tf) tm = O(1) tf = O(logV) tu = O(logV) KruskalMST(Graph G) initialize a disjointSet, call makeSet() on each vertex sort the edges by weight foreach(edge (u, v) in sorted order){ if(findSet(u) != findSet(v)){ add (u,v) to the MST union(u, v) } O(V) O(ElogV) O(E) O(logV) O(logV) O(V + ElogV + ElogV) Aside: O(V + ElogV + E) if you apply ackermann CSE 373 SP 18 - Kasey Champion

initialize a disjointSet, call makeSet() on each vertex KruskalMST(Graph G) initialize a disjointSet, call makeSet() on each vertex sort the edges by weight foreach(edge (u, v) in sorted order){ if(findSet(u) != findSet(v)){ add (u,v) to the MST union(u, v) } CSE 373 SP 18 - Kasey Champion

initialize a disjointSet, call makeSet() on each vertex KruskalMST(Graph G) initialize a disjointSet, call makeSet() on each vertex sort the edges by weight foreach(edge (u, v) in sorted order){ if(findSet(u) != findSet(v)){ union(u, v) } CSE 373 SP 18 - Kasey Champion

Implementation Use Nodes? In modern Java (assuming 64-bit JDK) each object takes about 32 bytes int field takes 4 bytes Pointer takes 8 bytes Overhead ~ 16 bytes Adds up to 28, but we must partition in multiples of 8 => 32 bytes Use arrays instead! Make index of the array be the vertex number Either directly to store ints or representationally We implement makeSet(x) so that we choose the representative Make element in the array the index of the parent CSE 373 SP 18 - Kasey Champion

Array Implementation rank = 0 rank = 3 rank = 3 1 2 3 4 5 6 7 8 9 10 1 11 2 6 12 15 3 4 5 7 10 13 14 16 17 8 9 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -1 -4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -1 Store (rank * -1) - 1 Each “node” now only takes 4 bytes of memory instead of 32 CSE 373 SP 18 - Kasey Champion

Practice rank = 2 rank = 1 rank = 0 rank = 2 1 2 3 4 5 6 7 8 9 10 11 13 4 7 9 10 12 15 14 1 11 2 8 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -3 -1 -2 CSE 373 SP 18 - Kasey Champion

Array Method Implementation makeSet(x) add new value to array with a rank of -1 findSet(x) Jump into array at index/value you’re looking for, jump to parent based on element at that index, continue until you hit negative number union(x, y) findSet(x) and findSet(y) to decide who has larger rank, update element to represent new parent as appropriate CSE 373 SP 18 - Kasey Champion

Graph Review Graph Definitions/Vocabulary Graph Traversals Vertices, Edges Directed/undirected Weighted Etc… Graph Traversals Breadth First Search Depth First Search Finding Shortest Path Dijkstra’s Topological Sort Minimum Spanning Trees Primm’s Kruskal’s Disjoint Sets Implementing Kruskal’s CSE 373 SP 18 - Kasey Champion

Interview Prep Treat it like a standardized test Typically 2 rounds Cracking the Coding Interview Hackerrank.com Leetcode.com Typically 2 rounds Tech screen “on site” interviews 4 general types of questions Strings/Arrays/Math Linked Lists Trees Hashing Optional: Design It’s a conversation! T – Talk E – Examples B – Brute Force O – Optimize W – Walk through I - Implement T – Test CSE 373 SP 18 - Kasey Champion