0% found this document useful (0 votes)
16 views85 pages

Lec-3-6-Divide-Conquer

The document discusses the Divide and Conquer (D&C) algorithm, detailing its template and applications in finding minimum, maximum, and second maximum values in arrays, as well as binary search and merge sort. It presents recurrence relations for these algorithms and explores modifications to merge sort for merging multiple sorted arrays. Additionally, it touches on counting inversions in arrays and integer multiplication using D&C principles.

Uploaded by

fakertoolzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views85 pages

Lec-3-6-Divide-Conquer

The document discusses the Divide and Conquer (D&C) algorithm, detailing its template and applications in finding minimum, maximum, and second maximum values in arrays, as well as binary search and merge sort. It presents recurrence relations for these algorithms and explores modifications to merge sort for merging multiple sorted arrays. Additionally, it touches on counting inversions in arrays and integer multiplication using D&C principles.

Uploaded by

fakertoolzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

Divide and Conquer1

1. We discuss the basic theme of DC, and the template.

2. Studied Order Statistics-1, which includes finding min, max-min, max-


second max.
3. for Min
4. fmin(A, begin, end){
If (begin==end) return A[begin];
Else mid= ((begin+end-1)/2));
Return min {fmin(begin,mid), fmin(mid+1, end)}; }
5. Recurrence relation for min: T (n) = 2T (n/2) + 1, T (2) = 1

6. Similarly, we can design the DnC algorithm for the other two problems.
7. For max-min: T (n) = 2T (n/2) + 2, T (2) = 1
8. For max-secondmax: T (n) = 2T (n/2) + 2, T (2) = 1

9. For max-min, max-secondmax: check the logic behind “the constant 2” in


T (n) = 2T (n/2) + 2
10. Binary search: T (n) = T (n/2) + constant
11. Merge sort: T (n) = 2T (n/2) + n, T (2) = 1

12. solved some problem with respect to merging.

• Consider the following modification to merge sort algorithm: divide


the input arrays into thirds(rather than halves), recursively sort each
third and finally combine the results using three way Merge subrou-
tine. What is the running time taken by this successive merging
algorithm, as a function of n , ignoring constant factors and lower
order terms?
• Suppose that you are given k sorted arrays, each with n elements,
and you want to combine them into a single array of kn elements.
One approach is to use Merge subroutine like wise, first merging the
first two arrays, then merging the result with the third array, then
with the fourth array, and so on until you merge in the k-th array.
What is the running time taken by this successive merging algorithm,
as a function of n and k, ignoring constant factors and lower order
terms?

1 Prepared by Pawan K. Mishra

1
Divide & Conquer

Prepared by Pawan K. Mishra


A general theme…

Combine (Prob(A[1..m]), Prob(A[1..m+1])), if n > n0


Prob(A[1..n])=
Basis solution , if n=n0

m = n/2, n/4 e.g.. Merge Sort, Binary Search…


m depends on the partition e.g.. Quick Sort
2
Prepared by Pawan K. Mishra
Meaning of D&C

Divide the problem into a number of subproblems that are


smaller instances of the same problem.

Conquer the subproblems by solving them recursively. If


the subproblem sizes are small enough, however, just solve
the subproblems in a straightforward manner.

Combine the solutions to the subproblems into the


solution for the original problem
3
Prepared by Pawan K. Mishra
P1. Finding the minimum of n elements in an
array

fmin(A, begin, end)


If (begin==end) return A[begin] T(1)=1
Else
mid= ⌊(begin+end-1)/2)⌋
Return min{fmin(begin,mid), fmin(mid+1, end)}

1 T(n/2) T(n/2)
T(n)= 1 + T(n/2)+ T(n/2)= 2T(n/2)+1
4
Prepared by Pawan K. Mishra
Can we divide in other fractions?

n/3, 2n/3
T(n)=T(n/3)+T(2n/3)+1 ??

n/3, n/3, n/3


T(n)=3T(n/3)+1 ??

5
Prepared by Pawan K. Mishra
P2. Finding the maximum and the minimum of
n elements in an array.
n elements

L n/2 elements n/2 elements R L: L_min L_max


R: R_min R_max

