0% found this document useful (0 votes)
14 views

Module 1 Chapter 2

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Module 1 Chapter 2

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 73

DESIGN AND ANALYSIS OF

ALGORITHMS

DIVIDE AND CONQUER


Algorithm Analysis and Design
DIVIDE AND CONQUER
STRATEGY

1. Divide
2. Conquer
3. Combine
Algorithm Analysis and Design

Problem
Control Abstraction for D and C

✓ A control abstraction is a procedure whose flow of control is clear but whose


primary operations are specified by other procedures whose precise meanings are
left undefined.

✓ 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.

Search here Key Search here


If key<A[m] If key>A[m]
A[0
]………..A[m-1] A[m] A[m+1]…..A[n-1]
Iterative Binary Search
Algorithm BinarySearch(A[0…n-1],key)
{
//Problem Description: This algorithm is for searching the element using binary search
//Input: An array A from which the key element is to be searched
//Output: Returns the index of an array element if it is equal to key otherwise it returns -1
low0 highn-1 while(low<=high) do
{
m(low+high) /2
if(key==A[m]) then
return m else
if(key<A[m]) then
highm-1 else

lowm+1 }
return -1
}

Binary search problem


index 0 1 2 3 4 5 6 7 8
1 3 4 6 7 8 10 13 14
Key=13

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.

T(n)= Ω(1) Best Case Complexity


2.Worst Case: The worst case occurs when the item to be
searched is compared with all the elements in the array.
The Recurrence relation can be written as
T(n)= 1 if n=1
T(n/2)+1 otherwise
Time required to Time required to
search either left/right part compare the middle
of the array element

Worst Case Complexity Derivation


T(n)=T(n/2)+1
= 1+T(n/4)+1
=2+T(n/4)
=2+T(n/8)+1
=3+T(n/8)
=i+T(n/2i)

To get initial condition replace 2i by n


T(n) =i+T(n/n)
T(n) =i+T(1)
T(n) =i+1 Eq(1)

Take log on both sides for the equation 2i=n


log22i=log2n

i log22=log2n

i= log2 n Substitute i in Eq(1)


T(n)=log2n+1
T(n)=log2n Worst Case Complexity

Performance Of Binary
Search Algorithm

Average
Best Case Worst Case
Case
• O(1) • O(logn) • O(logn)
Algorithm Analysis and Design

Finding Maximum & Minimum


