Data Structures ad Algorithms Autum 2017-2018
Outlie 1 Sortig Algorithms (cotd)
Outlie Sortig Algorithms (cotd) 1 Sortig Algorithms (cotd)
Heapsort Sortig Algorithms (cotd) Have see that we ca build a heap i liear time usig /2 calls of trickledow() If we ow call deletemi() times, we will get the elemets i smallest-to-largest (o-decreasig) order Where to store them as we peel them off? Do we eed a extra array? No Each time a deletemi() is called, place the result i the slot just opeed up by the shruk array Problem with this is that after deletemi()s the elemets ow appear largest-to-smallest (o-icreasig) Two solutios: 1 Use a (max)heap with the deletemax() operatio 2 After the deletemi()s reverse the cotets of the array to get it smallest-to-largest; reversig a array ca be doe i O()-time usig just O(1) additioal storage
Mergesort Sortig Algorithms (cotd) Mergesort strategy: To sort a array of umbers Sort the first half of the array Sort the secod half of the array Merge the two halves of the array Ruig time aalysis: Sortig a array comprisig oe elemet takes uit time To sort umbers we perform two sorts o /2 umbers ad the merge the resultig arrays Mergig two arrays of legth /2 each takes time sice we ca make oe (sychroised) pass over the two halves, copyig the appropriate elemet as we go
Mergesort (cotd) Sortig Algorithms (cotd) 2 2 2 k 2 k 2 2 2 + 2 2 2 + + + i=0 2 2 2 k O 0 () O 1 () O 2 () + + + + = O 2 k k=lg () ( k ) ( k ) = O 2 i = O( lg ) 2 i O = O(k ) i=0 = = = + + + +
Mergesort (cotd) Sortig Algorithms (cotd) Recurrece relatio: T (1) = 1 T () = 2T (/2) + Assume is a power of 2 ( = 2 k, k = log ) Ca argue the, that if is ot a power of 2, it falls betwee two powers of two: 2 k < < 2 k Two techiques to solvig the recurrece T () = 2T (/2) +
Mergesort Solutio 1 Divide through by, T () ad lettig T () = T ()/ = T (/2) 2 T () = T (/2) + 1 + 1 = T (/4) + 1 + 1 Thus, = T (1) + 1 + + 1 }{{} k T () = T ()/ = 1 + log T () = + log = O( log )
Mergesort Solutio 2 Therefore T () = 2T (/2) +, ad so, T (/2) = 2T (/4) + /2 T () = 2(2T (/4) + /2) + = 4T (/4) + 2 T () = 2 k T (/2 k ) + k Whe k = log, /2 k = 1 (sice = 2 k for some k) T () = T (1) + log = + log = O( log )
Did you kow? Sortig Algorithms (cotd) a log c = (c log c a ) log c = c log c a log c = c log c log c a = (c log c ) log c a = log c a I summary a log c = log c a
Outlie Sortig Algorithms (cotd) 1 Sortig Algorithms (cotd)
Geeral Solutios to Divide ad Coquer How do we solve recurreces of the form T () = at ( c ) + b T (1) = b Sice we are dividig by c, we will assume that is a power of c; that is, = c k or, k = log c Of course, this is ot the case i geeral but it is always the case that is sadwiched betwee two adjacet powers of c: c k c k+1
Geeral Solutios to Divide ad Coquer (cotd) T () = at ( c ) + b = a[at ( c 2 ) + b c ] + b = a 2 T ( c 2 ) + b a c + b = a 2 [at ( c 3 ) + b c 2 ] + b a c + b = a 3 T ( c 3 ) + b a2 c 2 + b a c + b = a log c T (1) + b[ alogc 1 c logc 1 + + a c + 1]
Geeral Solutios to Divide ad Coquer (cotd) T () = a log c T (1) + b log c 1 i=0 = a log c b log c 1 + b = b alog c c log c + b = b log c i=0 ( a c ) i i=0 log c 1 i=0 [( a ) logc ] +1 c 1 = b a c 1, ( a c ) i ( a c ( a c ) i, sice T (1) = b ) i, sice c log c = a c 1
Geeral Solutios to Divide ad Coquer (cotd) There are three cases to cosider with this expressio for T () ❶ a = c (eg mergesort; Alg 3 of MSS) ❷ a < c (quickselect() (described later)) ❸ a > c (Strasse matrix mult alg, a = 7, c = 2) I each case we will reduce the expressio as much as we ca ad the argue that sice is always bouded from above ad below by a power of c, the T () will be Θ of the fuctio of that we derive See also the Master Theorem page o Wikipedia
Geeral Solutios to Divide ad Coquer (cotd) Case ❶ a c = 1 (a = c) the T () = b [log c + 1] = Θ( log c )
Geeral Solutios to Divide ad Coquer (cotd) Case ❷ a c < 1 (a < c) the ( a ) logc +1 lim 0 c b [( a c ) logc ] +1 1 a c 1 ) logc +1 [ ( 1 a c = b 1 a c [ ] 1 b c a c = b c c a = Θ() ]
Geeral Solutios to Divide ad Coquer (cotd) Case ❸ a c > 1 (a > c) the ( a ) logc +1 lim c so the remaiig terms are isigificat, ad the ruig time should grow very rapidly Usig the Did you kow? fact, a log c = log c a ( a ) logc +1 T () = b c = b a ( a ) logc c c = ab c logc a = Θ( log c a )
Related Recurreces What if, istead of b work beig required at every level of recursio, it was a more geeral (complicated) expressio? For example, what about solvig where f () =, say? T () = at (/c) + f () The two Extra otes 1 liks i Week08 of the class resource matrix discuss recursio ad more geeral recurreces that arise The recurrece tree i the secod lik is a very useful way of thikig about solvig recurreces 1 With thaks to Jeff Erickso, Uiversity of Illiois, USA