Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 31 CSE 331 Nov 14, 2012.

Similar presentations


Presentation on theme: "Lecture 31 CSE 331 Nov 14, 2012."— Presentation transcript:

1 Lecture 31 CSE 331 Nov 14, 2012

2 Online Office

3 Integer Multiplication
Input: a = (an-1,..,a0) and b = (bn-1,…,b0) a = b = 1001 Output: c = a x b c =

4 Some notation Dec(a) = Dec(a1)2[n/2] + Dec(a0)
Dec(a) = an-12n-1+ an-22n-2+…+ a12+ a0 a = Dec(a) = 13 a1 = (an-1,..,a[n/2]) a0=(a[n/2]-1,…,a0) a1 = 11 and a0 = 01 Dec(a) = an-12n-1+…+ a[n/2]2[n/2]+a[n/2]-12[n/2]-1 +…+ a0 = 2[n/2] (an-12n-[n/2]-1+…+ a[n/2]) + a[n/2]-12[n/2]-1 +…+ a0 Dec(a) = Dec(a1)2[n/2] + Dec(a0)

5 The algorithm so far… Shift by O(n) bits Adding O(n) bit numbers Mult over n bits a  b = a1b1 22[n/2] + (a1b0+a0b1)2[n/2] + a0b0 Multiplication over n/2 bit inputs T(n) is O(n2) T(n) ≤ 4T(n/2) + cn T(1) ≤ c

6 Today’s agenda Design a better Divide and Conquer Multiplication Algorithm

7 The key identity a1b0+a0b1= (a1+a0)(b1+b0) - a1b1 - a0b0

8 The final algorithm Input: a = (an-1,..,a0) and b = (bn-1,…,b0) T(1) ≤ c Mult (a, b) If n = 1 return a0b0 T(n) ≤ 3T(n/2) + cn a1 = an-1,…,a[n/2] and a0 = a[n/2]-1,…, a0 Compute b1 and b0 from b O(nlog 3) = O(n1.59) run time x = a1 + a0 and y = b1 + b0 Let p = Mult (x, y), D = Mult (a1, b1), E = Mult (a0, b0) All green operations are O(n) time F = p - D - E return D  22[n/2] + F  2[n/2] + E a  b = a1b1 22[n/2] +( (a1+a0)(b1+b0) - a1b1 - a0b0 ) 2[n/2] + a0b0

9

10 (Old) Reading Assignment
Sec 5.2 of [KT]

11 Rankings

12 How close are two rankings?

13 Rest of today’s agenda Formal problem: Counting inversions
Divide and Conquer algorithm

14 Divide and Conquer Divide up the problem into at least two sub-problems Solve all sub-problems: Mergesort Recursively solve the sub-problems Solve some sub-problems: Multiplication Solve stronger sub-problems: Inversions “Patch up” the solutions to the sub-problems for the final solution


Download ppt "Lecture 31 CSE 331 Nov 14, 2012."

Similar presentations


Ads by Google