Linked-List Implementation. Linked-lists for two sets. Multiple Operations. UNION Implementation. An Application of Disjoint-Set 1/9/2014

Similar documents
Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Week 3: Connected Subgraphs

priority queue ADT heaps 1

Basic Polyhedral theory

Junction Tree Algorithm 1. David Barber

Searching Linked Lists. Perfect Skip List. Building a Skip List. Skip List Analysis (1) Assume the list is sorted, but is stored in a linked list.

Strongly Connected Components

Final Exam Solutions

Homework #3. 1 x. dx. It therefore follows that a sum of the

Section 6.1. Question: 2. Let H be a subgroup of a group G. Then H operates on G by left multiplication. Describe the orbits for this operation.

COMP108 Algorithmic Foundations

The Equitable Dominating Graph

Random Process Part 1

1 Minimum Cut Problem

First derivative analysis

Abstract Interpretation: concrete and abstract semantics

Solutions to Homework 5

Hydrogen Atom and One Electron Ions

cycle that does not cross any edges (including its own), then it has at least

Section 11.6: Directional Derivatives and the Gradient Vector

Minimum Spanning Trees

Supplementary Materials

Aim To manage files and directories using Linux commands. 1. file Examines the type of the given file or directory

10. EXTENDING TRACTABILITY

Propositional Logic. Combinatorial Problem Solving (CPS) Albert Oliveras Enric Rodríguez-Carbonell. May 17, 2018

SECTION where P (cos θ, sin θ) and Q(cos θ, sin θ) are polynomials in cos θ and sin θ, provided Q is never equal to zero.

CPSC 665 : An Algorithmist s Toolkit Lecture 4 : 21 Jan Linear Programming

The second condition says that a node α of the tree has exactly n children if the arity of its label is n.

NEW APPLICATIONS OF THE ABEL-LIOUVILLE FORMULA

MA1506 Tutorial 2 Solutions. Question 1. (1a) 1 ) y x. e x. 1 exp (in general, Integrating factor is. ye dx. So ) (1b) e e. e c.

COHORT MBA. Exponential function. MATH review (part2) by Lucian Mitroiu. The LOG and EXP functions. Properties: e e. lim.

AP Calculus BC Problem Drill 16: Indeterminate Forms, L Hopital s Rule, & Improper Intergals

u x v x dx u x v x v x u x dx d u x v x u x v x dx u x v x dx Integration by Parts Formula

Where k is either given or determined from the data and c is an arbitrary constant.

Calculus II (MAC )

EXST Regression Techniques Page 1

UNTYPED LAMBDA CALCULUS (II)

EEO 401 Digital Signal Processing Prof. Mark Fowler

Combinatorial Networks Week 1, March 11-12

Chapter 6 Folding. Folding

Chapter Finding Small Vertex Covers. Extending the Limits of Tractability. Coping With NP-Completeness. Vertex Cover

That is, we start with a general matrix: And end with a simpler matrix:

On spanning trees and cycles of multicolored point sets with few intersections

A Propagating Wave Packet Group Velocity Dispersion

Evans, Lipson, Wallace, Greenwood

From Elimination to Belief Propagation

The Matrix Exponential

Codes on Planar Graphs

Steinberg s Conjecture is false

Roadmap. XML Indexing. DataGuide example. DataGuides. Strong DataGuides. Multiple DataGuides for same data. CPS Topics in Database Systems

Lecture 16: Bipolar Junction Transistors. Large Signal Models.

The Matrix Exponential

CS 361 Meeting 12 10/3/18

Search sequence databases 3 10/25/2016

y = 2xe x + x 2 e x at (0, 3). solution: Since y is implicitly related to x we have to use implicit differentiation: 3 6y = 0 y = 1 2 x ln(b) ln(b)

Higher order derivatives

COUNTING TAMELY RAMIFIED EXTENSIONS OF LOCAL FIELDS UP TO ISOMORPHISM

Brief Introduction to Statistical Mechanics

Thus, because if either [G : H] or [H : K] is infinite, then [G : K] is infinite, then [G : K] = [G : H][H : K] for all infinite cases.

Chapter 13 GMM for Linear Factor Models in Discount Factor form. GMM on the pricing errors gives a crosssectional

Block Crossings in Storyline Visualizations

Lecture 14 (Oct. 30, 2017)

BINOMIAL COEFFICIENTS INVOLVING INFINITE POWERS OF PRIMES

Cycles and Simple Cycles. Paths and Simple Paths. Trees. Problem: There is No Completely Standard Terminology!

Square of Hamilton cycle in a random graph

September 23, Honors Chem Atomic structure.notebook. Atomic Structure

