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

Size: px
Start display at page:

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

Transcription

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

2 Making a Good Maze What s a Good Maze? Connected Just one path between any two rooms Random The Maze Construction Problem Given: collection of rooms: V connections between rooms (initially all closed): E Construct a maze: collection of rooms: V = V designated rooms in, i V, and out, o V collection of connections to knock down: E E such that one unique path connects every two rooms (Disjoint Sets) Data Structures and Programming Spring / 50

3 Maze Construction Algorithm While edges remain in E 1 Remove a random edge e = (u, v) from E How can we do this efficiently? 2 If u and v have not yet been connected add e to E mark u and v as connected How to check connectedness efficiently? (Disjoint Sets) Data Structures and Programming Spring / 50

4 Equivalence Relations An equivalence relation R( A A) must have three properties reflexive: x A, (x, x) R symmetric: (x, y) R (y, x) R transitive: (x, y) R (y, z) R (x, z) R Connection between rooms is an equivalence relation any room is connected to itself if room a is connected to room b, then room b is connected to room a if room a is connected to room b and room b is connected to room c, then room a is connected to room c (Disjoint Sets) Data Structures and Programming Spring / 50

5 Disjoint Set Union/Find ADT Union/Find operations create destroy union find Disjoint set partition property: element of a DS U/F structure belongs to exactly one set with a unique name Dynamic equivalence property: Union(a, b) creates a new set which is the union of the sets containing a and b (Disjoint Sets) Data Structures and Programming Spring / 50

6 Example Construct the maze on the right Initial (the name of each set is in boldface): {a}{b}{c}{d}{e}{f}{g}{h}{i} Randomly select edge 1 (Disjoint Sets) Data Structures and Programming Spring / 50

7 Example, First Step {a}{b}{c}{d}{e}{f}{g}{h}{i} find(b) b find(e) e find(b) find(e) so: add 1 to E union(b, e) {a}{b, e}{c}{d}{f}{g}{h}{i} (Disjoint Sets) Data Structures and Programming Spring / 50

8 Up-Tree Intuition Finding the representative member of a set is somewhat like the opposite of finding whether a given key exists in a set. So, instead of using trees with pointers from each node to its children; let s use trees with a pointer from each node to its parent. Each subset is an up-tree with its root as its representative member All members of a given set are nodes in that set s up-tree Hash table maps input data to the node associated with that data (Disjoint Sets) Data Structures and Programming Spring / 50

9 Find (Disjoint Sets) Data Structures and Programming Spring / 50

10 Union (Disjoint Sets) Data Structures and Programming Spring / 50

11 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

12 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

13 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

14 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

15 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

16 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

17 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

18 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

19 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

20 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

21 The Whole Example (Disjoint Sets) Data Structures and Programming Spring / 50

22 Nifty storage trick A forest of up-trees can easily be stored in an array. Also, if the node names are integers or characters, we can use a very simple, perfect hash. (Disjoint Sets) Data Structures and Programming Spring / 50

23 Room for Improvement: Weighted Union Always makes the root of the larger tree the new root Often cuts down on height of the new up-tree (Disjoint Sets) Data Structures and Programming Spring / 50

24 Weighted Union Find Analysis Finds with weighted union are O(max up-tree height) But, an up-tree of height h with weighted union must have at least 2 h nodes Hence, 2 max height n and max height log n So, find takes O(log n) (Disjoint Sets) Data Structures and Programming Spring / 50

25 Alternatives to Weighted Union Union by height Ranked union (cheaper approximation to union by height) (Disjoint Sets) Data Structures and Programming Spring / 50

26 Room for Improvement: Path Compression Points everything along the path of a find to the root Reduces the height of the entire access path to 1 (Disjoint Sets) Data Structures and Programming Spring / 50

27 Path Compression Example (Disjoint Sets) Data Structures and Programming Spring / 50

28 Two Heuristics 1 Union by Rank Store rank of tree in rep. Rank tree size. Make root with smaller rank point to root with larger rank. 2 Path Compression During Find-Set, flatten tree. (Disjoint Sets) Data Structures and Programming Spring / 50

29 Operations (Disjoint Sets) Data Structures and Programming Spring / 50

30 Find Set (Disjoint Sets) Data Structures and Programming Spring / 50

31 A Slow Growing Function (Disjoint Sets) Data Structures and Programming Spring / 50

32 Complex Complexity of Weighted Union + Path Compression Tarjan (1984) proved that m weighted union and find operations with path compression on a set of n elements have worst case complexity actually even a little better! O(m log (n)) For all practical purposes this is amortized constant time (log n 5) (Disjoint Sets) Data Structures and Programming Spring / 50