n/4 elements n/4 elements n/4 elements n/4 elements Min Max
……
….. 2 operations
2 elements 2 elements ………. T(n)=2T(n/2)+2
1 comparison require to find maximum and minimum. T(2)=1
6
Prepared by Pawan K. Mishra
P3. Finding the maximum and the second
maximum of n elements in an array.

Similarly, we can solve this problem too.

7
Prepared by Pawan K. Mishra
P4. Binary Search

T(n)=T(n/2)+constant

8
Prepared by Pawan K. Mishra
P5. Merge Sort

T(n)=2T(n/2)+O(n)
T(2)=constant

Merging two sorted arrays ??

9
Prepared by Pawan K. Mishra
Template: D&C

RecursiveD&C(A[1..n])
solve base case (n_0) ---- in constant time
if (n > n_0)
dosomething(A) other steps depend on problem
A_1= extract(A)
A_2= extract(A) findmin, findmid
B_1= RecursiveD&C(A_1) dosomething(A) findmax, findmedian
B_2= RecursiveD&C(A_2)
B =rebuild(B_1, B_2) extract(A) ---- divide(A), partition(A)
Return B
T(n)= contant, Base Case rebuild(B_1,B_2)– merge, compare
= aT(f(n))+bT(g(n))+.. +ExtraWork 10
Prepared by Pawan K. Mishra
P7. Compute a^n

I/P: a and an intger n


O/P: a^n.

11
Prepared by Pawan K. Mishra
P6. Bisection method: root finding

I/P: A polynomial function f(x), and +ve integer n


O/P:integer root “r” s.t. f(r)=0, where 0 ≤ r < n

e.g. f(x)=x^2-x-12=0, n=8


r=4

12
Prepared by Pawan K. Mishra
Prove via induction
T(n)=2T(n/2)+ c n
T(2)=constant
Thm: T(n)=O(n) A guess… so need to prove via induction.
Proof: (by induction)
Base Case: T(2)=constant = O(1)
Ind Hyp: Assume statement is true for k<n, i.e., T(k)=O(k). [Strong Induction]
Ind Step: To prove theorem for T(n), using ind hyp T(n/2)=O(n/2)
T(n/2) ≤ dn/2
T(n)= 2T(n/2)+ c n
≤ 2d(n/2)+ c n (by Hypothesis) can you find the the mistake ?
=(c+d) = O(n)
13
Prepared by Pawan K. Mishra
Prove via induction
T(n)=2T(n/2)+ c n
T(1)=constant
Thm: T(n) ≤ b n A guess… so need to prove via induction
Proof: (by induction)
Base Case: T(1)=constant ≤ b.2=b [here, constant/2 ≤ b]
Ind Hyp: Assume statement is true for k<n, i.e., T(k) ≤ bk. [Strong Induction]
Ind Step: To prove theorem for T(n), using ind hyp T(n/2) ≤ b n/2
T(n)= 2T(n/2)+ c n
≤ 2b(n/2)+ c n (by Hypothesis)
= (b+c)n
≠ bn So, the guess is wrong
14
Prepared by Pawan K. Mishra
Prove via induction
T(n)=2T(n/2)+ c n
T(2)=constant
Thm: T(n) ≤ b n log n A guess and we later find ‘b’. Need to prove via induction.
Proof: (by induction)
Base Case: T(2)=constant ≤ b.2log2=2b [here, constant/2 ≤ b]
Ind Hyp: Assume statement is true for k<n, i.e., T(k) ≤ bk. [Strong Induction]
Ind Step: To prove theorem for T(n), using ind hyp T(n/2) ≤ b n/2 log n/2
T(n)= 2T(n/2)+ c n
≤ 2 b (n/2) log n/2+ c n (by Hypothesis)
= 2 b n/2 log (n/2) +c n = bn logn –bn log 2+cn =bn logn – bn +cb
=bnlogn-(bn-cn) ≤ bnlogn
b =max{constant/2, c} – this guess will work (if bn-cn>0, b>c)
15
Prepared by Pawan K. Mishra
Not D&C Problem, but an interesting problem

Let given two sorted array A and B, find number of pairs


(i, j) such that A[i] > B[j].

For eg. A= [3,6,7,9] B=[1, 2, 5, 8, 10]

Output= 2+3+3+4=12