Derangements and Applications

Engineering 323 Beautiful HW #13 Page 1 of 6 Brown Problem 5-12

SCHUR S THEOREM REU SUMMER 2005

Figure 1: Closed surface, surface with boundary, or not a surface?

Approximate Maximum Flow in Undirected Networks by Christiano, Kelner, Madry, Spielmann, Teng (STOC 2011)

Hardy-Littlewood Conjecture and Exceptional real Zero. JinHua Fei. ChangLing Company of Electronic Technology Baoji Shannxi P.R.

MIT Sloan School of Management

Examples and applications on SSSP and MST

Addition of angular momentum

11/13/17. directed graphs. CS 220: Discrete Structures and their Applications. relations and directed graphs; transitive closure zybooks

Function Spaces. a x 3. (Letting x = 1 =)) a(0) + b + c (1) = 0. Row reducing the matrix. b 1. e 4 3. e 9. >: (x = 1 =)) a(0) + b + c (1) = 0

Outline. Computer Science 331. Computation of Min-Cost Spanning Trees. Costs of Spanning Trees in Weighted Graphs

Limiting value of higher Mahler measure

Computing and Communications -- Network Coding

Image Filtering: Noise Removal, Sharpening, Deblurring. Yao Wang Polytechnic University, Brooklyn, NY11201

5. B To determine all the holes and asymptotes of the equation: y = bdc dced f gbd

Recall that by Theorems 10.3 and 10.4 together provide us the estimate o(n2 ), S(q) q 9, q=1

Construction of asymmetric orthogonal arrays of strength three via a replacement method

u r du = ur+1 r + 1 du = ln u + C u sin u du = cos u + C cos u du = sin u + C sec u tan u du = sec u + C e u du = e u + C

10. The Discrete-Time Fourier Transform (DTFT)

ENGR 323 BHW 15 Van Bonn 1/7

BINOMIAL COEFFICIENTS INVOLVING INFINITE POWERS OF PRIMES. 1. Statement of results

COMPUTER GENERATED HOLOGRAMS Optical Sciences 627 W.J. Dallas (Monday, April 04, 2005, 8:35 AM) PART I: CHAPTER TWO COMB MATH.

ECE602 Exam 1 April 5, You must show ALL of your work for full credit.

Collisions between electrons and ions

, each of which is a tree, and whose roots r 1. , respectively, are children of r. Data Structures & File Management

Problem Set 6 Solutions

Addition of angular momentum

Finding low cost TSP and 2-matching solutions using certain half integer subtour vertices

Introduction to Arithmetic Geometry Fall 2013 Lecture #20 11/14/2013

Coupled Pendulums. Two normal modes.

Chapter 10. The singular integral Introducing S(n) and J(n)

CLONES IN 3-CONNECTED FRAME MATROIDS

Utilizing exact and Monte Carlo methods to investigate properties of the Blume Capel Model applied to a nine site lattice.

Transcription:

Disjoint Sts Data Strutur (Chap. 21) A disjoint-st is a olltion ={S 1, S 2,, S k } o distint dynami sts. Eah st is idntiid by a mmbr o th st, alld rprsntativ. Disjoint st oprations: MAKE-SET(x): rat a nw st with only x. assum x is not alrady in som othr st. UNION(x,y): ombin th two sts ontaining x and y into on nw st. A nw rprsntativ is sltd. FIND-SET(x): rturn th rprsntativ o th st ontaining x. Linkd-List Implmntation Eah st as a linkd-list, with had and tail, and ah nod ontains valu, nxt nod pointr and bak-to-rprsntativ pointr. Exampl: MAKE-SET osts O(1): just rat a singl lmnt list. FIND-SET osts O(1): just rturn bak-torprsntativ pointr. Multipl Oprations Linkd-lists or two sts Suppos multipl oprations: n: #MAKE-SET oprations (xutd at bginning). m: #MAKE-SET, UNION, FIND-SET oprations. m n, #UNION opration is at most n-1. St {,h,} had tail St {, g} had tail h g UNION o two Sts had g h tail An Appliation o Disjoint-St Dtrmin th onntd omponnts o an undirtd graph. CONNECTED-COMPONENTS(G) 1. or ah vrtx v V[G] 2. do MAKE-SET(v) 3. or ah dg (u,v) E[G] 4. do i FIND-SET(u) FIND-SET(v) 5. thn UNION(u,v) SAME-COMPONENT(u,v) 1. i FIND-SET(u)=FIND-SET(v) 2. thn rturn TRUE 3. ls rturn FALSE UNION Implmntation A simpl implmntation: UNION(x,y) just appnds x to th nd o y, updats all bak-to-rprsntativ pointrs in x to th had o y. Eah UNION taks tim linar in th x s lngth. Suppos n MAKE-SET(x i ) oprations (O(1) ah) ollowd by n-1 UNION UNION(x 1, x 2 ), O(1), UNION(x 2, x 3 ), O(2),.. UNION(x n-1, x n ), O(n-1) Th UNIONs ost 1+2+ +n-1=θ(n 2 ) So 2n-1 oprations ost Θ(n 2 ), avrag Θ(n) ah. Not good!! How to solv it??? 1

