Professional Documents
Culture Documents
2 Divide-and-conquer
Weak Induction
3 Divide-and-conquer
Strong Induction
4 Divide-and-conquer
Example: A Defective Chessboard
Triomino
5 Divide-and-conquer
Example: A Defective Chessboard
6 Divide-and-conquer
Mathematical Induction
1
2
3
2n
2n+1
2n
8 Divide-and-conquer
Proof vs. Algorithm
Algorithm Proof
9 Divide-and-conquer
Outline
Content:
– A first recurrence: the mergesort algorithm
– Counting inversions
– Finding the closest pair of points
Reading:
– Chapter 5
10 Divide-and-conquer
Warm Up: Searching
Problem: Searching
Given
– A sorted list of n distinct integers
– integer x
Find
– j if x equals some integer of index j
Solution:
– Naïve idea: compare one by one
Correct but slow: O(n)
– Better idea?
Hint: input is sorted Use known information
to improve your solution
11 Divide-and-conquer
Binary Search
D&C paradigm
Search a sorted array
Divide: Break the input Divide: check
Check the the middle
middle element
into several parts of the element
same type. Search the subarray
Conquer: Solve the Conquer:
recursivelysearch the
problem in each part subarray recursively
recursively.
Combine: Combine the Combine: trivial
solutions to sub-problems
into an overall solution
0 5 13 19 22 41 55 68 72 81 98 < 55
55 68 72 81 98 > 55
55 68 = 55
12 Divide-and-conquer
value
Mergesort
John von Neumann, 1945
index
http://en.wikipedia.org/wiki/File:Merge_sort_animation2.gif
13 Divide-and-conquer
Divide and Conquer
Divide-and-conquer
– Divide: Break the input into several parts of the same type.
– Conquer: Solve the problem in each part recursively.
– Combine: Combine the solutions to sub-problems into an overall
solution.
14 Divide-and-conquer
A Divide-and-Conquer Template
Spend only linear time for the initial division and final
recombining
15 Divide-and-conquer
Mergesort (1/2)
Problem: Sorting
Given
– A set of n numbers
Find
– Sorted list in ascending order
Solution: many!
Mergesort fits the divide-and-conquer template
A L G O R I T H M S
– Divide the input into two halves.
A L G O R I T H M S
– Sort each half recursively.
Need base case
A G L O R H I M S T
Stop recursion
– Merge two halves into one.
A G H I L M O R S T
16 Divide-and-conquer
Mergesort (2/2)
Running time: A L G O R I T H M S
– T(n) for input size n
A L G I T H
– Divide: lines 1-2, D(n)
– Conquer: lines 3-4, 2T(n/2) A G L O R H I T M S
– Combine: line 5, C(n)
– T(n) = 2T(n/2) + D(n) + C(n) A G L O R H I M S T
17
A G H
Divide-and-conquer I L M O R S T
Implementation: Division and Merging
Mergesort(A, p, r)
Running time: T(n) // A[p..r]: initially unsorted
– T(n) for input size n 1. if (p < r) then
– Divide: lines 1-2, D(n) 2. q = (p+r)/2
O(1) for array 3. Mergesort(A, p, q)
– Combine: line 5, C(n) 4. Mergesort(A, q+1, r)
5. Merge(A, p, q, r)
– Q: Linear time O(n)? How?
Efficient merging
– See the demonstration of Merge
18 Divide-and-conquer
Implementation: Division and Merging
Mergesort(A, p, r)
Running time: T(n) // A[p..r]: initially unsorted
– T(n) for input size n 1. if (p < r) then
– Divide: lines 1-2, D(n) 2. q = (p+r)/2
O(1) for array 3. Mergesort(A, p, q)
– Combine: line 5, C(n) 4. Mergesort(A, q+1, r)
5. Merge(A, p, q, r)
– Q: Linear time O(n)? How?
Efficient merging
– Linear number of comparisons
– Use an auxiliary array
A G L O R H I M S T
– O(n) A G H I L
Merge sort is often the best choice for sorting a linked list
– Q: Why? How efficient on running time and storage?
19 Divide-and-conquer
Recurrence Relation
Mergesort(A, p, r)
Running time: T(n) // A[p..r]: initially unsorted
1. Base case: for n 2, T(n) c 1. if (p < r) then
2. T(n) = 2T(n/2) + D(n) + C(n) 2. q = (p+r)/2
T(n) = 2T(n/2) + O(1) + O(n) 3. Mergesort(A, p, q)
T(n) 2T(n/2) + cn 4. Mergesort(A, q+1, r)
5. Merge(A, p, q, r)
20 Divide-and-conquer
Solving Recurrences
21 Divide-and-conquer
Unrolling – Recursion Tree
Procedure
1. Analyzing the first few levels
2. Identifying a pattern T(n) T(n) = 2T(n/2) + cn
T(2) = c
3. Summing over all levels
T(n)
cn Lvl 0: cn
T(n/2)
cn/2 T(n/2)
cn/2 Lvl 1: cn
T(n/4)
cn/4 T(n/4)
cn/4 T(n/4)
cn/4 T(n/4)
cn/4 Lvl 2: cn
k=log n …
2
cn/2j Lvl j: cn
…
T(2) T(2) T(2) T(2) … T(2) T(2) T(2) T(2) Lvl k: cn
O(n log2n)
22 Divide-and-conquer
Substituting
23 Divide-and-conquer
Counting Inversions
24 Divide-and-conquer
Counting Inversions
Counting inversions
– Divide: separate the list into two pieces.
– Conquer: recursively count inversions in each half.
– Combine: count inversions where ai and aj are in different halves,
and return sum of three quantities.
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7 Divide: O(1)
5 red-red inversions 8 green-green inversions
(5,4), (5,2), (4,2), (6,3), (9,3), (9,7), (12,11), Conquer: 2T(n/2)
(8,2), (10,2) (12,3), (12,7), (11,3), (11,7)
9 red-green inversions
(5,3), (4,3), (8,6), (8,3), (8,7), (10,6), (10,9), (10,3), (10,7) Combine: ??
Total: 5 + 8 + 9 = 22 inversions
26 Divide-and-conquer
Combine?
1 2 4 5 8 10 3 6 7 9 11 12
4 2 2 1 0 0 Count: O(n)
(4,3), (5,3), (8,6), (10,3), (8,6), (10,6), (8,7), (10,7), (10,9)
9 red-green inversions
1 2 3 4 5 6 7 8 9 10 11 12 Merge: O(n)
Combine: O(n)
Total: O(n log2 n)
27 Divide-and-conquer
Implementation: Counting Inversions
28 Divide-and-conquer
Closest Pair of Points
M. I. Shamos and D. Hoey, 1975
29 Divide-and-conquer
Closest Pair of Points
30 Divide-and-conquer
Closest Pair of Points: First Attempt
Q: How?
A: Brute-force? (n2) comparisons.
31 Divide-and-conquer
Divide-and-Conquer
8
21
12
32 Divide-and-conquer
Combining the Solutions (1/4)
21
12
33 Divide-and-conquer
Combining the Solutions (2/4)
If qQ and rR for which d(q, r) < , then each of q and r
lies within a distance of L.
Pf: Suppose such q and r exist; let q = (qx, qy) and r = (rx, ry).
– By definition, qx x* rx.
– x* - qx rx - qx d(q, r) < ; rx - x* rx - qx d(q, r) < .
Q L x = x*
L: R
r
q d(r*0, r*1)
d(q*0, q*1)
2
3 21
12 4
5
6
35 Divide-and-conquer
Combining the Solutions (4/4)
36 Divide-and-conquer
Closest Pair Algorithm
Closest-Pair(P)
1. construct Px and Py // O(n log n)
2. (p*0, p*1) = Closest-Pair-Rec(Px, Py) // T(n)
Closest-Pair-Rec(Px, Py)
1. if |P| 3 then return closest pair measured by all pair-wise distances
2. x* = (n/2)-th smallest x-coordinate in Px
3. construct Qx, Qy, Rx, Ry // O(n)
4. (q*0, q*1) = Closest-Pair-Rec(Qx, Qy) // T(n/2)
5. (r*0, r*1) = Closest-Pair-Rec(Rx, Ry) // T(n/2)
6. = min(d(q*0, q*1), d(r*0, r*1))
7. L = {(x, y): x = x*}; S = {points in P within distance of L}
8. construct Sy // O(n)
9. for each sS do
10. compute distance from s to each of next 15 points in Sy
11.d(s, s’) = min distance of all computed distances // O(n)
12.if d(s, s’) < then return (s, s’)
13.else if d(q*0, q*1) < d(r*0, r*1) then return (q*0, q*1)
14.else return (r*0, r*1)
37 Divide-and-conquer