CS 4 -- Spring 29 Powering numer Prolem: Compute n, where n N. Nive lgorithm: Θ(n). ore Divide & Conquer Crol Wenk Slides ourtesy of Chrles Leiserson with smll hnges y Crol Wenk 2//9 CS 4 nlysis of lgorithms Divide-nd-onquer lgorithm: (reursive squring) n/2 n/2 if n is even; n = (n )/2 (n )/2 if n is odd. T(n) = T(n/2) + Θ() T(n) = Θ(log n). 2//9 CS 4 nlysis of lgorithms 2 Fioni numers Reursive definition: if n = ; F n = if n = ; F n + F n 2 if n 2. 2 8 2 4 L Nive reursive lgorithm: Ω(φ n ) (exponentil time), where φ = ( + )/2 is the golden rtio. 2//9 CS 4 nlysis of lgorithms Computing Fioni numers Nive reursive squring: F n = φ n / rounded to the nerest integer. Reursive squring: Θ(log n) time. This method is unrelile, sine floting-point rithmeti is prone to round-off errors. ottom-up (one-dimensionl dynmi progrmming): Compute F, F, F 2,, F n in order, forming eh numer y summing the two previous. Running time: Θ(n). 2//9 CS 4 nlysis of lgorithms 4
Convex Hull Convex Hull: Divide & Conquer Given set of pins on pinord nd ruer nd round them How does the ruer nd look when it snps tight? We represent onvex hull s the sequene of points on the onvex hull polygon, in ounter-lokwise order. 6 4 2 Preproessing: sort the points y x- oordinte Divide the set of points into two sets nd : ontins the left n/2 points, ontins the right n/2 points Reursively ompute the onvex hull of Reursively ompute the onvex hull of erge the two onvex hulls 2//9 CS 4 nlysis of lgorithms 2//9 CS 4 nlysis of lgorithms 6 erging Finding the lower tngent Find upper nd lower tngent With those tngents the onvex hull of n e omputed from the onvex hulls of nd the onvex hull of in O(n) liner time = rightmost point of = leftmost point of while T= not lower tngent to oth onvex hulls of nd do{ while T not lower tngent to onvex hull of do{ =- while T not lower tngent to onvex hull of do{ =+ n e heked in onstnt time 4 =2 4= 6 7 2 right turn or left turn? 2//9 CS 4 nlysis of lgorithms 7 2//9 CS 4 nlysis of lgorithms 8
Convex Hull: Runtime Convex Hull: Runtime Preproessing: sort the points y x- oordinte Divide the set of points into two sets nd : ontins the left n/2 points, ontins the right n/2 points Reursively ompute the onvex hull of Reursively ompute the onvex hull of erge the two onvex hulls O(n log n) just one O() T(n/2) T(n/2) O(n) Runtime Reurrene: T(n) = 2 T(n/2) + n Solves to T(n) = Θ(n log n) 2//9 CS 4 nlysis of lgorithms 9 2//9 CS 4 nlysis of lgorithms n trix multiplition Input: = [ ij ], = [ ij ]. Output: C = [ ij ] =. 2 2 L n L = 2 O L nn n ij = 2 n L n L 2 O L nn n 2//9 CS 4 nlysis of lgorithms ik k= kj i, j =, 2,, n. 2 L n L O L nn Stndrd lgorithm for i to n do for j to n do ij for k to n do ij ij + ik kj Running time = Θ(n ) 2//9 CS 4 nlysis of lgorithms 2
Divide-nd-onquer lgorithm IDE: n n mtrix = 2 2 mtrix of (n/2) (n/2) sumtries: r = e+ g s = f+ h t = e+d g u = f +d h r t s = u e d g C = f h 8 reursive mults of (n/2) (n/2) sumtries 4 dds of (n/2) (n/2) sumtries 2//9 CS 4 nlysis of lgorithms nlysis of D&C lgorithm # sumtries T(n) = 8 T(n/2) + Θ(n 2 ) sumtrix size work dding sumtries n log = n log 2 8 = n CSE T(n) = Θ(n ). No etter thn the ordinry lgorithm. 2//9 CS 4 nlysis of lgorithms 4 Strssen s ide ultiply 2 2 mtries with only 7 reursive mults. P = ( f h) P 2 = ( + ) h P = ( + d) e P 4 = d (g e) P = ( + d) (e + h) P 6 = ( d) (g + h) P 7 = ( ) (e + f ) r = P + P 4 P 2 + P 6 s = P + P 2 t = P + P 4 u = P + P P P 7 7 mults, 8 dds/sus. Note: No reline on ommuttivity of mult! Strssen s ide ultiply 2 2 mtries with only 7 reursive mults. P = ( f h) P 2 = ( + ) h P = ( + d) e P 4 = d (g e) P = ( + d) (e + h) P 6 = ( d) (g + h) P 7 = ( ) (e + f ) r = P + P 4 P 2 + P 6 =( + d)(e + h) + d (g e) ( + ) h + ( d)(g + h) = e + h + de + dh + dg de h h + g + h dg dh = e + g 2//9 CS 4 nlysis of lgorithms 2//9 CS 4 nlysis of lgorithms 6
Strssen s lgorithm. Divide: Prtition nd into (n/2) (n/2) sumtries. Form P-terms to e multiplied using + nd. 2. Conquer: Perform 7 multiplitions of (n/2) (n/2) sumtries reursively.. Comine: Form C using + nd on (n/2) (n/2) sumtries. T(n) = 7 T(n/2) + Θ(n 2 ) 2//9 CS 4 nlysis of lgorithms 7 nlysis of Strssen T(n) = 7 T(n/2) + Θ(n 2 ) n log = n log 2 7 n 2.8 CSE T(n) = Θ(n log 7 ). The numer 2.8 my not seem muh smller thn, ut euse the differene is in the exponent, the impt on running time is signifint. In ft, Strssen s lgorithm ets the ordinry lgorithm on tody s mhines for n or so. est to dte (of theoretil interest only): Θ(n 2.76L ). 2//9 CS 4 nlysis of lgorithms 8 Conlusion Divide nd onquer is just one of severl powerful tehniques for lgorithm design. Divide-nd-onquer lgorithms n e nlyzed using reurrenes nd the mster method (so prtie this mth). Cn led to more effiient lgorithms 2//9 CS 4 nlysis of lgorithms 9