Algorithm straightmaxmin(a,n,max,min)
{
max:=a[0]; i=1 1,2,4,5

min:=a[0]; Max=2 2>1 for i:= 1 to n-1 do


Min=1 2<1

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

{ 3>2 max=3 if (a[i]>max) then


max:=a[i];
else if (a[i]<min) then min:=a[i];
}
Algorithm Analysis and Design

Best case- sorted in ascending order - n-1Comparisons


Worst case- sorted in descending order - 2(n-1)
Comparisons

Divide and Conquer Approach


• Split input into smaller subsets
• Repeat until input size is 1 or 2
Algorithm Analysis and Design

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

Time required to Time required to Time required to find max &min in


second sublist
find max &min in conquer max &min first sublist from
both the
sublist
Recurrence Relation
T(n)=2T(n/2)+2 eq(1)
Substitute n/2 in place of n in eq(1)
T(n/2)=2T(n/4)+2 eq(2)
Substitute n/4in place of n in eq(1)
T(n/4)=2T(n/8)+2 eq(3)
Substitute eq(2) in eq(1)
T(n)=2(2T(n/4)+2)+2
=4T(n/4)+4+2 eq(4)
substitute eq(3) in eq(4)
T(n)=4(2T(n/8)+2)+4+2
=8T(n/8)+8+4+2
So T(n)= 23 T(n/ 23 )+ 23+ 22 + 21
In general we can write it as
T(n)= 2k T(n/ 2k )+ 21+ 22 + 23 +…..+ 2k eq(5)
Recurrence Relation
T(n)= 2 log2 n-1 T(2)+ 21+ 22
+ 23 +…..+ 2 log2 n-1 =
(
= 2 log2 n+ 21+ 22 + 23 + 3
…..+ 2 log2 n-1 n
log n-1
2 2 2
-
=n/2+[2( -1)] 4
2-1 )
2
/
=n/2+2. n - 2
geometric series formula
2
k
= a(rk-1)
=n/2+n-2
=1.5n-2 log2

=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.

⚫ It divides input array in two


halves, calls itself for the two
halves and then merges the two
sorted halves.

⚫ The merge() function is used for


merging two halves.
Consider the elements
38,27,43,3,9,82,10 using Merge sort
0
1
2
3
4
5
6
Sort the elements 8
3297154
using Merge
Sort
Merge Sort
Algorithm
Algorithm Mergesort (A[0…n-
1], low, high)
{
//P.D: This algorithm is for
sorting the elements using merge
sort. //Input: An Array A of
unsorted elements.
/
/

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

Time required to merge 2 sub lists


Time required to Time required to sort left
part of the sort right part of
array the array

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

T(n)= Ɵ(n log2n )


Master Theorem
Therefore
Performance Of Merge Sort
Algorithm

Average
Best Case Worst Case
Case
• Ɵ(n log2n ) • Ɵ(n log2n ) • Ɵ(n log2n )

Quick Sort
⚫ This sorting algorithm uses the idea of divide
and conquer.

⚫ It finds the element called pivot which divides the


array into two halves in such a way that elements
in the left half are smaller than pivot and elements
in the right half are greater than pivot.

Steps in Quick Sort Algorithm


Quick Sort Algorithm follows 3 steps
•Find pivot that divides the array into two
halves.

•Quick sort the left half.

•Quick sort the right half.


Quick sort Algorithm
Algorithm Quick(A[0…n-1],low, high)
{
//P.D:This algorithm performs sorting of the elements given in arrray A[0..n-1]
//i/p: An array A[0..n-1] in which unsorted elements are given.
//o/p: An array A[0..n-1] which is in sorted format.
if(low<high)
{
//split the array into 2 sub arrays
m=partition(A[low…high]) Quick(A[low….m-
1])
Quick(A[m+1…high])
}
Algorithm partition(a[low…high])
{
//PD:This algorithm partitions the subarray using first element as pivot element.
//i/p:A subarray A with with low as leftmost index of the array and high as the rightmost index of the
array
//o/p:The partitioning of array A is done and pivot occupies its proper position .
pivot=A[low] i=low j=high
while(i<=j) do
{
while(A[i]<=pivot) do
i=i+1
while(A[j]>=pivot) do
j=j-1
if(i<=j) then
swap(A[i],A[j])
}
swap(A[low],A[j])
return j
}

Apply Quick sort algorithm on 5 3 1 9 8 2 4 7

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

Swap A[3] and A[6]


0 1 2 3 4 5 6 7
5 3 1 4 8 2 9 7
P i-> i j <- j
Swap A[4] and A[5]

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

Swap A[low] and A[4]


0 1 2 3 4 5 6 7
2 3 1 4 5 8 9 7

Left sublist pivot Right sublist


Algorithm Analysis and Design

Consider left sublist


0 1 2 3
2 3 1 4
P, i j
2 3 1 4
p i j <-j
Swap A[i] and A[j]
0 1 2 3
2 1 3 4
P j i
Swap A[j] and A[low]
1 2 3 4
p

Consider right sublist


0 1 2
8 9 7
P,i j
0 1 2
8 9 7
P i j
Swap A[i] and A[j]
8 7 9
j i
Swap A[j] and A[low]
7 8 9
So finally we get
1 2 3 4 5 7 8 9
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

Time required to partition into 2


sub lists
Time required to Time required to sort left
part of the sort right part of
array the array

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

T(n)= Ɵ(n log2n )


Master Theorem
Therefore
Performance Of Quick Sort
Algorithm

Average
Best Case Case

•Ɵ(n logn )•Ɵ(n logn )

Worst case Time Complexity


⚫ The worst case occurs when at each invocation of the procedure ,the
current array is partitioned into 2 sub arrays with one of them being empty.

⚫ This situation occurs if all the elements are arranged in ascending order or
descending order.

⚫ Ex: consider an array[22,33,44,55] then partitions would be:

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

Worst Case Time Complexity


Recurrence relation can be written as
0 for n=1
T(n)= T(0)+T(n-1)+n for n>1

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

T(n)= O(n2 ) Worst Case Complexity

Performance Of Quick Sort


Algorithm
Average
Best Case Worst Case
Case
• Ɵ(n logn ) • Ɵ(n logn ) • Ɵ(n2 )

Strassen’s Matrix Multiplicataions


• To multiply 2 matrices of size n i,e C=A*B.we require
8 multiplications and 4 additions.

• Thus the time complexity of normal matrix


multiplications is O(n3).
• But Strassen showed that 2*2 matrix
multiplication can be accomplished in 7
multiplications and 18 additions.
⚫ The Divide and Conquer approach can be used for
implementing Strassen’s matrix multiplication.
⚫ Divide: Divide matrices into sub matrices A0, A1, A2
..etc.
⚫ Conquer: Use a group of matrix multiplication
equations.
⚫ Combine: Recursively multiply sub matrices and get
the final result of multiplication after performing
required addition/subtractions.
Strassesn’s formula’s for Matrix Multiplication
[ A11

[
A12 B11 B12

[= [
X
C11
[[ C12

A21 A22 B21 B22


C21 C22

S1= (A11+ A22 )(B11 +B22 )


S2= (A21+ A22 )B11 C11 =s1+s4-s5+s7
S3= A11(B12 - B22 )
C12 =s3+s5
S4= A22 (B21 –B11 )
C21 =s2+s4
S5= (A11+ A12 )B22
C22=s1+s3-s2+s6
S6= (A21 –A11 ) (B11 +B12 )

S7= (A12 -A22 )(B21 +B22 )

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= A22 (B21 – B11 )


1 43 93 2
= 6 77 6 -
2 5

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

102 134 58 124

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

You might also like