Lec-3-6-Divide-Conquer
Lec-3-6-Divide-Conquer
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
1
Divide & Conquer
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 ??
5
Prepared by Pawan K. Mishra
P2. Finding the maximum and the minimum of
n elements in an array.
n elements
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.
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
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
11
Prepared by Pawan K. Mishra
P6. Bisection method: root finding
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
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].
17
Prepared by Pawan K. Mishra
Merge and Count
18
Prepared by Pawan K. Mishra
Merge and Count
19
Prepared by Pawan K. Mishra
Merge and Count
23
Prepared by Pawan K. Mishra
P8: Counting Inversion
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:
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?
# 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]
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)
33
Prepared by Pawan K. Mishra
Another D&C Approach: Karatsuba Algo
34
Prepared by Pawan K. Mishra
Another D&C Approach: Karatsuba Algo
(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
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
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
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
40
Prepared by Pawan K. Mishra
K-th smallest element
choose x; Suppose, the selected pivot throw away this much 0.3n=3n/10,
3n/10
56
Prepared by Pawan K. Mishra
Closest Pair
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.
complexity??
PL PR
dL
s=min(dL, dR) dR
s s
dL
s=min(dL, dR) dR
dL
s=min(dL, dR) dR
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
s s
Do we need to
compare this pair?
NO!!
dL dR
s=min(dL, dR)
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
s/2
so, in a box, can have one point.
s/2
• All O(n)
23
Prepared by Pawan K. Mishra
2 dimensions, divide and conquer
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
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)
28
Prepared by Pawan K. Mishra