CH 05
CH 05
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 1
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 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 2
Divide-and-Conquer Examples
Sorting: mergesort and quicksort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 3
General Divide-and-Conquer Recurrence
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4
General Divide-and-Conquer Recurrence
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 5
Mergesort
Split array A[0..n-1] in two about equal halves and make
copies of each half in arrays B and C
Sort arrays B and C recursively
Merge sorted arrays B and C into array A as follows:
• Repeat the following until no elements remain in one of
the arrays:
– compare the first elements in the remaining
unprocessed portions of the arrays
– copy the smaller of the two into A, while
incrementing the index indicating the unprocessed
portion of that array
• Once all elements in one of the arrays are processed,
copy the remaining unprocessed elements from the
other array into A.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 6
Pseudocode of Mergesort
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 7
Pseudocode of Merge
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 8
Mergesort Example
8 3 2 9 7 1 5 4
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. 9
Analysis of Mergesort
All cases have same efficiency: Θ(n log 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. 10
A. Levitin “Introduction to the Design & Analysis of
Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. 11
Upper Saddle River, NJ. All Rights Reserved.
Quicksort
Select a pivot (partitioning element) – here, the first element
Rearrange the list so that all the elements in the first s
positions are smaller than or equal to the pivot and all the
elements in the remaining n-s positions are larger than or
equal to the pivot (see next slide for an algorithm)
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 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 12
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. 13
Quicksort Example
5 3 1 9 8 2 4 7
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 14
Analysis of Quicksort
Best case: split in the middle — Θ(n log n)
Worst case: sorted array! — Θ(n2)
Average case: random arrays — Θ(n log n)
Improvements:
• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
• elimination of recursion
These combine to 20-25% improvement
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 16
Binary Tree Algorithms
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. 17
Binary Tree Algorithms (cont.)
TL TR
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. 18
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
Efficiency: n 2
one-digit multiplications
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
First Divide-and-Conquer Algorithm
A small example: A B where A = 2135 and B = 4014
A = (21·102 + 35), B = (40 ·102 + 14)
So, A B = (21 ·102 + 35) (40 ·102 + 14)
= 21 40 ·104 + (21 14 + 35 40) ·102 + 35 14
2135 4014
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 22
Strassen’s Matrix Multiplication
M1 + M 4 - M 5 + M7 M 3 + M5
=
M2 + M4 M1 + M 3 - M 2 + M6
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
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 = nlog 27 ≈ n2.807 vs. n3 of brute-force alg.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 25
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
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 26
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. There can be
no more than 5 such points following p on the strip list!
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 27
Efficiency of the Closest-Pair 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. 28
Quickhull Algorithm
Convex hull: smallest convex set that includes given points
Assume points are sorted by x-coordinate values
P1
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 29
Efficiency of Quickhull Algorithm
Finding point farthest away from line P1P2 can be done in
linear time
Time efficiency:
• worst case: Θ(n2) (as quicksort)
• average case: Θ(n) (under reasonable assumptions about
distribution of points given)
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson
Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 30