CS473-Algorthm I Lecture b Dyamc Table CS 473 Lecture X
Why Dyamc Table? I ome applcato: We do't kow how may object wll be tored a table. We may allocate pace for a table But, later we may fd out that t ot eough. The, the table mut be reallocated wth a larger ze. All the object tored the orgal table Mut be coped over to the ew table. CS 473 Lecture X
Why Dyamc Table? Smlarly, f may object are deleted from the table: t may be worthwhle to reallocate the table wth a maller ze. Th problem called Dyamcally Expadg ad Cotractg a table. CS 473 Lecture X 3
Why Dyamc Table? Ug amortzed aaly we wll how that, The amortzed cot of erto ad deleto O(). Eve though the actual cot of a operato large whe t trgger a expao or a cotracto. We wll alo how how to guaratee that The uued pace a dyamc table ever exceed a cotat fracto of the total pace. CS 473 Lecture X 4
Operato TABLE-INSERT: Iert to the table a tem that occupe a gle lot. TABLE-DELETE: Remove a tem from the table & free t lot. CS 473 Lecture X 5
Load Factor Load Factor of a Dyamc Table T ( T) Number of tem tored the table ze( umber of lot ) of the table For a empty table by defto 0 ( T) 0 CS 473 Lecture X 6
Ierto-Oly Dyamc Table Table-Expao: Aumpto: Table allocated a a array of lot A table fll up whe all lot have bee ued equvaletly, whe t load factor become Table-Expao occur whe A tem to be erted to a full table CS 473 Lecture X 7
Ierto-Oly Dyamc Table A Commo Heurtc Allocate a ew table that ha twce a may lot a the old oe. Hece, erto are performed f oly / α(t) CS 473 Lecture X 8
Table Iert TABLE-INSERT (T, x) f ze[t] = 0 the allocate table[t] wth lot ze[t] f um[t] = ze[t] the allocate ew-table wth.ze[t] lot copy all tem table[t] to ew-table free table[t] table[t] ew-table[t] ze[t].ze[t] ert x to table[t] um[t] um[t] + ed table[t] : poter to block of table torage um[t] : umber of tem the table ze[t] : total umber of lot the table Itally, table empty, o um[t] = ze[t] = 0 CS 473 Lecture X 9
Table Expao Rug tme of TABLE-INSERT proportoal to the umber of elemetary ert operato. Ag a cot of to each elemetary erto Aalyze a equece of TABLE-INSERT operato o a tally empty table CS 473 Lecture X 0
Cot of Table Expao What cot c of the -th operato? If there room the curret table (or th the frt operato) c = (oly oe elemetary ert operato) If the curret table full, a expao occur, the the cot c =. for the elemetary erto of the ew tem - for the tem that mut be coped from the old table to the ew table. CS 473 Lecture X
Cot of Table Expao If operato are performed, The wort cae cot of a operato O() Therefore the total rug tme O( ) Th boud ot tght, becaue Expao doe ot occur o ofte the coure of operato. CS 473 Lecture X
Amortzed Aaly of Iert Table tally empty. Oberve: The Aggregate Method -th operato caue a expao oly whe - a power of. c f a exact power of otherwe CS 473 Lecture X 3
The Aggregate Method Therefore the total cot of TABLE-INSERT operato lg lg j j c 3 j0 j0 3 4 5 6 7 8 9 0 3 4 5 6 7 8 9 0... c 3 5 9 7... Expao cot... 4 8 6 The amortzed cot of a gle operato 3/=3 = O() CS 473 Lecture X 4
The Accoutg Method Ag the followg amortzed cot Table-Expao : 0 Ierto of a ew tem : 3 Ierto of a ew tem a) (a a actual cot) for ertg telf to the table b) (a a credt) for movg telf the ext expao c) (a a credt) for movg aother tem ( the ext expao) that ha already moved the lat expao CS 473 Lecture X 5
Accoutg Method Example Sze of the table: M Immedately after a expao (jut before the erto) um[t] = M/ ad ze[t] = M where M a power of. Table cota o credt. 3 4 5 6 7 8 9 0 3 4 5 6 X X X X X X X X CS 473 Lecture X 6
Accoutg Method Example t erto 3 4 5 6 7 8 9 0 3 4 5 6 X X X X X X X X Z $ $ (c) d erto (b) (a) $ for erto 3 4 5 6 7 8 9 0 3 4 5 6 X X X X X X X X Z Z $ $ $ $ CS 473 Lecture X 7
Accoutg Method Example M/th Ierto 3 4 5 6 7 8 9 0 3 4 5 6 X X X X X X X X Z Z Z Z Z Z Z Z $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ Thu, by the tme the table cota M tem ad full each tem the table ha $ of credt to pay for t move durg the ext expao CS 473 Lecture X 8
The Potetal Method Defe a potetal fucto Φ that 0 mmedately after a expao buld to the table ze by the tme table become full Next expao ca be pad for by the potetal. CS 473 Lecture X 9
Defto of Φ Oe poble Φ : Φ(T) = *um[t] ze[t] Immedately after a expao ze[t] = *um[t] Φ(T) = 0 Immedately before a expao ze[t] = um[t] Φ(T) = um[t] The tal value for the potetal 0 CS 473 Lecture X 0
Defto of Φ Sce the table at leat half full (.e. um[t] ze[t] / ) Φ(T) alway oegatve. Thu, the um of the amortzed cot of TABLE-INSERT operato a upper boud o the um of the actual cot. CS 473 Lecture X
Aaly of -th Table Iert : um[t] after the -th operato : ze[t] after the -th operato Φ : Potetal after the -th operato Itally we have = = Φ = 0 Note that, = - + alway hold. CS 473 Lecture X
CS 473 Lecture X 3 Iert wthout Expao If the -th TABLE-INSERT doe ot trgger a expao, = ; c = 3 ) ( ) ) (( ) ( ) ( ˆ c c
CS 473 Lecture X 4 Iert wth Expao If the -th TABLE-INSERT doe trgger a expao, the 3 ) ( ) ) (( ) ( ) ( ) ( ˆ ; ; c c c
CS 473 Lecture X 5
Addg Delete Operato TABLE-DELETE: Remove the pecfed tem from the table. It ofte derable to cotract the table. I table cotracto, we would lke to preerve two properte Load factor of dyamc table bouded below by a cotat Amortzed cot of a table operato bouded above by a cotat We aume that the cot ca be meaured term of elemetary erto ad deleto CS 473 Lecture X 6
Expao ad Cotracto A atural trategy for expao ad cotracto Double the table ze whe a tem erted to a full table Halve the ze whe a deleto would caue (T) < / Th trategy guaratee ( T) Ufortuately, t ca caue the amortzed cot of a operato to be qute large CS 473 Lecture X 7
Wort-Cae for α(t) ½ Coder the followg wort cae cearo We perform operato o a empty table where a power of Frt / operato are all erto, cot a total of Θ() at the ed: we have um[t] = ze[t] = / Secod / operato repeat the equece I D D I that I D D I I D D I I D D I... CS 473 Lecture X 8
Example: =6 Wort-Cae for α(t) ½ :... 7 8 9 0 3 4 5 6 oper: I I... I I I D D I I D D I... 7 8 9 8 7 8 9 8 7 8... 8 8 6 6 8 8 6 6 8 8 I the ecod / operato E C E C The frt INSERT caue a expao Two further DELETE caue cotracto Two further INSERT caue expao... ad o o Hece there are /8 expao ad /8 cotracto The cot of each expao ad cotracto / CS 473 Lecture X 9
Wort-Cae for α(t) ½ Thu the total cot of operato Θ( ) ce Frt / operato : 3 Secod / operato : (/4)*(/)= /8 The amortzed cot of a operato Θ() The dffculty wth th trategy After a expao, we do ot perform eough deleto to pay for a cotracto After a cotracto, we do ot perform eough erto to pay for a expao CS 473 Lecture X 30
Improvg Expao Cotracto We ca mprove upo th trategy by allowg α(t) to drop below ½ We cotue to double the table ze whe a tem erted to a full table But, we halve the table ze (perform cotracto) whe a deleto caue α(t) < ¼ rather tha α(t) < ½, Therefore, ¼ α(t) CS 473 Lecture X 3
Improvg Expao Cotracto Hece after a expao, α(t) = ½, thu at leat half of the tem the table mut be deleted before a cotracto ca occur. Smlarly, after a cotracto α(t) = ½, thu the umber of tem the table mut be doubled by erto before a expao ca occur. CS 473 Lecture X 3
Potetal Method Defe the potetal fucto a follow Φ(T) = 0 mmedately after a expao or cotracto Recall that, α(t) = ½ mmedately after ad expao or cotracto, therefore the potetal hould buld up to um[t] a α(t) creae to or decreae to ¼ So that the ext expao or cotracto ca be pad by the potetal. CS 473 Lecture X 33
Φ(α) w.r.t. α(t) M=um[T] whe a expao or cotracto occur CS 473 Lecture X 34
Decrpto of New Φ Oe uch Φ or ( T) ( T) um[ T] ze[ T] ze[t] um[ T] um[ T]( / ) um[ T](/ ) f (T) f (T) f (T) f (T) CS 473 Lecture X 35
Decrpto of New Φ Φ = 0 whe α = ½ Φ = um[t] whe α = ¼ Φ = 0 for a empty table (um[t] = ze[t]=0, α[t] = 0) Φ alway oegatve CS 473 Lecture X 36
Amortzed Aaly Operato are: TABLE-INSERT TABLE-DELETE c : : Actual Cot um[ T] cˆ : : AmortzedCot ze[ T] : : ( T) ( T) after the -th operato CS 473 Lecture X 37
Table Iert = - + - = Table cotracto may ot occur. α - ½ Aaly detcal to that for table expao Therefore, ĉ = 3 whether the table expad or ot. α - < ½ ad α < ½ Expao may ot occur (ĉ =, = - ) cˆ c (3 / ) ( / ) ( ) 0 CS 473 Lecture X 38
CS 473 Lecture X 39 Table Iert α - < ½ ad α ½ α = ½ Expao may ot occur (c = ; = - ; = / ) 3 / 3 / 3 3 3 / 3 3 3 / 3 3 ) ( / ) ( ) / ( ˆ c c Thu, amortzed cot of a TABLE-INSERT operato at mot 3.
CS 473 Lecture X 40 Table Delete = - - - = + Table expao may ot occur. α - ½ ad ¼ α < ½ (It doe ot trgger a cotracto) = - ad c = ad α < ½ ) ( / / ) / ( ) / ( ˆ c c
CS 473 Lecture X 4 Table Delete α - = ¼ (It doe trgger a cotracto) = - / ; = - /; ad c = + α - > ½ (α ½ ) Cotracto may ot occur (c = ; = - ) / / ) / ( ) / ( ) ( ˆ c c ) ( ) ( ) ( ˆ c c
CS 473 Lecture X 4 Table Delete α - = ½ (α < ½) Cotracto may ot occur c = ; = - ; = - /; ad Φ - =0) ) ( / but / 0 ) / ( ˆ c c
Table Delete Thu, the amortzed cot of a TABLE-DELETE operato at mot Sce the amortzed cot of each operato bouded above by a cotat The actual tme for ay equece of operato o a Dyamc Table O() CS 473 Lecture X 43