CS341 Lec03 Armin
CS341 Lec03 Armin
Winter 2025
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 1 / 19
Divide-and-Conquer
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 2 / 19
Divide-and-Conquer
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 2 / 19
Divide-and-Conquer
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 2 / 19
Divide-and-Conquer
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 2 / 19
When can we use Divide and Conquer?
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 3 / 19
When can we use Divide and Conquer?
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 3 / 19
When can we use Divide and Conquer?
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 3 / 19
Counting inversions
Collaborative filtering:
matches users preference (movies, music, ...)
determine users with similar tastes
recommends new things to users based on preferences of
similar users
Padlet
The basis of collaborative filtering is ...
https://padlet.com/arminjamshidpey/CS341
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 4 / 19
Counting inversions
(2, 3), (2, 5), (2, 8), (4, 5), (4, 8), (6, 7), (6, 8), (7, 8)
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 5 / 19
Counting inversions
(2, 3), (2, 5), (2, 8), (4, 5), (4, 8), (6, 7), (6, 8), (7, 8)
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 5 / 19
Toward a divide-and-conquer algorithm
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 6 / 19
Toward a divide-and-conquer algorithm
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 6 / 19
Transverse inversions
Goal: how many pairs (i, j) with i ≤ n/2, j > n/2, A[i] > A[j]?
Remark: this number does not change if both sides are sorted
So assume that we sort left and right after the recursive calls.
[1, 2, 5, 6, 3, 4, 7, 8]
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 7 / 19
Option 1
Algorithm: take each i ≤ n/2 and binary-search its position in
the right-hand side.
this is O(log(n)) per i, so total O(n log(n))
plus another O(n log(n)) for sorting left and right
recurrence: T (n) ≤ 2T (n/2) + O(n log(n))
gives T (n) = O(n log2 (n))
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 8 / 19
Option 1
Algorithm: take each i ≤ n/2 and binary-search its position in
the right-hand side.
this is O(log(n)) per i, so total O(n log(n))
plus another O(n log(n)) for sorting left and right
recurrence: T (n) ≤ 2T (n/2) + O(n log(n))
gives T (n) = O(n log2 (n))
Sketchy proof:
T (n) ≤ 2T (n/2) + n log(n)
≤ 4T (n/4) + n log(n/2) + n log(n)
≤ 8T (n/8) + n log(n/4) + n log(n/2) + n log(n)
≤ · · · ≤ n(log(n) + log(n/2) + · · · + log(2))
≤ n log2 (n)
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 8 / 19
Option 2: enhance mergesort
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 9 / 19
Merge(A[1..n]) (both halves of A assumed sorted)
1. copy A into a new array S; c = 0
2. i = 1; j = n/2 + 1;
3. for (k ← 1; k ≤ n; k++) do
4. if (i > n/2) A[k] ← S[j++]
5. else if (j > n) A[k] ← S[i++]; c = c + n/2
6. else if (S[i] < S[j]) A[k] ← S[i++]; c = c + j − (n/2 + 1)
7. else A[k] ← S[j++]
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 10 / 19
Merge(A[1..n]) (both halves of A assumed sorted)
1. copy A into a new array S; c = 0
2. i = 1; j = n/2 + 1;
3. for (k ← 1; k ≤ n; k++) do
4. if (i > n/2) A[k] ← S[j++]
5. else if (j > n) A[k] ← S[i++]; c = c + n/2
6. else if (S[i] < S[j]) A[k] ← S[i++]; c = c + j − (n/2 + 1)
7. else A[k] ← S[j++]
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 10 / 19
Multiplying polynomials
1. for i = 0, . . . , n − 1 do
2. for j = 0, . . . , n − 1 do
3. hi+j = hi+j + fi gj
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 11 / 19
Divide-and-conquer
Idea: write F = F0 + F1 xn/2 , G = G0 + G1 xn/2 . Then
H = F0 G0 + (F0 G1 + F1 G0 )xn/2 + F1 G1 xn
Analysis:
4 recursive calls in size n/2
Θ(n): additions to compute F0 G1 +F1 G0 and etc.
Analysis:
3 recursive calls in size n/2
Θ(n) additions to compute F0 + F1 and G0 + G1
multiplications by xn/2 and xn are free
Θ(n) additions and subtractions to combine the results
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 13 / 19
Karatsuba’s algorithm
Idea: use the identity
Analysis:
3 recursive calls in size n/2
Θ(n) additions to compute F0 + F1 and G0 + G1
multiplications by xn/2 and xn are free
Θ(n) additions and subtractions to combine the results
FFT:
if we use complex coefficients, FFT can be used to multiply
polynomials
FFT follows the same recurrence as merge sort,
T (n) = 2T (n/2) + Θ(n)
so we can multiply polynomials in Θ(n log(n)) ops over C
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 14 / 19
Multiplying matrices
1. for i = 1, . . . , n do
2. for j = 1, . . . , n do
3. for k = 1, . . . , n do
4. ci,k = ci,k + ai,j bj,k
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 15 / 19
Divide-and-conquer
Setup: write
A1,1 A1,2 B1,1 B1,2
A= B=
A2,1 A2,2 B2,1 B2,2
Padlet
Can we do better than 8 recursive calls?
https://padlet.com/arminjamshidpey/CS341
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 16 / 19
Strassen’s algorithm
Compute
Q1 = (A1,1 − A1,2 )B2,2
Q2 = (A2,1 − A2,2 )B1,1
C1,1 = Q1 − Q3 − Q5 + Q7
Q3 = A2,2 (B1,1 + B2,1 )
C1,2 = Q4 − Q1
Q4 = A1,1 (B1,2 + B2,2 ) and
C2,1 = Q2 + Q3
Q5 = (A1,1 + A2,2 )(B2,2 − B1,1 )
C2,2 = −Q2 − Q4 + Q5 + Q6
Q6 = (A1,1 + A2,1 )(B1,1 + B1,2 )
Q7 = (A1,2 + A2,2 )(B2,1 + B2,2 )
Padlet
Can we multiply two 2 × 2 matrices with less than 7
multiplications?
https://padlet.com/arminjamshidpey/CS341
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 17 / 19
What this means
Direct generalization
an algorithm that does k multiplications for matrices of
size ℓ gives T (n) ∈ Θ(nlogℓ (k) )
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 18 / 19
What this means
Direct generalization
an algorithm that does k multiplications for matrices of
size ℓ gives T (n) ∈ Θ(nlogℓ (k) )
Going beyond
an algorithm that does k multiplications for matrices of
size ℓ, m by m, p gives T (n) ∈ Θ(n3 logℓmp (k) )
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 18 / 19
What this means
Direct generalization
an algorithm that does k multiplications for matrices of
size ℓ gives T (n) ∈ Θ(nlogℓ (k) )
Going beyond
an algorithm that does k multiplications for matrices of
size ℓ, m by m, p gives T (n) ∈ Θ(n3 logℓmp (k) )
Best exponent to date (using more than just
divide-and-conquer)
O(n2.37188 ), improves from previous record O(n2.37286 )
galactic algorithms
A. Jamshidpey, C. Roberts (UW) Lec 03: Divide and Conquer Winter 2025 18 / 19