Professional Documents
Culture Documents
Algorithm Design and Analysis (ADA)
Algorithm Design and Analysis (ADA)
(ADA)
CSE222
Divide and Conquer Algorithms
1. Divide the problem into smaller
subproblems
Output : 3
Counting Inversions : Example
Input : 1, 3, 5, 2, 4, 6
Output :
Output :
Output :
3. Split inversions : i in X, j in Y
Example Again..
1 5 3 2 4 6
X Y
1. Left inversion : H2 XG
2. Right inversions : 11
3. Split inversions : X 27 Y 2
Key Idea for Divide and Conquer
Suppose A is divided in to X and Y .
An inversion pair (i, j) is :
f
i, j = 1 𝟏 𝟑 𝟓 𝟐 𝟒 𝟔
for k=1 to n
if X(i) < Y(j) I j
D(k) = X(i)
i++
else
D(k) = Y(j)
I
j++
Key Idea # 2 : Why sorted subarrays help ?
Recap of ‘Merge’ from Mergesort
X ,Y : Sorted arrays of length n/2 Merge exposes split inversions :
Z : Output array of length n
IT
i, j = 1 𝟏 𝟑 𝟓 𝟐 𝟒 𝟔
for k=1 to n
if X(i) < Y(j) i j
D(k) = X(i)
i++ 2
else
D(k) = Y(j)
12
j++
Key Idea # 2 : Why sorted subarrays help ?
Recap of ‘Merge’ from Mergesort
X ,Y : Sorted arrays of length n/2 Merge exposes split inversions :
Z : Output array of length n
i, j = 1 𝟏 𝟑 𝟓 𝟐 𝟒 𝟔
for k=1 to n
if X(i) < Y(j) i j
D(k) = X(i)
i++
else
I 23
D(k) = Y(j)
j++
Key Idea # 2 : Why sorted subarrays help ?
Recap of ‘Merge’ from Mergesort
X ,Y : Sorted arrays of length n/2 Merge exposes split inversions :
Z : Output array of length n
I
i, j = 1 𝟏 𝟑 𝟓 𝟐 𝟒 𝟔
for k=1 to n
if X(i) < Y(j) i j
D(k) = X(i)
If
i++
else
D(k) = Y(j)
I 234
j++
Key Idea # 2 : Why sorted subarrays help ?
Recap of ‘Merge’ from Mergesort
X ,Y : Sorted arrays of length n/2 Merge exposes split inversions :
Z : Output array of length n
i, j = 1 𝟏 𝟑 𝟓 𝟐 𝟒 𝟔
for k=1 to n
if X(i) < Y(j) i j
D(k) = X(i)
i++
else
D(k) = Y(j)
I 23456
j++
Key Idea # 2 : Why sorted subarrays help ?
Recap of ‘Merge’ from Mergesort
X ,Y : Sorted arrays of length n/2 Merge exposes split inversions :
Z : Output array of length n
i, j = 1 𝟏 𝟑 𝟓 𝟐 𝟒 𝟔
for k=1 to n
if X(i) < Y(j) i j
D(k) = X(i)
i++
else
D(k) = Y(j)
j++
A General Claim
Claim. The split inversions involving an element y in the
subarray Y is precisely n/2-i+1, where i is position of the
first pointer when y is copied to D
it itE
Proof:
x i Xli Y Y 4
Da For all elements
bet i I
Do u u u
Xli x 721 y sick
Key Idea # 2 : Why sorted subarrays help ?
CountAndMerge
X ,Y : Sorted arrays of length n/2
Z : Output array of length n, count = 0
i, j = 1 Return count
for k=1 to n
if X(i) < Y(j)
D(k) = X(i)
i++
else
D(k) = Y(j)
j++ a
count = count + (n/2 – i + 1)
Algorithm :
SortAndCountInv (array A, length n)
If n==1 return 0 T IA
X = A[1,2,…n/2] ,Y = A[n/2+1, …n ]
d
(B, x) = SortAndCountInv (X, n/2) E
s (C, y) = SortAndCountInv (Y, n/2)
(D, z) = CountAndMerge (X,Y,a n)
t
Return x+y+z D BC
Matrix Multiplication : Divide and Conquer
Matrix Multiplication
Square Matrices
n xn n x m n xn
𝑋 𝑌 = 𝑍
Matrix Multiplication
If
i
𝑋 𝑌 = 𝑍
dot
𝑍𝑖𝑗 ∶ The product of ith row of X and jth column of Y
k
Zig 2 i n
Yay Cns
algorithm
Matrix Multiplication
𝑋 𝑌 = 𝑍
𝑋 𝑌 = 𝑍
0cm
Matrix Multiplication
𝑋 𝑌 = 𝑍
𝐴 𝐵 𝐸 𝐹
𝐶 𝐷 𝐺 𝐻
d 1 Check
A Et BE A Ft BH
f
C Et DG CF 1 DH
Recursive algorithm I
StepIII Recursively
8 products
compute the
Runtime 1 n 8 T M2 to ne 043
Master Thu ba D 2
1 n
95
1 n n
Recursive Algorithm II : Strassen (1969)
Step 1 : Recursively Compute 7 cleverly chosen
products.
1 a 7 TAKI 10cal
M i m2 8704
Applying O
Recursive Algorithm II : Strassen (1969)
Step 1 : Recursively Compute 7 cleverly chosen
products.
Pi A CF H Pz A By H
Pz Ct D E pg D G E
Y
E II
ATD Et H1 PG B D GTA
Ps
Pz A c Etf
I
Matrix Multiplication : Trivia
Strassen : 𝑂(𝑛2.8074 )
W
At least 15 research papers till December 2020 on this topic !