COP 4531 Complexity & Analysis of Data Structures & Algorithms

Similar documents
Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Maintaining Disjoint Sets

Data Structures for Disjoint Sets

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

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

Disjoint Sets 3/22/2010

Union-Find and Amortization

Disjoint Sets. Definition Linked List Representation Disjoint-Set Forests. CS 5633 Analysis of Algorithms Chapter 21: Slide 1

Disjoint-set data structure: Union-Find. Lecture 20

Amortized Analysis. The time required to perform a sequence of (data structure) operations is averaged over all the operations performed.

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

COMP Analysis of Algorithms & Data Structures

Homework remarking requests

Complexity of Prim s Algorithm

CSE 100 Disjoint Set, Union Find

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Chapter 23. Minimum Spanning Trees

Outline. Disjoint set data structure Applications Implementation

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.

CS 310 Advanced Data Structures and Algorithms

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

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

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum spanning trees

CSE 100: GRAPH ALGORITHMS

CSC263 Week 11. Larry Zhang.

Minimum Spanning Trees My T. UF

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

Course Review for Finals. Cpt S 223 Fall 2008

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley

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

Dynamic Data Structures. John Ross Wallrabenstein

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

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

CMSC 341 Lecture 20 Disjointed Sets

CMSC 341 Lecture 20 Disjointed Sets

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

Greedy Algorithms Part Three

Minimum Spanning Trees

CMSC 341 Lecture 20 Disjointed Sets

Union-Find: A Data Structure for Disjoint Set Operations

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Kruskal s MST Algorithm

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

Course Review. Cpt S 223 Fall 2009

CS S-14 Disjoint Sets 1

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

Lecture 13: Minimum Spanning Trees Steven Skiena

Union Find. Data Structures and Algorithms Andrei Bulatov

Union-Find: A Data Structure for Disjoint Set Operations

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4)

Chapter 5. Greedy algorithms

Amortized Analysis and Union-Find , Inge Li Gørtz

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

Minimum Spanning Tree (5A) Young Won Lim 5/11/18

Greedy Approach: Intro

Disjoint set (Union-Find)

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

CSE 373: Data Structures and Algorithms. Disjoint Sets. Autumn Shrirang (Shri) Mare

Lecture Notes for Chapter 23: Minimum Spanning Trees

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

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

Disjoint Sets. Andreas Klappenecker [using notes by R. Sekar]

CISC 320 Midterm Exam

6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro

UNION FIND. naïve linking link-by-size link-by-rank path compression link-by-rank with path compression context

Union-Find Structures. Application: Connected Components in a Social Network

Union-Find Disjoint Set

LECTURE 26 PRIM S ALGORITHM

Course Review. Cpt S 223 Fall 2010

CSE 21 Mathematics for Algorithm and System Analysis

COS 528 Disjoint Sets and Compressed Trees. Robert E. Tarjan 2013

Lecture Summary CSC 263H. August 5, 2016

Solutions to Exam Data structures (X and NV)

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Minimum Spanning Trees. COMPSCI 355 Fall 2016

Optimization I : Brute force and Greedy strategy

Lecture 6 Basic Graph Algorithms

CSE 638: Advanced Algorithms. Lectures 10 & 11 ( Parallel Connected Components )

CS 561, Lecture 9. Jared Saia University of New Mexico

CSci 231 Final Review

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Advanced Set Representation Methods

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

CSE 100: UNION-FIND HASH

Problem Set 8 Solutions

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Concurrent Disjoint Set Union. Robert E. Tarjan Princeton University & Intertrust Technologies joint work with Siddhartha JayanB, Princeton

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

Algorithm Design and Analysis

Disjoint Sets. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION

Trees, Glorious Trees. From one new year to another new year, and from one new academic year to another new academic year ראש השנה לאילנות

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Dijkstra s algorithm for shortest paths when no edges have negative weight.

EECS 477: Introduction to algorithms. Lecture 10

Transcription:

COP 4531 Complexity & Analysis of Data Structures & Algorithms Lecture 8 Data Structures for Disjoint Sets Thanks to the text authors who contributed to these slides

