Divide and Conquer
Divide and Conquer
Paradigm
Merge sort
Quick sort
Binary search
Integer multiplication
Matrix multiplication
2
Paradigm
Paradigm
Merge sort
Quick sort
Binary search
Integer multiplication
Matrix multiplication
4
Insertion sort
5
Insertion sort
Example
5 6 2 2 10 12 9 10 9 3
5 6 2 2 10 12 9 10 9 3
i=2 5 6
i=3 2 5 6
i=4 2 2 5 6
i=5 2 2 5 6 10
i=6 2 2 5 6 10 12
i=7 2 2 5 6 9 10 12
i=8 2 2 5 6 9 10 10 12
i=9 2 2 5 6 9 9 10 10 12
i=10 2 2 3 5 6 9 9 10 10 12
6
Insertion sort
Algorithm
void insertSort(){
int i,j;
for(i=2;i<=n;i++){
j=i;
while (j>1 && a[j].Key<a[j-1].Key){
Swap(&a[j], &a[j-1]);
j=j-1;
}
}
}
T(n) = O(n2)
Any better solutions?
7
Merge sort
MergeSort A[1..n]
if (n==1) Done
else
MergeSort A[1..n/2]
MergeSort A[n/2+1, n]
Merge the two sorted sub array
8
Merge sort
Example
9
Merge sort
Complexity
T(1) = 1
T(n) = 2T(n/2) + O(n)
T(n) = O(nlogn)
10
Plan
Paradigm
Merge sort
Quick sort
Binary search
Integer multiplication
Matrix multiplication
11
Quicksort overview
12
Pivoting
The first element
The last element
The middle-most element
13
Partitioning
Usages of 2 indexes l, r (left, right)
Move left index l until we find an element > pivot
Move right index r until we find an element <= pivot
If indexes haven’t crossed (l<=r), swap values and repeat
movements
14
Quicksort algorithm
15
Quicksort example
16
partition(i, j)
int partition(int i, int j){
int l, r;
KeyType pivot = a[i].key;
l=i;
r=j;
while (l<r){
do{ l=l+1;} while (a[l].key<=pivot);
do{ r=r-1;} while (a[r].key> pivot);
if (l<r) swap(&a[l], &a[r]);
}
swap(&a[i], &a[r]);
return r;
}
17
quickSort(i, j)
18
Quicksort performance
See book
19
Plan
Paradigm
Merge sort
Quick sort
Binary search
Integer multiplication
Matrix multiplication
20
Search a value in an array
Input:
x: a value to look
An array A with n elements
Output
1 if x is found in A, 0 if not
21
Sequential search
int linearSearch(int x, int A[], int n){
int i;
for(i=0; i<=n-1;i++)
if (x == A[i]) return 1;;
return 0;
}
T(n) = O(n)
Any better solutions?
22
Binary search
Assume that A is sorted
int binarySearch(int x, int A[], int low, int high){
if (low > high) return 0;
else{
int mid = (low+high)/2;
if (x==A[mid]) return 1;
else if (x<A[mid])
return binarySearch(x, A, low, mid-1);
else
return binarySearch(x,A, mid+1, high);
}
23
Binary search analysis
T(1) = 1
T(n) = T(n/2) +1, n>1
T(n) = O(logn)
24
Plan
Paradigm
Merge sort
Quick sort
Binary search
Integer mulitiplication
Matrix multiplication
25
Integer multiplication
Input:
2 integer X, Y; each has n-digits
Output
R = X.Y
Basic integer multiplication: O(n2)
26
Integer multiplication
Divide and conquer
Assume: n = power of 2
Rewrite
X = Abn/2 + B
Y = Cbn/2 + D
The base of X, Y is b
XY = ACbn + (AD+BC)bn/2 + BD
Example
X = 5127 Y = 7093 b = 10
X = 51.102 + 27 A = 51 B = 27
Y = 70.102 + 93 C = 70 D = 93
27
Integer multiplication
Divide and conquer
T(1) = 1
T(n) = 4T(n/2) + O(n)
T(n) = O(n2)
28
Integer multiplication
Karatsuba algorithm
Improvement
X.Y=A.C.10n + [(A-B).(D-C)+A.C+B.D].10n/2 + B.D
T(1) = 1
T(n) = 3T(n/2) + O(n)
T(n) = O(nlog3)
Paradigm
Merge sort
Quick sort
Binary search
Integer multiplication
Matrix multiplication
30
Matrix multiplication
Input:
Matrices X, Y have n*n size
Output
Calculate Z = X*Y
Basic calculation:
T(n) = O(n3)
31
Matrix multiplication
Divide and conquer
Divide each X, Y into 4 sub matrices:
32
Matrix multiplication
Strassen algorithm
33
Matrix multiplication
Strassen algorithm
Example n = 70:
Basic multiplication: 343.000
Strassen: ~150.000
34
Thanks for your attention!
35