Algorithms Chapter 4 - Divide and Conquer
Algorithms Chapter 4 - Divide and Conquer
Divide-and-Conquer
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-2
Divide-and-Conquer Technique (cont.)
a problem of size n
(instance)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-4
General Divide-and-Conquer Recurrence
T(n) = aT(n/b) + f (n) where f(n) (nd), d 0
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-5
Mergesort
Split array A[0..n-1] into 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.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-6
Pseudocode of Mergesort
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-7
Pseudocode of Merge
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
The non-recursive
version of Mergesort
3 8 2 9 1 7 4 5 starts from merging
single elements into
2 3 8 9 1 4 5 7 sorted pairs.
1 2 3 4 5 7 8 9
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-9
Analysis of Mergesort
All cases have same efficiency: Θ(n log n)
T(n) = 2T(n/2) + Θ(n), T(1) = 0
Number of comparisons in the worst case is close to
theoretical minimum for comparison-based sorting:
log2 n! ≈ n log2 n - 1.44n
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-10
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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-11
Partitioning Algorithm
or i > r
< or j = l
5 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-13
Analysis of Quicksort
Best case: split in the middle — Θ(n log n)
Worst case: sorted array! — Θ(n2) T(n) = T(n-1) + Θ(n)
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
l 0; r n-1
while l r do
m (l+r)/2
if K = A[m] return m
else if K < A[m] r m-1
else l m+1
return -1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-15
Analysis of Binary Search
Time efficiency
• worst-case recurrence: Cw (n) = 1 + Cw( n/2 ), Cw (1) = 1
solution: Cw(n) = log2(n+1)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-16
Binary Tree Algorithms
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-17
Binary Tree Algorithms (cont.)
TL TR
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-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
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-20
Second Divide-and-Conquer Algorithm
2135 4014
Brute-force algorithm
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
m 1 + m4 - m 5 + m 7 m 3 + m5
=
m 2 + m4 m1 + m3 - m2 + m6
m1 = (a00 + a11) * (b00 + b11)
m2 = (a10 + a11) * b00
m3 = a00 * (b01 - b11)
m4 = a11 * (b10 - b00)
m5 = (a00 + a01) * b11
m6 = (a10 - a00) * (b00 + b01)
7 multiplications
m7 = (a01 - a11) * (b10 + b11)
18 additions
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-24
Strassen’s Matrix Multiplication
M1 + M4 - M 5 + M7 M 3 + M5
=
M2 + M 4 M1 + M 3 - M 2 + M 6
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-25
Formulas for Strassen’s Algorithm
M1 = (A00 + A11) (B00 + B11)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-27
Closest-Pair Problem by Divide-and-Conquer
Step 0 Sort the points by x (list one) and then by y (list two).
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-28
Closest Pair by Divide-and-Conquer (cont.)
Step 2 Find recursively the closest pairs for the left and right
subsets.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-30
Efficiency of the Closest-Pair Algorithm
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-31
Quickhull Algorithm
Convex hull: smallest convex set that includes given points. An
O(n^3) bruteforce time is given in Levitin, Ch 3.
Assume points are sorted by x-coordinate values
Identify extreme points P1 and P2 (leftmost and rightmost)
Compute upper hull recursively:
• find point Pmax that is farthest away from line P1P2
• compute the upper hull of the points to the left of line P1Pmax
• compute the upper hull of the points to the left of line PmaxP2
Compute lower hull in a similar manner
Pmax
P2
P1
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-32
Efficiency of Quickhull Algorithm
Finding point farthest away from line P1P2 can be done in
linear time
Time efficiency: T(n) = T(x) + T(y) + T(z) + T(v) + O(n),
where x + y + z +v <= n.
• worst case: Θ(n2) T(n) = T(n-1) + O(n)
• average case: Θ(n) (under reasonable assumptions about
distribution of points given)
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4 4-33