33 One-pass path compression We can modify the structure of the tree as each edge is processed to attempt to save on the overall run-time of the algorithm. When we do a Find, we can do a bit of maintenance work along the path traversed. (Disjoint Sets) Data Structures and Programming Spring / 50

34 Analysis n elements (n Make-Sets, at most n 1 Unions) m Make-Set, Union, and Find-Set operations m = Ω(n) worst case = O(m α(m, n)) = O(m log n) α(m, n) is the inverse of Ackermann s function a very very very slow function A(1, j) = 2j for j 1 A(i, 1) = A(i 1, 2) for i 2 A(i, j) = A(i 1, A(i, j 1)) for i, j 2 α(m, n) = min{i 1 : A(i, m/n) > log n} (Disjoint Sets) Data Structures and Programming Spring / 50

35 Ackermann s Function (Disjoint Sets) Data Structures and Programming Spring / 50

36 Properties of Ranks: # 1 For all tree roots x, size(x) 2 rank(x) Basis: rank = 0, 2 0 = 1 = only one root node Induction: Let s Union(a, b) rank is increased by 1 only if rank[a] = rank[b] size(a b) = size(a)+size(b) 2 rank[a]+2rank[b] = 2 rank[a]+1 = 2 rank[a b] (Disjoint Sets) Data Structures and Programming Spring / 50

37 Properties of Ranks: # 2 The ranks of the nodes on a path from a leaf to a root increase monotonically (Disjoint Sets) Data Structures and Programming Spring / 50

38 Properties of Ranks: # 3 The number of nodes of rank r is at most n/2 r each node of rank r is the root of a subtree of at least 2 r nodes (property #1) all subtrees are disjoint Hence, there are at most n/2 r disjoint subtrees of rank r there are at most n/2 r nodes of rank r (Disjoint Sets) Data Structures and Programming Spring / 50

39 Analysis Actual cost of Find-Set( x ) is the number of nodes of the path from x to the root Accounting method : deposit 1 dollar for every node on the path from x to the root For m operations, how many dollars did we spend? Partition ranks into groups group 0 contains only elements rank r goes to group G(r) the largest rank in any group g is F(g), F = G 1 the number of ranks in any group g > 0 is F(g) F(g 1) (Disjoint Sets) Data Structures and Programming Spring / 50

40 Rank Grouping : Example (Disjoint Sets) Data Structures and Programming Spring / 50

41 Money Deposit (Disjoint Sets) Data Structures and Programming Spring / 50

42 Money Deposit (Disjoint Sets) Data Structures and Programming Spring / 50

43 Money Deposit Rules Deposit one dollar at the central account if v is the root the parent of v is the root the parent of v is in a different group from v Otherwise deposit one dollar into the node After m operations, the total deposit at the central account is at most m(g(n) + 2) Each operation deposits at most (G(n) + 2) dollars (Disjoint Sets) Data Structures and Programming Spring / 50

44 Central Account Deposit (Disjoint Sets) Data Structures and Programming Spring / 50