16
Prepared by Pawan K. Mishra
Let given two sorted arrays A and B, find the number of pairs
(i, j) such that A[i] > B[j].

For eg. A= {3,6,7,9} B={1, 2, 5, 8, 10}


Output= 2+3+3+4=12

Naïve approach: check for each element of A, traverse in B.

17
Prepared by Pawan K. Mishra
Merge and Count

A= {3,6,7,9} B={1, 2, 5, 8, 10}

Lets Merge to get sorted order

Big Sorted Array= [1,2,3,5,6,7,8,9,10]

18
Prepared by Pawan K. Mishra
Merge and Count

A= {3,6,7,9} B={1, 2, 5, 8, 10}

Lets Merge to get sorted order

Big Sorted Array= [1,2,3,5,6,7,8,9,10] – element in red


came from array B.

19
Prepared by Pawan K. Mishra
Merge and Count

A= {3,6,7,9} B={1, 2, 5, 8, 10}

Lets Merge to get sorted order

Big Sorted Array= [1,2,3,5,6,7,8,9,10] – elements in red


came from the array B.
Lets see the status of array A, when an element came from
the array B in the big sorted array.
20
Prepared by Pawan K. Mishra
Merge and Count

A= {3,6,7,9} B={1, 2, 5, 8, 10}

Lets Merge to get sorted order


Big Sorted Array= [1,2,3,5,6,7,8,9,10] – element in red
came from array B.
Lets see the status of array A, when element entered from
B in the big sorted array.
When 1 entered– How many elements are there in array A
which are not traversed fully by the pointer of A? 4
21
Prepared by Pawan K. Mishra
When 1 entered– How many elements are there in array A
which are not traversed fully by the pointer of A? 4
When 2 entered– How many elements are there in array A
which are not traversed fully by the pointer of A? 4
When 5 entered– How many elements are there in array A
which are not traversed fully by the pointer of A? 3
When 8 entered– How many elements are there in array A
which are not traversed fully by the pointer of A? 1
When 10 entered– How many elements are there in array A
which are not traversed fully by the pointer of A? 0

Prepared by Pawan K. Mishra 12 22


Merge and Count

Array got sorted and we got the count in O(n)

23
Prepared by Pawan K. Mishra
P8: Counting Inversion

I/P: Given an array A of distinct integers.

O/P: The number of inversions of A is the number of pairs


(i,j) of array A with i<j and A[i] > A[j].

A=[1, 3, 5, 4, 2]
0 + 1 + 2 + 1 + 0 = 4 pairs (3,2), (5,4) (5,2) (4,2)
24
Prepared by Pawan K. Mishra
Application of inversion

My web-series order:

Breaking Bad Prison Break Panchayat Game of Thrones Mirzapur


1 2 3 4 5

Friend’s web-series order:

Breaking Bad, Panchayat, Mirzapur , Game of Thrones, Prison Break


1 3 5 4 2
25
Prepared by Pawan K. Mishra
Naïve approach

inversion=0
for i=1 to n-1
for j=i+1 to n O(n2)-algorithm.
if A[i] > A[j] then
inversion=inversiom +1
Return inversion

26
Prepared by Pawan K. Mishra
Can we do better?

Using D&C. Any suggestion?


Divide problem in subproblems of size half and recurse, be
smart to combine the solutions of subproblem.
problem of size n

L prob. of size n/2 prob. of size n/2 R

# inversions in L = NL # inversions in L = NR
Total = NL + NR + number of pairs (i,j) s.t. L[i] > R[j] 27
Prepared by Pawan K. Mishra
# inversions in L = NL # inversions in L = NR
Total = NL + NR + number of pairs (i,j) s.t. L[i] > R[j]

This comes from combining solution


A way to combine solutions:
We need to solve the problem in O(nlogn), and we are
traversing both halves, so we cannot go beyond O(n) to do
extra work.
28
Prepared by Pawan K. Mishra
# inversions in L = NL # inversions in L = NR
Total = NL + NR + number of pairs (i,j) s.t. L[i] > R[j]

This comes from combining solution


Even smarter way to combine solutions:
Call Merge and Count routine (Last problem)

