Beruflich Dokumente
Kultur Dokumente
Divide-and-conquer technique
a problem of size n
subproblem 1
of size n/2
subproblem 2
of size n/2
a solution to
subproblem 1
a solution to
subproblem 2
a solution to
the original problem
a < bk
a = bk
a > bk
T(n) = (nk)
T(n) = (nk lg n )
T(n) = (nlog b a)
Mergesort
Algorithm:
Split array A[1..n] in two and make
copies of each half
in arrays B[1.. n/2 ] and C[1.. n/2 ]
Sort arrays B and C
Merge sorted arrays B and C into array A
Mergesort
Algorithm:
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.
C: 4 9 5
B: 1 2 6 7
C: 4 5 9
B: 1
Putting it Together
A: 7 2 1 6 4 9 5
B: 7 2 1 6
C: 4 9 5
B: 1 2 6 7
C: 4 5 9
J: 1 K: 6
F: 4 9 G: 5 Split Again
L: 4 M: 9
N: 5
Efficiency of mergesort
Quicksort
A[i]p
A[i]>p
Example: 8 1 12 2 6 10 14 15 4 13 9 11 3 7 5
Efficiency of quicksort
Efficiency of quicksort
Improvements:
better pivot selection: median of three
partitioning avoids worst case in sorted
files
switch to insertion sort on small subfiles
elimination of recursion
these combine to 20-25% improvement
Considered the method of choice for internal
sorting for large files (n 10000)
QuickHull Algorithm
Inspired by Quicksort compute Convex Hull:
Assume points are sorted by x-coordinate values
Identify extreme points P1 and P2 (part of hull)
P2
P1
QuickHull Algorithm
P2
P1
Efficiency of QuickHull
algorithm
Efficiency of QuickHull
algorithm
If points are not initially sorted by xcoordinate value, this can be accomplished
in ( n log n) no increase in asymptotic
efficiency class
Other algorithms for convex hull:
Grahams scan
DCHull
also in ( n log n)
Closest-Pair Problem:
Divide and Conquer
k
k 1
(n 1) n
2
Closest-Pair Algorithm
Given: A set of points in 2-D
Closest-Pair Algorithm
Step 1: Sort the points in one D
Closest-Pair Algorithm
Lets sort based on the X-axis
O(n log n) using quicksort or mergesort
4
3
1
13
10
11
8
12
14
Closest-Pair Algorithm
Step 2: Split the points, i.e.,
Draw a line at the mid-point between 7 and 8
3
1
13
10
11
8
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
Advantage: Normally, wed have to compare
each of the 14 points with every other point.
(n-1)n/2 = 13*14/2 = 91 comparisons
4
3
1
13
10
11
8
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
Advantage: Now, we have two sub-problems of
half the size. Thus, we have to do 6*7/2
comparisons twice, which is 42 comparisons
solution d = min(d1, d2)
4
d1
d2
7
3
5
13
10
11
8
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
Advantage: With just one split we cut the
number of comparisons in half. Obviously, we
gain an even greater advantage if we split the
sub-problems.
d = min(d1, d2)
d1
d2
7
3
5
13
10
11
8
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
Problem: However, what if the closest two
points are each from different sub-problems?
d1
d2
7
3
5
13
10
11
8
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
Here is an example where we have to compare
points from sub-problem 1 to the points in subproblem 2.
d1
6
7
3
1
13
d2
10
11
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
However, we only have to compare points inside
the following strip.
d = min(d1, d2)
4
d1
3
1
13
d2
10
11
8
12
Sub-Problem 1
Sub-Problem 2
14
Closest-Pair Algorithm
Step 3: But, we can continue the advantage by
splitting the sub-problems.
3
1
13
10
11
8
12
14
Closest-Pair Algorithm
Step 3: In fact we can continue to split until each
sub-problem is trivial, i.e., takes one comparison.
3
1
13
10
11
8
12
14
Closest-Pair Algorithm
Finally: The solution to each sub-problem is
combined until the final solution is obtained
3
1
13
10
11
8
12
14
Closest-Pair Algorithm
Finally: On the last step the strip will likely be
very small. Thus, combining the two largest subproblems wont require much work.
3
1
13
10
11
8
12
14
Closest-Pair Algorithm
3
1
13
10
11
8
12
14
Closest-Pair Problem:
Divide and Conquer
Remember
Long Term