hw3 - CS 170 Algorithms Fall 2014 David Wagner HW 3 Due Sept 19 6:00pm Instructions This homework is due Friday September 19 at 6:00pm electronically

# hw3 - CS 170 Algorithms Fall 2014 David Wagner HW 3 Due...

• Homework Help
• klyefuelopt
• 4
• 75% (16) 12 out of 16 people found this document helpful

This preview shows page 1 - 3 out of 4 pages.

CS 170 Algorithms Fall 2014 David Wagner HW 3 Due Sept. 19, 6:00pm Instructions. This homework is due Friday, September 19, at 6:00pm electronically. Same rules as for prior homeworks. 1. (10 pts.)Practice with recurrence relationsSolve the following recurrence relations. Express your answer usingΘ(·)notation. (For instance, if youwere given the recurrence relationT(n) =T(n-1)+3, the solution would beT(n) =Θ(n).T(n) =O(n2)would receive no credit, even though it is a valid upper bound, as it is not the best upper bound.) You don’tneed to show your work or justify your answer for this problem.(a)F(n) =F(n-4)+1.(b)G(n) =G(n/2)+3.(c)H(n) =H(n/4)+1.(d)I(n) =I(n/2)+n.(e)J(n) =2J(n/4)+6.(f)K(n) =2K(n/4)+n.(g)L(n) =2L(3n/4)+1.(h)M(n) =2M(n-1)+1.2. (15 pts.)Procedural Terrain GenerationRecursive algorithms can be useful for generating objects with fractal structure, like realistic rocky terrain.We can store the shape of the ground as an(n+1)×(n+1)array of height values, so thatH[x][y]is theheight at point(x,y). Say the heights at the corners(0,0),(0,n),(n,0), and(n,n)have been given, and weneed to fill in the rest of the grid.Consider the following algorithm, more complex versions of which are the basis of the terrain generationprocedures in many video games. The function Rand()returns a random number and takes constant time.Algorithm MidpointDisplacement(n):1. Call FillIn(0,n,0,n).Algorithm FillIn(‘,r,t,b):1. Ifr-‘ <2 orb-t<2, return.2. Letx:=b(+r)/2c.3. Lety:=b(t+b)/2c.4. SetH[x][y]:= (H[][t]+H[][b]+H[r][t]+H[r][b])/4+Rand().5. SetH[][y]:= (H[][t]+H[][b])/2.6. SetH[x][t]:= (H[][t]+H[r][t])/2.7. SetH[r][y]:= (H[r][t]+H[r][b])/2. CS 170, Fall 2014, HW 3 1   • • • 