Professional Documents
Culture Documents
- Example
- Binary Search
- Merge Sort
- Quick Sort
1
Divide and Conquer
2
Divide-and-Conquer
Consequence.
Brute force: n2.
Divide-and-conquer: n log n.
3
Closest Pair of Points
4
Closest Pair of Points: First Attempt
5
Closest Pair of Points: First Attempt
6
Closest Pair of Points
Algorithm.
Divide: draw vertical line L so that roughly n points on each
side.
7
Closest-Pair Problem:
Divide and Conquer
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Closest-Pair Algorithm
Step 2: Split the points, i.e.,
Draw a line at the mid-point between 7 and 8
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Sub-Problem 1 Sub-Problem 2
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
9
6 13
2
14
11
3 7 10
5
1 8
12
Sub-Problem 1 Sub-Problem 2
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
9
d1 6 13
2 d2
14
11
3 7 10
5
1 8
12
Sub-Problem 1 Sub-Problem 2
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)
4
9
d1 6 13
2 d2
14
11
3 7 10
5
1 8
12
Sub-Problem 1 Sub-Problem 2
Closest-Pair Algorithm
Problem: However, what if the closest two
points are each from different sub-problems?
4
9
d1 6 13
2 d2
14
11
3 7 10
5
1 8
12
Sub-Problem 1 Sub-Problem 2
Closest-Pair Algorithm
Here is an example where we have to compare
points from sub-problem 1 to the points in sub-
problem 2.
d1 6 13
2 d2
9 14
7 11
3 10
5 8
1
12
Sub-Problem 1 Sub-Problem 2
Closest-Pair Algorithm
However, we only have to compare points
inside the following strip.
d = min(d1, d2)
4
9
d1 d d 13
2 6
d2
14
11
3 7 10
5
1 8
12
Sub-Problem 1 Sub-Problem 2
Closest-Pair Algorithm
Step 3: But, we can continue the advantage by
splitting the sub-problems.
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Closest-Pair Algorithm
Step 3: In fact we can continue to split until
each sub-problem is trivial, i.e., takes one
comparison.
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Closest-Pair Algorithm
Finally: The solution to each sub-problem is
combined until the final solution is obtained
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Closest-Pair Algorithm
Finally: On the last step the strip will likely
be very small. Thus, combining the two largest
sub-problems wont require much work.
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Closest-Pair Algorithm
In this example, it takes 22 comparisons to find the
closets-pair.
The brute force algorithm would have taken 91
comparisons.
But, the real advantage occurs when there are millions of
points.
4
9
6 13
2
14
11
3 7 10
5
1 8
12
Closest Pair Divide & Conquer
dimension d = 2
Partition two dimensional set S into subsets S1 and S2 by a
vertical line l at the median x coordinate of S.
Solve the problem recursively on S1 and S2.
Let {p1, p2} be the closest pair in S1 and {q1, q2} in S2.
Let 1 = distance(p1,p2) and 2 = distance(q1,q2)
Let = min(1, 2)
l
S1 S2
p1
1
p2
q1
q2 2
Closest Pair Divide & Conquer
dimension d = 2
In order to merge we have to
determine if exists a pair of points {p, P1 l P2
q} where p S1, q S2 and
distance(p, q) < .
If so, p and q must both be within of
l. p1
Let P1 and P2 be vertical regions of q1
1
the plane of width on either side of l. p2 S2
If {p, q} exists, p must be within P1 S1
2
and q within P2.
However, every point in S1 and S2 may q2
be a candidate, as long as each is
within of l, which implies: O(n/2)
O(n/2) = O(n2)
Can we do better ?
Closest Pair Divide & Conquer
dimension d = 2
For a point p in P1, which portion of P1 l P2
P2 should be checked?
We only need to check the points
that are within of p.
Thus we can limit the portion of P2.
The points to consider for a point
p must lie within 2 rectangle R. S1 S2
p
At most, how many points are
there in rectangle R?
Closest Pair Divide & Conquer
dimension d = 2
Songs
A B C D E
Inversions
Me 1 2 3 4 5
3-2, 4-2
You 1 3 4 2 5
29
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
1 5 4 8 10 2 6 9 12 11 3 7
30
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
Divide: separate list into two pieces.
Divide: O(1).
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7
31
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
Divide: separate list into two pieces.
Conquer: recursively count inversions in each half.
Divide: O(1).
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7 Conquer: 2T(n / 2)
5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-7
32
Counting Inversions: Divide-and-Conquer
Divide-and-conquer.
Divide: separate 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.
Divide: O(1).
1 5 4 8 10 2 6 9 12 11 3 7
1 5 4 8 10 2 6 9 12 11 3 7 Conquer: 2T(n / 2)
9 blue-green inversions
Combine: ???
5-3, 4-3, 8-6, 8-3, 8-7, 10-6, 10-9, 10-3, 10-7
Total = 5 + 8 + 9 = 22.
33
Merge and Count
18 7 14 10 3 19 17 25 16 2 23 11
34
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
auxiliary array
current Inversions:
Total: 0
35
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 auxiliary array
current Inversions: 6
Total: 6
36
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 auxiliary array
current Inversions: 6
Total: 6
37
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 auxiliary array
current Inversions: 6
Total: 6
38
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 auxiliary array
current Inversions: 6
Total: 6
39
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 auxiliary array
current Inversions: 6
Total: 6
40
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 auxiliary array
current Inversions: 6
Total: 6
41
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 auxiliary array
current Inversions: 6
Total: 6
42
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 auxiliary array
current Inversions: 6
Total: 6
43
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 auxiliary array
Inversions: 6 + 3
Total: 9
44
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 auxiliary array
Inversions: 6 + 3
Total: 9
45
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 auxiliary array
Inversions: 6 + 3
Total: 9
46
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 auxiliary array
Inversions: 6 + 3
Total: 9
47
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 auxiliary array
Inversions: 6 + 3 + 2
Total: 11
48
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 auxiliary array
Inversions: 6 + 3 + 2
Total: 11
49
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 auxiliary array
Inversions: 6 + 3 + 2 + 2
Total: 13
50
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 auxiliary array
Inversions: 6 + 3 + 2 + 2
Total: 13
51
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 auxiliary array
Inversions: 6 + 3 + 2 + 2
Total: 13
52
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 auxiliary array
Inversions: 6 + 3 + 2 + 2
Total: 13
53
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 19 auxiliary array
Inversions: 6 + 3 + 2 + 2
Total: 13
54
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 19 auxiliary array
Inversions: 6 + 3 + 2 + 2
Total: 13
55
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 19 23 auxiliary array
Inversions: 6 + 3 + 2 + 2 + 0
Total: 13
56
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 19 23 auxiliary array
Inversions: 6 + 3 + 2 + 2 + 0
Total: 13
57
Merge and Count
3 7 10 14 18 19 2 11 16 17 23 25 N/2 = 6
2 3 7 10 11 14 16 17 18 19 23 25 auxiliary array
Inversions: 6 + 3 + 2 + 2 + 0 + 0
Total: 13
58