T(n)=2T(n/2)+O(n)=O(nlogn)
29
Prepared by Pawan K. Mishra
P9:Integer Multiplication: two n-digits numbers
I/P: A= a1a2a3 ….an
B= b1b2b3….bn
O/P: A*B

Eg: 5678
1234
22 712 2n operations are required: n for multiplications
1 7 034- n for carry additions.
11 3 56-- for each row: 2n operations
56 7 8 --- n rows: thus 2n2 operations in total.
7 0 0 6 5 2 need to add each column (at most 2n2 operations)

30
Prepared by Pawan K. Mishra
D&C Approach
I/P: A= a1a2a3 ….an
B= b1b2b3….bn
O/P: A*B A1 A2
A= a1a2a3 ….an A=
B= b1b2b3….bn B=
B1 B2
5678=56*102+78
1234=12*102+34 A= A1*10n/2 + A2
B= B1*10n/2 + B2
31
Prepared by Pawan K. Mishra
A1 A2
A= a1a2a3 ….an A=
B= b1b2b3….bn B=
B1 B2
A= A1*10n/2 + A2
B= B1*10n/2 + B2 AB= A1*B1*10n + A1* B2*10n/2 +
A2* B1*10n/2 + A2 * B2
32
Prepared by Pawan K. Mishra
D&C Approach

A= A1*10n/2 + A2
B= B1*10n/2 + B2 AB= A1*B1*10n + A1* B2*10n/2 +
A2* B1*10n/2 + A2*B2
T(n/2)

for shifting and adding

T(n)= 4 T(n/2) + c n shifting is easy

33
Prepared by Pawan K. Mishra
Another D&C Approach: Karatsuba Algo

AB= A1*B1*10n + A1* B2*10n/2 + A2* B1*10n/2 + A2 * B2


= A1*B1*10n + (A1* B2 + A2* B1)*10n/2 + A2 * B2

(A1+ A2)(B1+ B2)=A1B1 + A1B2 + A2B1 + A2B2


A1B2 + A2B1= (A1+ A2)(B1+ B2)- A1B1 - A2B2

34
Prepared by Pawan K. Mishra
Another D&C Approach: Karatsuba Algo

AB= A1*B1*10n + A1* B2*10n/2 + A2* B1*10n/2 + A2 * B2


= A1*B1*10n + (A1* B2 + A2* B1)*10n/2 + A2 * B2 …….(1)

(A1+ A2)(B1+ B2)=A1B1 + A1B2 + A2B1 + A2B2


A1B2 + A2B1= (A1+ A2)(B1+ B2)- A1B1 - A2B2 …..(2)

(by putting 2 in 1)
AB= A1*B1*10n + A1* B2*10n/2 + A2* B1*10n/2 + A2 * B2
= A1*B1*10n + ((A1+ A2)(B1+ B2)- A1B1 - A2B2)*10n/2 + A2 * B2
35
Prepared by Pawan K. Mishra
Another D&C Approach: Karatsuba Algo

AB= A1*B1*10n + A1* B2*10n/2 + A2* B1*10n/2 + A2* B2


= A1*B1*10n + ((A1+ A2)(B1+ B2) - A1B1 - A2B2)*10n/2 + A2* B2

T(n/2)

sum of two n/2 digits can give you at most n/2+1 digits
Thus, we are multiplying two (n/2+1) digits number …
36
Prepared by Pawan K. Mishra
Another D&C Approach: Karatsuba Algo

AB= A1*B1*10n + A1* B2*10n/2 + A2* B1*10n/2 + A2* B2


= A1*B1*10n + ((A1+ A2)(B1+ B2) - A1B1 - A2B2)*10n/2 + A2* B2

T(n/2)

T(n)=3T(n/2)+cn = O(nlog3)=O(n1.58)
37
Prepared by Pawan K. Mishra
Example
A*B=1234*4321

A1 = 12 A1B1=12*43 A2B2=34*21
A2 = 34 (A1+A2) (B1+B2)-A1B1-A2B2
B1 = 43 = (12 +34)(43+21)- A1B1-A2B2
B2 = 21

