08 - 25 jan - divide and conquer
TRANSCRIPT
CS 321. Algorithm Analysis & Design Lecture 8
Divide and ConquerPart III - Multiplication and Selection
T (n) aT (n/b) +O(nd)
T (n) = O(ndlog n)
T (n) = O(nd)
T (n) = O(nlogb a)
if a = bd
if a < bd
if a > bd
Multiplying two n-bit integers
1 1 0 1
1 0 1 1
1 1 0 1
1 0 1 1
1 1 0 1
1 0 1 1
1 1 0 1
1 1 0 1
1 0 1 1
1 1 0 1
1 1 0 1
1 0 1 1
1 1 0 1
1 1 0 1
1 1 0 1
1 0 1 1
1 1 0 1
1 1 0 1
0 0 0 0
1 1 0 1
1 0 0 0 1 1 1 1
1 1 0 1
1 0 1 1
1 1 0 1
1 1 0 1
0 0 0 0
1 1 0 1
1 0 0 0 1 1 1 1
O(n2) operations.
x
y
x
y
xy = (2(n/2)xL + xR)(2(n/2)yL + yR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
x
y
xL xR
yL yR
xy = (2(n/2)xL + xR)(2(n/2)yL + yR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
x
y
xL xR
yL yR
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
x
y
xL xR
yL yR
T(n) = 4T(n/2) + O(n)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
x
y
xL xR
yL yR
T(n) = 4T(n/2) + O(n)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
bd = 2 and a = 4
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
(xL + xR)(yL + yR) - (xLyL + xRyR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
(xL + xR)(yL + yR) - (xLyL + xRyR)
T(n) = 3T(n/2) + O(n)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
(xL + xR)(yL + yR) - (xLyL + xRyR)
T(n) = 3T(n/2) + O(n)
≤ n(log3) = n1.59
Finding the kth smallest element
Pick a “pivot”.
Pick a “pivot”.
p elements on the left and (n-p) on the right.
p elements on the left and (n-p) on the right.
if p is larger than k:
p elements on the left and (n-p) on the right.
if p is larger than k:
p elements on the left and (n-p) on the right.
p elements on the left and (n-p) on the right.
if p is smaller than k:
p elements on the left and (n-p) on the right.
if p is smaller than k:
p elements on the left and (n-p) on the right.
if p is smaller than k:
(k-p)th smallest element
Group the n elements into (n/5) buckets of size five each.
Find the median in each bucket.
Identify the Median of these Medians (MOM).
Use the MOM as the pivot.
Use the MOM as the pivot.
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
5
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
5
7
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
5
7
5
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
5
7
5
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
5
7
5
5
4 4 2 1 2
5
1 2 2 4 7
9
6 4 5 5 9
4
7 4 5 9 7
3 8
2
2
5
7
5
5
At least 3n/10 elements are smaller than the pivot, and at least 3n/10 elements are larger.
At least 3n/10 elements are smaller than the pivot, and at least 3n/10 elements are larger.
Dealing with at most 7n/10 elements in the next level of recursion.
At least 3n/10 elements are smaller than the pivot, and at least 3n/10 elements are larger.
Dealing with at most 7n/10 elements in the next level of recursion.
T(n) ≤ T(n/5) + T(7n/10) + O(n)
At least 3n/10 elements are smaller than the pivot, and at least 3n/10 elements are larger.
Dealing with at most 7n/10 elements in the next level of recursion.
T(n) ≤ T(n/5) + T(7n/10) + O(n)
Exercise: T(n) = O(n)