You are on page 1of 19
Week Il. Lecture Alotes Topics: Disjomt set dete structure Union-Find Augmented disjoint set deta structure Network flow Disjoint-set dele structuse (Union- Find ) Frovlem: Maintain o Ay namnie. collection of pairwise - disjpint sets Se fs, Sarees 8,4 . Each set Si has one element disting ui shed as the representative element, rep [51 Must support 3 operations: * Maxe- Set(a) + adds new set fa} to S with repliasl = # [jor ony ae Si fos all i) * Unton lay): replaces sets Sar Sy vith Sq USy in S Soy amy ary in distinct sets Sa+ Sy, . Finn - 567 (x): returns representative replonl of set Sa containing element a. Simple linked-list solution Store each set Sie {Xue wer Me} as an (unordered ) doubly inked list. Define vepresertative element rep [5] 4o be the front of the list, %- 6: (ee Te HE reploi] *MAKE-SET La) inikalizes nos @ lone node — O11) * CWd- SET (a) walks left in the list containing a until it teaches the front of the ist — O(n) * UNION (my) concatenates the \ists containing % andy, leaving ep. oS €wo- setial - Oly) Simple boalanced-tree Solution Store each set S: fa may x5 a5 @ loalonced tree Lignoring Keys). define vepresentative element repli] to be the root of the tree. *MAKE-SET [®) imitializes gce [rams ts. t00 4 f a5 @ lone node — Oli) + FINd- SET (a) walks up the tree Coutaining % unl it teaches the root — 0 Log ») © Union (4) concatenstes the avees containing mond 4 changing rep. — B Lag n) Plan of attack We wil) build oa simple disjoint union dete structure thet, in aw amortized sense, performs 3 than 6 (gm) per operation, even let} Ug dg dg n), ete, but not quite eu). nificantly beHey vr than “Bll Ig») To reach this goal, we will introduce tuo Key tricks Each trick converts a trivial Oln) solution into a Simple Al tg n) amortized solution. Together, the two tricks yield a wuch better solution. First trick arises in an augmented linked Mi Second trick orises in o tree Structure. Augsnented linked - list solution Store set Sir fama. %X} ao unordered doulaly linked Vist. Define veplSi] to be Front of the list, a. Each element a; alse stores pointer rteplaj] 40 sepldi] [-——_ s[ [ol feeb |u| OL [oe repli] * Fuip- Set (m) vetums repla} — 0) * UNION (My) concatenates the lists containing 1 and y ond updates the rep pointers for all elements in the Wet containing ys = Bly Exowple of augmented linkeal- ist solution Each element 4, Stoves pointer rep (451 40 replSJ. UNION (4,4) * Concatenates the lists containing xond y, avd s updotes the vep pointers for all elements inthe list containing y- Sn US, % rep SI PI T=E [x] TL, | ts reply] Alternative Concatenation UNION (%y) could instead * concatenate the lists containing y and x, and + updote the Tep pointers jor all elements in the het containing mn rep eT se | [af foth sy (bei Reta Yepl sy) ts SaUSy: ep «ft. PRD Tepl dy c——-. ala tee TeployS Tepl SauSy] Trek 1: Smaller indo larger ATER T+ Smaller into larger To save werk, concatenate smaller list into the end of the lerger list. Cost ¢ Bengt of smaller list) Aug ment list 4o store its weight ( elements) let'n" denote the overall number of elements CequivalenHy, the number of Mane. SeT operations), Let "wm’ denote the total number of operations Let “f" denote the vumber of Fuvb- S&T operations. Tweorem: Cost of al) UNION's is Olnlgn). Corollary : Total cost is Olm+nign), Analysis of Trine 4 To save work, concatenate smaller \ist inte the end of the larger Not. Coste Olr4 length of smalley list) ‘Theorem: Total cost of UNIONS is Olwtgn) Proofs Monitor an element x ond set Sx containing it- After initial Make- SETLA), weight [Se]e1. Each “tim Hme Sn is united with Sy, weight [6yJ > weight [da], pay t to update tepla], and weight [4a] at least doubles Grereasing by welahtTsy1), Each Hme Sy vio united with smaller set Sar pay nothing, and weight [$4] only increases. Thus pay flgn fovar Re pre senting sets as trees Store each set Si {r,a2--.%K} ad an Umorderedl, potentially unbalanced, not necessarily binary tree, Storing only parent pointers. rep(SiJ is the tree root. See Los ty tn, 06/0] *MAKG- SETI») imitializes a asa love node — OU) + Find- SET G9) walks Up the vera) tree containing « unt) it a“ tal teaches the root - 0 (deptrtal) * UNION lay) concatenates the Gl tl le trees containing % and y Trick 1 adapted 40 trees Umon (my) con use o Simple concatenation strategy : Make wot FID -SET(Y) achild of root Fivd- Seta). > Fwoo- setly)= Fwn- set (a) We can adapt Trick +o [a] this content also: Merae tree with smaller foc By weiaht into tree with fal & larger weight: Height of tree increases only when it's size doubles, So height is logavittmie in weight. Thus total cost to Om fg) When we enecute a Fwd- SET operation and welk up 9 peth p to the tvot, we Know the representatives for oll nodes on poth p- Poth compre Ssion makes all of those nodes direct children of the root. Cost of Finn- set lo is HN O( dept tT) cost of Fivp- SETL*) is sk Oldept tT) \xs\ j a) [aed Tl (| Fp 5€7 LY) Cost of Ewp- se Tl”) is shy Dldeptafal) Finn- 5€TL4,) Analysis of Trick 2 alone. Theorem: Total cost of Finp-ser is olwlg n) Proof: Amortization ly potential Puncton . The weight of @ node x is # nodes in its subtvee, Define +(ou,--- Mee Di lg weight ta U er etn NION (%isj) ineveases potentia) of root Fino- Seta), by at most dg weight L root Fwp- setlaj) | # \gn. Each step down pre made ley Finp-se7l%), except the First, moves c's Sulstyee out of po subtree, Thus if weighticl > weight£p], d decreases by 71. paying for he step down. There can be at most Ign steps pc for which weight [cl <¥, weight Cp], Theovem: If all Union eperations occur before all Fwh-Ser operations, then total cost is Olmn) Prook: TP o@ FWo- SET operation troverses o poth with kK nodes, costin OL®) time, then k-2 nodes ave wmede new childven of the qwot. This chan ge con happen only once for each of the w elements. So, the total cost of Gnd. SET Is Ol 44m). Ackermann’s funtion A jar, if Keo Define ALi) = Ge Reet), ih ue) — iterate jai Hmes Aolj)* j+) Ald 2 Ay) ~ 24 : A+ > A,G)W 25272) Ag = F 2 o Asli: 2049 ie ° aon Aah) > 2 2 204s e Auli) ib o@ let bigger: AW? 2 Define alnl= min See Aye vn} £4 Sov prackeal n Analy sis of Tricks b+ 2 Theorem: In qeveral, Jotal cost 15 olm al), — Application: Dynowmie Connectivity Suppose o graph ie given to us incrementally by Abp - VERTEX (v) Avp- engeluv) avd we want te support comectivity queries: CONNECTED (uw): 7 Are u and N inthe same connected component: fox enomple, we want to maintain a spanning forest, so we check whether each new edge conn) ecks O previously disconnected pair of vertices. Sets of vertices represent counected components. Suppose a graph is given to us incrementally by Abp- UERTEX (v) - Maxe- SE T(¥) bp. BOGE Luv) = IF wot CONNECTED law) then UNION (ww) and CONNECTEDLas Fis SETI Find $6719) Flow networks Definition: A ylow net work is o divected qraph G+ Wve) with two distinguished vertices: a Source $ and a sink t. Coch edge (uv) EE has avon: wegative capacity cluv). TP (uv) ¢ &, then clu) =o. Emam ple: Definition: A positive Plow on G& isa Punckon pivavek 6ak shy me the Fotlousing . - Capecity constraint: For all uveV Of pluv) 4 cluv) - Flow conservation; For all uéV- tetd luv) - 5 plw) = 0 fa? vev [ VEN The value of @ flow is the net flow out of the Source: S. ploy) - S plus) ven ve A Slows on a network Copecity positive ee] Flow conservation (like KirchoPPé current law): * Flow into u is 242% * Flow oul of u is ona 3 The value of this Plow is 1-042 3. Manimum-#love problem: Given o flow network 6, Pind 2 Plow of manimum value in 6. In the eloove Figure, velue of menimum flow = 4. Flow Cancellation Without loss of emerality , positive How goes either from u to V, er From v tou, lout not both. / Net pow From The capacity > ig UO in both constraint and 2:5( | he —T cases ist low conservation ore preserved by this trans formation INTUITION: View Flow as a sate, not o quant A notational simpli Fieation IDEA: Work with the net flow between two verkces, wether than with the positive jow. Defmition: A net) How on G is a Punction ji VXV3R satisfying the Following : - Capacity constraint: For all uve V, sluy) ¢ cluy) ~ Flow conpervation : For al) ué V- §5,t3 9 Lone assumption z flu) = 0 ee of two ~ Skew - symmetry : for al) uvev Flu) © - Fly) Equivalence of definitions Theorem: The two definitions are equivalent Pro? Let 4lus) = pluv)- plu) Capacity constraint: Sime plu) £ clu) and plyu) %d, we have plu) ¢ elu). Flow conservation: SF slows S Uptuy) - plww)) vev vev + S pluw- ZL pluw) veV véev Skew Symmetry : qu) © plu) - pty) == [pluw)- plunr) e = Flu) Next, consider flaw), if jluv)> 0 plu) = ° Fi flav) 20 Copacity Constraint: by dePinition plund70. Since pla) £ clin), iE Follows thet pluiv) £ cluy) Flow Conservation: IP fluw)>0, then plu) plva) Hue), TP flav) $0, then pluv)- pluwds —flvu)> slay) Loy skew symmetry) There Pore, S_pluw)- S_ ply) = = pw) veu vey vev. Le. Notation Definition: The value ofa Plow 4) denoted by Vy) is iven lo aye SHAW) = Gls) vev Tmplicit Summation notation: A set used in an arithmetic formula tTepresents Sum over the elements of the set, Example: Slow conservation; HUN): 0 jor a weve {se4. Simple properties of flow Lemma: sl%4x) © 0 31 © -4lLr) slevy, 2)+ 4062) F012) SFXOY: Theorem lale SLE) Feoogs 1g) = 415-V) «FW slv-5,V)— Ovatt braces + gles) e jv. t+ SIV, V-5-#) > Ale) Flow into the sink Ile glow) * 4 Syt)e 4. Cuts A cut (5.7) Ba Plow network G+ (v,€) ib © partition of No such thet $65 and + ET TP 4 is @ Pow on G, then the How across the cut is 3.7) $LSt) = (242) 4 [-2 41-742) = 4 Lemma: Foy any How £ and ony cut (,T) 131 4U5,7). Prot: FT) = Fld.) — 4155) = 4L5,v) Hi glen)-415-2.™) ® plsv) > \4) Capacity of a cut Capacity eprceut (oe) ie ce oul) c(s,7) = (942) +(142+3) ze. Upper loound on the monimum flow value Theorem: The value of any flow is bounded above by the capacity of ony cut. Proof: eed be gl50) © FS flu) ues veT 25.9.clu) ves VET 2 ¢ tL Re sidual_nletwork definition: Let 4 be @ flow on G = le) The tesidual network Gy, &) io the graph with strictly positive tesiaual es tund Betas flu) >0. Edges in Ey admit more jlow. Emample: on 4 “@ p «db Rs 2 L emma: \6,) ¢ 21e) Augmenting Patina Definition: Any pote Pro th in G ed along an augment ng Po wn 6 tok in Gy is Om augmenting pa with teopect £. The jlow value tom be imeread th p &y Cylp) © min fey tut (wre P Eas G: us ath or as a tp 2 ns oS Sy: 2 2 ’ 2 Man-jow, min cut theorem Theorem: The Following are equivelent : Lg is @ manimum flow 2. J admits no augmenting 3. [We e Let) for some cut ls.7) paths

You might also like