Wightd-Union Huristi Instad appnding x to y, appnding th shortr list to th longr list. Assoiatd a lngth with ah list, whih indiats how many lmnts in th list. Rsult: a squn o m MAKE-SET, UNION, FIND-SET oprations, n o whih ar MAKE-SET oprations, th running tim is O(m+nlg n). Why??? Hints: Count th numbr o updats to bak-to-rprsntativ pointr or any x in a st o n lmnts. Considr that ah tim, th UNION will at last doubl th lngth o unitd st, it will tak at most lg n UNIONS to unit n lmnts. So ah x s bak-to-rprsntativ pointr an b updatd at most lg n tims. Union by Rank & Path Comprssion Union by Rank: Eah nod is assoiatd with a rank, whih is th uppr bound on th hight o th nod (i.., th hight o subtr rootd at th nod), thn whn UNION, lt th root with smallr rank point to th root with largr rank. Path Comprssion: usd in FIND-SET(x) opration, mak ah nod in th path rom x to th root dirtly point to th root. Thus rdu th tr hight. Disjoint-st Implmntation: Forsts Path Comprssion Rootd trs, ah tr is a st, root is th rprsntativ. Eah nod points to its parnt. Root points to itsl. d d h d St {,h,} St {,d} h d UNION Straightorward Solution Thr oprations MAKE-SET(x): rat a tr ontaining x. O(1) FIND-SET(x): ollow th hain o parnt pointrs until to th root. O(hight o x s tr) UNION(x,y): lt th root o on tr point to th root o th othr. O(1) It is possibl that n-1 UNIONs rsults in a tr o hight n-1. (just a linar hain o n nods). So n FIND-SET oprations will ost O(n 2 ). Algorithm or Disjoint-St Forst UNION(x,y) 1. LINK(FIND-SET(x),FIND-SET(y)) MAKE-SET(x) 1. p[x] x 2. rank[x] 0 LINK(x,y) 1. i rank[x]>rank[y] 2. thn p[y] x 3. ls p[x] y 4. i rank[x]=rank[y] 5. thn rank[y]++ FIND-SET(x) 1. i x p[x] 2. thn p[x] FIND-SET(p[x]) 3. rturn p[x] Worst as running tim or m MAKE-SET, UNION, FIND-SET oprations is: O(mα(n)) whr α(n) 4. So narly linar in m. 2

Analysis o Union by Rank with Path Comprssion (by amortizd analysis) Disuss th ollowing: A vry quikly growing untion and its vry slowly growing invrs Proprtis o Ranks Proving tim bound o O(mα(n)) whr α(n) is a vry slowly growing untion. How Quik A k (j) Inras Lt us s A k (1): or k=0,1,2,3,4. A 0 (1)=1+1=2 A 1 (1)=2.1+1=3 A 2 (1)=2 1+1 (1+1)-1=7 A 3 (1)=A 2 (1+1) (1)=A 2 (2) (1)=A 2 (A 2 (1))=A 2 (7)=2 7+1 (7+1)- 1=2 8.8-1=2047 A 4 (1)=A 32 (1)=A 3 (A 3 (1)) =A 3 (2047)=A 2 (2048) (2047) >> A 2 (2047) =2 2048.2048-1 >2 2048 =(2 4 ) 512 =(16) 512 >>10 80. (stimatd numbr o atoms in univrs) A vry quikly growing untion and its invrs For intgrs k 0 and j 1, din A k (j): A k (j)= j+1 i k=0 A k-1 (j+1) (j) i k 1 Whr A k-10 (j)=j, A k-1 (i) (j)= A k-1 (A k-1 (i-1) (j)) or i 1. k is alld th lvl o th untion and i in th abov is alld itrations. A k (j) stritly inras with both j and k. Lt us s how quik th inras is!! Invrs o A k (n):α(n) α(n)=min{k: A k (1) n} (so, A α(n) (1) n ) α(n)= 0 or 0 n 2 1 n =3 2 or 4 n 7 3 or 8 n 2047 4 or 2048 n A 4 (1). Extrmly slow inrasing untion. α(n) 4 or all pratial purposs. Quiknss o Funtion A k (j) s Inras Lmma 21.2 (Pag 510): For any intgr j, A 1 (j) =2j+1. Proo: By indution on i, prov A 0i (j) =j+i. So A 1 (j)= A 0 (j+1) (j) =j+(j+1)=2j+1. Lmma 21.3 (Pag 510): For any intgr j, A 2 (j) =2 j+1 (j+1)-1. Proo: By indution on i, prov A 1i (j) =2 i (j+1)-1 A 2 (j)= A 1 (j+1) (j) = 2 j+1 (j+1)-1. O(mα(n)) bound: Proprty o Ranks Lmma 21.4 (pag 511): For all nods x, rank[x] rank[p[x]], with strit inquality i x p[x]. Corollary 21.5 (pag 511): As w ollow th path rom any nod to th root, th nod ranks stritly inras. Lmma 21.6 (pag 512): Evry nod had rank at most n-1. Proo: rank bgins with 0, inras possibly with only LINK oprations, whih is at most n-1 tim. In at, at most log(n). 3