45 Node Accounts Let node v be in group g. How many dollars can be deposited into node v? If one dollar is deposited into a node v, v will get a new parent of higher rank. How many times can v be moved before its parent get pushed out of group g? Answer : the number of ranks in group g = F(g) F(g 1) 1 (F(g) is the largest rank # in group g ) (Disjoint Sets) Data Structures and Programming Spring / 50

46 The Number of Nodes in a Group The number of nodes, V(g), in group g > 0 is at most n/2 F(g 1) (Disjoint Sets) Data Structures and Programming Spring / 50

47 Total Node Deposit The maximum number of dollars deposited to all nodes in group g is at most nf(g)/2 F(g 1) the number of nodes in group g > 0 is at most n/2 F(g 1) each node gets at most F(g) F(g 1) 1 dollars F(g) F(g 1) 1 < F(g) The total node deposit is at most G(n) nf(g)/2 F(g 1) g=1 (Disjoint Sets) Data Structures and Programming Spring / 50

48 Total Deposit G(n) m(g(n) + 2) + n g=1 F(g)/2 F(g 1) Central account deposit + Total node deposit What are G(n) and F(g)? (Disjoint Sets) Data Structures and Programming Spring / 50

49 Total Deposit A Loose Bound Let F(g) = g, G(r) = r m(n + 2) + n G(n) m(g(n) + 2) + n g=1 F(g)/2 F(g 1) becomes n g/2 g 1 < m(n + 2) + 2n = O(mn) g=1 Very loose upper bound, worse than O(m log n) (Disjoint Sets) Data Structures and Programming Spring / 50

50 Total Deposit Let F(g) = 2 F(g 1), G(r) = 1 + log n Note that m = Ω(n) G(n) m(g(n) + 2) + n g=1 becomes F(g)/2 F(g 1) 1+log n m(1 + log n + 2) + n 2 F(g 1) /2 F(g 1) g=1 = m(3 + log n) + n(1 + log n) = O(m log n) (Disjoint Sets) Data Structures and Programming Spring / 50

Union-Find: A Data Structure for Disjoint Set Operations

Union-Find: A Data Structure for Disjoint Set Operations Union-Find: A Data Structure for Disjoint Set Operations 1 Equivalence Relations A equivalence relation R is defined on a set S, if for every pair of elements (a,b) in S, a R b is either false or true

More information

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

Union-find algorithms. (1) How do you determine whether a vertex x is connected to a vertex y in a graph? Union-find algorithms Consider the following problems: (1) How do you determine whether a vertex x is connected to a vertex y in a graph? (2) Let vertices correspond to objects and edges mean "is in the

More information

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

CSE373: Data Structures & Algorithms Lecture 11: Implementing Union-Find. Lauren Milne Spring 2015 CSE: Data Structures & Algorithms Lecture : Implementing Union-Find Lauren Milne Spring 05 Announcements Homework due in ONE week Wednesday April 9 th! TA sessions Catie will be back on Monday. The plan

More information

Today s Outline. Motivation. Disjoint Sets. Disjoint Sets and Dynamic Equivalence Relations. Announcements. Today s Topics:

Today s Outline. Motivation. Disjoint Sets. Disjoint Sets and Dynamic Equivalence Relations. Announcements. Today s Topics: Today s Outline Disjoint Sets and Dynamic Equivalence Relations Announcements Assignment # due Thurs 0/ at pm Today s Topics: Disjoint Sets & Dynamic Equivalence CSE Data Structures and Algorithms 0//0

More information

Union-Find: A Data Structure for Disjoint Set Operations

Union-Find: A Data Structure for Disjoint Set Operations Union-Find: A Data Structure for Disjoint Set Operations 1 The Union-Find Data Structure Purpose: Great for disjoint sets Operations: Union ( S 1, S 2 ) Find ( x ) Performs a union of two disjoint sets

More information

Advanced Set Representation Methods

Advanced Set Representation Methods Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation

More information

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

Amortized Analysis and Union-Find , Inge Li Gørtz Amortized Analysis and Union-Find 02283, Inge Li Gørtz 1 Today Amortized analysis 3 different methods 2 examples Union-Find data structures Worst-case complexity Amortized complexity 2 Amortized Analysis

More information

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

Union-Find Structures. Application: Connected Components in a Social Network Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 01 Union-Find Structures Union-Find 1 Application: Connected Components in a Social

More information

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

Outline Purpose Disjoint Sets Maze Generation. Disjoint Sets. Seth Long. March 24, 2010 March 24, 2010 Equivalence Classes Some Applications Forming Equivalence Classes Equivalence Classes Some Applications Forming Equivalence Classes Equivalence Classes The class of all things equivelant

More information

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

Disjoint Sets. Andreas Klappenecker [using notes by R. Sekar] Disjoint Sets Andreas Klappenecker [using notes by R. Sekar] Equivalence Classes Frequently, we declare an equivalence relation on a set S. So the elements of the set S are partitioned into equivalence

More information

Data Structures for Disjoint Sets

Data Structures for Disjoint Sets Data Structures for Disjoint Sets Manolis Koubarakis 1 Dynamic Sets Sets are fundamental for mathematics but also for computer science. In computer science, we usually study dynamic sets i.e., sets that

More information

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms 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

More information

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

Disjoint Sets. The obvious data structure for disjoint sets looks like this. CS61B Summer 2006 Instructor: Erin Korber Lecture 30: 15 Aug. Disjoint Sets Given a set of elements, it is often useful to break them up or partition them into a number of separate, nonoverlapping groups.

More information

Disjoint Sets and the Union/Find Problem

Disjoint Sets and the Union/Find Problem Disjoint Sets and the Union/Find Problem Equivalence Relations A binary relation R on a set S is a subset of the Cartesian product S S. If (a, b) R we write arb and say a relates to b. Relations can have

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Disjoin Sets and Union-Find Structures CLRS 21.121.4 University of Manitoba 1 / 32 Disjoint Sets Disjoint set is an abstract data type

More information

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees

CSE 332 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees CSE 33 Data Abstractions: Disjoint Set Union-Find and Minimum Spanning Trees Kate Deibel Summer 0 August 3, 0 CSE 33 Data Abstractions, Summer 0 Making Connections You have a set of nodes (numbered -9)

More information

Data Structures in Java. Session 18 Instructor: Bert Huang

Data Structures in Java. Session 18 Instructor: Bert Huang Data Structures in Java Session 18 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3134 Announcements Homework posted, due 11/24 Old homeworks, midterm exams Review Shortest Path algorithms

More information

Data Structures in Java. Session 18 Instructor: Bert Huang

Data Structures in Java. Session 18 Instructor: Bert Huang Data Structures in Java Session 18 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3134 Announcements Homework 4 due Homework 5 posted, due 11/24 Graph theory problems Programming: All-pairs

More information

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

Disjoint Sets. Based on slides from previous iterations of this course. Disjoint Sets Based on slides from previous iterations of this course Today s Topics Exam Discussion Introduction to Disjointed Sets Disjointed Set Example Operations of a Disjointed Set Types of Disjointed

More information

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

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs Representations of Weighted Graphs (as Matrices) A B Algorithms and Data Structures: Minimum Spanning Trees 9.0 F 1.0 6.0 5.0 6.0 G 5.0 I H 3.0 1.0 C 5.0 E 1.0 D 28th Oct, 1st & 4th Nov, 2011 ADS: lects

More information

CMSC 341 Lecture 20 Disjointed Sets

CMSC 341 Lecture 20 Disjointed Sets CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements

More information

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

22c:31 Algorithms. Kruskal s Algorithm for MST Union-Find for Disjoint Sets 22c:1 Algorithms Kruskal s Algorithm for MST Union-Find for Disjoint Sets 1 Kruskal s Algorithm for MST Work with edges, rather than nodes Two steps: Sort edges by increasing edge weight Select the first

More information

Optimization I : Brute force and Greedy strategy

Optimization I : Brute force and Greedy strategy Chapter 3 Optimization I : Brute force and Greedy strategy A generic definition of an optimization problem involves a set of constraints that defines a subset in some underlying space (like the Euclidean

More information

CMSC 341 Lecture 20 Disjointed Sets

CMSC 341 Lecture 20 Disjointed Sets CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements

More information

CSE 100: GRAPH ALGORITHMS

CSE 100: GRAPH ALGORITHMS CSE 100: GRAPH ALGORITHMS Dijkstra s Algorithm: Questions Initialize the graph: Give all vertices a dist of INFINITY, set all done flags to false Start at s; give s dist = 0 and set prev field to -1 Enqueue

More information

CMSC 341 Lecture 20 Disjointed Sets

CMSC 341 Lecture 20 Disjointed Sets CMSC 341 Lecture 20 Disjointed Sets Prof. John Park Based on slides from previous iterations of this course Introduction to Disjointed Sets Disjoint Sets A data structure that keeps track of a set of elements

More information

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

Union/Find Aka: Disjoint-set forest. Problem definition. Naïve attempts CS 445 CS 5 Union/Find Aka: Disjoint-set forest Alon Efrat Problem definition Given: A set of atoms S={1, n E.g. each represents a commercial name of a drugs. This set consists of different disjoint subsets.

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

Self Adjusting Data Structures

Self Adjusting Data Structures Self Adjusting Data Structures Pedro Ribeiro DCC/FCUP 2014/2015 Pedro Ribeiro (DCC/FCUP) Self Adjusting Data Structures 2014/2015 1 / 31 What are self adjusting data structures? Data structures that can

More information

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND CSE 373 MAY 0 TH SPANNING TREES AND UNION FIND COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend COURSE LOGISTICS HW4 due tonight, if you want feedback by the weekend HW5 out tomorrow

More information

Disjoint Sets 3/22/2010

Disjoint Sets 3/22/2010 Disjoint Sets A disjoint-set is a collection C={S 1, S 2,, S k } of distinct dynamic sets. Each set is identified by a member of the set, called representative. Disjoint set operations: MAKE-SET(x): create

More information

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

Building a network. Properties of the optimal solutions. Trees. A greedy approach. Lemma (1) Lemma (2) Lemma (3) Lemma (4) Chapter 5. Greedy algorithms Minimum spanning trees Building a network Properties of the optimal solutions Suppose you are asked to network a collection of computers by linking selected pairs of them.

More information

Chapter 5. Greedy algorithms

Chapter 5. Greedy algorithms Chapter 5. Greedy algorithms Minimum spanning trees Building a network Suppose you are asked to network a collection of computers by linking selected pairs of them. This translates into a graph problem

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7

Minimum Spanning Trees and Union Find. CSE 101: Design and Analysis of Algorithms Lecture 7 Minimum Spanning Trees and Union Find CSE 101: Design and Analysis of Algorithms Lecture 7 CSE 101: Design and analysis of algorithms Minimum spanning trees and union find Reading: Section 5.1 Quiz 1 is

More information

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

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University Cpt S 223 Course Overview 1 Course Goals Learn about new/advanced data structures Be able to make design choices on the suitable data structure for different application/problem needs Analyze (objectively)

More information

Reading. Disjoint Union / Find. Union. Disjoint Union - Find. Cute Application. Find. Reading. CSE 326 Data Structures Lecture 14

Reading. Disjoint Union / Find. Union. Disjoint Union - Find. Cute Application. Find. Reading. CSE 326 Data Structures Lecture 14 Reading Disjoint Union / Find Reading Chapter 8 CE Data tructures Lecture Disjoint Union/Find - Lecture Disjoint Union - Find Maintain a set of pairwise disjoint sets {,,, {,,8, {9, {, Each set has a unique

More information

Disjoint set (Union-Find)

Disjoint set (Union-Find) CS124 Lecture 6 Spring 2011 Disjoint set (Union-Find) For Kruskal s algorithm for the minimum spanning tree problem, we found that we needed a data structure for maintaining a collection of disjoint sets.

More information

Binary Heaps in Dynamic Arrays

Binary Heaps in Dynamic Arrays Yufei Tao ITEE University of Queensland We have already learned that the binary heap serves as an efficient implementation of a priority queue. Our previous discussion was based on pointers (for getting

More information

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

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29 Lecture 13 Connectedness in graphs Spanning trees in graphs Finding a minimal spanning tree Time costs of graph problems and NP-completeness Finding a minimal spanning tree: Prim s and Kruskal s algorithms

More information

EECS 477: Introduction to algorithms. Lecture 10

EECS 477: Introduction to algorithms. Lecture 10 EECS 477: Introduction to algorithms. Lecture 10 Prof. Igor Guskov guskov@eecs.umich.edu October 8, 2002 1 Lecture outline: data structures (chapter 5) Heaps, binomial heaps Disjoint subsets (union-find)

More information

lecture24: Disjoint Sets

lecture24: Disjoint Sets lecture24: Largely based on slides by Cinda Heeren CS 225 UIUC 22nd July, 2013 Announcements mp6 due tonight mp7 out soon! mt3 tomorrow night (7/23) Optional review instead of lab tomorrow Code Challenge

More information

Greedy Algorithms Part Three

Greedy Algorithms Part Three Greedy Algorithms Part Three Announcements Problem Set Four due right now. Due on Wednesday with a late day. Problem Set Five out, due Monday, August 5. Explore greedy algorithms, exchange arguments, greedy

More information

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

Disjoint Sets. Data Structures and Algorithms CSE 373 SP 18 - KASEY CHAMPION Disjoint Sets Data Structures and Algorithms CSE 373 SP 8 - KASEY CHAMPION Warm Up Finding a MST using Kruskal s algorithm b 8 c 7 d 4 2 9 a 7 i 6 4 4 e 0 8 h g 2 f CSE 373 SP 8 - KASEY CHAMPION 2 Warm

More information

CSci 231 Final Review

CSci 231 Final Review CSci 231 Final Review Here is a list of topics for the final. Generally you are responsible for anything discussed in class (except topics that appear italicized), and anything appearing on the homeworks.

More information

Disjoint Sets ADT 1. A disjoint sets data structure keeps track of multiple sets which do not share any elements. Here s the ADT: UnionFind ADT

Disjoint Sets ADT 1. A disjoint sets data structure keeps track of multiple sets which do not share any elements. Here s the ADT: UnionFind ADT Disjoint Sets ADT 1 A disjoint sets data structure keeps track of multiple sets which do not share any elements. Here s the ADT: UnionFind ADT find(x) union(x, y) Returns a number representing the set

More information

Algorithms. AVL Tree

Algorithms. AVL Tree Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other

More information

Disjoint Sets. Maintaining Disjoint Sets Complexity Analysis

Disjoint Sets. Maintaining Disjoint Sets Complexity Analysis Disjoint Sets Maintaining Disjoint Sets Complexity Analysis Disjoint Sets - Definition Set representatives called canonical elements. Elements are integers beween 1 and n. Each element can be accessed

More information

Exercises: Disjoint Sets/ Union-find [updated Feb. 3]

Exercises: Disjoint Sets/ Union-find [updated Feb. 3] Exercises: Disjoint Sets/ Union-find [updated Feb. 3] Questions 1) Suppose you have an implementation of union that is by-size and an implementation of find that does not use path compression. Give the

More information

Outline. Disjoint set data structure Applications Implementation

Outline. Disjoint set data structure Applications Implementation Disjoint Data Sets Outline Disjoint set data structure Applications Implementation Data Structures for Disjoint Sets A disjoint-set data structure is a collection of sets S = {S 1 S k }, such that S i

More information

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

UNION FIND. naïve linking link-by-size link-by-rank path compression link-by-rank with path compression context UNION FIND naïve linking link-by-size link-by-rank path compression link-by-rank with path compression context Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley http://www.cs.princeton.edu/~wayne/kleinberg-tardos

More information

Union-Find with Constant Time Deletions

Union-Find with Constant Time Deletions Union-Find with Constant Time Deletions Stephen Alstrup 1, Inge Li Gørtz 1, Theis Rauhe 1, Mikkel Thorup 2, and Uri Zwick 3 1 Department of Theoretical Computer Science, IT University of Copenhagen, Denmark.

More information

) $ f ( n) " %( g( n)

) $ f ( n)  %( g( n) CSE 0 Name Test Spring 008 Last Digits of Mav ID # Multiple Choice. Write your answer to the LEFT of each problem. points each. The time to compute the sum of the n elements of an integer array is: # A.

More information

Balanced Search Trees

Balanced Search Trees Balanced Search Trees Computer Science E-22 Harvard Extension School David G. Sullivan, Ph.D. Review: Balanced Trees A tree is balanced if, for each node, the node s subtrees have the same height or have

More information

CSE 100 Disjoint Set, Union Find

CSE 100 Disjoint Set, Union Find CSE 100 Disjoint Set, Union Find Union-find using up-trees The union-find data structure 1 0 2 6 7 4 3 5 8 Perform these operations: Find(4) =! Find(3) =! Union(1,0)=! Find(4)=! 2 Array representation

More information

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution Albert-Ludwigs-Universität, Inst. für Informatik Prof. Dr. Fabian Kuhn M. Ahmadi, O. Saukh, A. R. Molla November, 20 Algorithm Theory, Winter Term 20/6 Problem Set - Sample Solution Exercise : Amortized

More information

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm Spanning Trees 1 Spanning Trees the minimum spanning tree problem three greedy algorithms analysis of the algorithms 2 The Union-Find Data Structure managing an evolving set of connected components implementing

More information

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18 CS 124 Quiz 2 Review 3/25/18 1 Format You will have 83 minutes to complete the exam. The exam may have true/false questions, multiple choice, example/counterexample problems, run-this-algorithm problems,

More information

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

Cpt S 223 Fall Cpt S 223. School of EECS, WSU Course Review Cpt S 223 Fall 2012 1 Final Exam When: Monday (December 10) 8 10 AM Where: in class (Sloan 150) Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS700Z Algorithm Design and Analysis Lecture 7 Binary heap, binomial heap, and Fibonacci heap Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 / Outline Introduction

More information

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis CS711008Z Algorithm Design and Analysis Lecture 7. Binary heap, binomial heap, and Fibonacci heap Dongbo Bu Institute of Computing Technology Chinese Academy of Sciences, Beijing, China 1 / 108 Outline

More information

Union-Find and Amortization

Union-Find and Amortization Design and Analysis of Algorithms February 20, 2015 Massachusetts Institute of Technology 6.046J/18.410J Profs. Erik Demaine, Srini Devadas and Nancy Lynch Recitation 3 Union-Find and Amortization 1 Introduction

More information

Lecture Notes for Advanced Algorithms

Lecture Notes for Advanced Algorithms Lecture Notes for Advanced Algorithms Prof. Bernard Moret September 29, 2011 Notes prepared by Blanc, Eberle, and Jonnalagedda. 1 Average Case Analysis 1.1 Reminders on quicksort and tree sort We start

More information

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

Disjoint-set data structure: Union-Find. Lecture 20 Disjoint-set data structure: Union-Find Lecture 20 Disjoint-set data structure (Union-Find) Problem: Maintain a dynamic collection of pairwise-disjoint sets S = {S 1, S 2,, S r }. Each set S i has one

More information

ICS 691: Advanced Data Structures Spring Lecture 3

ICS 691: Advanced Data Structures Spring Lecture 3 ICS 691: Advanced Data Structures Spring 2016 Prof. Nodari Sitchinava Lecture 3 Scribe: Ben Karsin 1 Overview In the last lecture we started looking at self-adjusting data structures, specifically, move-to-front

More information

13 A: External Algorithms II; Disjoint Sets; Java API Support

13 A: External Algorithms II; Disjoint Sets; Java API Support 13 A: External Algorithms II; ; Java API Support Martin Henz April 15, 2009 Generated on Friday 13 17 A: th External April, 2009, Algorithms 12:37 II; ; Java API Support 1 1 External Sorting 2 3 4 13 A:

More information

CSE 521: Design and Analysis of Algorithms I

CSE 521: Design and Analysis of Algorithms I CSE 521: Design and Analysis of Algorithms I Greedy Algorithms Paul Beame 1 Greedy Algorithms Hard to define exactly but can give general properties Solution is built in small steps Decisions on how to

More information

Chapter 2: Basic Data Structures

Chapter 2: Basic Data Structures Chapter 2: Basic Data Structures Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority Queues and Heaps Dictionaries and Hash Tables Spring 2014 CS 315 2 Two

More information

CPS222 Lecture: Sets. 1. Projectable of random maze creation example 2. Handout of union/find code from program that does this

CPS222 Lecture: Sets. 1. Projectable of random maze creation example 2. Handout of union/find code from program that does this CPS222 Lecture: Sets Objectives: last revised April 16, 2015 1. To introduce representations for sets that can be used for various problems a. Array or list of members b. Map-based representation c. Bit

More information

CSC263 Week 11. Larry Zhang.

CSC263 Week 11. Larry Zhang. CSC263 Week 11 Larry Zhang http://goo.gl/forms/s9yie3597b Announcements A2 due next Tuesday Course evaluation: http://uoft.me/course-evals ADT: Disjoint Sets What does it store? What operations are supported?

More information

Course Review. Cpt S 223 Fall 2010

Course Review. Cpt S 223 Fall 2010 Course Review Cpt S 223 Fall 2010 1 Final Exam When: Thursday (12/16) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)

More information

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim: We have seen that the insert operation on a RB takes an amount of time proportional to the number of the levels of the tree (since the additional operations required to do any rebalancing require constant

More information

CS-301 Data Structure. Tariq Hanif

CS-301 Data Structure. Tariq Hanif 1. The tree data structure is a Linear data structure Non-linear data structure Graphical data structure Data structure like queue FINALTERM EXAMINATION Spring 2012 CS301- Data Structure 25-07-2012 2.

More information

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees Dynamic Access Binary Search Trees 1 * are self-adjusting binary search trees in which the shape of the tree is changed based upon the accesses performed upon the elements. When an element of a splay tree

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17 01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are

More information

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25 Multi-way Search Trees (Multi-way Search Trees) Data Structures and Programming Spring 2017 1 / 25 Multi-way Search Trees Each internal node of a multi-way search tree T: has at least two children contains

More information

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Concept Exam Code: 16 All questions are weighted equally. Assume worst case behavior and sufficiently large input sizes unless otherwise specified. Strong induction Consider this

More information

CSE 100 Advanced Data Structures

CSE 100 Advanced Data Structures CSE 100 Advanced Data Structures Overview of course requirements Outline of CSE 100 topics Review of trees Helpful hints for team programming Information about computer accounts Page 1 of 25 CSE 100 web

More information

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1 Binary Search Trees < > = Binary Search Trees 1 Ordered Dictionary (Map) ADT get (k): record with key k put (k,data): add record (k,data) remove (k): delete record with key k smallest(): record with smallest

More information

Data Structures and Algorithms Week 4

Data Structures and Algorithms Week 4 Data Structures and Algorithms Week. About sorting algorithms. Heapsort Complete binary trees Heap data structure. Quicksort a popular algorithm very fast on average Previous Week Divide and conquer Merge

More information

N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label

N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label CS 0 Theory of Algorithms Supplement Disjoint Set Structures Disjoint Set Structures N objects numbered 0, 1,, N 1, grouped into disjoint sets One member of a set chosen to be the label of the set Example:

More information

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps ) CSE 548: Analysis of Algorithms Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Fall 2012 Fibonacci Heaps ( Fredman & Tarjan,

More information

1 Minimum Cut Problem

1 Minimum Cut Problem CS 6 Lecture 6 Min Cut and Karger s Algorithm Scribes: Peng Hui How, Virginia Williams (05) Date: November 7, 07 Anthony Kim (06), Mary Wootters (07) Adapted from Virginia Williams lecture notes Minimum

More information

Notes on Binary Dumbbell Trees

Notes on Binary Dumbbell Trees Notes on Binary Dumbbell Trees Michiel Smid March 23, 2012 Abstract Dumbbell trees were introduced in [1]. A detailed description of non-binary dumbbell trees appears in Chapter 11 of [3]. These notes

More information

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014

CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up. Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms Lecture 28: Final review and class wrap-up Nicki Dell Spring 2014 Final Exam As also indicated on the web page: Next Tuesday, 2:30-4:20 in this room Cumulative but

More information

Algorithms (IX) Yijia Chen Shanghai Jiaotong University

Algorithms (IX) Yijia Chen Shanghai Jiaotong University Algorithms (IX) Yijia Chen Shanghai Jiaotong University Review of the Previous Lecture Shortest paths in the presence of negative edges Negative edges Dijkstra s algorithm works in part because the shortest

More information

Priority Queues. Meld(Q 1,Q 2 ) merge two sets

Priority Queues. Meld(Q 1,Q 2 ) merge two sets Priority Queues MakeQueue Insert(Q,k,p) Delete(Q,k) DeleteMin(Q) Meld(Q 1,Q 2 ) Empty(Q) Size(Q) FindMin(Q) create new empty queue insert key k with priority p delete key k (given a pointer) delete key

More information

CS 6783 (Applied Algorithms) Lecture 5

CS 6783 (Applied Algorithms) Lecture 5 CS 6783 (Applied Algorithms) Lecture 5 Antonina Kolokolova January 19, 2012 1 Minimum Spanning Trees An undirected graph G is a pair (V, E); V is a set (of vertices or nodes); E is a set of (undirected)

More information

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

CSE 373: Data Structures and Algorithms. Disjoint Sets. Autumn Shrirang (Shri) Mare 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

More information

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 06/ Fabian Kuhn Examples Dictionary: Operations: insert(key,value), delete(key), find(key) Implementations: Linked list: all operations take O(n) time (n:

More information

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1 CPS 100 Exam 2 Solutions Spring 199 Dr Rodger 1 (3 pts) A virtual function in C++ is bound dynamically or statically? dynamic 2 (3 pts) When does one use a class template in C++? Templates are used to

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017

Round 3: Trees. Tommi Junttila. Aalto University School of Science Department of Computer Science. CS-A1140 Data Structures and Algorithms Autumn 2017 Round 3: Trees Tommi Junttila Aalto University School of Science Department of Computer Science CS-A1140 Data Structures and Algorithms Autumn 2017 Tommi Junttila (Aalto University) Round 3 CS-A1140 /

More information

Data Structures and Algorithms Chapter 4

Data Structures and Algorithms Chapter 4 Data Structures and Algorithms Chapter. About sorting algorithms. Heapsort Complete binary trees Heap data structure. Quicksort a popular algorithm very fast on average Previous Chapter Divide and conquer

More information

Data Structure Lecture#14: Non-Binary Trees (Chapter 6) U Kang Seoul National University

Data Structure Lecture#14: Non-Binary Trees (Chapter 6) U Kang Seoul National University Data Structure Lecture#14: Non-Binary Trees (Chapter 6) U Kang Seoul National University U Kang 1 In This Lecture The concept of the general tree, its ADT, and its operation Motivation and the main idea

More information

CSL 730: Parallel Programming

CSL 730: Parallel Programming CSL 73: Parallel Programming General Algorithmic Techniques Balance binary tree Partitioning Divid and conquer Fractional cascading Recursive doubling Symmetry breaking Pipelining 2 PARALLEL ALGORITHM

More information

COMP251: Disjoint sets

COMP251: Disjoint sets COMP251: Disjoint sets Jérôme Waldispühl School of Computer Science McGill University Based on slides from M. Langer (McGill) Announces Assignment 1 is due on Wednesday February 1 st. Submit your soluoon

More information

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees Dynamic Access Binary Search Trees 1 * are self-adjusting binary search trees in which the shape of the tree is changed based upon the accesses performed upon the elements. When an element of a splay tree

More information

CSE 5095 Notes. Author : Michael Tedford. Class Date : 18th February 2014

CSE 5095 Notes. Author : Michael Tedford. Class Date : 18th February 2014 CSE 5095 Notes Author : Michael Tedford Class Date : 18th February 2014 Union-Find: Consider the n singleton sets {1}, {2},..., {n}. Consider a sequence of Union and Find operations performed on these

More information