38
Prepared by Pawan K. Mishra
Example A1B1=12*43
1 2
A*B=1234*4321 4 3
A1 = 12 A2 = 34 B1 = 43 B2 = 21 1*4=4
2*3=6
(1+2)(4+3)-4-6=11
A1B1=12*43 A2B2=34*21 4*102+11*10+6=516
(A1+A2)*(B1+B2)-A1B1-A2B2

= (12 +34)*(43+21)- A1B1-A2B2 A2B2=34*21 3 4 4 6


= (46)*(64)- A1B1-A2B2 21 6 4
3*2=6 4*6=24
4*1=4 6 *4=24
(3+4)(2+1)-6-4=11 (4+6)(4+6)-24-24=52
6*102+11*10+4=714 24*102+52*10+24=2944

39
Prepared by Pawan K. Mishra
Example A1B1=12*43
1 2
A*B=1234*4321 4 3 (A1+A2)*(B1+B2)
1*4=4
A1 = 12 A2 = 34 B1 = 43 B2 = 21
2*3=6
(1+2)(4+3)-4-6=11
A1B1=12*43 A2B2=34*21 4*102+11*10+6=516
(A1+A2)*(B1+B2)-A1B1-A2B2

= (12 +34)*(43+21)- A1B1-A2B2 A2B2=34*21 3 4 4 6


= (46)*(64)- A1B1-A2B2 21 6 4
Final Solution: 3*2=6 4*6=24
4*1=4 6 *4=24
(3+4)(2+1)-6-4=11 (4+6)(4+6)-24-24=52
A1B1104 +((A1+ A2)(B1+ B2) - A1B1 - A2B2)102 + A2 B2
6*102+11*10+4=714 24*102+52*10+24=2944
516 *104+(2944-714-516) *102+714
=5332114

40
Prepared by Pawan K. Mishra
K-th smallest element

• I/P: Array of n numbers.


• OP: Select k-th smallest element

Naïve way: Sort it, and return it. O(nlogn)


Can we do better ? ---- in O(n)??
YES!!!!! ------“Median of Median”- algorithm

MANUEL BLUM, ROBERT W. FLOYD, VAUGHAN PRATT, RONALD L.


RIVEST, AND ROBERT E. TARJAN

Prepared by Pawan K. Mishra


Let’s try..Select(A,k)

Suppose we select a pivot element x of Array A (not randomly, just to explain).


A can be portioned in two sets… A1={all elements of A < x}
A2={all elements of A > x}
Select(A, k)
choose x; We are throwing away something, but might not be in fractions

If |A1|= k-1, return x; Unlucky in all the recursive call


If k < |A1|, Select(A[1], k) T(n) <= T(n-1)+ O(n) ----- O(n2)
Else Select(A[2], k-|A1|-1) for partition

Prepared by Pawan K. Mishra


Let’s try..Select(A,k)

Suppose we select a pivot element x of Array A (not randomly, just to explain).


A can be portioned in two sets… A1={all elements of A < x}
A2={all elements of A > x}
Select(A, k) in each recursive call

choose x; Suppose, the selected pivot throw away this much 0.3n=3n/10,

If |A1|= k-1, return x;


If k < |A1|, Select(A[1], k) T(n) <= ( )+ T(7n/10)+ O(n)
Else Select(A[2], k-|A1|-1) for partion

Prepared by Pawan K. Mishra


T(n) <= (need to do something to get a good pivot)+ T(7n/10)+ O(n)

Divide array A into group of size 5.


n/5 groups
Find the median of each group.
3rd element in each group– create the list of all selected -- B
Find median of medians using the same Select(B, n/10)

Prepared by Pawan K. Mishra


Array A divided into group of 5, n/5 groups….

Prepared by Pawan K. Mishra


Array A divided into group of 5, n/5 groups….

Prepared by Pawan K. Mishra


Array A divided into group of 5, n/5 groups….

Prepared by Pawan K. Mishra


.
A divided into group of 5, n/5 groups….
Median in constant time for each group, as each group contain 5
elements.

Prepared by Pawan K. Mishra


.
A divided into group of 5, n/5 groups….
Median in constant time for each group, as each group contain 5
elements. So, overall linear time. Add to extra work O(n).

Prepared by Pawan K. Mishra


.
A divided into group of 5, n/5 groups….
Median in constant time for each group, as each group contain 5
elements. So, overall linear time. Add to extra work O(n).
Median of Medians- Select(B, n/10)