Data Structures for Disjoint Sets Also known as union-find. Maintain collection S = {S 1,, S k } of disjoint dynamic (changing over time) sets. Each set is identified by a representative, which is some member of the set. It doesn t matter which member is the representative, as long as if we ask for the representative twice without modifying the set, we get the same answer both times. 2

Operations MAKE-SET(x): make a new set S i = {x}, and add S i to S. UNION(x, y): if x S x, y S y, then S = S S x S y {S x S y }. Representative of new set is any member of S x S y, often x or y. Destroys S x and S y (since sets must be disjoint). FIND-SET(x): returns representative of set containing x. Analysis in terms of: n = # of elements = # of MAKE-SET operations, m = total # of operations. 3

Analysis Since MAKE-SET counts toward total # of operations, m n. Can have at most n - 1 UNION operations, since after n - 1 UNIONs, only 1 set remains. Assume that the first n operations are MAKE-SET (helpful for analysis, usually not really necessary). 4

Example Application: Dynamic connected components Connected-Components(G) for each vertex v G.V Make-Set(v) for each edge (u, v) G.E if Find-Set(u) Find-Set(v) Union(u, v)! Same-Component(u, v) if Find-Set(u) == Find-Set(v) return True else return False When implementing Connected-Components, each vertex needs a handle to its object in the disjoint-set data structure each object in the disjoint-set data structure needs a handle to its vertex 5

Example 6

Implementing Union-Find: Linked list representation Each set is a singly linked list, represented by an object with attributes head: the first element in the list, assumed to be the set s representative, and tail: the last element in the list. Objects may appear within the list in any order. Each object in the list has attributes for the set member, pointer to the set object, and next. 7

Sets using linked lists and basic implementation of the union operation 8

Analysis of complexity of operations Make-Set(x): create a singleton list complexity: Θ(1) Find-Set(x): follow vertex x pointer back to the list object, and then follow the head pointer to the representative complexity: Θ(1) Union(x, y): append y s list onto end of x s list. Use x s tail pointer to find the end Need to update the pointer back to the set object for every node on y s list. If appending a large list onto a small list, it can take a while. What is worst case complexity for n union operations? Turns out to be Θ(n 2 ) for n operations or an amortized complexity of Θ(n) per union operation 9

Worst case for n union operations can be Θ(n 2 ) 10

Weighted-union heuristic Always append the smaller list to the larger list (break ties arbitrarily) A single union can still take Ω(n) consider case if both sets have n/2 members However, a sequence of m operations on n elements takes O(m + n lg n) time Why? Each Make-set and Find-set still takes O(1). But an object s representative updated by union must always be in the smaller set each time of the update. After the update it must be in a set of twice the size Thus each representative can only be updated lg n times. 11

Can we still do better? Yes, with a Disjoint-set forest representation First Try 1 tree per set. Root is representative Each node points only to its parent Make-Set: make a single-node tree Union: make one root child of the other Find-Set: follow pointers to the root 12

Problem with naïve implementation of Disjoint-set forests Note that Union is Θ(1) but Find-Set is not. What is Find-Set s complexity? Well, the worst case is if we create a linear chain of nodes 13

We do better using two heuristics Union by rank: make the root of the smaller tree (fewer nodes) a child of the root of the larger tree we actually use rank, which is an upper bound on the height of a node make root with smaller rank child of root with larger rank Path compression: Let Find path be the nodes visited during Find-Set. make all nodes on find path direct children of root 14

Complexity of m operations on n elements using disjoin-set forests Path compression (shown above) and union by rank together result in: Worst case running time is now O(m α(n)) where α(n) is a very very slowing growing function of n. Effective, it can be viewed as a constant which gives us a running time linear in m 15

Summary We have two good ways to represent algorithms that use Union-Find linked lists which give us O(m + n lg n) running time disjoint forests which effectively give us O(m) running time Union-Find is used in the spanning tree algorithms that we next explore (Kruskal s algorithm and Prim s algorithm) we assume the best representation when needed in analyzing the running time of these algorithms 16