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

Size: px
Start display at page:

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

Transcription

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

2 Today Amortized analysis 3 different methods 2 examples Union-Find data structures Worst-case complexity Amortized complexity 2

3 Amortized Analysis Amortized analysis. Average running time per operation over a worst-case sequence of operations. Time required to perform a sequence of data operations is averaged over all the operations performed. Motivation: traditional worst-case-per-operation analysis can give too pessimistic bound if the only way of having an expensive operation is to have a lot of cheap ones before it. Different from average case analysis: average over time, not input. 3

4 Amortized Analysis Methods. Aggregate method Accounting method Potential method 4

5 Aggregate method Aggregate. Determine total cost. Amortized cost = total cost/#operations. 5

6 Dynamic Tables Doubling strategy. Start with empty array of size 1. Insert: If array is full create a new array of double the size and reinsert all elements. Analysis: n insert operations. Assume n is a power of 2. Number of insertions log n = O(n). Total cost: O(n). Amortized cost per insert: O(1). 6

7 Accounting method Accounting. Some types of operations are overcharged. Credit allocated with elements in the data structure used to pay for subsequent operations Total credit non-negative at all times -> total amortized cost an upper bound on the actual cost. 7

8 Dynamic Tables Amortized costs: Amortized cost of insertion: 3 1 for own insertion 1 for its first reinsertion. 1 to pay for reinsertion of one of the items that have already been reinserted once. 8

9 Dynamic Tables Analysis: keep 2 credits on each element in the array that is beyond the middle. table not full: insert costs 1, and we have 2 credits to save. table full, i.e., doubling: half of the elements have 2 credits each. Use these to pay for reinsertion of all in the new array. Amortized cost per operation: x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 9

10 Example: Stack with MultiPop Stack with MultiPop. Push(e): push element e onto stack. MultiPop(k): pop top k elements from the stack Worst case: Implement via linked list or array. Push: O(1). MultiPop: O(k). Amortized cost per operation: 2. 10

11 Stack: Aggregate Analysis Amortized analysis. Sequence of n Push and MultiPop operations. Each object popped at most once for each time it is pushed. #pops on non-empty stack #Push operations n. Total time O(n). Amortized cost per operation: 2n/n = 2. 11

12 Stack: Accounting Method Amortized analysis. Sequence of n Push and MultiPop operations. Pay 2 credits for each Push. Keep 1 credit on each element on the stack. Amortized cost per operation: Push: 2 MultiPop: 1 (to pay for pop on empty stack). 12

13 Potential method Potential functions. Prepaid credit (potential) associated with the data structure (money in the bank). Can be used to pay for future operations. Ensure there is always enough money in the bank. Amortized cost of an operation: potential cost plus increase in potential due to the operation. Di: data structure after i operations Potential function Φ(Di) maps Di onto a real value. amortized cost = actual cost + Δ(Di) = actual cost + Φ(Di) - Φ(Di-1). 13

14 Potential Functions Amortized cost: amortized cost = actual cost + Δ(Di) = actual cost + Φ(Di) - Φ(Di-1). Stack. Φ(Di) = #elements on the stack. amortized cost of Push = 1 + Δ(Di) = 2. amortized cost of MultiPop(k): If k =min(k, S ) elements are popped. if S : amortized cost = k + Φ(Di) -Φ(Di-1) = k - k = 0. if S = : amortized cost = 1 + Δ(Di) = 1. 14