Prepared by Pawan K. Mishra


.
A divided into group of 5, n/5 groups….
Median in constant time for each group, as each group contain 5
elements. So, overall linear time. Add to extra work O(n).
Median of Medians- Select(B, n/10)
T(n/5) is required….

Prepared by Pawan K. Mishra


Claim: The median of median will be a good pivot,
i.e., it throw away 3n/10 elements.
Median of Medians- Select(B, n/10)
3n/10 elements

Prepared by Pawan K. Mishra


Claim: The median of median will be a good pivot,
i.e., it throw away 3n/10 elements.
Median of Medians- Select(B, n/10)

3n/10

Prepared by Pawan K. Mishra


Claim: The median of median will be a good pivot,
i.e., it throw away 3n/10 elements.

Select(A, k) choose x;  median of medians


Create partition A1 and A2 based on median of medians
If |A1|= k-1, return x;
If k < |A1|, Select(A[1], k)  because of x, there are at least 3n/10 elements in A1
Else
Select(A[2], k-|A1|-1)  because of x, there are at least 3n/10 elements in A2

Since, |A1| => 3n/10, therefore |A2| < 7n/10


and Thus both A1 and A2 are bounded
Since, |A2| => 3n/10 , therefore |A1| < 7n/10 by 7n/10 elements.

Prepared by Pawan K. Mishra


Claim: The median of median will be a good pivot,
i.e., it throw away 3n/10 elements.

Select(A, k) |A1| => 3n/10 implies |A2| < 7n/10


choose x;  median of medians
Create partition A1 and A2 based on median of medians
If |A1|= k-1, return x;
If k < |A1|, Select(A[1], k)  because of x, there are at least 3n/10 elements in A1
Else
Select(A[2], k-|A1|-1)  because of x, there are at least 3n/10 elements in A2
|A2| => 3n/10 implies |A1| < 7n/10

Prepared by Pawan K. Mishra


Time Complexity Analysis.
• T(n)=T(n/5)+T(7n/10)+cn
To prove T(n)= O(n)
Guess: T(n) <=Rn (for some R, we will later fix this R)
Ind Hyp: T(n)<=Rn is true for all k<n.
• Ind step: T(n)=T(n/5)+T(7n/10)+cn <= Rn/5+ R7n/10+cn
=(0.2R+0.7R+c)n=(0.9R+c)n
If 10 c = R =Rn

56
Prepared by Pawan K. Mishra
Closest Pair

Prepared by Pawan K. Mishra


P12. Closest pair of n points

I/P: Given P={p1, p2,…, pn} a set of n points. Each pi=(xi,yi).


O/P: Find the closest pair of points.

Prepared by Pawan K. Mishra


P12. Closest pair of n points

I/P: Given P={p1, p2,…, pn} a set of n points. Each pi=(xi,yi).


O/P: Find the closest pair of points.

Prepared by Pawan K. Mishra


Closest pair of n points

I/P: Given P={p1, p2,…, pn} a set of n points. Each pi=(xi,yi).


O/P: Find the closest pair of points.

Naïve algorithm:
1. For each point p in P, check all points in P \ {p}.
2.Compute the minimum distances with respect to p, i.e., min_p
3. Repeat step 1 and 2 for all points in P,
4. Finally return min of all min_p’s.

Prepared by Pawan K. Mishra


Let’s do the problem in 1-Dimension
y-coordinate of all points are same.
O(nlogn)
1.Sort each point with respect to x-coordinate.

2. For each point pi , check two neighbours pi-1 and pi+1.


3. Do it for all points and return the minimum. O(n)

Prepared by Pawan K. Mishra


Can we do via divide and conquer?
Here input is not sorted….. Means the point are not sorted with
respect to x-coordinate…. ??

How to apply D&C??

Intuition is to divide points set in equal halves ??– But how??

We need median to partition in two halves….??

Prepared by Pawan K. Mishra


Lets apply the unknown algorithm to partition point set in two halves.

P split in two halves PL and PR.

Recurse on both halves.

PL contains the median point (W.L.O.G)…

Prepared by Pawan K. Mishra


P

complexity??
PL PR

Suppose the closest pair in PL is dL s=min (dL, dR)


