08 - 25 jan - divide and conquer
TRANSCRIPT
![Page 1: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/1.jpg)
CS 321. Algorithm Analysis & Design Lecture 8
Divide and ConquerPart III - Multiplication and Selection
![Page 2: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/2.jpg)
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
![Page 3: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/3.jpg)
Multiplying two n-bit integers
![Page 4: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/4.jpg)
1 1 0 1
1 0 1 1
![Page 5: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/5.jpg)
1 1 0 1
1 0 1 1
![Page 6: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/6.jpg)
1 1 0 1
1 0 1 1
1 1 0 1
![Page 7: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/7.jpg)
1 1 0 1
1 0 1 1
1 1 0 1
![Page 8: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/8.jpg)
1 1 0 1
1 0 1 1
1 1 0 1
1 1 0 1
![Page 9: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/9.jpg)
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
![Page 10: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/10.jpg)
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.
![Page 11: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/11.jpg)
x
y
![Page 12: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/12.jpg)
x
y
xy = (2(n/2)xL + xR)(2(n/2)yL + yR)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
![Page 13: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/13.jpg)
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
![Page 14: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/14.jpg)
x
y
xL xR
yL yR
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
![Page 15: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/15.jpg)
x
y
xL xR
yL yR
T(n) = 4T(n/2) + O(n)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
![Page 16: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/16.jpg)
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
![Page 17: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/17.jpg)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
![Page 18: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/18.jpg)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
![Page 19: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/19.jpg)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
![Page 20: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/20.jpg)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
![Page 21: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/21.jpg)
xy = 2nxLyL + 2(n/2)(xLyR +yLxR) + xRyR
(xL + xR)(yL + yR) = (xLyL + xLyR + yLxR + xRyR)
(xL + xR)(yL + yR) - (xLyL + xRyR)
![Page 22: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/22.jpg)
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)
![Page 23: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/23.jpg)
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
![Page 24: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/24.jpg)
Finding the kth smallest element
![Page 25: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/25.jpg)
![Page 26: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/26.jpg)
Pick a “pivot”.
![Page 27: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/27.jpg)
Pick a “pivot”.
![Page 28: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/28.jpg)
![Page 29: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/29.jpg)
p elements on the left and (n-p) on the right.
![Page 30: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/30.jpg)
p elements on the left and (n-p) on the right.
if p is larger than k:
![Page 31: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/31.jpg)
p elements on the left and (n-p) on the right.
if p is larger than k:
![Page 32: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/32.jpg)
p elements on the left and (n-p) on the right.
![Page 33: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/33.jpg)
p elements on the left and (n-p) on the right.
if p is smaller than k:
![Page 34: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/34.jpg)
p elements on the left and (n-p) on the right.
if p is smaller than k:
![Page 35: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/35.jpg)
p elements on the left and (n-p) on the right.
if p is smaller than k:
(k-p)th smallest element
![Page 36: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/36.jpg)
Group the n elements into (n/5) buckets of size five each.
![Page 37: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/37.jpg)
Find the median in each bucket.
![Page 38: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/38.jpg)
Identify the Median of these Medians (MOM).
![Page 39: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/39.jpg)
Use the MOM as the pivot.
![Page 40: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/40.jpg)
Use the MOM as the pivot.
![Page 41: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/41.jpg)
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
![Page 42: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/42.jpg)
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
![Page 43: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/43.jpg)
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
![Page 44: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/44.jpg)
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
![Page 45: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/45.jpg)
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
![Page 46: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/46.jpg)
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
![Page 47: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/47.jpg)
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
![Page 48: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/48.jpg)
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
![Page 49: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/49.jpg)
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
![Page 50: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/50.jpg)
At least 3n/10 elements are smaller than the pivot, and at least 3n/10 elements are larger.
![Page 51: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/51.jpg)
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.
![Page 52: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/52.jpg)
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)
![Page 53: 08 - 25 Jan - Divide and Conquer](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58efcbd21a28ab85148b4583/html5/thumbnails/53.jpg)
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)