cse101-w12-dq1

cse101-w12-dq1 - CSE 101, Winter 2012 Design and Analysis...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 101, Winter 2012 Design and Analysis of Algorithms Lecture 4: Divide and Conquer (I) Class URL: http://vlsicad.ucsd.edu/courses/cse101-w12/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Divide and Conquer (“DQ”) First “paradigm” or “framework” DQ(S) if S is small return ADHOC(S) else decompose S into subproblems S 1 ,…, S k // divide for i = 1 to k do y i = DQ(S i ) // conquer recombine y i into solution y // compose return y “Universal method”: Mergesort, Quicksort, FFT, Matrix/Integer arithmetic are classic examples
Background image of page 2
DQ for Sorting • Divide (into two equal parts) • Conquer (solve for each part separately) • Combine separate solutions • Mergesort – Divide into two equal parts – Sort each part using Mergesort (recursion!!!) – Merge two sorted subsequences
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Merging Two Subsequences Sequence #1: x[1]-x[2]- … - x[k] Sequence #2: y[1]-y[2]- … - y[l] if y[i] < x[j] compare y[i+1] and x[j] < k+l-1 edges = # (comparisons) = linear time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Background image of page 4
Mergesort Execution Example 285 179 652 351 423 310 861 254 450 520 285 179 652 351 310 861 450 285 179 652 351 423 310 861 254 450 520 285 179 652 351 423 310 254 520 310 285 310 179 652 351 285 310 179 652 351 351 652 351 652 285 310 351 652 179 285 310 351 652 423 179 861 254 450 520 285 310 652 423 179 861 520 310 351 652 423 179 254 450 520 285 652 254 450 520 423 861 254 450 520 285 310 351 652 423 179 861 450 520 520 285 310 351 652 254 179 423 450 520 861 254 285 310 351 423 179 450 520 652 861
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Tree of Recursions in Mergesort 1 2 3 4 5 6 7 8 1 3 5 8 2 4 6 7 1 5 3 8 5 1 8 3 4 7 2 6 7 4 6 2 log n n comparisons per level log n levels total runtime = n log n
Background image of page 6
Integer Multiplication • Multiplying Large Integers (Karatsuba-Ofman) –A = r n-1 a n-1 + … + r 1 a 1 + a 0 , r = radix • “Classic” approach = what you learned 12 years ago a n-1 a n-2 …a 1 a 0 X b n-1 b n-2 …b 1 b 0 -------------------------------- (n 2 ) work
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(Unhelpful) DQ for Integer Multiplication Can we apply D/Q? – Let n = 2s, r = 10 radix – A = wx, B = yz – AB = xz + 10 s (wz + xy) + 10 2s wy Complexity analysis – 4 n/2-digit multiplications: xz, wz, xy, wy – Digit-shifting: multiplication by 10 s , 10 2s – 3 additions T(n) = 4T(n/2) + (n) T(n) 4 T(n/2) + cn 4 [ 4T(n/4) + cn/2] + cn = 16 T(n/4) + (1 + 2)cn 16 [4T(n/8) + cn/4] + (1 + 2)cn = 64 T(n/8) + (1 + 2 + 4)cn 4 k T(n/2 k ) + (1 + 2 + 4 + … + 2 k-1 )cn For k = log 2 n: T(n) n 2 T(1) + cn 2 = O(n 2 )
Background image of page 8
(Helpful) DQ for Integer Multiplication Observation: r’ = (w+x)(y+z) = wy + (wz+xy) + xz –r (w+x)(y+z) –p wy –q xz – return 10 2s p + 10 s (r’-p-q) + q – T(n) = 3T(n/2) + (n) T(n) 3 T(n/2) + cn 3 [ 3T(n/4) + cn/2] + cn = 9 T(n/4) + (1 + 3/2)cn 9 [3T(n/8) + cn/4] + (1 + 3/2)cn = 27 T(n/8) + (1 + 3/2 + 9/4)cn 3 k T(n/2 k ) + (1 + 3/2 + 9/4 + … + (3/2) k-1 )cn For k = log 2 n: T(n) 3 log 2 n T(1) + cn 2 (3/2) log 2 n = O(3 log 2 n ) = O(n log 2 3 ) = O(n 1.5849… ) [(3/2) k – 1] / (3/2 – 1) = 2[(3/2) k –1] What is (x n – 1) / (x – 1) ? a log b c = c log b a (Take log base b)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Matrix Multiplication • A = […] , B = […] are n x n matrices •a 11 , a 12 , etc are n/2 x n/2 submatrices • M = AB = […] – where m 11 = a 11 b 11 + a 12 b 21 etc. – Evaluation requires 8 multiplies, 4 adds • T(n) = 8T(n/2) + O(n)   (n 3 ) (will see why later) X = a 11 a 12 a 21 a 22 b b 12 b 21 b 22 m m 12 m 21 m 22
Background image of page 10
Strassen’s Matrix Multiplication p 1 = (a 21 + a 22 -a 11 )(b 22 -b 12 + b 11 ) p 2 = a 11 b 11 p 3 = a 12 b 21 p 4 = (a 11 -a 21 )(b 22 -b 12 ) p 5 = (a 21 +a 22 )(b 12 11 ) p 6 = (a 12 -a 21 +a 11 -a 22 )b 22 p 7 = a 22 (b 11 +b
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.