the closest pair in PR is dR
dL s dR

Prepared by Pawan K. Mishra


In 2D

Prepared by Pawan K. Mishra


In 2D

dL
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

s s

dL
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

s s Can this point be used ?


No, so check points in strip.

dL
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

s s
So, we need to find the
closest pair in strip only.
naïve way: O(n2)
dL T(n)=T(n/2)+ O(n2)
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

s s
Do we need to
compare this pair?
NO!!

dL dR
s=min(dL, dR)

Prepared by Pawan K. Mishra


In 2D

s s
Do we need to
compare this pair?
NO!!
Let’s dissects the strip.
dL dR
s=min(dL, dR) s
s/2 2/2 s/2 s/2
s s

Prepared by Pawan K. Mishra


s/sqrt(2)

s/2
so, in a box, can have one point.
s/2

Prepared by Pawan K. Mishra


for green point, do we need
to check these red points ?
NO? Why?
s

Prepared by Pawan K. Mishra


for green point, we need
to check these red points ?
Yes? Why?
s 7 points

Prepared by Pawan K. Mishra


how to find seven points for each point?

Prepared by Pawan K. Mishra


O(nlog2n)
for green point, we need
to check these red points ?
Yes? Why?
s 7 points

so, for each point, we need to


check at most next 7 points.
T(n)=2T(n/2)+nlogn To get next 7, need to sort

Prepared by Pawan K. Mishra


2 dimensions, divide and conquer

• Split set of points into two halves by vertical line

• Recursively compute closest pair in left and right half

• Need to then compute closest pairs across separating line

• How can we do this efficiently?

Prepared by Pawan K. Mishra


Sorting points by x and y

Given n points P = {p1,p2,…,pn}, compute


➢ Px : P sorted by x coordinate
➢ Py : P sorted by y coordinate

Divide P by vertical line into equal


size sets Q and R.

Need to efficiently compute


Qx, Qy, Rx, Ry
22
Prepared by Pawan K. Mishra
Sorting points by x and y

• Need to efficiently compute


Qx, Qy, Rx, Ry
➢Qx is first half of Px
➢ Rx is second half of Px
• When splitting Px,
note the largest x coordinate in Q, xQ
• Separate Py as Qy, Ry by checking x
coordinate with xQ. xQ

• All O(n)
23
Prepared by Pawan K. Mishra
2 dimensions, divide and conquer

• Basic recursive call is ClosestPair(Px, Py)

• Set up recursive calls ClosestPair(Qx, Qy)


and ClosestPair(Rx, Ry) for left and right half of P in
time O(n)
• How to combine these recursive solutions?

24
Prepared by Pawan K. Mishra
Combining solutions
• Let dQ be closest distance in Q
and dR be closest distance in R
• Let d be min(dQ , dR)
• Only need to consider points
across the separator at most distance
d from separator
➢Any pair outside this strip
cannot be closest pair overall

25
Prepared by Pawan K. Mishra
Combining solutions
• From Qy, Ry, extract Sy,
points in d-band sorted by y coordinate
• Scan Sy from bottom to top,
comparing each point against next
7 points in Sy
• Linear scan

26
Prepared by Pawan K. Mishra
Algorithm
ClosestPair(Px,Py)
if (|Px| <= 3)
compute pairwise distances and
return the closest pair and distance

Construct (Qx, Qy, Rx, Ry)


(dQ ,q1, q2) = ClosestPair(Qx, Qy)
(dR ,r1, r2) = ClosestPair(Rx, Ry)
Construct Sy and scan to find (dS,s1, s2)

Return (dQ ,q1, q2) , (dR ,r1, r2) , (dS,s1, s2) depending on which among
(dQ ,dR, dS) is minimum

27
Prepared by Pawan K. Mishra
Analysis
• Computing ( Px, Py ) from P takes O(n log n) (before recursion call)
• Recursive algorithm
➢ Setting up (Qx, Qy, Rx, Ry) from ( Px, Py ) is O(n)
➢ Setting up Sy from Qy, Ry is O(n)
➢ Scanning Sy is O(n)

T(n) = 2T(n/2) +O(n)=O(n log n)

28
Prepared by Pawan K. Mishra

You might also like