Divide and Conquer
Click to add Text
Plan
Paradigm
Merge sort
Quick sort
Binary search
Integer multiplication
Matrix multiplication
2
Paradigm
Given a problem of size n divide it into
subproblems of size bn , a ≥ 1, b > 1 (*)
(a)
Solve each subproblem recursively.
Combine solutions of subproblems to get
overall solution.
n
T(n) = aT( b ) + [work for merge]
3
Plan
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
Most efficient general purpose sort, O(nlogn)
Simple quicksort has worst case of O(n2), which can be avoided
Basic strategy
Split array (or list) of data to be sorted into 2 subarrays so that:
Everything in first subarray is smaller than a known value
Everything in second subarray is larger than that value
Technique is called ‘partitioning’
Known value is called the ‘pivot element’
Once we’ve partitioned, pivot element will be located in its final
position
Then we continue splitting the subarrays into smaller subarrays,
until the resulting pieces have only one element (using
recursion)
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
Find a pivot v (the first element)
Partition the array A[i,j ] into 2 subarray: A[i, k] and A[k+1, j]
Call quicksort recursively on A[i, k] and A[k+1, j] until we can’t find
any pivot
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)
void quickSort (int i, int j){
if (i<j){
int r = partition(i,j);
quickSort(i,r);
quickSort(r+1, j);
}
}
18
Quicksort performance
The worst case: T(n) = O(n2)
The average case: T(n) = O(nlogn)
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)
Tom-Cook multiplication: O(n1.46)
29
Plan
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:
T(n) = 8T(n/2) + O(n2)
T(n) = O(n3)
32
Matrix multiplication
Strassen algorithm
33
Matrix multiplication
Strassen algorithm
T(n) = 7T(n/2) + O(n2)
T(n) = O(nlog7)
Example n = 70:
Basic multiplication: 343.000
Strassen: ~150.000
François Le Gal: O(n2.37)
34
Thanks for your attention!
35