15 Potential Functions Amortized cost: amortized cost = actual cost + Δ(Di) = actual cost + Φ(Di) - Φ(Di-1). Dynamic tables { 2(k L/2) if k L/2 Φ(Di) = 0 otherwise L = current array size, k = number of elements in array. amortized cost of insertion: Array not full: amortized cost = = 3 Array full (doubling): Actual cost = L + 1, Φ(Di-1) = L, Φ(Di)=2: amortized cost = L (2 - L) = 3. 15

16 Amortized Cost vs Actual Cost Total cost: amortized cost = (actual cost + Δ(Di)) = actual cost + Φ(Dn) - Φ(D0). actual cost = amortized cost + Φ(D0) - Φ(Dn). If potential always nonnegative and Φ(D0) = 0 then actual cost amortized cost. 16

17 Potential Method Summary: 1. Pick a potential function, Φ, that will work (art). 2. Use potential function to bound the amortized cost of the operations you're interested in. 3. Bound Φ(D0) - Φ(Dfinal) Techniques to find potential functions: if the actual cost of an operation is high, then decrease in potential due to this operation must be large, to keep the amortized cost low. 17

18 Union-Find Data Structures 18

19 Union-Find Data Structure Union-Find data structure: Makeset(x): Create a singleton set containing x and return its identifier. Union(A,B): Combine the sets identified by A and B into a new set, destroying the old sets. Return the identifier of the new set. Find(x): Return the identifier of the set containing x. Only requirement for identifier: find(x) = find(y) iff x and y are in the same set. Applications: Connectivity, Kruskal s algorithm for MST,... 19

20 A Simple Union-Find Data Structure Quick-Union: Each set represented by a tree. Elements are represented by nodes. Root is also identifier. Make-Set(x): Create a new node x. Set p(x) = x. Find(x): Follow parent pointers to the root. Return the root. Union(A,B): Make root(b) a child of root(a). 20

21 A Simple Union-Find Data Structure Quick-Union: Union(A,B): Make root(b) a child of root(a). Union(7,5) Union(3,1) Union(7,8) Union(3,7)

22 A Simple Union-Find Data Structure Quick-Union: Each set represented by a tree. Elements are represented by nodes. Root is also identifier. Make-Set(x): Create a new node x. Set p(x) = x. Find(x): Follow parent pointers to the root. Return the root. Union(A,B): Make root(b) a child of root(a). Analysis: Make-Set(x) and Union(A,B): O(1) Find(x): O(h), where h is the height of the tree containing x. Worst-case O(n). 22

23 A Simple Union-Find Data Structure Quick Find: Each set represented by a tree of height at most one. Elements are represented by nodes. Root is also identifier. Make-Set(x): Create a new node x. Set p(x) = x and size(x) = 1. Find(x): Follow parent pointer to root. Return root. Union(A,B): Move all elements from smallest set to larger set (change parent pointers). I.e., set p(b) = A and size(a) = size(a) + size(b). 23

24 A Simple Union-Find Data Structure Quick Find: Union(A,B): Move all elements from smallest set to larger set (change parent pointers). Union(7,5) Union(3,1) Union(7,8) Union(3,7)

25 A Simple Union-Find Data Structure Quick Find: Each set represented by a tree of height at most one. Elements are represented by nodes. Root is also identifier. Make-Set(x): Create a new node x. Set p(x) = x and size(x) = 1. Find(x): Follow parent pointer to root. Return root. Union(A,B): Move all elements from smallest set to larger set (change parent pointers). I.e., set p(b) = A and size(a) = size(a) + size(b). Analysis: Make-Set(x) and Find(x): O(1) Union(A,B): O(n) 25

26 Amortized Complexity of Quick-Find Amortized analysis: Consider a sequence of k Unions. Observation 1: How many elements can be touched by the k Unions? Consider an element x: What can we say about the size of the set containing x before and after a union that changes x s parent pointer? How large can the set containing x be after the k Unions? How many times can x s parent pointer be changed? 26

27 Amortized Complexity of Quick-Find Amortized analysis: Each time x s parent pointer changes the size of the set containing it at least doubles. At most 2k elements can be touched by k unions. Size of set containing x after k unions at most 2k. x s parent pointer is updated at most lg(2k) times. In total O(k log k) parent pointers updated in a sequence of k unions. Amortized time per union: O(log k). Lemma. Using the Quick-Find data structure a Find operation takes worst case time O(1), a Make-Set operation time O(1), and a sequence of n Union operations takes time O(n log n). 27

28 A Better Union-Find Data Structure Union-by-Weight or Union-by-Rank. Union-by-Weight. Make the root of the smallest tree a child of the root of the bigger tree. Union-by-Rank. Each node x has an integer rank(x) associated. Make-Set(x): Create a new node x. Set p(x) = x and rank(x) = 0. Find(x): Follow parent pointers to the root. Return the root. Union(A,B): 3 cases: rank(a) > rank(b). Make B a child of A. rank(a) < rank(b). Make A a child of B. rank(a) = rank(b). Make B a child of A and set rank(a) = rank(a)+1. 28

29 Analysis of Union-by-Rank Increasing ranks. rank(x) < rank(p(x)). A root of set containing x: Find(x) takes O(rank(A)+1) time. rank(a) lg n: Show A 2 rank(a) by induction. A=Makeset(x): rank(a)=0 and A = 2 0 = 1. A=Union(B,C): 2 cases rank(a)=rank(b) or rank(a)=rank(c): ok, since set only got larger. rank(b)=rank(c)=k and rank(a)=k+1. A = B + C 2 k + 2 k = 2 k+1. Lemma. Using the Union-by-Rank data structure a Find operation takes worst case time O(log n), and a Make-Set or Union operation takes time O(1). 29

30 Path Compression Path compression. After each Find(x) operation, for all nodes y on the path from x to the root, set p(y) = x. Example. Find(6)

31 Path Compression Path compression. After each Find(x) operation, for all nodes y on the path from x to the root, set p(y) = x. Tarjan: Union-by-Rank and path compression. Starting from an empty data structure the total time of n Makes-Set operations, at most n Union operations, and m Find operations takes time O(n + m α(n)). α(n). Extremely slowly growing inverse Ackermann s function. Analysis complicated, but algorithm simple. 2 one-pass variants path halving and path splitting. Same asymptotic running time. 31

32 Ackermann s function Ackermann s function { j + 1 if k = 0, A k (j) = A (j+1) k 1 (j) if k 1. Inverse Ackermann: Grows extremely slowly. α(n) = min{k : A k (1) n}. 32

33 Path Compression: Analysis Potential function Potential of node x after i operations: Potential of forest after i operations: φ i (x). Φ i (x) = x φ i (x). Auxiliary functions: level(x) = max{k : rank(p(x)) A k (rank(x))}. iter(x) = max{j : rank(p(x)) A (j) level(x) (rank(x))} Properties: (1) 0 level(x) α(x). (2) 1 iter(x) rank(x). 33

34 Path Compression: Analysis Potential function φ i (x) = { α(n) rank(x) if x root or rank(x) = 0 (α(n) level(x)) rank(x) iter(x) if x not root and rank(x) 1. Potential of forest after i operations: Properties: (1) 0 level(x) α(x). Φ i (x) = x φ i (x). (2) 1 iter(x) rank(x). (3) 0 φ i (x) α(n) rank(x). (4) If x not root and rank(x) > 0, φ i (x) < α(n) rank(x). 34

35 Path Compression: Analysis Potential function φ i (x) = { α(n) rank(x) if x root or rank(x) = 0 (α(n) level(x)) rank(x) iter(x) if x not root and rank(x) 1. Potential of forest after i operations: Makeset(x): O(1) Union(A,B): O(α(n)) Find(x): O(α(n)) Φ i (x) = x φ i (x). Lemma (used in analysis of Union and Find). Suppose x not root, and ith operation Union or Find. Then φ i (x) φ i 1 (x). If also rank(x) > 0 and either level(x) or iter(x) changes, then φ i (x) φ i 1 (x) 1. 35

36 Summary Amortized analysis. 2 Examples: Dynamic tables, stack with multipop. Union-Find Data Structure. Union-by-Rank + path compression: worst case + amortized bounds. 36

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

CS 161: Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms CS 161: Design and Analysis of Algorithms Greedy Algorithms 2: Minimum Spanning Trees Definition The cut property Prim s Algorithm Kruskal s Algorithm Disjoint Sets Tree A tree is a connected graph with

More information

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

Disjoint Sets. (Disjoint Sets) Data Structures and Programming Spring / 50 Disjoint Sets (Disjoint Sets) Data Structures and Programming Spring 2017 1 / 50 Making a Good Maze What s a Good Maze? Connected Just one path between any two rooms Random The Maze Construction Problem

More information

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico CS 561, Lecture Topic: Amortized Analysis Jared Saia University of New Mexico Outline Amortized Analysis Aggregate, Taxation and Potential Methods Dynamic Table Union Find Data Structure 1 Amortized Analysis

More information

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico

CS 561, Lecture Topic: Amortized Analysis. Jared Saia University of New Mexico CS 561, Lecture Topic: Amortized Analysis Jared Saia University of New Mexico Outline Amortized Analysis Aggregate, Taxation and Potential Methods Dynamic Table Union Find Data Structure 1 Amortized Analysis

More information

Amortized Analysis. A Simple Analysis. An Amortized Analysis. Incrementing Binary Numbers. A Simple Analysis

Amortized Analysis. A Simple Analysis. An Amortized Analysis. Incrementing Binary Numbers. A Simple Analysis Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance of each operation in the worst case Three

More information

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

Amortized Analysis. The time required to perform a sequence of (data structure) operations is averaged over all the operations performed. Amortized Analysis The time required to perform a sequence of (data structure) operations is averaged over all the operations performed. No probabilistic assumptions - not an average case analysis! 1 Example:

More information

CS583 Lecture 12. Previously. Jana Kosecka. Amortized/Accounting Analysis Disjoint Sets. Dynamic Programming Greedy Algorithms

CS583 Lecture 12. Previously. Jana Kosecka. Amortized/Accounting Analysis Disjoint Sets. Dynamic Programming Greedy Algorithms CS583 Lecture 12 Jana Kosecka Amortized/Accounting Analysis Disjoint Sets Dynamic Programming Greedy Algorithms Slight digression Amortized analysis Disjoint sets Previously 1 Amortized Analysis Amortized

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

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

COP 4531 Complexity & Analysis of Data Structures & Algorithms COP 4531 Complexity & Analysis of Data Structures & Algorithms Amortized Analysis Thanks to the text authors who contributed to these slides What is amortized analysis? Analyze a sequence of operations

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

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

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

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Lecture slides by Kevin Wayne. Last updated on Apr 8, :13 AM

DATA STRUCTURES. amortized analysis binomial heaps Fibonacci heaps union-find. Lecture slides by Kevin Wayne. Last updated on Apr 8, :13 AM DATA STRUCTURES amortized analysis binomial heaps Fibonacci heaps union-find Lecture slides by Kevin Wayne http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on Apr 8, 2013 6:13 AM Data structures

More information

Graduate Algorithms CS F-12 Amortized Analysis

Graduate Algorithms CS F-12 Amortized Analysis Graduate Algorithms CS673-2016F-12 Amortized Analysis David Galles Department of Computer Science University of San Francisco 12-0: Amortized Analysis Standard Stack Push(S,elem) Pop(S) How much time for

More information

Self-Adjusting Heaps

Self-Adjusting Heaps Heaps-0 Self-Adjusting Heaps No explicit structure. Adjust the structure in a simple, uniform way, so that the efficiency of future operations is improved. Amortized Time Complexity Total time for operations

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

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

Dynamic Data Structures. John Ross Wallrabenstein

Dynamic Data Structures. John Ross Wallrabenstein Dynamic Data Structures John Ross Wallrabenstein Recursion Review In the previous lecture, I asked you to find a recursive definition for the following problem: Given: A base10 number X Assume X has N

More information

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

COS 528 Disjoint Sets and Compressed Trees. Robert E. Tarjan 2013 COS 528 Disjoint Sets and Compressed Trees Robert E. Tarjan 2013 Disjoint set union Devise a data structure for an intermixed sequence of the following kinds of operaions: make- set(x) (x in no set): create

More information

Introduction. Introduction: Multi-Pop Stack Example. Multi-Pop Stack Cost (clever) Multi-Pop Stack Cost (naïve)

Introduction. Introduction: Multi-Pop Stack Example. Multi-Pop Stack Cost (clever) Multi-Pop Stack Cost (naïve) Introduction Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.) Why and when we care: You have an algorithm that

More information

Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.

Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually. Introduction Today we begin studying how to calculate the total time of a sequence of operations as a whole. (As opposed to each operation individually.) Why and when we care: You have an algorithm that

More information

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch]

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch] Amortized Analysis Andreas Klappenecker [partially based on the slides of Prof. Welch] 1 People who analyze algorithms have double happiness. First of all they experience the sheer beauty of elegant mathematical

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

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

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

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

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

CSE 202: Design and Analysis of Algorithms Lecture 3

CSE 202: Design and Analysis of Algorithms Lecture 3 CSE 202: Design and Analysis of Algorithms Lecture 3 Instructor: Kamalika Chaudhuri Announcement Homework 1 out Due on Mon April 11 in class No late homeworks will be accepted Greedy Algorithms Direct

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

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

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

16 Greedy Algorithms

16 Greedy Algorithms 16 Greedy Algorithms Optimization algorithms typically go through a sequence of steps, with a set of choices at each For many optimization problems, using dynamic programming to determine the best choices

More information

Melding priority queues

Melding priority queues Melding priority queues Ran Mendelson 1, Robert E. Tarjan 2,3, Mikkel Thorup 4, and Uri Zwick 1 1 School of Computer Science, Tel Aviv University, Tel Aviv 69978, Israel 2 Department of Computer Science,

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

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

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

Graduate Analysis of Algorithms Prof. Karen Daniels

Graduate Analysis of Algorithms Prof. Karen Daniels UMass Lowell Computer Science 9.503 Graduate Analysis of Algorithms Prof. Karen Daniels Fall, 203 Lecture 3 Monday, 9/23/3 Amortized Analysis Stack (computational geometry application) Dynamic Table Binary

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

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

Amortized Analysis. Prof. Clarkson Fall Today s music: : "Money, Money, Money" by ABBA

Amortized Analysis. Prof. Clarkson Fall Today s music: : Money, Money, Money by ABBA Amortized Analysis Prof. Clarkson Fall 2015 Today s music: : "Money, Money, Money" by ABBA Review Current topic: Reasoning about performance Efficiency Big Oh Today: Alternative notions of efficiency Amortized

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

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

CS Prof. Clarkson Fall 2014

CS Prof. Clarkson Fall 2014 CS 3110 Lecture 25: Amortized Analysis Prof. Clarkson Fall 2014 Today s music: "Money, Money, Money" by ABBA "Mo Money Mo Problems" by The Notorious B.I.G. "Material Girl" by Madonna Review Current topic:

More information

AMORTIZED ANALYSIS OF ALGORITHMS FOR SET UNION. union(a, B): Combine the sets named A and B into a new set, named A. WITH BACKTRACKING*

AMORTIZED ANALYSIS OF ALGORITHMS FOR SET UNION. union(a, B): Combine the sets named A and B into a new set, named A. WITH BACKTRACKING* SIAM J. COMPUT. Vol. 18, No. 1, pp. 1-11, February 1989 1989 Society for Industrial and Applied Mathematics 001 AMORTIZED ANALYSIS OF ALGORITHMS FOR SET UNION WITH BACKTRACKING* JEFFERY WESTBROOK" AND

More information

Bob s Notes for COS 226 Fall : Introduction, Union-Find, and Percolation. Robert E. Tarjan 9/15/13. Introduction

Bob s Notes for COS 226 Fall : Introduction, Union-Find, and Percolation. Robert E. Tarjan 9/15/13. Introduction Bob s Notes for COS 226 Fall 2013 1: Introduction, Union-Find, and Percolation Robert E. Tarjan 9/15/13 Introduction Welcome to COS 226! This is the first of a series of occasional notes that I plan to

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

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

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

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

Introduction to Algorithms

Introduction to Algorithms Introduction to Algorithms 6.046J/8.40J/SMA5503 Lecture 4 Prof. Charles E. Leiserson How large should a hash table be? Goal: Make the table as small as possible, but large enough so that it won t overflow

More information

Greedy Approach: Intro

Greedy Approach: Intro Greedy Approach: Intro Applies to optimization problems only Problem solving consists of a series of actions/steps Each action must be 1. Feasible 2. Locally optimal 3. Irrevocable Motivation: If always

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

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

CS 310 Advanced Data Structures and Algorithms

CS 310 Advanced Data Structures and Algorithms CS 0 Advanced Data Structures and Algorithms Weighted Graphs July 0, 07 Tong Wang UMass Boston CS 0 July 0, 07 / Weighted Graphs Each edge has a weight (cost) Edge-weighted graphs Mostly we consider only

More information

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( )

Let the dynamic table support the operations TABLE-INSERT and TABLE-DELETE It is convenient to use the load factor ( ) 17.4 Dynamic tables Let us now study the problem of dynamically expanding and contracting a table We show that the amortized cost of insertion/ deletion is only (1) Though the actual cost of an operation

More information

COMP251: Amortized Analysis

COMP251: Amortized Analysis COMP251: Amortized Analysis Jérôme Waldispühl School of Computer Science McGill University Based on (Cormen et al., 2009) Overview Analyze a sequence of operations on a data structure. We will talk about

More information

CISC 621 Algorithms, Midterm exam March 24, 2016

CISC 621 Algorithms, Midterm exam March 24, 2016 CISC 621 Algorithms, Midterm exam March 24, 2016 Name: Multiple choice and short answer questions count 4 points each. 1. The algorithm we studied for median that achieves worst case linear time performance

More information

Lecture XII DISJOINT SETS

Lecture XII DISJOINT SETS 1. Union Find Problem Lecture XII Page 1 Lecture XII DISJOINT SETS Let S be a set of items, and let be an equivalence relation on S. Theunion-find problem concerns the maintainence of this equivalence

More information

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

CS F-12 Amortized Analysis 1

CS F-12 Amortized Analysis 1 CS673-2016F-12 Amortized Analysis 1 12-0: Amortized Analysis Standard Stack Push(S,elem) Pop(S) How much time for each operation? 12-1: Amortized Analysis Standard Stack Push(S,elem)O(1) Pop(S)O(1) Multipop(S,k)

More information

CS170{Spring, 1999 Notes on Union-Find March 16, This data structure maintains a collection of disjoint sets and supports the following three

CS170{Spring, 1999 Notes on Union-Find March 16, This data structure maintains a collection of disjoint sets and supports the following three CS170{Spring, 1999 Notes on Union-Find March 16, 1999 The Basic Data Structure This data structure maintains a collection of disjoint sets and supports the following three operations: MAKESET(x) - create

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

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

Lecture XIII DISJOINT SETS

Lecture XIII DISJOINT SETS 1. Union Find Problem Lecture XIII Page 1 textit There are many 100 things to do before you die lists on the web. Implementing Union-Find should be one of those things in a computer scientist s list. Lecture

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

2 A Template for Minimum Spanning Tree Algorithms

2 A Template for Minimum Spanning Tree Algorithms CS, Lecture 5 Minimum Spanning Trees Scribe: Logan Short (05), William Chen (0), Mary Wootters (0) Date: May, 0 Introduction Today we will continue our discussion of greedy algorithms, specifically in

More information

CSE 548: (Design and) Analysis of Algorithms

CSE 548: (Design and) Analysis of Algorithms Intro Aggregate Charging Potential Table resizing Disjoint sets 1 / 33 CSE 548: (Design and) Analysis of Algorithms Amortized Analysis R. Sekar Intro Aggregate Charging Potential Table resizing Disjoint

More information

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

6,8:15. MA/CSSE 473 Day 37. Student Questions. Kruskal data structures. Disjoint Set ADT. Complexity intro 6,8:15 MA/CSSE 473 Day 37 Student Questions Kruskal data structures Disjoint Set ADT Complexity intro Data Structures for Kruskal A sorted list of edges (edge list, not adjacency list) Edge e has fields

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

/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

Exam 1 Solutions. Jonathan Turner 10/4/01 9/27/01. CS 541 Algorithms and Programs. Be neat and concise, but complete.

Exam 1 Solutions. Jonathan Turner 10/4/01 9/27/01. CS 541 Algorithms and Programs. Be neat and concise, but complete. CS 541 Algorithms and rograms Exam 1 Solutions Jonathan Turner 10/4/01 9/27/01 Be neat and concise, but complete. 1. (15 points) State the general greedy method for finding minimum spanning trees. Show

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

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

Lecture 13: Minimum Spanning Trees Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.sunysb.edu/ skiena Problem of the Day Your job is to

More information

Maintaining Disjoint Sets

Maintaining Disjoint Sets Maintaining Disjoint Sets Ref: Chapter 21 of text. (Proofs in Section 21.4 omitted.) Given: A collection S 1, S 2,..., S k of pairwise disjoint sets. Each set has a name, its representative, which is an

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

CPS 231 Exam 2 SOLUTIONS

CPS 231 Exam 2 SOLUTIONS CPS 231 Exam 2 SOLUTIONS Fall 2003 1:00-2:25, Tuesday November 20th Closed book exam NAME: Problem Max Obtained 1 10 2 25 3 (a) 15 3 (b) 15 3 (c) 10 4 (a) 10 4 (b) 15 4 (c) 10 Total 110 1 [10 points ]

More information

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

Dijkstra s algorithm for shortest paths when no edges have negative weight. Lecture 14 Graph Algorithms II 14.1 Overview In this lecture we begin with one more algorithm for the shortest path problem, Dijkstra s algorithm. We then will see how the basic approach of this algorithm

More information

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term.

Outline for Today. Why could we construct them in time O(n)? Analyzing data structures over the long term. Amortized Analysis Outline for Today Euler Tour Trees A quick bug fix from last time. Cartesian Trees Revisited Why could we construct them in time O(n)? Amortized Analysis Analyzing data structures over

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

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

Lecture Summary CSC 263H. August 5, 2016

Lecture Summary CSC 263H. August 5, 2016 Lecture Summary CSC 263H August 5, 2016 This document is a very brief overview of what we did in each lecture, it is by no means a replacement for attending lecture or doing the readings. 1. Week 1 2.

More information

Lecture 13: Minimum Spanning Trees Steven Skiena

Lecture 13: Minimum Spanning Trees Steven Skiena Lecture 13: Minimum Spanning Trees Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794 4400 http://www.cs.stonybrook.edu/ skiena Problem of the Day Your job

More information

DDS Dynamic Search Trees

DDS Dynamic Search Trees DDS Dynamic Search Trees 1 Data structures l A data structure models some abstract object. It implements a number of operations on this object, which usually can be classified into l creation and deletion

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

Rank-Pairing Heaps. Bernard Haeupler Siddhartha Sen Robert E. Tarjan. SIAM JOURNAL ON COMPUTING Vol. 40, No. 6 (2011), pp.

Rank-Pairing Heaps. Bernard Haeupler Siddhartha Sen Robert E. Tarjan. SIAM JOURNAL ON COMPUTING Vol. 40, No. 6 (2011), pp. Rank-Pairing Heaps Bernard Haeupler Siddhartha Sen Robert E. Tarjan Presentation by Alexander Pokluda Cheriton School of Computer Science, University of Waterloo, Canada SIAM JOURNAL ON COMPUTING Vol.

More information

Union Find. Data Structures and Algorithms Andrei Bulatov

Union Find. Data Structures and Algorithms Andrei Bulatov Union Find Data Structures and Algorithms Andrei Bulatov Algorithms Union Find 6-2 Union Find In a nutshell, Kruskal s algorithm starts with a completely disjoint graph, and adds edges creating a graph

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

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

Lists and Arrays. Algorithms and Data Structures. (c) Marcin Sydow. Introduction. Linked Lists. Abstract Data Structure. Amortised Analysis

Lists and Arrays. Algorithms and Data Structures. (c) Marcin Sydow. Introduction. Linked Lists. Abstract Data Structure. Amortised Analysis and s and s Lists and Topics covered by this lecture: and s Singly Doubly The Concept of Stack Queue The Concept of of Complexity Potential function method total cost and accounting methods Examples on

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

10 Data Structures for Disjoint Sets

10 Data Structures for Disjoint Sets E pluribus unum (Out of many, one) Official motto of the United States of America John: Who s your daddy? C mon, you know who your daddy is! Who s your daddy? D Argo, tell him who his daddy is!" D Argo:

More information

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions: Direct Addressing - key is index into array => O(1) lookup Hash table: -hash function maps key to index in table -if universe of keys > # table entries then hash functions collision are guaranteed => need

More information

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

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic Algorithms Union Find 16- Union Find Union Find Data Structures and Algorithms Andrei Bulatov In a nutshell, Kruskal s algorithm starts with a completely disjoint graph, and adds edges creating a graph

More information