Module 1 Chapter 2
Module 1 Chapter 2
ALGORITHMS
1. Divide
2. Conquer
3. Combine
Algorithm Analysis and Design
Problem
Control Abstraction for D and C
✓ The control abstraction for divide and conquer technique is DANDC(P), where P is
the problem to be solved.
Algorithm DAndC(P)
{
if SMALL(P) then return S(P);
else {
divide P into smaller instances P1,P2,- - -, Pk for k>1
apply DAndC to each of these subproblems;
return combine(DAndC(P1),- - - ,DAndC(Pk));
}
}
Binary Search Algorithm
⚫ Binary search is the search technique which works efficiently on the sorted
lists.
⚫ Binary search follows divide and conquer approach in which, the list is
divided into two halves
⚫ The search item is compared with the middle element of the list. If the
match is found then, the location of middle element is returned otherwise.
⚫ Else we search into either of the halves depending upon the result produced
through the match.
lowm+1 }
return -1
}
Mid=(low+high)/2 (0+8)/2=4
1 3 4 6 7 8 10 13 14
If (key==a[mid]) 13>7
if(13==7) false
8 10 13 14
Mid=(5+8)/2=6.5≈7
8 10 13 14
true
If key==a[mid] If(13==13)
Returns index=7
Binary Search Example2
Binary Search Example3
Recursive Binary Search
Algorithm Binarysearch(a,i,j,x)
{
if (i==j) then
{
if (x==a[i]) then return i;
else return 0;
}
else
{
mid:= (i+j)/2; if (x==a[mid]) then
return mid; else if (x<a[mid])
then return Binarysearch(a,i,mid-
1,x); else return
Binarysearch(a,mid+1,j,x);
}
}
Time Complexity
1.BestCase: The Best occurs when the item to be searched is
present in middle of the array. so the total number of
comparisons is 1.
i log22=log2n
Performance Of Binary
Search Algorithm
Average
Best Case Worst Case
Case
• O(1) • O(logn) • O(logn)
Algorithm Analysis and Design
i=2
{ 4>2 max=4 if (a[i]>max) then
4<1 min=1
max:=a[i]; i=3
if (a[i]<min) then min:=a[i]; 5>4 max=5
} 5<1 min=1
n-1+n-1=2n-2=2(n-1)
Algorithm Analysis and Design
}
The above algorithm requires 2(n-1) Comparisons
Improvement
Algorithm straightmaxmin(a,n,max,min)
{
2 3 4 5 max:=a[0]; min:=a[0]; Max=2
Min=2
for i:= 1to n-1do i=1
MaxMin Algorithm
Algorithm Analysis and Design
Algorithm maxmin(i,j,max,min) 20 10
{if (i==j) then max:=min:=a[i]; i=0 J=1
else if (i==j-1) then{ If(a[0]<a[1])=10<20
if (a[i]<a[j]) then Max=20
{ Min=10
max:=a[j];min:=a[i];
20<10 F
}
Max=20
else
Min=10
{ max:=a[i];min:=a[j]
;
}
}
else
{ mid:= (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; }}
Finding the maximum and minimum from a given list of n elements
0 1 2 3 4 5 6 7 8
50 40 -5 -9 45 90 65 25 75
i j
0 8 90 -9
Mid=(i+j)/2
0 4 50 -9 5 8 90 25
0 2 50 -5 3 4 45 -9 5 6 90 65 7 8 75 25
0 1 50 40 2 2 -5 -5
i j max min
Finding the maximum and minimum
from a given list of n elements
Consider the elements 5,7,1,4,10,6
0 1 2 3 4 5
5 7 1 4 10 6
0 5 10 1
0 2 7 1 3 5 10 4
0 1 7 5 2 2 1 1 3 4 10 4 5 5 6 6
Algorithm Analysis and Design
Time Complexity
T(n)= = 0 for n=1
1 for n=2
T(n/2)+T(n/2)+2 for n>2
=O(n) n=(k+1)
log22
Assumption
K= log
Let n/ 2k =2
2 n-1
n= 2k .2 n= 2k+1
Apply log on both the sides
Performance Of
Max &
Min
Algorith
m
Average
Best Case Worst C
Case
• O(n) • O(n) • O
MERGE
SORT
⚫ Merge sort is an efficient,
general-purpose, comparison
based sorting algorithm.
⚫ Merge Sort is a Divide and
Conquer algorithm.
O
u
t
p
u
t
:
S
o
r
t
e
d
a
r
r
a
y
A
[
0
…
n
-
1
]
i
f
(
l
o
w
<
h
i
g
h
)
t
h
e
n
{
mid:= (low+high)/2
Mergesort(A,low,mid);
Mergesort(A,mid+1,high);
Merge(A,low,mid,high);
}
}
Algorithm Analysis and Design
Algorithm Merge
Algorithm Merge(A, low, mid, high) //Copy remaining
While(i<=mid) do elements of left sub
list to temp
{k:=low ; i:=low; j:=mid+1; {
while ((i<=mid) and (j<=high)) temp[k]:=A[i];
do i:=i+1;
{ //If smaller element is present in left sub list copy that k:=k+1;
smaller element to temp array
if (A[i]<=A[j]) then }
{ temp[k]:=A[i] While(j<=high) //Copy remaining
elements of right sub
do list to temp
i:=i+1;k:=k+1;}
{
else
//If smaller element is present in right sub list copy that temp[k]:=A[j];
smaller element to temp array j:=j+1;
k:=k+1;
{ temp[k]:=A[j];
j:=j+1;
k:=k+1;}}
}}
Algorithm Analysis and Design
Time Complexity
Recurrence relation can be written as
T(n) = 0 for n=1
T(n/2)+T(n/2)+n for
n>1
Recurrence Relation
T(n)=2T(n/2)+n
The above equation is of the form T(n)=a
T(n/b)+f(n)
In the above equation a=2,b=2,d=1
a=bd holds good
2=21
Average
Best Case Worst Case
Case
• Ɵ(n log2n ) • Ɵ(n log2n ) • Ɵ(n log2n )
Quick Sort
⚫ This sorting algorithm uses the idea of divide
and conquer.
0 1 2 3 4 5 6 7
5 3 1 9 8 2 4 7
P, i j
0 1 2 3 4 5 6 7
5 3 1 9 8 2 4 7
P i -> i -> i j <- j
0 1 2 3 4 5 6 7
5 3 1 4 2 8 9 7
P i j
0 1 2 3 4 5 6 7
5 3 1 4 2 8 9 7
P j i
T(n/2)+T(n/2)+n for
n>1
Recurrence Relation
T(n)=2T(n/2)+n
The above equation is of the form T(n)=a
T(n/b)+f(n)
In the above equation a=2,b=2,d=1
a=bd holds good
2=21
Average
Best Case Case
⚫ This situation occurs if all the elements are arranged in ascending order or
descending order.
22 [33,44,55] //after partition, there are no elements at the left of 22 and 3 elements at
right side of 22
⚫ In general, if the array has n elements after partitioning, in the worst case
n-1 elements will be there towards right & 0 elements will be there towards
left of the pivot element
Time required to
partition It indicates that there are no elements towards left & it Time
the array into 2 subarrays
required to
can be equated to zero sort remaining n-1
required to partition the elements
array into 2 subarrays
Recurrence Relation
T(n)=T(0)+T(n-1) +n eq(1)
T(n)=T(n-1) +n
= T(n-2) +(n-1) +n =
T(n-3) +(n-2)+(n-1) +n
:
:
In general we can write it as
T(n)=T(n-n) + 1+ 2+3 +..+(n-2)+(n-1)+ n
=T(0) +1+ 2+3 +..+(n-2)+(n-1)+ n
= 0 + 1+ 2+ 3+..+(n-2)+(n-1)+ n
=1+ 2+ 3+..+(n-2)+(n-1)+ n
=n(n+1)/2
=n2 /2 + n/2
[
A12 B11 B12
[= [
X
C11
[[ C12
Problem on Strassen’s
Implement Strassen’s matrix multiplication on A and B
5 3 0 23 247
4 3 2 62 529
A=7 8 1 4B=3 903
9 4 6 77 621
The given matrix is of the order 4*4.Hence we will subdivide it into 2*2
sub matrices.
1 2
13
2
4 7
5
3
0
2
1 2 5 2 9
4 3 2 6
A=B=
3 9 0 3 7 8 1 4
22 7 6 2 1
9 4 6 7
S1= (A11+ A22 )(B11 +B22 )
5 31 420
3 3
= 4 3+ 6 7 2 5+2 1
S1= =6736*3+7*4 6*5+7*6
10 104 6 10*3+10*4 10*5+10*6
46 72
S1=
70 110
S2= (A21+ A22 )B11
3 2
79 84+16 472
5
48 76
S2=67 85
S3= A11(B12 - B22 )
54 33 42 79 - 02
31
S3 =20 44
16 40
S4 =20 11
35 49
S5= (A11+ A12 )B22
= 5 3+0 20 3
4 32 62
1
10 20
S5 =
18 27
S6= (A21 –A11 ) (B11 +B22 )
7 85 33 24 7
= 9 4- 4 32 5 +2 9
34 88
S6 =39 59
S7= (A12 -A22 )(B21 +B22 ) 02
26 - 16 47 * +37 96 02
31
-21 -26 S7 =
-21 -55
C11 =s1+s4-s5+s7
= 35 37C22=s1+s3-s2+s6 66 77
=5258 128
124
C12 =s3+s5
=30 64
34
67
C21 =s2+s4
68 87 =
102 134
Thus the final product matrix c will be
C11 C12
=3566 3777 30 34 6467
C21 C22
68 87 52 128
Time Complexity
T(n)= 1 n=1
7T(n/2)+18(n/2)2 n>1
By using Master’s Theorem
T(n)=a T(n/b)+f(n)
T(n)= 7T(n/2)+18(n/2)2
a=7,b=2,d=2 Since a>
bd
T(n)=Ɵ(nlogba )
=Ɵ(nlog27 )=Ɵ(n2.807 )
T(n)= Ɵ(n2.807 ) Time Complexity
Algorithm Analysis and Design
Thank You