ALGORITHM ANALYSIS AND DESIGN
DIVIDE AND CONQUER
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
1
May 6, 2015 2:15 AM Algorithm Analysis and Design
Problem
Strategy
solution
Problem
solution
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
2
May 6, 2015 2:15 AM Algorithm Analysis and Design
Solving Subproblems
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
3
May 6, 2015 2:15 AM Algorithm Analysis and Design
Control Abstraction
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
4
May 6, 2015 2:15 AM Algorithm Analysis and Design
Control Abstraction for DandC
Algorithm DandC(P)
{ if small(P) then return(P);
else
{divide P into smaller instances P1,P2,- - -, Pk for k>1
apply DandC to each subproblem ;
return combine(DandC(P1),- - - ,DandC(Pk));}}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
5
May 6, 2015 08:04 PM Algorithm Analysis and Design
Time Complexity
T(n) = g(n) when n is small
T(n1)+T(n2)+- - -+T(nk)+f(n)
T(n) = T(1) n=1
a* T(n/b) +f(n) n>1
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
6
May 6, 2015 2:15 AM Algorithm Analysis and Design
Time Complexity Contd..
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
7
May 6, 2015 2:15 AM Algorithm Analysis and Design
Example
T(n) = 1 for n=1
T(n/2)+c for n>1
h(n)=f(n)/nlog b a=c*(logn)0
So u(n)= θ(logn)
T(n)=n log 1 [c+ θ (logn)] = θ (logn)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
8
May 6, 2015 2:15 AM Algorithm Analysis and Design
Binary Search
Algorithm binsearch(a,i,j,x)
{if (i=j) then
{ if (x=a[i]) then return i;
else return 0; }
else
{mid:= (i+j) div 2;
if (x=a[mid] then return mid;
else if (x<a[mid]) then
return binsearch(a,i,mid-1,x);
else return binsearch(a,mid+1,j,x); }}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
9
May 6, 2015 2:15 AM Algorithm Analysis and Design
Computing Complexity
1 3 5 7 9 11 13
Unsuccessfull cases
Example
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
10
May 6, 2015 2:15 AM Algorithm Analysis and Design
Binary Search Complexity �
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
11
May 6, 2015 2:15 AM Algorithm Analysis and Design
Average Case Complexity
T(n)= 20*1+2 1*2+- - - + 2 k-1 * k
= [21*1+2 2*2+- - - + 2 k-1 *( k-1)] +[ 20+2 1+- - - + 2 k-1 ]
= [( k-2)* 2 k-1+2] + [2(k+1)-1]
since we have i=1 ε k i*2i =(k-1)*2(k+1)+2
=k*2 k+2
=θ (nlogn)
so average case complexity θ(logn)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
12
May 6, 2015 2:15 AM Algorithm Analysis and Design
Finding Max & Min
Algorithm smaxmin(i,j,n,max,min)
{max:=a[1]; min:=a[1];
For i:= 2to n do
{if (a[i]>max) then max:=a[i];
if (a[i]<min) then min:=a[i];
}}
2(n-1) Comparisons
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
13
May 6, 2015 2:15 AM Algorithm Analysis and Design
Improvement
Algorithm smaxmin(i,j,n,max,min)
{max:=a[1]; min:=a[1];
For i:= 2to n do
{if (a[i]>max) then max:=a[i];
else if (a[i]<min) then min:=a[i];
}}
Worst case- sorted in descending order - 2(n-1) Comparisons
Best case- sorted in ascending order - n-1Comparisons
Average case – half cases a[i] is greater - n/2+(n-1) =3n/2 -1
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
14
May 6, 2015 2:15 AM Algorithm Analysis and Design
Divide and Conquer Approach
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
15
May 6, 2015 2:15 AM Algorithm Analysis and Design
MAXMIN
Algorithm maxmin(i,j,max,min)
{if (i=j) then max:=min:=a[i];
else if (i=j-1) then
if (a[i]<a[j]) then
{max:=a[j];min:=a[i];}
else {max:=a[i];min:=a[j];}}
else
{ mid:= L(i+j)/2 ;
maxmin( i,mid,max,min);
maxmin(mid+1,j,max1,min1);
if (max<max1) then max:=max1;
if (min>min1) then min:= min1;}}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
16
May 6, 2015 2:15 AM Algorithm Analysis and Design
Time Complexity
T(n)= T(n/2)+T(n/2)+2 for n>2; =1 for n=2
When n is a power of 2, T(n) = 2*T(n/2) +2
= - - - - -
= 2 k-1 *T(2) +2* (2 k-1 –1) = 2 k-1 +2 k –2
= 3*n/2 –2
it is the best, average and worst case complexity.
Compared to the 2n-1 comparisons of straight maxmin this approach is better.
But it requires logn +1 levels of stack.
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
17
May 6, 2015 2:15 AM Algorithm Analysis and Design
Considering Index Comparisons
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
18
May 6, 2015 2:15 AM Algorithm Analysis and Design
Improvement
Algorithm maxmin(i,j,max,min)
{if (i > j) then
if (a[i]<a[j]) then {max:=a[j];min:=a[i];}
else {max:=a[i];min:=a[j];}}
else
{ mid:= L(i+j)/2 ;
maxmin( i,mid,max,min);
maxmin(mid+1,j,max1,min1);
if max<max1) then max:=max1;
if (min>min1) then min:= min1;}}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
19
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
C(n)= 2* C(n/2)+3 for n>2
=2 for n=2
=2 k +3* 2 k-1 –3
=5n/2 –3
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
20
May 6, 2015 2:15 AM Algorithm Analysis and Design
Comparison
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
21
May 6, 2015 2:15 AM Algorithm Analysis and Design
Summary
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
22
May 6, 2015 2:15 AM Algorithm Analysis and Design
Merge Sort
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
23
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm Mergesort
Algorithm mergesort(low,high)
{ if (low<high) then
{ mid:= L (low+high)/2
megesort(low,mid);
mergesort(mid+1,high);
mege(low,mid,high);
}}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
24
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm Merge
Algorithm merge(low,mid,high)
{h:=low;i:=low; j:=mid+1;
while ((h<=mid) and (j<=high)) do
{if (a[h]<=a[j]) then
{b[i]:=a[h];h:=h+1;}
else
{b[i]:=a[j];j:=j+1;}
i:=i+1;}
if (h>mid) then
for k:=j to high do
{b[i]:=a[k];i:=i+1;}
else
for k:=h to mid do
{b[i]:=a[k];i := i+1;}
for k:=low to high do a[k]:=b[k];
}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
25
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
T(n) = 2 T(n/2) + cn
= 22 T(n/4) + 2cn
= 2k T(n/2k) + kcn
= 2k T(1) +kcn = an + cn log n
= O(n log n)
T(n)= 2*T(n/2)+cn for n>1
a for n=1
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
26
May 6, 2015 2:15 AM Algorithm Analysis and Design
Refinements
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
27
May 6, 2015 2:15 AM Algorithm Analysis and Design
Refinement 1
Algorithm insertion sort(a,n)
{for j:=2 to n do
{ item := a[j]; i := j-1;
while ((i>=1) and (item<a[j])) do
{a[i+1]:= a[i]; i := i-1; }
a[i+1] := item; } }
For n<16 insertion sort is used.
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
28
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
29
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm2
algorithm mergesort2(low,high)
{if (high-low)<15 then ///when size is <16
return insertionsort1(a,link,low,high)
else
{ mid := L(low+high)/2 ///divides into 2
q:=mergesort(low,mid);
r:=mergesort(mid+1,high);
return merge1(q,r);}}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
30
May 6, 2015 2:15 AM Algorithm Analysis and Design
Refinement 2
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
31
May 6, 2015 2:15 AM Algorithm Analysis and Design
Demonstration
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
25
0
11
0
link |
value |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
1
18
0
17
0
13
0
45
0
28
0
30
0
3
6
8
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
32
May 6, 2015 2:15 AM Algorithm Analysis and Design
25
0
11
1
18
0
17
3
Demonstration Contd..
0 | 1 | 0 | 3 | 6 | 0 | 8 | 0 |
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
3
4
1
13
6
45
0
28
8
30
0
6
7
8
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
33
May 6, 2015 2:15 AM Algorithm Analysis and Design
Demonstration Contd..
0 | 4 | 1 | 3 | 7 | 0 | 8 | 6 |
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
0 | 4 | 1 | 3 |
25 | 11 | 18 | 17 |
7 | 0 | 8 | 6 |
13 | 45 | 28 | 30 |
5
4
3
1
7
Sorting Over
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
34
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm3
Algorithm merge1(q,r)
{i:=q;j:=r;k:=0;
while ((i<>0) and j<>0)) do
{if (a[i] < a[j]) then
{link[k]:=i;k:=i;i:=link[i];}
else {link[k]:=j;k:=j;j:=link[j];}}
If (i = 0) then link[k]:=j; else link[k]:=i;
return link[0];}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
35
May 6, 2015 2:15 AM Algorithm Analysis and Design
Quick Sort
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
36
May 6, 2015 2:15 AM Algorithm Analysis and Design
Working Principle
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
37
May 6, 2015 2:15 AM Algorithm Analysis and Design
Partition
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
38
May 6, 2015 2:15 AM Algorithm Analysis and Design
Working
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
13 | 11 | 18 | 17 | 25 | 45 | 28 | 30 |
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
39
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm Quicksort
Algorithm quicksort(p,q)
{ if (p<q) then
{j:=partition(a,p,q+1);
quicksort(p,j-1);
quicksort(j+1,q);}}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
40
May 6, 2015 2:15 AM Algorithm Analysis and Design
Algorithm Partition
algorithm partition(a,m,p)
{v:=a[m]; i:=m; j:=p;
repeat
{ repeat
i:= i+1;
until (a[i]>=v);
repeat
j:= j-1;
until (a[j]<=v);
if (i<j) then
{temp:=a[i];
a[i]:=a[j];
a[j]:=temp;}
until (i>=j);
a[m]:=a[j];
a[j]:=v;
return j;}
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
41
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
25 | 11 | 18 | 17 | 13 | 45 | 28 | 30 |
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
42
May 6, 2015 2:15 AM Algorithm Analysis and Design
Best Case Complexity
T(n) = 2 T(n/2) + an
= 22 T(n/4) + an + an
= 2k T(n/2k) + akn
= n + an log n
= O(n log n)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
43
May 6, 2015 2:15 AM Algorithm Analysis and Design
Worst Case Complexity
= O(n2)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
44
May 6, 2015 2:15 AM Algorithm Analysis and Design
Average Case�
Ca(N)= n+1 + 1 ε k (Ca(k-1)+Ca(n-k))
Ca(N)= n+1 +1/n* 1 ε k (Ca(k-1)+Ca(n-k))
n*Ca(n)= n*(n+1) + 1 ε k (Ca(k-1)+Ca(n-k))
= n*(n+1) +2[ca(0)+Ca(1)+- - - + Ca(n-1)]
(n-1)*Ca(n-1) = n*(n-1) +2[ca(0)+Ca(1)+- - - + Ca(n-2)]
(n)*Ca(n)- (n-1)*Ca(n-1) = 2*n +2*Ca(n-1)
Ca (n) 2 + Ca(n-1)
n+1 = n+1 n
Ca (n) 2 + 2 + Ca(n-2)
n+1 = n+1 n n-1
= 3 ε n+1 (1/k) + Ca(1)/2
<= 1/x dx = log(n+1)-log 2 = O(logn) So Complexity is O(nlogn)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
45
May 6, 2015 2:15 AM Algorithm Analysis and Design
Comparison
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
46
May 6, 2015 2:15 AM Algorithm Analysis and Design
Matrix Multiplication
For i:= 1 to m
For j:=1 to p
c[i,j]:=0;
For k:= 1 to n
c[i,j]:=c[i,j] + a[i,k]*b[k,j]
Complexity is O(mnp)
Complexity is O(n3) if both matrices are n x n
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
47
May 6, 2015 2:15 AM Algorithm Analysis and Design
Divide and Conquer Approach
A11 A12 B11 B12 C11 C12]
A21 A22 B21 B22 C21 C22]
[
[
[
[
[
[
X
=
a13 a14
a23 a24
a11 a12
a21 a22
a31 a32
a41 a42
a33 a34
a43 a44
b13 b14
b23 b24
b11 b12
b21 b22
b31 b32
b41 b42
b33 b34
b43 b44
C11=A11B11+ A12B21
C12=A11B12+ A12B22
C21=A21B11+ A22B21
C22=A21B12+ A22B22
c13 c14
c23 c24
c11 c12
c21 c22
c31 c32
c41 c42
c33 c34
c43 c44
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
48
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
T(n)= a for n<=2
= 8 T(n/2)+cn2 for n>2
T(n)= 8 T(n/2)+cn2
= 8[8 T(n/22)+c(n/2)2 ]+cn2
= 82 T(n/22)+2cn2 +cn2
= 83 T(n/23)+22cn2 + 2cn2 + cn2
- - - - - - - - - - - - -
= 8k T(n/2k)+2k-1cn2 + - -+2cn2 + cn2
= 8k* a +[2k-1]cn2 = a*(23 )k +[n-1]cn2=O(n3)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
49
May 6, 2015 2:15 AM Algorithm Analysis and Design
Refinements
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
50
May 6, 2015 2:15 AM Algorithm Analysis and Design
Strassen’s Multiplication�
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
51
May 6, 2015 2:15 AM Algorithm Analysis and Design
Formulation
P=( A11 +A22 ) (B11+ B22)
Q=( A21 +A22 ) B11
R= A11 (B12 - B22)
S = A22 (B21 – B11)
T=( A11 +A12 ) B22
U=( A21 – A11 ) (B11+ B12)
V=( A12 - A22 ) (B21+ B22)
C11=P+S-T+V
C12= R+ T
C21=Q+S
C22= P+R-Q+U
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
52
May 6, 2015 2:15 AM Algorithm Analysis and Design
Complexity
T(n)= a for n<=2
= 7 T(n/2)+cn2 for n>2
T(n)= 7 T(n/2)+cn2 = 7[7 T(n/22)+c(n/2)2 ]+cn2
= 72 T(n/22)+(7/4)cn2 +cn2
- - - - - - - - - - - - -
= 7k T(n/2k)+ (7/4) k-1cn2 + - -+ (7/4) cn2 + cn2
= 7k* a +[(7/4) k-1]cn2 / [7/4-1]
≡ a* 7logn +(7/4) logn*cn2= a* nlog7 +n logn7/4*cn2
= a* nlog7 +c*n logn7-log 4+log 4 = O(nlog 7) = O(n2.81)
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
53
May 6, 2015 2:15 AM Algorithm Analysis and Design
Discussion
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
54
May 6, 2015 2:15 AM Algorithm Analysis and Design
Computer Science and Engineering, M.A.College of Engineering, Kothamangalam
55