Professional Documents
Culture Documents
Algorithm structure
Iterative ⇒ execute action in loop
Recursive ⇒ reapply action to subproblem(s)
Problem type
Satisfying ⇒ find any satisfactory solution
Optimization ⇒ find best solutions (vs. cost
metric)
subproblem 1 subproblem 2
of size n/2 of size n/2
a solution to a solution to
subproblem 1 subproblem 2
a solution to
the original problem
2 10 9 6 4 5
The solution should be 2 followed by 4
What if?? 2 8 2 7 9 3
2 6 7 3 5 6 9 2 4 1
We divide the values into pairs
2 6 7 3 5 6 9 2 4 1
We sort each pair
2 6 3 7 5 6 2 9 1 4
2 6 3 7 5 6 2 9 1 4
Lowest 2,3
The next one (5 and 6)
Lowest 2,3
The next one (2 and 9)
Lowest 2,2
The next one (1 and 4)
Lowest 1,2
int Fib(n)
{ int Fib(n) {
if (n < 2) return (n); if (n < 2) return (n);
else { else {
return (Fib (n-1) + Fib (n-2)); int F1 = 1; F2 = 0;
} for (i = 2; i <= n; i++)
} { F = F1 + F2;
F2 = F1;
F1 = F;
}
return (F)
}
(n-1)
| 1 1 | | F(1) |
= | | * | |
| 1 0 | | F(0) |
(n-1)
| 1 1 |
| |
| 1 0 |
Mergesort
Partition array into two parts
Recursively mergesort each half
Merge two sorted arrays into single sorted array
Goal: sort it
1 2 2 3 4 5 6 7
7 2 8 5 4 2 4 5 7 8
2 5 4 7 8 2 4 5 7 8
2 5 4 2 4 5
4 5 4 5
2 7 4 5 8 7 8
2 2 4 5 7
7 4 5 8 8
2 4 2 4 5 7 8
7 5 8
Divide and Conquer Technique 34
Merge Sort Example
7 2 8 5 4 2 4 5 7 8
7 2 8 5 4 2 7 4 5 8
7 2 8 5 4 7 2 8 4 5
5 4 5 4
Split Merge
Step 1 – Divide
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6
5 2 4 7 1 3 2 6
Step 2 – Conquer
5 2 4 7 1 3 2 6 O(n)
2 5 4 7 1 3 2 6 O(n)
2 4 5 7 1 2 3 6 O(n)
1 2 2 3 4 5 6 7 O(n)
logn iterations, each iteration takes O(n) time. Total Time: O(n logn)
Step 3 – Combine
5 2 2 5
4 5 7 4 5 7
1 2 2 1 2 2 3
2 3 6 3 6
4 5 7 Etcetera…
1 2 2 3 4
6 1 2 2 3 4 5 6 7
20 4 7 6 1 3 9 5
Divide 20 4 7 6 1 3 9 5
20 4 7 6 1 3 9 5
20 4 7 6 1 3 9 5
4 20 6 7 1 3 5 9
Conquer
4 6 7 20 1 3 5 9
1 3 4 5 6 7 9 20
assumes n is a power of 2
Divide and Conquer Technique 46
Karatsuba Multiplication
To multiply two n-digit integers:
Add two ½n digit integers.
Multiply three ½n-digit integers.
Add, subtract, and shift ½n-digit integers to
obtain result.
A B A C C
T(n) n
T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) T(n/4) 9(n/4)
... ...
... ...
AB = ( A1104 + A0 ) ( B1104 + B0 )
= A1B1108 + (A1B0+A0B1)104 + A0B0
XY - A1B1 - A0B0
= A1B0+A0B1
AB = ( A1104 + A0 ) ( B1104 + B0 )
= A1B1108 + (A1B0+A0B1)104 +A0B0
= A1B1108 + (XY - A1B1 - A0B0)104 + A0B0
T(n) = 3T(n/2) + O(n)
T(1) = O(1)
Recursively compute XY, A1B1 , A0B0,
T(n) = nlog23 = O(n1.59)
3 multiplications
6 additions
53
2 Conquer
Divide and shiftings
Technique
Matrix Multiplication
Matrix Multiplication
Matrix multiplication. Given two n-by-n matrices A and B,
compute C = AB.
7 multiplications.
18 = 10 + 8 additions (or subtractions).
Analysis.
Assume n is a power of 2.
T(n) = # arithmetic operations.
Divide and Conquer Technique 58
Fast Matrix Multiplication in Practice
Implementation issues.
Sparsity.
Caching effects.
Numerical stability.
Odd matrix dimensions.
Crossover to classical algorithm around n = 128.
21
12
8
21
12
21
δ = min(12, 21)
12
21
δ = min(12, 21)
12
L
7
5
4 21
δ = min(12, 21)
12 3
L
7
5
4 21
δ = min(12, 21)
12 3
26
return δ.
}