Documente Academic
Documente Profesional
Documente Cultură
Divide-and-Conquer
Divide-and-Conquer
The most-well known algorithm design strategy:
1.
Divide instance of problem into two or more smaller
instances
2.
3.
4-2
subproblem 2
of size n/2
a solution to
subproblem 1
a solution to
subproblem 2
a solution to
the original problem
It general leads to a
recursive algorithm!
4-3
Divide-and-Conquer Examples
4-4
T(n) (nlog b a )
T(n) ?
T(n) ?
T(n) ?
(n^2)
(n^2log n)
(n^3)
4-5
Mergesort
4-6
Pseudocode of Mergesort
4-7
Pseudocode of Merge
4-8
Mergesort Example
8 3 2 9 7 1 5 4
8 3 2 9
8 3
7 1 5 4
2 9
3 8
71
2 9
5 4
1 7
2 3 8 9
4 5
1 4 5 7
The non-recursive
version of Mergesort
starts from merging
single elements into
sorted pairs.
1 2 3 4 5 7 8 9
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
4-9
Analysis of Mergesort
4-10
Quicksort
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
4-11
Partitioning Algorithm
<
or i > r
or j = l
4-12
Quicksort Example
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
4-13
Analysis of Quicksort
Improvements:
better pivot selection: median of three partitioning
switch to insertion sort on small subfiles
elimination of recursion
These combine to 20-25% improvement
4-14
Binary Search
Very efficient algorithm for searching in sorted array:
K
vs
A[0] . . . A[m] . . . A[n-1]
If K = A[m], stop (successful search); otherwise, continue
searching by the same method in A[0.. m-1] if K < A[m]
and in A[m+1..n-1] if K > A[m]
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.
4-15
Time efficiency
worst-case recurrence: Cw (n) = 1 + Cw( n/2 ), Cw (1) = 1
solution: Cw(n) = log2(n+1)
This is VERY fast: e.g., Cw(106) = 20
4-16
a
b
a
c
b
c
d e
4-17
TL
TR
4-18
4-19
4-20
4-21
4-22
Brute-force algorithm
c00 c01
a00 a01
=
*
c10 c11
a10 a11
b00 b01
b10 b11
=
a10 * b00 + a11 * b10
8 multiplications
4 additions
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
4-23
m3 + m5
m1 + m 3 - m2 + m6
7 multiplications
18 additions
4-24
A00 A01
=
C10 C11
B00 B01
*
A10 A11
B10 B11
M1 + M 4 - M 5 + M7
M 3 + M5
=
M2 + M 4
M1 + M 3 - M 2 + M6
4-25
4-26
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.
Algorithms with better asymptotic efficiency are known but they
are even more complex and not used in practice.
4-27
4-28
4-29
4-30
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.
Pmax
P2
P1
Copyright 2007 Pearson Addison-Wesley. All rights reserved.
4-32
4-33