Subhash Suri UC Santa Barbara
Divide and Conquer
•
A general paradigm for algorithm design; inspired
by emperors and colonizers.
•
Three-step process:
1.
Divide
the problem into smaller problems.
2.
Conquer
by solving these problems.
3.
Combine
these results together.
•
Examples: Binary Search, Merge sort, Quicksort
Subhash Suri UC Santa Barbara
Merge Sort: Illustration
2 5 4 6 1 3 2 6
6 3 2 1 6 5 4 2
1 2 2 3 4 5 6 6 5 2 4 6 1 3 2 6
5 2 4 6 1 3 2 6
5 2 4 6 1 3 2 6
Subhash Suri UC Santa Barbara
Merge Sort
•
Sort an
unordered
array of numbers
A
.
Merge-Sort
(A, p, q)
1.
if
p
≥
q
return
A
;
2.
r
=
b
(p
+
q)/2
c
3.
Merge-Sort
(
A, p, r
)
4.
Merge-Sort
(
A, r
+ 1
, q
)
5.
MERGE
(A, p, q, r)
Merge sort:
• Divide: Split array of size n into two subarrays of size n/2
• Conquer: Two recursive calls on subarrays of size n/2
• Combine: Merge two sorted subarrays to create sorted array of size n
Let T(n) = running time of merge sort on array of size n
T(n) = Tdivide(n) + Tconquer(n) + Tcombine(n)
T(n) = θ(1) + [T(n/2) + T(n/2)] + θ(n) T(n) = 2 T(n/2) + θ(n)
• Number of recursive subproblems = 2
• Size of each subproblem = n/2
• Time for all the non-recursive steps = θ(n)
Later we’ll see how to solve these kinds of recurrence equations to determine T(n)
Subhash Suri UC Santa Barbara
Binary Search
•
Search for
x
in a
sorted
array
A
.
Binary-Search
(A, p, q, x)
1.
if
p > q
return -1;
2.
r
=
b
(p
+
q)/2
c
3.
if
x
=
A
[
r
]
return
r
4.
else if
x < A[r]
Binary-Search
(A, p, r, x)
5.
else Binary-Search
(A, r
+ 1, q, x)
Binary search: Find(27)
0 1 2 3 4 5 6 7 8 9 10 13 18 24 27 34 41 49 55 60 66 72
27 < 41 0 1 2 3 4
13 18 24 27 34 27 > 24
3 4 27 34 found
Let T(n) = running time of binary search on array of size n T(n) = Tdivide(n) + Tconquer(n) + Tcombine(n)
T(n) = θ(1) + T(n/2) + 0 T(n) = T(n/2) + θ(1)
• Number of recursive subproblems = 1
• Size of each subproblem = n/2
• Time for all the non-recursive steps = θ(1)
Later we’ll see how to solve these kinds of recurrence equations to determine T(n)
Subhash Suri UC Santa Barbara
Multiplying Numbers
•
We want to multiply two
n
-bit numbers. Cost is
number of elementary bit steps.
•
Grade school method has
Θ(
n
2)
cost.:
xxxxxxxx xxxxxxxx xxxxxxxxx xxxxxxxxx
xxxxxxxxx
xxxxxxxxxxxxxxxx ..
.
Polynomial multiplication and Large integer multiplication:
Given two polynomials:
P = 5x3 + 7x2 + 6x + 2 ⇒ [5, 7, 6, 2] = [p3, p2, p1, p0]
Q = x3 – 8x2 + 9x – 1 ⇒ [1, –8, 9, –1] = [q3, q2, q1, q0]
P*Q = (5x3 + 7x2 + 6x + 2)(x3 – 8x2 + 9x – 1)
Alternatively, given two large integers:
P = 9863 = 9x3 + 8x2 + 6x + 3 where x = 10
Q = 7245 = 7x3 + 2x2 + 4x + 5 where x = 10
P*Q = (9863)(7245) = (9x3 + 8x2 + 6x + 3)(7x3 + 2x2 + 4x + 5)
where x = 10
Subhash Suri UC Santa Barbara
Why Bother?
•
Doesn’t hardware provide multiply? It is fast,
optimized, and free. So, why bother?
•
True for numbers that fit in one computer word.
But what if numbers are very large.
•
Cryptography (encryption, digital signatures)
uses big number “keys.” Typically 256 to 1024
bits long!
•
n
2multiplication too slow for such large numbers.
•
Karatsuba’s (1962) divide-and-conquer scheme
Divide-and-conquer Algorithm 1 for Polynomial multiplication:
P = 5x3 + 7x2 + 6x + 2 = (5x + 7)x2 + (6x + 2) = Ax2 + B
Q = x3 – 8x2 + 9x – 1 = (x – 8)x2 + (9x – 1) = Cx2 + D
A = 5x + 7 ⇒ [5, 7] B = 6x + 2 ⇒ [6, 2]
C = x – 8 ⇒ [1, –8]
D = 9x – 1 ⇒ [9, –1]
Let n = number of terms in P and Q P = Axn/2 + B
Q = Cxn/2 + D
A, B, C, D are polynomials with n/2 terms
P*Q = (Axn/2 + B)(Cxn/2 + D) = (AC)xn + (BC + AD)xn/2 + (BD)
Four recursive subproblems: A*C
B*C
A*D
B*D
Let T(n) = running time for the preceding Algorithm 1
T(n) = 4 T(n/2) + θ(n)
• Number of recursive subproblems = 4
• Size of each subproblem = n/2
• Time for all the non-recursive steps = θ(n):
o adding n-term polynomials
o xn and xn/2 which are just shifts (not products) o also note the final product P*Q has < 2n terms
Again, later we’ll see how to solve these kinds of recurrence equations to determine T(n)
Subhash Suri UC Santa Barbara
Karatsuba’s Algorithm
•
Let
X
and
Y
be two
n
-bit numbers. Write
X
=
a b
Y
=
c d
•
a, b, c, d
are
n/
2
bit numbers. (Assume
n
= 2
k.)
XY
= (
a
2
n/2+
b
)(
c
2
n/2+
d
)
Subhash Suri UC Santa Barbara
An Example
•
X
= 4729
Y
= 1326
.
•
a
= 47;
b
= 29
c
= 13;
d
= 26
.
•
ac
= 47
∗
13 = 611
•
ad
= 47
∗
26 = 1222
•
bc
= 29
∗
13 = 377
•
bd
= 29
∗
26 = 754
•
XY
= 6110000 + 159900 + 754
Subhash Suri UC Santa Barbara
Karatsuba’s Algorithm
•
This is D&C: Solve 4 problems, each of size
n/2
;
then perform
O(n)
shifts to multiply the terms by
2
nand
2
n/2.
•
We can write the recurrence as
T
(n) = 4T
(n/2) +
O
(n)
Divide-and-conquer Algorithm 2 for Polynomial multiplication
(Karatsuba’s algorithm)
:As before,
P*Q = (Axn/2 + B)(Cxn/2 + D) = (AC)xn + (BC + AD)xn/2 + (BD)
This time we write (BC + AD) = (A+B)(C+D) – (AC) – (BD)
So only three recursive calls: A*C
B*D
(A+B)*(C+D)
Again stop the recursion when n = 1
Let T(n) = running time for the preceding Algorithm 2
T(n) = 3 T(n/2) + θ(n)
• Number of recursive subproblems = 3
• Size of each subproblem = n/2
• Time for all the non-recursive steps = θ(n)
Again, later we will see how to solve these kinds of recurrence equations to determine T(n)
Majority element problem:
Given array A of size n, does there exist any value M that appears more than n/2 times in array A?
Majority element algorithm:
• Phase 1: Use divide-and-conquer to find candidate value M
• Phase 2: Check if M really is a majority element, θ(n) time, simple loop
Phase 1 details:
• Divide:
o Group the elements of A into n/2 pairs
o If n is odd, there is one unpaired element, x
Check if this x is majority element of A
If so, then return x, but otherwise discard x
o Compare each pair (y, z)
If (y==z) keep y and discard z If (y!=z) discard both y and z
o So we keep ≤ n/2 elements
• Conquer: One recursive call on subarray of size ≤ n/2
Example:
A = [7, 7, 5, 2, 5, 5, 4, 5, 5, 5, 7]
(7, 7) (5, 2) (5, 5) (4, 5) (5, 5) (7) A = [7, 5, 5]
(7, 5) (5) ⇒ return 5 (candidate, also majority)
Example:
A = [1, 2, 3, 1, 2, 3, 1, 2, 9, 9]
(1, 2) (3, 1) (2, 3) (1, 2) (9, 9)
A = [9] ⇒ return 9 (candidate, but not majority)
Let T(n) = running time of Phase 1 on array of size n
T(n) = T(n/2) + θ(n)
• Number of recursive subproblems = 1
• Size of each subproblem = n/2 [worst-case] • Time for all the non-recursive steps = θ(n)
Later we’ll see how to solve these kinds of recurrence equations to determine T(n)
Subhash Suri UC Santa Barbara
Recurrence Solving: Review
•
T
(
n
) = 2
T
(
n/
2) +
cn
, with
T
(1) = 1.
•
By term expansion.
T
(
n
) = 2
T
(
n/
2) +
cn
= 2
¡
2
T
(
n/
2
2) +
cn/
2
¢
+
cn
= 2
2T
(
n/
2
2) + 2
cn
= 2
2¡
2
T
(
n/
2
3) +
cn/
2
2¢
+ 2
cn
= 2
3T
(
n/
2
3) + 3
cn
...
= 2
iT
(
n/
2
i) +
icn
•
Set
i
= log
2n
. Use
T
(1) = 1.
Subhash Suri UC Santa Barbara
The Tree View
•
T
(
n
) = 2
T
(
n/
2) +
cn
, with
T
(1) = 1.
T(n/4) T(n/4) cn/8 cn/8 cn/8 cn/4 cn/4 T(n/8) cn/2 cn/2 T(n/2) T(n/2) T(n) cn T(n/4)
cn/4 T(n/4)cn/4
cn/8 cn/8 cn/8 cn/8 cn/8 cn Total Cost cn
2(cn/2) = cn
4(cn/4) = cn
8(cn/8) =
•
#
leaves
=
n
;
#
levels
= log
n
.
Subhash Suri UC Santa Barbara
Solving By Induction
•
Recurrence:
T
(
n
) = 2
T
(
n/
2) +
cn
.
•
Base case:
T
(1) = 1
.
•
Claim:
T
(
n
) =
cn
log
n
+
cn
.
T
(
n
) = 2
T
(
n/
2) +
cn
= 2 (
c
(
n/
2) log(
n/
2) +
cn/
2) +
cn
=
cn
(log
n
−
1 + 1) +
cn
Subhash Suri UC Santa Barbara
More Examples
•
T
(
n
) = 4
T
(
n/
2) +
cn
,
T
(1) = 1
.
Level Work
0
1
2
3
i
cn
4i cn /2i
= 2 cni 4cn/2 = 2cn
16cn/4 = 4cn n/4
n/2
n
Subhash Suri UC Santa Barbara
More Examples
Level Work
0
1
2
3
i
cn
4i cn /2i
= 2 cni 4cn/2 = 2cn
16cn/4 = 4cn n/4
n/2
n
n/8
•
Stops when
n/
2
i= 1, and
i
= log
n
.
Subhash Suri UC Santa Barbara
By Term Expansion
T
(
n
) = 4
T
(
n/
2) +
cn
= 4
2T
(
n/
2
2) + 2
cn
+
cn
= 4
3T
(
n/
2
3) + 2
2cn
+ 2
cn
+
cn
...
= 4
iT
(
n/
2
i) +
cn
¡
2
i−1+ 2
i−2+
. . .
+ 2 + 1
¢
= 4
iT
(
n/
2
i) + 2
icn
•
Terminates when
2
i=
n
, or
i
= log
n
.
•
4
i= 2
i×
2
i=
n
×
n
=
n
2.
Subhash Suri UC Santa Barbara
More Examples
T
(
n
) = 2
T
(
n/
4) +
√
n,
T
(1) = 1
.
T
(n) = 2T
(n/4) +
√
n
= 2
³
2T
(n/4
2) +
p
n/4
´
+
√
n
= 2
2T
(n/4
2) + 2
√
n
= 2
2³
2T
(n/4
3) +
p
n/4
2´
+ 2
√
n
= 2
3T
(n/4
3) + 3
√
n
...
Subhash Suri UC Santa Barbara
More Examples
•
Terminates when
4
i=
n
, or when
i
= log
4n
=
log2 nlog2 4
=
12log
n
.
T
(
n
) = 2
12 logn+
√
n
log
4
n
Subhash Suri UC Santa Barbara
Master Method
T
(n) =
aT
³
n
b
´
+
f
(n)
n n/b n/b2 a children a a a a a a
a f(n/b )2 2
a f(n/b ) af(n/b)
i i
f(n)
Subhash Suri UC Santa Barbara
Master Method
n
n/b
n/b2
a children
a
a a
a
a a
a f(n/b )2 2
a f(n/b ) af(n/b)
i i
f(n)
Total Cost
•
#
children multiply by factor
a
at each level.
•
Number of leaves is
a
logbn=
n
logb a. Verify by
Subhash Suri UC Santa Barbara
Master Method
•
By recursion tree, we get
T
(
n
) = Θ(
n
logb a) +
log
X
b n−1i=0
a
if
³
n
b
i´
•
Let
f
(n) = Θ(n
plog
kn)
, where
p, k
≥
0
.
•
Important:
a
≥
1
and
b >
1
are constants.
•
Case I:
p <
log
ba.
n
logb agrows faster than
f
(
n
).
Subhash Suri UC Santa Barbara
Master Method
•
By recursion tree, we get
T
(
n
) = Θ(
n
logb a) +
log
X
b n−1i=0
a
if
³
n
b
i´
•
Let
f
(n) = Θ(n
plog
kn)
, where
p, k
≥
0
.
•
Case II:
p
= log
ba.
Both terms have same growth rates.
Subhash Suri UC Santa Barbara
Master Method
•
By recursion tree, we get
T
(
n
) = Θ(
n
logb a) +
log
X
b n−1i=0
a
if
³
n
b
i´
•
Let
f
(n) = Θ(n
plog
kn)
, where
p, k
≥
0
.
•
Case III:
p >
log
ba.
n
logb ais slower than
f
(
n
).
Subhash Suri UC Santa Barbara
Applying Master Method
•
Merge Sort:
T
(
n
) = 2
T
(
n/
2) + Θ(
n
).
a
=
b
= 2,
p
= 1, and
k
= 0. So
log
ba
= 1, and
p
= log
ba
. Case II applies, giving us
T
(n) = Θ(n
log
n)
•
Binary Search:
T
(
n
) =
T
(
n/
2) + Θ(1).
a
= 1
, b
= 2
, p
= 0, and
k
= 0. So
log
ba
= 0, and
p
= log
ba
. Case II applies, giving us
Subhash Suri UC Santa Barbara
Applying Master Method
•
T
(
n
) = 2
T
(
n/
2) + Θ(
n
log
n
).
a
=
b
= 2,
p
= 1, and
k
= 1.
p
= 1 = log
ba
, and Case
II applies.
T
(n) = Θ(n
log
2n)
•
T
(
n
) = 7
T
(
n/
2) + Θ(
n
2).
a
= 7
, b
= 2,
p
= 2, and
log
b2 = log 7
>
2. Case I
applied, and we get
Subhash Suri UC Santa Barbara
Applying Master Method
•
T
(
n
) = 4
T
(
n/
2) + Θ(
n
2√
n
).
a
= 4
, b
= 2,
p
= 2
.
5, and
k
= 0. So
log
ba
= 2, and
p >
log
ba
. Case III applies, giving us
T
(n) = Θ(n
2√
n)
•
T
(
n
) = 2
T
(
n/
2) + Θ
³
nlogn
´
.
Subhash Suri UC Santa Barbara
Matrix Multiplication
•
Multiply two
n
×
n
matrices:
C
=
A
×
B
.
•
Standard method:
C
ij=
P
nk=1A
ik×
B
kj.
•
This takes
O(n)
time per element of
C
, for the
total cost of
O(n
3)
to compute
C
.
•
This method, known since Gauss’s time, seems
hard to improve.
•
A very surprising discovery by Strassen (1969)
broke the
n
3asymptotic barrier.
Subhash Suri UC Santa Barbara
Divide and Conquer
•
Let
A, B
be two
n
×
n
matrices. We want to
compute the
n
×
n
matrix
C
=
AB
.
A
=
µ
a
11a
12a
21a
22¶
B
=
µ
b
11b
12b
21b
22¶
C
=
µ
c
11c
12c
21c
22¶
Subhash Suri UC Santa Barbara
Divide and Conquer
•
The product matrix can be written as:
c
11=
a
11b
11+
a
12b
21c
12=
a
11b
12+
a
12b
22c
21=
a
21b
11+
a
22b
21c
22=
a
21b
12+
a
22b
22•
Recurrence for this D&C algorithm is
T
(
n
) = 8
T
(
n/
2) +
O
(
n
2).
Subhash Suri UC Santa Barbara
Strassen’s Algorithm
•
Strassen chose these submatrices to multiply:
P
1= (
a
11+
a
22)(
b
11+
b
22)
P
2= (
a
21+
a
22)
b
11P
3=
a
11(
b
12−
b
22)
P
4=
a
22(
b
21−
b
11)
P
5= (
a
11+
a
12)
b
22P
6= (
a
21−
a
11)(
b
11+
b
12)
Subhash Suri UC Santa Barbara
Strassen’s Algorithm
•
Then,
c
11=
P
1+
P
4−
P
5+
P
7c
12=
P
3+
P
5c
21=
P
2+
P
4c
22=
P
1+
P
3−
P
2+
P
6•
Recurrence for this algorithm is
Subhash Suri UC Santa Barbara
Strassen’s Algorithm
•
The recurrence
T
(
n
) = 7
T
(
n/
2) +
O
(
n
2)
.
solves to
T
(
n
) =
O
(
n
log2 7) =
O
(
n
2.81).
•
Ever since other researchers have tried other
products to beat this bound.
•
E.g. Victor Pan discovered a way to multiply two
70
×
70
matrices using
143
,
640
multiplications.
•
Using more advanced methods, the current best
algorithm for multiplying two
n
×
n
matrices runs
Subhash Suri UC Santa Barbara
Quick Sort Algorithm
•
Simple, fast, widely used in practice.
•
Can be done “in place;” no extra space.
•
General Form:
1.
Partition:
Divide into two subarrays,
L
and
R
;
elements in
L
are all
smaller
than those in
R
.
2.
Recurse:
Sort
L
and
R
recursively.
3.
Combine:
Append
R
to the end of
L
.
Subhash Suri UC Santa Barbara
Partition
•
Partition returns the index of the cell containing
the pivot in the reorganized array.
11 4 9 7 3 10 2 6 13 21 8
Subhash Suri UC Santa Barbara
Quick Sort Algorithm
•
QuickSort
(
A, p, q
)
sorts the subarray
A
[
p
· · ·
q
]
.
•
Initial call with
p
= 0
and
q
=
n
−
1.
QuickSort(A, p, q
)
if
p
≥
q
then return
i
←
random(
p, q
)
Subhash Suri UC Santa Barbara
Analysis of QuickSort
•
Lucky Case:
Each Partition splits array in halves.
We get
T
(n) = 2T
(n/2) + Θ(n) = Θ(n
log
n)
.
•
Unlucky Case:
Each partition gives unbalanced
split. We get
T
(
n
) =
T
(
n
−
1) + Θ(
n
) = Θ(
n
2).
•
In worst case, Quick Sort as bad as BubbleSort.
The worst-case occurs when the list is already
sorted, and the last element chosen as pivot.
•
But, while BubbleSort
always
performs poorly on
certain inputs, because of
random pivot,
Subhash Suri UC Santa Barbara
Analyzing QuickSort
•
T
(
n
)
: runtime of randomized QuickSort.
•
Assume all elements are distinct.
•
Recurrence for
T
(n)
depends on two subproblem
sizes, which depend on random partition element.
•
If pivot is
i
smallest
element, then exactly
(
i
−
1)
items in
L
and
(
n
−
i
)
in
R
. Call it an
i
-split.
•
What’s the probability of
i
-split?
Subhash Suri UC Santa Barbara
Solving the Recurrence
T
(
n
) =
n
X
i=1
1
n
(runtime with
i
-split) +
n
+ 1
=
1
n
n
X
i=1
(
T
(
i
−
1) +
T
(
n
−
i
)) +
n
+ 1
=
2
n
n
X
i=1
T
(
i
−
1) +
n
+ 1
=
2
n
n
X
−1i=0
Subhash Suri UC Santa Barbara
Solving the Recurrence
•
Multiply both sides by
n
. Subtract the same
formula for
n
−
1
.
nT
(
n
) = 2
n
X
−1i=0
T
(
i
) +
n
2+
n
(
n
−
1)
T
(
n
−
1) = 2
n
X
−2i=0
Subhash Suri UC Santa Barbara
Solving the Recurrence
nT
(
n
) = (
n
+ 1)
T
(
n
−
1) + 2
n
T
(
n
)
n
+ 1
=
T
(
n
−
1)
n
+
2
n
+ 1
=
T
(
n
−
2)
n
−
1
+
2
n
+
2
n
+ 1
...
=
T
(2)
3
+
nX
i=32
i
= Θ(1) + 2 ln
n
Subhash Suri UC Santa Barbara
Median Finding
•
Median
of
n
items is the item with rank
n/
2
.
•
Rank of an item is its position in the list if the
items were sorted in ascending order.
•
Rank
i
item also called
ith statistic
.
•
Example:
{16
,
5
,
30
,
8
,
55}.
•
Popular statistics are
quantiles
: items of rank
n/4, n/2,
3n/4
.
Subhash Suri UC Santa Barbara
Median Finding
•
After spending
O
(
n
log
n
)
time on sorting, any
rank can be found in
O
(
n
)
time.
Subhash Suri UC Santa Barbara
Min and Max Finding
•
We can find items of rank 1 or
n
in
O
(
n
)
time.
minimum
(A)
min
←
A
[0]
for
i
= 1
to
n
−
1
do
if
min
> A
[
i
]
then
min
←
A
[
i
];
return
min
•
The algorithm
minimum
finds the smallest
(rank 1) item in
O
(
n
)
time.
Subhash Suri UC Santa Barbara
Both Min and Max
•
Find both min and max using
3
n/
2
comparisons.
MIN-MAX
(A)
if
|
A
|
= 1, then return
min = max =
A
[0]
Divide
A
into two equal subsets
A
1, A
2(min
1,
max
1) :=
MIN-MAX
(
A
1)
(min
2,
max
2) :=
MIN-MAX
(
A
2)
if
min
1≤
min
2then return
min = min
1else return
min = min
2Subhash Suri UC Santa Barbara
Both Min and Max
•
The recurrence for this algorithm is
T
(
n
) = 2
T
(
n/
2) + 2
.
Subhash Suri UC Santa Barbara
Finding Item of Rank
k
•
Direct extension of min/max finding to rank
k
item will take
Θ(
kn
)
time.
•
In particular, finding the median will take
Ω(
n
2)
time, which is worse than sorting.
•
Median can be used as a perfect pivot for
(deterministic) quick sort.
•
But only if found faster than sorting itself.
•
We present a linear time algorithm for selecting
Subhash Suri UC Santa Barbara
Linear Time Selection
SELECT
(k
)
1.
Divide items into
b
n/
5
c
groups of 5 each.
2.
Find the
median
of each group (using sorting).
3.
Recursively find
median
of
b
n/5
c
group medians.
4.
Partition using median-of-median as pivot.
5.
Let low side have
s
, and high side have
n
−
s
items.
6.
If
k
≤
s
, call
select
(
k
)
on low side; otherwise, call
Subhash Suri UC Santa Barbara
Illustration
• Divide items into bn/5c groups of 5 items each.
• Find the median of each group (using sorting).
• Use SELECT to recursively find the median of the bn/5c group medians.
Group 1 Group 2 Group 3 Group 4 Group 5 Group 6
medians
x = median of
Subhash Suri UC Santa Barbara
Illustration
• Partition the input by using this median-of-median as pivot.
• Suppose low side of the partition has s elements, and high side has n − s elements.
• If k ≤ s, recursively call SELECT(k) on low side; otherwise, recursively call SELECT(k − s) on high side.
Items => x
Group 6
Subhash Suri UC Santa Barbara
Recurrence
•
For runtime analysis, we bound the number of
items
≥
x
, the median of medians.
•
At least half the medians are
≥
x
.
•
At least half of the
b
n/
5c
groups contribute at
least 3 items to the high side. (Only the last
group can contribute fewer.
•
Thus, items
≥
x
are at least
3
³
n
10
−
2
´
≥
3n
10
−
6.
Subhash Suri UC Santa Barbara
Recurrence
•
Recursive call to
select
is on size
≤
7
n/
10 + 6
.
•
Let
T
(
n
) =
worst-case complexity of
select
.
•
Group medians, and partition take
O(n)
time.
•
Step 3 has a recursive call
T
(
n/
5), and Step 5 has
a recursive call
T
(7
n/
10 + 6).
•
Thus, we have the recurrence:
T
(n)
≤
T
(
n
5
) +
T
(
7n
Subhash Suri UC Santa Barbara
Recurrence
T
(n)
≤
T
(
n
5
) +
T
(
7n
10
+ 6) +
O(n)
•
Inductively verify that
T
(
n
)
≤
cn
for some
constant
c
.
T
(
n
)
≤
c
(
n/
5) +
c
(7
n/
10 + 6) +
O
(
n
)
≤
9
cn/
10 + 6
c
+
O
(
n
)
≤
cn
•
In above, choose
c
so that
c(n/10
−
6)
beats the
Subhash Suri UC Santa Barbara
Convex Hulls
1.
Convex hulls
are to CG what sorting is to
discrete algorithms.
2.
First order
shape approximation
. Invariant under
rotation and translation.
p
Subhash Suri UC Santa Barbara
Convex Hulls
•
Many aplications in
robotics, shape analysis, line
fitting
etc.
•
Example:
if
CH
(
P
1)
∩
CH
(
P
2) =
∅, then objects
P
1and
P
2do not intersect.
•
Convex Hull Problem:
Subhash Suri UC Santa Barbara
Divide and Conquer
A B
CH(A) CH(B)
Upper Tangent
•
Sort points by
X
-coordinates.
•
Divide points into equal halves
A
and
B
.
•
Recursively
compute
CH
(
A
)
and
CH
(
B
)
.
Subhash Suri UC Santa Barbara
Merging Convex Hulls
Lower Tangent
•
a
=
rightmost point of
CH
(
A
)
.
•
b
=
leftmost point of
CH
(
B
).
•
while
ab
not lower tangent of
CH
(A)
and
CH
(B
)
do
1.
while
ab
not lower tangent to
CH
(
A
)
set
a
=
a
−
1
(move
a
CW);
2.
while
ab
not lower tangent to
CH
(
B
)
set
b
=
b
+ 1
(move
b
CCW);
Subhash Suri UC Santa Barbara
Tangent Finding
a
b
Subhash Suri UC Santa Barbara
Analysis of D&C
A B
CH(A) CH(B)
Upper Tangent