Spla Tees Handout Amotied Analsis Last time we discussed amotied analsis of data stuctues A wa of epessing that even though the wost-case pefomance of an opeation can be bad, the total pefomance of a sequence of opeations cannot be too bad. One wa of thinking of amotied time is as being an aveage : If an sequence of n opeations takes less than T (n) time, the amotied time pe opeation is T (n)/n. We fomall defined amotied time using the idea that we ove-chage some opeations and stoe the ove-chage as cedits/potential that can then help pa fo late opeations (potential method) Conside pefoming n opeations on an initial data stuctue D 0 D i is data stuctue afte ith opeation. c i is actual cost (time) of ith opeation. Potential function: Φ : D i R c i amotied cost of ith opeation: c i = c i + Φ(D i ) Φ(D i ) Given Φ(D 0 ) = 0 and Φ(D i ) 0: n i= c i n i= c i We also discussed two eamples of amotied analsis Stack with Multipop (O(n) wost-case, O() amotied). Incement on bina counte (O(log n) wost-case, O() amotied). In both cases we could ague fo O() amotied pefomance without actuall doing potential calculation we just think about potential/cedits as being distibuted on cetain pats of the data stuctue and let opeations put and take cedits while maintaining some invaiant (accounting method).
Spla tees We have peviousl discussed bina seach tees and how the can be kept balanced (O(log n) height) duing inset and delete opeations (ed-black tees). Rebalancing athe complicated Eta space used fo the colo of each node We also discussed skip lists which ae a lot simple than ed-black tees Onl guaantee O(log n) epected pefomance No eta infomation is used fo ebalance infomation though Spla tees ae seach tees that magicall balance themselves (no ebalance infomation is stoed) and have amotied O(log n) pefomance. Recall seach tees: Bina tee with elements in nodes If node v holds element e then Spla tee: all elements in left subtee < e all elements in left subtee > e Nomal (possibl unbalanced) seach tee T All opeations implemented using one basic opeation, Spla: Spla(, T ) seaches fo in T and eoganies tee such that (o min element > o ma element < ) is in oot Seach(, T ): Spla(, T ) and inspect oot Inset(, T ): Spla(, T ) and ceate new oot with T spla(,t) o
Delete(, T ): Spla(, T ) and emove oot tee falls into T and T. Spla(, T ) Make T ight son of new oot of T afte spla spla(,t) T spla(,) All opeations pefom O() Spla s and use O() eta time. O(log n) amotied Spla gives O(log n) amotied bound on all opeations. Implementation of Spla: Seach fo like in nomal seach tee Repeatedl otate up until it becomes the oot. We distinguish between thee cases:. is child of oot (no gandpaent): otate() e.g. T T. has paent and gandpaent and both and left (ight) childen: otate() followed b otate() Note: Does not wok with otate() and otate() e.g. T T T T T T
. has paent and gandpaent and one of and is a left child and the othe is a ight child: otate() followed b otate() e.g. T T T T T T Note: A Spla can take O(n) wost-case time (ve unbalanced tee) But Spla tees somehow seem to sta nicel balanced Eamples: Spla(, T ) (tpe) (tpe) (tpe) (tpe) Spla(, T ) (tpe) (tpe)
Analsis: We will use accounting method to show that all opeations (Spla) takes O(log n) amotied time. We will imagine that each node in tee has cedits on it We will use some cedits to pa fo (pat of) otations duing a spla We will see that we onl have to place O(log n) new cedits (on oot) when pefoming an Inset o Delete Note that we will ignoe cost of seaching fo, since the otations cost at least as much as the seach ( if we can bound amotied otation cost we also bound seach cost). Let T () be tee ooted at. We will maintain the cedit invaiant that each node holds µ() = log T () cedits. We will pove the following lemma: Less than o equal to (µ(t ) µ()+o()) cedits ae needed to pefom Spla(, T ) opeation and maintain cedit invaiant Using this lemma we get that a spla opeation uses at most log n +O() = O(log n) cedits (time). As an Inset o a Delete equies us to inset at most O(log n) eta cedits (on the oot) moe than the ones used on the Spla, we get the O(log n) amotied bound. Poof of lemma: Case : Let µ and µ be the value of µ befoe and afte a otate opeation in case,, o. Duing a Spla opeation we pefom a numbe of, sa k 0, case and opeations and possibl a case opeation. Net time we will show that the cost of one opeation is: Case : (µ () µ() + O()) Case : (µ () µ()) Case : (µ () µ()) When we sum ove all k + opeations in a spla we get (µ(t ) µ() + O()) whee µ() is the numbe of cedits on befoe the Spla. Note that it is impotant that we onl have the O() tem in case. We have: µ () = µ(), µ () µ () and all othe µ s ae unchanged. To maintain invaiant we use: µ () + µ () µ() µ() = µ () µ() µ () µ() (µ () µ()) To do actual otation we use O() cedits.
Case : We have µ () = µ(), µ () µ (), µ () µ (), µ() µ() and all othe µ s ae unchanged. To maintain invaiant we use: µ () + µ () + µ () µ() µ() µ() = µ () + µ () µ() µ() = (µ () µ()) + (µ () µ()) (µ () µ()) + (µ () µ()) = (µ () µ()) Case : This means that we can use the emaining µ () µ() cedits to pa fo otation, unless µ () = µ() (can happen since µ() = log T () ). We will show that if µ () = µ() then µ () + µ () + µ () < µ() + µ() + µ() which means that the opeation actuall eleases cedits we can use fo the otation: Assume µ () = µ() and µ () + µ () + µ () µ() + µ() + µ() We have µ() = µ () = µ() µ() = µ() = µ() and µ () + µ () + µ () µ() + µ() + µ() = µ() = µ () µ () + µ () µ () Since µ () µ () and µ () µ () we get µ () = µ () = µ () Since µ() = µ () we have µ() = µ() = µ() = µ () = µ () = µ () which cannot be tue (and thus ou initial assumption cannot be tue): Let a be T () befoe otations (a = T + T + ) Let b be T () afte otations (b = T + T + ) Since µ() = µ () = µ () we have log a = log b = log(a + b + ) but then we have the following contadiction: if a b: log(a + b + ) log a = + log a > log a if a > b: log(a + b + ) log b = + log b > log b Can be poved analogousl to case.