Divide and Conquer Algorithms
Antonio Carzaniga
Faculty of Informatics University of Lugano
Outline
Merging
Searching
Sorting
Multiplying
Merging
Given two sequencesA={a1,a2, . . . ,an}and
B={b1,b2, . . . ,bm}, output a sequenceX ={x1,x2, . . . ,xl} such that
◮ every element ofAappears once inX
◮ every element ofBappears once inX
Merging
Given two sequencesA={a1,a2, . . . ,an}and
B={b1,b2, . . . ,bm}, output a sequenceX ={x1,x2, . . . ,xl} such that
◮ every element ofAappears once inX
◮ every element ofBappears once inX
◮ every element ofX appears inAor inBor in both
Example:
A={34,7,11,31,14,51,8,21,10} B={51,21,14,15,27,31,2}
Merging
Given two sequencesA={a1,a2, . . . ,an}and
B={b1,b2, . . . ,bm}, output a sequenceX ={x1,x2, . . . ,xl} such that
◮ every element ofAappears once inX
◮ every element ofBappears once inX
◮ every element ofX appears inAor inBor in both
Example:
A={34,7,11,31,14,51,8,21,10} B={51,21,14,15,27,31,2}
A Simple Merge Algorithm
Algorithm strategy
◮ iterate through every positioni, first throughAand thenB
◮ outputa
iifaiis not in{a1,a2, . . . ,ai−1} ◮ outputb
A Simple Merge Algorithm
Algorithm strategy
◮ iterate through every positioni, first throughAand thenB
◮ outputa 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
Complexity
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
Complexity
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬FIND(A,B[i])∧ ¬FIND(B[1. .i−1],B[i]) 8 thenoutputB[i]
Complexity
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬FIND(A,B[i])∧ ¬FIND(B[1. .i−1],B[i]) 8 thenoutputB[i]
Searching
Searching
Given a sequenceAand a valuekey, returnTRUEifAcontains key, orFALSE ifAdoes not contain a valuekey
FIND(A,key)
1 fori ←1tolength(A)
2 do
3 ifA[i] =key
Searching
Given a sequenceAand a valuekey, returnTRUEifAcontains key, orFALSE ifAdoes not contain a valuekey
FIND(A,key)
1 fori ←1tolength(A)
2 do
3 ifA[i] =key
4 then returnTRUE 5 returnFALSE
5 ifvalue(item) =key 6 then returnTRUE
7 i ←i+1
Searching
Given a sequenceAand a valuekey, returnTRUEifAcontains key, orFALSE ifAdoes not contain a valuekey
FIND(A,key)
1 fori ←1tolength(A)
2 do
3 ifA[i] =key
4 then returnTRUE 5 returnFALSE
5 ifvalue(item) =key 6 then returnTRUE
7 i ←i+1
8 item←next(item) 9 returnFALSE
Searching
Given a sequenceAand a valuekey, returnTRUEifAcontains key, orFALSE ifAdoes not contain a valuekey
FIND(A,key)
1 fori ←1tolength(A)
2 do
3 ifA[i] =key
4 then returnTRUE 5 returnFALSE
5 ifvalue(item) =key 6 then returnTRUE
7 i ←i+1
8 item←next(item) 9 returnFALSE
Complexity of
M
ERGE
S
IMPLE
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬FIND(A,B[i])∧ ¬FIND(B[1. .i−1],B[i]) 8 thenoutputB[i]
T(N) = N
∑
i=1
Complexity of
M
ERGE
S
IMPLE
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬FIND(A,B[i])∧ ¬FIND(B[1. .i−1],B[i]) 8 thenoutputB[i]
Complexity of
M
ERGE
S
IMPLE
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬FIND(A,B[i])∧ ¬FIND(B[1. .i−1],B[i]) 8 thenoutputB[i]
Complexity of
M
ERGE
S
IMPLE
MERGESIMPLE(A,B) 1 fori ←1tolength(A)
2 do
3 if¬FIND(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬FIND(A,B[i])∧ ¬FIND(B[1. .i−1],B[i]) 8 thenoutputB[i]
Searching (2)
Searching (2)
Given asortedsequenceAand a valuekey, return TRUEifA constains the valuekey, orFALSE otherwise
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Binary Search
BINARYSEARCH(A,key) 1 first ←1
2 last ←length(A) 3 whilefirst ≤last
4 domiddle← ⌈(first+last)/2⌉
5 ifA[middle] =key 6 then returnTRUE 7 elseiffirst =last 8 then returnFALSE 9 elseifA[middle]>key 10 thenlast ←middle−1 11 else fisrt ←middle+1
12 returnFALSE 12
Merging Sorted Sequences
A slightly different problem:
Given twosortedsequencesA={a1,a2, . . . ,an}and B={b1,b2, . . . ,bm}, wherea1≤a2≤. . . ≤an}and
b1≤b2≤ . . .≤bm, output a sequenceX ={x1,x2, . . . ,xl}such that
◮ every element ofAappears once inX
◮ every element ofBappears once inX
A Better Merge Algorithm
MERGESIMPLE2(A,B) 1 fori ←1tolength(A)
2 do
3 if¬BINARYSEARCH(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬BINARYSEARCH(A,B[i])
A Better Merge Algorithm
MERGESIMPLE2(A,B) 1 fori ←1tolength(A)
2 do
3 if¬BINARYSEARCH(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬BINARYSEARCH(A,B[i])
8 ∧¬BINARYSEARCH(B[1. .i−1],B[i]) 9 thenoutputB[i]
T(N) = N
∑
i=1
A Better Merge Algorithm
MERGESIMPLE2(A,B) 1 fori ←1tolength(A)
2 do
3 if¬BINARYSEARCH(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬BINARYSEARCH(A,B[i])
8 ∧¬BINARYSEARCH(B[1. .i−1],B[i]) 9 thenoutputB[i]
T(N) = N
∑
i=1
A Better Merge Algorithm
MERGESIMPLE2(A,B) 1 fori ←1tolength(A)
2 do
3 if¬BINARYSEARCH(A[1. .i−1],A[i]) 4 thenoutputA[i]
5 fori ←1tolength(B)
6 do
7 if¬BINARYSEARCH(A,B[i])
8 ∧¬BINARYSEARCH(B[1. .i−1],B[i]) 9 thenoutputB[i]
An Even Better Merge Algorithm
Intuition: AandBaresorted e.g.
An Even Better Merge Algorithm
Intuition: AandBaresorted e.g.
A={3,7,12,13,34,37,70,75,80} B={1,5,6,7,34,35,40,41,43}
An Even Better Merge Algorithm
Intuition: AandBaresorted e.g.
A={3,7,12,13,34,37,70,75,80} B={1,5,6,7,34,35,40,41,43}
so just like in BINARYSEARCHI can avoid looking for an element ofx if thefirstelement I see isy >x
High-level algorithm strategy
◮ step through every positioni ofAand every positionjofB; at
Algorithm
A
B
3 7 12 13 34 37 70 75 80
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=1
j=1
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=1
j=1
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=1
j=2
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=1
j=2
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=2
j=2
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=2
j=2
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=2
j=3
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=2
j=3
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=2
j=4
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=2
j=4
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=3
j=5
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=3
j=5
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=4
j=5
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=4
j=5
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=5
j=5
Algorithm
A
B
3 7 12 13 34 37 70 75 80
1 5 6 7 34 35 40 41 43
i=5
j=5
Algorithm (2)
Complexity of
M
ERGE
MERGE(A,B) 1 i,j←1 2 X ← ∅
3 whilei ≤length(A)∨j ≤length(B)
4 do ifi ≤length(A)∧(j>length(B)∨A[i]<B[j]) 5 thenX ←X◦A[i]
6 i ←i+1
7 else X ←X◦B[j]
8 j ←j+1
Complexity of
M
ERGE
MERGE(A,B) 1 i,j←1 2 X ← ∅
3 whilei ≤length(A)∨j ≤length(B)
4 do ifi ≤length(A)∧(j>length(B)∨A[i]<B[j]) 5 thenX ←X◦A[i]
6 i ←i+1
7 else X ←X◦B[j]
8 j ←j+1
Complexity of
M
ERGE
MERGE(A,B) 1 i,j←1 2 X ← ∅
3 whilei ≤length(A)∨j ≤length(B)
4 do ifi ≤length(A)∧(j>length(B)∨A[i]<B[j]) 5 thenX ←X◦A[i]
6 i ←i+1
7 else X ←X◦B[j]
8 j ←j+1
9 returnX
Complexity of
M
ERGE
MERGE(A,B) 1 i,j←1 2 X ← ∅
3 whilei ≤length(A)∨j ≤length(B)
4 do ifi ≤length(A)∧(j>length(B)∨A[i]<B[j]) 5 thenX ←X◦A[i]
6 i ←i+1
7 else X ←X◦B[j]
8 j ←j+1
9 returnX
T(N) = Θ(N)
Complexity of
M
ERGE
MERGE(A,B) 1 i,j←1 2 X ← ∅
3 whilei ≤length(A)∨j ≤length(B)
4 do ifi ≤length(A)∧(j>length(B)∨A[i]<B[j]) 5 thenX ←X◦A[i]
6 i ←i+1
7 else X ←X◦B[j]
8 j ←j+1
9 returnX
T(N) = Θ(N)
Complexity of
M
ERGE
Can we do better? No!
Using
M
ERGE
So now we have alinear-complexitymerge procedure ◮ merges twosortedsequences
Using
M
ERGE
So now we have alinear-complexitymerge procedure ◮ merges twosortedsequences
◮ produces a sorted sequence
Using
M
ERGE
So now we have alinear-complexitymerge procedure ◮ merges twosortedsequences
◮ produces a sorted sequence
We could use that to implement a sort algorithm
Idea
◮ assume that two parts,A
L andAR, of the input sequenceAhave
Using
M
ERGE
So now we have alinear-complexitymerge procedure ◮ merges twosortedsequences
◮ produces a sorted sequence
We could use that to implement a sort algorithm
Idea
◮ assume that two parts,A
L andAR, of the input sequenceAhave
already been sorted
◮ then you can use MERGEto combine them to obtain the final
Using
M
ERGE
So now we have alinear-complexitymerge procedure ◮ merges twosortedsequences
◮ produces a sorted sequence
We could use that to implement a sort algorithm
Idea
◮ assume that two parts,A
L andAR, of the input sequenceAhave
already been sorted
◮ then you can use MERGEto combine them to obtain the final
result
Merge Sort
MERGESORT(A) 1 iflength(A) =1 2 then returnA 3 m← ⌊length(A)/2⌋
4 AL← MERGESORT(A[1. . .m])
Merge Sort
MERGESORT(A) 1 iflength(A) =1 2 then returnA 3 m← ⌊length(A)/2⌋
4 AL← MERGESORT(A[1. . .m])
5 AR ←MERGESORT(A[m+1. . .length(A)]) 6 returnMERGE(AL,AR)
Merge Sort
MERGESORT(A) 1 iflength(A) =1 2 then returnA 3 m← ⌊length(A)/2⌋
4 AL← MERGESORT(A[1. . .m])
5 AR ←MERGESORT(A[m+1. . .length(A)]) 6 returnMERGE(AL,AR)
The complexity of MERGESORTis
Merge Sort
MERGESORT(A) 1 iflength(A) =1 2 then returnA 3 m← ⌊length(A)/2⌋
4 AL← MERGESORT(A[1. . .m])
5 AR ←MERGESORT(A[m+1. . .length(A)]) 6 returnMERGE(AL,AR)
The complexity of MERGESORTis
T(N) =2T(N/2) +O(N)
Divide and Conquer
Divide and Conquer
MERGESORTexemplifies thedivide and conquerstrategy
General strategy: given a problemP on input dataA ◮ dividethe inputAinto partsA
1,A2, . . . ,Ak with|Ai|<|A|=N ◮ solveproblemPfor the individualk parts
Divide and Conquer
MERGESORTexemplifies thedivide and conquerstrategy
General strategy: given a problemP on input dataA ◮ dividethe inputAinto partsA
1,A2, . . . ,Ak with|Ai|<|A|=N ◮ solveproblemPfor the individualk parts
◮ combinethe partial solutions to obtain the solution forA
Complexity analysis
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
6 then returnA[1]◦MERGER(A[2. . .length(A)],B) 7 else returnB[1]◦MERGER(A,B[2. . .length(B)]) ◮ again, this algorithm is a bit incorrect (Exercise: Fix it.)
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
6 then returnA[1]◦MERGER(A[2. . .length(A)],B) 7 else returnB[1]◦MERGER(A,B[2. . .length(B)]) ◮ again, this algorithm is a bit incorrect (Exercise: Fix it.)
The complexity of MERGER is
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
6 then returnA[1]◦MERGER(A[2. . .length(A)],B) 7 else returnB[1]◦MERGER(A,B[2. . .length(B)]) ◮ again, this algorithm is a bit incorrect (Exercise: Fix it.)
The complexity of MERGER is
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
6 then returnA[1]◦MERGER(A[2. . .length(A)],B) 7 else returnB[1]◦MERGER(A,B[2. . .length(B)]) ◮ again, this algorithm is a bit incorrect (Exercise: Fix it.)
The complexity of MERGER is
T(N) =C1+T(N−1) =C1N=O(N)
A Divide-and-Conquer Merge
MERGER(A,B) 1 iflength(A) =0 2 then returnB 3 iflength(B) =0 4 then returnA 5 ifA[1]<B[1]
6 then returnA[1]◦MERGER(A[2. . .length(A)],B) 7 else returnB[1]◦MERGER(A,B[2. . .length(B)]) ◮ again, this algorithm is a bit incorrect (Exercise: Fix it.)
The complexity of MERGER is
Divide-and-Conquer Multiplication
Divide-and-Conquer Multiplication
Going back to multiplication. . .
Divide-and-Conquer Multiplication
Going back to multiplication. . .
x = XL XR and y = YL YR
which meansx =2ℓ/2x
L+xRandy =2ℓ/2yL+yR, so. . .
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
Divide-and-Conquer Multiplication
Going back to multiplication. . .
x = XL XR and y = YL YR
which meansx =2ℓ/2x
L+xRandy =2ℓ/2yL+yR, so. . .
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
we reduced the problem of multiplying two numbers ofℓbits into the problem of multiplyingfournumbers ofℓ/2 bits. . .
Divide-and-Conquer Multiplication
Going back to multiplication. . .
x = XL XR and y = YL YR
which meansx =2ℓ/2x
L+xRandy =2ℓ/2yL+yR, so. . .
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
we reduced the problem of multiplying two numbers ofℓbits into the problem of multiplyingfournumbers ofℓ/2 bits. . .
T(ℓ) =4T(ℓ/2) +O(ℓ)
Divide-and-Conquer Multiplication (2)
Again, we have
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
Divide-and-Conquer Multiplication (2)
Again, we have
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
Divide-and-Conquer Multiplication (2)
Again, we have
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
but notice thatxLyR+xRyL= (xL+xR)(yR+yL)−xLyL−xRyR, so
Divide-and-Conquer Multiplication (2)
Again, we have
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
but notice thatxLyR+xRyL= (xL+xR)(yR+yL)−xLyL−xRyR, so
xy =2ℓxLyL+2ℓ/2((xL+xR)(yR+yL)−xLyL−xRyR) +xRyR
Divide-and-Conquer Multiplication (2)
Again, we have
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
but notice thatxLyR+xRyL= (xL+xR)(yR+yL)−xLyL−xRyR, so
xy =2ℓxLyL+2ℓ/2((xL+xR)(yR+yL)−xLyL−xRyR) +xRyR
Only 3 multiplications: xLyL,(xL+xR)(yR+yL), andxRyR
Divide-and-Conquer Multiplication (2)
Again, we have
xy = (2ℓ/2xL+xR)(2ℓ/2yL+yR)
=2ℓxLyL+2ℓ/2(xLyR+xRyL) +xRyR
but notice thatxLyR+xRyL= (xL+xR)(yR+yL)−xLyL−xRyR, so
xy =2ℓxLyL+2ℓ/2((xL+xR)(yR+yL)−xLyL−xRyR) +xRyR
Only 3 multiplications: xLyL,(xL+xR)(yR+yL), andxRyR
T(ℓ) =3T(ℓ/2) +O(ℓ)
which, as we will see, leads to a much better complexity
T(ℓ) =O(ℓlog23) =O(ℓ1.59
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
1 X ←MERGESORT(A) 2 returnX[⌊length(A)/2⌋]
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
1 X ←MERGESORT(A) 2 returnX[⌊length(A)/2⌋]
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
1 X ←MERGESORT(A) 2 returnX[⌊length(A)/2⌋]
Is it correct? Yes
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
1 X ←MERGESORT(A) 2 returnX[⌊length(A)/2⌋]
Is it correct? Yes
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
1 X ←MERGESORT(A) 2 returnX[⌊length(A)/2⌋]
Is it correct? Yes
How long does it take? T(N) =O(NlogN)
Computing the Median
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
◮ e.g., what is the median ofA={2
,36,5,21,8,13,11,20,5,4,1}?
Idea: first sort, then pick the element in the middle SIMPLEMEDIAN(A)
1 X ←MERGESORT(A) 2 returnX[⌊length(A)/2⌋]
Is it correct? Yes
How long does it take? T(N) =O(NlogN)
Computing the Median (2)
Computing the Median (2)
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
Computing the Median (2)
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
Generalizating, thek-smallestelement of a sequenceAis a valuev ∈Asuch that |A| −k elements ofAare greater or equal tov
E.g.,
Computing the Median (2)
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
Generalizating, thek-smallestelement of a sequenceAis a valuev ∈Asuch that |A| −k elements ofAare greater or equal tov
E.g.,
◮ fork =1, the minimum ofA
Computing the Median (2)
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
Generalizating, thek-smallestelement of a sequenceAis a valuev ∈Asuch that |A| −k elements ofAare greater or equal tov
E.g.,
◮ fork =1, the minimum ofA
◮ fork =⌊|A|/2⌋, the median ofA ◮ what is the6th smallestelement of
Computing the Median (2)
Themedianof a sequenceAis a valuem∈Asuch that half the values inAare smaller thanmand half are bigger thanm
Generalizating, thek-smallestelement of a sequenceAis a valuev ∈Asuch that |A| −k elements ofAare greater or equal tov
E.g.,
◮ fork =1, the minimum ofA
◮ fork =⌊|A|/2⌋, the median ofA ◮ what is the6th smallestelement of
A={2,36,5,21,8,13,11,20,5,4,1}?
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
E.g.,A={2,36,5,21,8,13,11,20,5,4,1} and we must compute the 7th smallest value inA we pick a splitting value, sayv =5
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
E.g.,A={2,36,5,21,8,13,11,20,5,4,1} and we must compute the 7th smallest value inA we pick a splitting value, sayv =5
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
E.g.,A={2,36,5,21,8,13,11,20,5,4,1} and we must compute the 7th smallest value inA we pick a splitting value, sayv =5
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
E.g.,A={2,36,5,21,8,13,11,20,5,4,1} and we must compute the 7th smallest value inA we pick a splitting value, sayv =5
AL={2,4,1} Av ={5,5} AR ={36,21,8,13,11,20}
k-Smallest Element
Idea: we split the sequenceAin three parts based on achosen value v ∈A
◮ A
Lcontains the set of elements that areless than v
◮ A
vcontains the set of elements that areequal to v
◮ A
Rcontains the set of elements that aregreater then v
E.g.,A={2,36,5,21,8,13,11,20,5,4,1} and we must compute the 7th smallest value inA we pick a splitting value, sayv =5
AL={2,4,1} Av ={5,5} AR ={36,21,8,13,11,20}
k-Smallest Element (2)
We useselect(A,k)to denote the k-smallest element ofA
select(A,k) =
select(AL,k) ifk ≤ |AL|
k-Smallest Element (2)
We useselect(A,k)to denote the k-smallest element ofA
select(A,k) =
select(AL,k) ifk ≤ |AL|
v if|AL|<k ≤ |AL|+|Av| select(AR,k− |AL| − |Av|) ifk >|AL|+|Av|
k-Smallest Element (2)
We useselect(A,k)to denote the k-smallest element ofA
select(A,k) =
select(AL,k) ifk ≤ |AL|
v if|AL|<k ≤ |AL|+|Av| select(AR,k− |AL| − |Av|) ifk >|AL|+|Av|
ComputingAL,Av, andARtakesO(N)steps
k-Smallest Element (2)
We useselect(A,k)to denote the k-smallest element ofA
k-Smallest Element (2)
We useselect(A,k)to denote the k-smallest element ofA
select(A,k) =