CIS 11 Data Structures ad Algorithms with Java Sprig 019 Code Sippets ad Recurreces Moday, February 4/Tuesday, February 5 Learig Goals Practice provig asymptotic bouds with code sippets Practice solvig recurreces usig the methods of iteratio ad recurrece trees Code Sippets We ca apply our kowledge of Big-Oh ad summatios to fid the ru time of a sippet of our code Besides recursio, ested iteratio is where our code s efficiecy will be bottleecked We should cosider the loop as a summatio, ad use our kowledge to simplify it from there Try startig from the iermost loop with fixed bouds ad workig outwards Problems Problem 1 Problem 1 a Provide a ruig time aalysis of the followig loop That is, fid both Big-Oh ad Big Ω: for ( it i = 0 ; i < ; i ++ for ( it j = i ; j <= ; j++ for ( it k = i ; k <= j ; k++ sum++; Problem 1 b Provide a ruig time aalysis of the followig loop: for ( it i = 4 ; i < ; i = i i for ( it j = ; j < Math s q r t ( i ; j = j+j System out p r i t l ( ; Recurreces As you have see i class, recurreces are equatios that ca help us describe the ruig time of a recursive algorithm You have thus far see two differet ways of solvig recurreces: Iteratio I this method, we expad T ( fully by substitutio ad solve for T ( directly Recursio trees I this method, we draw the recursive calls to T ( i a tree format ad cout the amout of work doe i each level of the tree I this lab, we will focus o the method of iteratio Let s first go through some examples before ruig through problems 1
Example: Method of Iteratio I Let s examie the followig recurrece T ( T ( 1 + 1 0 otherwise Usig the method of iteratio, we expad T ( as follows: T ( 1 + = [T ( + ( 1] + = [[T ( + ( ] + ( 1] + = i i=1 Example: Method of Iteratio II = 1 ( + 1( = + 1 = Θ( This is a more complex versio of the recurrece i problem 1c What makes this tricky is all of the algebraic maipulatio, but the strategy of settig the problem up is the same! Note: this is probably the most difficult thigs will get i terms of solvig recurreces via iteratio T + 1 This is tricky, but let s jump right i First, let s assume that is some power of such that = k = k = log T + ( = T + + ( ( = T + + + We ca also write T = T T = T + + k 1 T k 1 = k T k + k 1 k 1
If we add all of the equatios above ad subtract commo terms, we ed up with the followig (also usig the base case + + + + k 1 + k k 1 = k + k 1 ( i i=0 = k + k 1 1 ( = log log + 1 = log + ( log = log + c log c 1 = log = log = Θ( log That last step might be a bit cofusig We observe that log 06 = 05 + ɛ for some positive ɛ (We itroduce ɛ to make evaluatig the limit simpler We ca cofirm that T ( is i fact Θ( log via the limit defiitio of Big-Θ (c + 1 05+ɛ c 05 lim 05+ɛ = c + 1 Therefore, we have a Big-Θ Example: Method of Recursio Trees Let s examie the recurrece of merge sort For those that are ufamiliar with it, the algorithm works by takig a usorted array, sortig the left ad right halves of the array recursively, ad the mergig the two sorted halves together to ed up with the fial sorted list Let T ( represet the time the algorithm takes for a iput of size Sice the two halves are sorted recursively by the same algorithm, but with iputs that are each half the size of the origial, each half should take time T The mergig takes liear time So we ca write T + c for some costat c The recursio-tree is show below
Note that at the very top level (ie, the ed of the algorithm, it costs c to merge the two sorted halves of the array But to get there, we eeded to solve the two problems of size Each costs c to solve Therefore, across that level, the total cost is c + c = c We ca cotiue this all the way util we get to the very bottom of the tree, which are sigle elemets Note the that every level eds up costig c The height of the tree is lg Therefore, the total cost is c lg To see why the height is lg, observe that subproblem sizes decrease by a factor of each time we go dow oe level, we stop whe we reach sigleto elemets The subproblem size for a ode at depth i is Thus, the subproblem size hits = 1 whe i = 1 or, equivaletly, whe i = lg You ca read more i examples i CLRS 44 Problems You may assume i the followig cases that is either some power of or You may also fid the followig helpful: k i=0 Geometric series: q i = qk+1 1 q 1 = 1 qk+1 1 q Sum of biomial coefficiets over upper idex: j=0 ( j = m ( + 1 m + 1 4
Problem 1 Problem Solve the followig recurreces 1 4 5 T ( 1 + ( T + 1 T + 1 T ( 1 + 1 T ( + 1 1 Problem The CIS160 ad CIS11 TAs are havig aother social gatherig after goig to see the Pigeohole Priciple Players of Philadelphia (PPPP For some reaso or aother, the 160 TAs particularly ejoy playig the Tower of Haoi ad isist that the 11 TAs compete with them to see who ca solve the game the fastest Of course, the clever 11 TAs are very proficiet with their ruig time aalysis skills, ad kow that a optimal strategy is detailed by the followig code: fuctio HaoiTower(h, f rom, to, mid if h 1 the HaoiTower(h 1, from, mid, to MoveDisk(f rom, to HaoiTower(h 1, mid, to, from O(1 time operatio Problem What is the ruig time of the above algorithm? Prove it iductively 5