O(mα(n)) bound proo Using amortizd analysis (Chap. 17) Using LINK instad UNION (vry UNION is don by two FIND-SETs and on LINK) Lmma 21.7 (pag 512): Suppos onvrting a squn S' o m' MAKE-SET, UNION, and FIND-SET oprations into a squn S o m MAKE-SET, LINK, FIND-SET by turning UNION to two FIND-SETs and on LINK, thn i S runs in O(mα(n)), thn S' runs in O(m'α(n)). Proo: baus o m' m 3m', thus m=o(m'). Rlations among rank[p[x]], lvl(x) and itr(x) Sin rank[p[x]] monotonially inras ovr tim, in ordr or itr(x) to dras, lvl(x) must inras. Or say anothr way, as long as lvl(x) rmains unhangd, itr(x) must ithr inras or rmains unhangd. Potntial Funtion For ah nod x, assign a potntial untion φ q (x) atr q oprations. Thn potntial or ntir orst, Φ q = x φ q (x) Φ 0 =0 at th bginning. Φ q will nvr b ngativ. φ q (x)= α(n) rank[x] i x is a root or rank[x]=0. [α(n)-lvl(x)] rank[x]-itr(x) othrwis. Proprtis or Potntial Funtion φ q (x) Lmma 21.8 (pag 514): For vry nod x, and or all q, 0 φ q (x) α(n) rank[x] Proo: i x is a root or rank[x]=0, thn orrt by dinition. Suppos x is not a root and rank[x]>0, φ q (x)= [α(n)-lvl(x)] rank[x]-itr(x) (α(n)-(α(n)-1)) rank[x]-rank[x] =rank[x]-rank[x]=0. φ q (x)= [α(n)-lvl(x)] rank[x]-itr(x) [α(n)-0] rank[x]-1= α(n) rank[x]-1<α(n) rank[x] lvl(x) and itr(x) lvl(x)=max{k: rank[p[x]] A k (rank[x])} 0 Lvl(x)<α(n), sin rank[p[x]] rank[x]+1=a 0 (rank[x]) and A α(n) (rank[x]) A α(n) (1) n > rank[p[x]]. itr(x)=max{i: rank[p[x]] A lvl(x) (i) (rank[x])} 1 itr(x) rank[x], sin rank[p[x]] A lvl(x) (rank[x])=a lvl(x) (1) (rank[x]) and A lvl(x) (rank[x]+1) (rank[x])=a lvl(x)+1 (rank[x])>rank[p[x]]. Potntial Changs o Oprations Lmma 21.9 (pag 515): Lt x b a nod that is not a root, and suppos qth opration is ithr LINK or FIND-SET. Thn atr th qth opration, φ q (x) φ q-1 (x). Morovr, i rank[x] 1 and ithr lvl[x] or itr(x) hangs du to th qth opration, thn φ q (x) φ q-1 (x)-1. Proo: x not root rank[x] not hang n not hang α(n) not hang. I rank[x]=0, thn φ q (x) = φ q-1 (x)=0. suppos rank[x]>0. I lvl(x) not hang, I itr(x) not hang, φ q (x) = φ q-1 (x), sin all kp sam I itr(x) inras, thn at las by 1, φ q (x) will dras at last 1. I lvl(x) inrass (at last by 1), thn (α(n)-lvl(x)) rank[x] drops at last by rank[x]. Suppos itr(x) drops, thn, th drop is at most rank[x]-1. so φ q (x) will drop at last rank[x]-(rank[x]-1)=1. Thus φ q (x) φ q-1 (x)-1. 4

Amortizd Costs o Oprations Lmma 21.10 (pag 515): Th amortizd ost o ah MAKE-SET opration is O(1). Proo: rat a singl nod x with rank 0, so φ q (x) =0. no othr hang to th orst, so Φ q= Φ q-1. Th lt is th atual ost, whih is O(1). Proo o Lmma 21.12 (ont.) Proo that at las max(0,s-(α(n)+2)) nods on th ind path hav thir potntial dras by at last 1. Lt x b a nod on th ind path: rank[x]>0, ollowd somwhr by y that is not a root, and lvl(y)=lvl(x) just bor FIND-SET. At most α(n)+2 nods do not satisy: 1th nod, root nod, th last nod w or whih lvl(w)=0,1,, α(n)-1. Thus at last max(0,s-(α(n)+2)) nods satisy. Lt us ix x, show x s potntial drass by at last 1. Amortizd Costs o Oprations (ont.) Lmma 21.11 (pag 515): Th amortizd ost o ah LINK opration is O(α(n)). Proo: (LINK(x,y) maks y th parnt o x). Atual ost or LINK opration is O(1). Considring potntial hang: Thr kinds o nods: x, y, and th old hildrn o y. By Lmma 21.9, th potntial o y s old hildrn not inras. For x (hangd to non-root rom a root), φ q (x)= [α(n)-lvl(x)] rank[x]- itr(x) [α(n)-0] rank[x]-1= α(n) rank[x]-1<α(n) rank[x]=φ q-1 (x). For y, rank[y] may stay sam or inras by 1, so φ q (y)=α(n) rank[y]=φ q- 1(y) or φ q-1 (y)+α(n). Thus th potntial inras du to th LINK opration is at most α(n). Thus th amortizd ost is O(1)+O(α(n))=O(α(n)) Proo o Lmma 21.12 (ont.) Lt k=lvl(x)=lvl(y), Just prior to path omprssion ausd by FIND-SET, w hav rank[p[x]] A k (itr(x)) (rank[x]) (by itr(x) s d.) rank[p[y]] A k (rank[y]) (by lvl(y) s d.) rank[y] rank[p[x]] (sin y ollows x somwhr). Lt i=itr(x) bor path omprssion, w hav rank[p[y]] A k (rank[y]) A k (rank[p[x]]) (sin A k (j) is stritly inrasing) A k (A k (itr(x)) (rank[x])) =A k (i+1) (rank[x]) Atr path omprssion, rank[p[x]]=rank[p[y]], whih not dras, and rank[x] not hang, so rank[p[x]] A k (i+1) (rank[x]). Whih mans that ithr itr(x) inrass (to at last i+1), or lvl(x) to inras. Thus by Lmma 21.9, φ q (x) φ q-1 (x)-1. that is x s potntial drass by at last 1. As a rsult, w prov th lmma 21.12. Amortizd Costs o Oprations (ont.) Lmma 21.12 (pag 516): Th amortizd ost o ah FIND-SET opration is O(α(n)). Proo: suppos thr ar s nods in th ind path. Th atual ost o FIND-SET is O(s). Root s potntial dos not hang and no othr nod s potntial inrass (by Lmma 21.9). At last max(0,s-(α(n)+2)) nods on th ind path hav thir potntial dras by at last 1. Thus th amortizd ost is at most O(s)-(s-(α(n)+2)) =O(α(n)). Uppr bound or Disjoint-sts Thorm 21.13 (pag 517): A squn o m MAKE-SET, UNION, FIND- SET oprations, n o whih ar MAKE-SET oprations, an b prormd on a disjoint-st orst with union by rank and path omprssion in worst ast tim O(mα(n)). 5

Summary Disjoint st Thr oprations Dirnt implmntations and dirnt osts Forst implmntation: Union by rank and path omprssion Proprtis: rank, lvl, itr. Amortizd analysis o th oprations: Potntial untion. A k (j) union: A k (j)= j+1 i k=0 A (j+1) k-1 (j) i k 1 Whr A k-10 (j)=j, A (i) k-1 (j)= A k-1 (A (i-1) k-1 (j)) or i 1. k is alld th lvl o th untion and i in th abov is alld itrations. α(n)=min{k: A k (1) n} A typial xampl using Disjoint St Kruskal's algorithm (Minimum Spanning Tr) sort th dgs o G in inrasing ordr by lngth kp a subgraph S o G, initially mpty or ah dg in sortd ordr i th ndpoints o ar disonntd in S add to S rturn S Not: grdy algorithm Analysis: Th tsting whthr two ndpoints ar disonntd looks lik it should b slow (linar tim pr itration, or O(mn) total). in at, onstant tim. 6