Академический Документы
Профессиональный Документы
Культура Документы
Analysis of Algorithms
Lecture Notes 5
- Divide and Conquer Algorithms
Divide-and-Conquer
The most-well known algorithm design strategy:
1. Divide instance of problem into two or more
smaller instances
2
Divide-and-Conquer Technique (cont.)
a problem of size n
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
A. Levitin “Introduction to the Design &
Analysis of Algorithms,” 3rd ed., Ch. 5
3
©2012 Pearson Education, Inc. Upper
Saddle River, NJ. All Rights Reserved.
Divide-and-Conquer Examples
• Binary search: decrease-by-half (or degenerate
divide&conq.)
5
Fake-Coin Problem
• Problem: Among n identical-
looking coins, one is fake
• Have a balance scale that can
compare any two sets of coins
• Suppose that fake one is lighter
than the genuine one
• Which coin is the fake one?
Fake-Coin Problem
• Decrease-by-a-constant-factor strategy
– Divide n coins into two piles of n/2 coins each
– Leave one extra coin aside if n is odd
– Put the two piles on the scale
– If the piles weigh the same, the coin put aside
must be fake
– Otherwise, proceed in the same manner with
the lighter pile
Fake-Coin Problem
• Input size: n (number of coins)
• Basic operation: Weighting
• Worst-case
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 8 9
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 15
Analysis of Mergesort
• All cases have same efficiency: Θ(n log n) -> Show by
master theorem and also using backward substitution
method
A[i]p A[i]p
• Exchange the pivot with the last element in the first (i.e., )
subarray — the pivot is now in its final position
• Sort the two subarrays recursively
A. Levitin “Introduction to the Design &
Analysis of Algorithms,” 3rd ed., Ch. 5
17
©2012 Pearson Education, Inc. Upper
Saddle River, NJ. All Rights Reserved.
Quicksort Algorithm
Hoare’s Partitioning Algorithm
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5
©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 19
Quicksort Example
5 3 1 9 8 2 4 7
• Improvements:
– better pivot selection: median of three partitioning
– switch to insertion sort on small subfiles
– elimination of recursion
These combine to 20-25% improvement
Efficiency: Θ(n)
A. Levitin “Introduction to the Design &
Analysis of Algorithms,” 3rd ed., Ch. 5
22
©2012 Pearson Education, Inc. Upper
Saddle River, NJ. All Rights Reserved.
Binary Tree Algorithms (cont.)
Efficiency: Θ(n)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 23
Multiplication of Large Integers
Consider the problem of multiplying two (large) n-digit
integers represented by arrays of their digits such as:
A = 12345678901357986429 B = 87654321284820912836
The grade-school algorithm:
a1 a2 … an
b1 b2 … bn
(d10) d11d12 … d1n
(d20) d21d22 … d2n
…………………
(dn0) dn1dn2 … dnn
26
Integer Multiplication
• Note that in these solutions multiplying by
powers of 10 are not considered to be
separate multiplications because they are
simply shift operations.
Divide and Conquer Algorithms
• Similarly for many problems both brute force
and divide&conquer solutions exist.
• For example computing nth power of a given
number a.
Computing integer power an
• Brute force algorithm
Algorithm power(a,n)
value 1 a n a a a
for i 1 to n do
value value a
return value
• Complexity: O(n)
Computing integer power an
• Divide and Conquer algorithm
Algorithm power(a,n) n2 n
a a 2
if n is even
if (n = 1) a n
n
n
return a a 2 a 2 a if n is odd
partial power(a,floor(n/2))
if n mod 2 = 0
return partial partial
else
return partial partial a
• Complexity: T(n) = T(n/2) + O(1) T(n) is O(log n)
Strassen’s Matrix Multiplication
Strassen observed [1969] that the product of
two matrices can be computed as follows:
Let A and B be two n × n matrices where n is a
power of 2.
31
Formulas for Strassen’s Algorithm
M1 = (A00 + A11) (B00 + B11)
Number of multiplications:
M(n) = 7M(n/2), M(1) = 1
Solution: M(n) = 7log 2n ≈ n2.807 vs. n3 of brute-force
alg.
Algorithms with better asymptotic efficiency are
known but they are even more complex.
33
Closest-Pair Problem
• Remember the problem
• Brute-force solution ?
• Efficiency of brute force approach ?
Closest-Pair Problem by Divide-and-Conquer
Step 1 Divide the points given into two subsets Pl
and Pr by a vertical line x = m so that half the
points lie to the left or on the line and half the
points lie to the right or on the line.
x=m
dl
d
r
d d 35
Closest Pair by Divide-and-Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and right
subsets.
Step 3 Set d = min{dl, dr}
We can limit our attention to the points in the symmetric
vertical strip S of width 2d as possible closest pair. (The
points are stored and processed in increasing order of
their y coordinates.)
Step 4 Scan the points in the vertical strip S from the lowest up.
For every point p(x,y) in the strip, inspect points in
in the strip that may be closer to p than d.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc.
Upper Saddle River, NJ. All Rights Reserved.
36
Efficiency of the Closest-Pair Algorithm
37
Summary
Divide-and-Conquer
• Divide-and conquer is a general
algorithm design paradigm:
– Divide: divide the input data S in
two or more disjoint subsets S1, S2,
…
– Recur: solve the subproblems
recursively
– Conquer: combine the solutions for
S1, S2, …, into a solution for S
• The base case for the recursion
are subproblems of constant size
• Analysis can be done using
recurrence equations