You are on page 1of 25

MODULE-2

Design and analysis of algorithm


Module-2
Contents

Divide and Conquer : General method, Binary search, Recurrence equation for divide and
conquer, Finding the maximum and minimum, Merge sort, Quick sort, Strassen’s matrix
multiplication, Advantages and Disadvantages of divide and conquer. Decrease and
Conquer Approach : Topological sort.

Assignment Submitted by:

1.nagratna
2. maitre
3.arshiya afreen
4.sushma
5.mamtha patil
6.karishma
7.rekha.b
8.aishwarya
9.prajwal
1o.raju

Guided by:
Tejeshwini C SB.E., M.Tech
Assistant Professor

Divide and Conquer


 Definition:
Divide & conquer is a general algorithm design strategy with a general plan as follows:
1. DIVIDE:

CSE,DEPT Page 1
MODULE-2

A problem‘s instance is divided into several smaller instances of the same


problem, ideally of about the same size.
2. RECUR:
Solve the sub-problem recursively.
3. CONQUER:
If necessary, the solutions obtained for the smaller instances are combined to get a
solution to the original instance.

NOTE:
 The base case for the recursion is sub-problem of constant size.

GENERAL METHOD

 DIVIDE & CONQURE ALOGRITHMS WORKS ACCORDING TO THE


FOLLOWING GENERAL PLAN:-
 A problem is instance is divided into several smaller instances of same problems.
 The smaller instance are solved.
 If necessary the solution obtain for the smaller instances are combined to get a solution to the
original problem

 ALGORITHM D & C TECHNIQUE:-


Problem of size

Sub problem 2
Set problem1 of Of size n/2
size n/2

Solution to sub problem1 Solution to sub progrAM2

SOLUTION TO THE ORIGINAL


PROBLEM
STEP 1:- if problem (p) is small enough, find its solution without applying D&C S(P),
STEP 2:- if problem is large ,divide P into smaller instance P1,p2…..Pk where k is >1.
STEP3:- apply D & C method to each of sub problem recursively.
STEP 4:- return combine (D And C(P1).D and C(P2)………………D and C(PK));

 ALORITHM D and C(P)

CSE,DEPT Page 2
MODULE-2
{ if small (p) then s(p);
Else
{
Divided p into smaller instance p1,p2,p3….Pk,,,,, k>1
Apply DA and C tom each of these sub problems;
Return combine(D And C(P1),D And C(P2),,,,,,,,,,,,,,,D And(p3))
}
}

The complexity of many divide &conquer algorithms is given by recurrence of the form

T(n)={G(N)
T(N1)+T(N2)+…..+T(NK)+F(N)
N is small

STEP 1: A problem instance is divided into several smaller instance of the same kind, identically of the same size.
STEP 2: A smaller instance of problem are solved recursively.
STEP 3: If necessary the solution obtained for the smaller instances combined to get solution for instance.

NOTE :
1. D AND C TOP DOWN APPORACH LIKE FUNCTIONS.
2. IN GENERAL D&C TECHNIQUE IS A SEQUENTIOL.

 Binary Search

Description:
* Binary search is a divide and conquer technique for searching a key in any set (numbers or characters)
* Binary search can applied only for sorted or ordered set (ascending order)
* Here we divide the entire array into two equal parts hence it is called binary search

Algorithm steps:-
Step 1: find the mid value by the formula
Mid= low + high /2
Step 2: It works by comparing a search tree k with array mid elements
A[mid]
Step 3: If they match the algorithms stop by returning the mid value
Step 4: otherwise the same operation is repeated recursively for the first half of the array if k>a[mid]
Step 5: else return-1 for the unsuccessful search

CSE,DEPT Page 3
MODULE-2

Algorithm:-

Algorithm can be implemented as recursive or non-recursive algorithm.

ALGORITHM Binary Search ( A[0 … n-1], key)


//implements non-recursive binary search
//Input: Array A in ascending order, key k
//Output: Returns position of the key matched else -1

l→0
r→n-1

while l ≤ r do
m→( l + r) / 2
if key = = A[m]

return m
else
if key < A[m]
r→m-1
else
l→m+1
return -1

Analysis:-
• Input size: Array size, n
• Basic operation: key comparison
• Depend on:
Best – key matched with mid element
Worst – key not found or key sometimes in the list
• Let C(n) denotes the number of times basic operation is executed. Then
Cworst(n) = Worst case efficiency. Since after each comparison the algorithm
divides the problem into half the size, we have
Cworst(n) = Cworst(n/2) + 1 for n>1
C(1) = 1
• Solving the recurrence equation using master theorem, to give the number of
times the search key is compared with an element in the array, we have:
• C(n) = C(n/2) + 1
a=1
b=2
f(n) = n0 ; d = 0
case 2 holds:
C(n) = Θ (ndlog n)
= Θ (n0log n)
= Θ ( log n)
CSE,DEPT Page 4
MODULE-2

Example:-
20 32 43 49 52

n=5 k=32
1st iteration:- 2nd iteration :- 3rd iteration
l=0 r=4 l=0 r=1 l=1 r=1
0< =r 0<=1 1<=1
True True True

m=0+4/2=2 m=0+1/2=0 m=1+1/2


32=A [2] 32=20 32=32
32=43 False True
False

32< =A [2] 32<=A[0] return 1


32<=43 32<=20
True False
r=1 return-1

Finding maximum and minimum in an array:


Min & Max of an array by using brute force method:
5 4 -1 -9
Mi a[0]= 5 4 -1 -9
Ma a[0]= 5
1. The draw back in the brute force technique is algorithm dates takes timt t(n) to get the results.
2. This can be done using recursive method that uses D & C technique.
3. In the algorithm the list of elements is divided at the mid in order to obtain 2 sub list.
4. From both sub list max and min element are chosen.
5. Two max & min are compared from real max & real min elements are determined.
6. The process continous recursively till the end of a list.

5 4 -1 -9

5 4 -1 -9
Max=5 max=-9 5
Min=4 min=-1

Divide the array into 2 parts until it becomes the array of 2 nos.
Apple conquer process by comparing 2 temp min & max hence we find out real min & max.
Max= 5 > -9
Min= 4< -1 =-1

Dept of CSE Page 22


MODULE-2

ALGORITHM:
Max _ Min _Val(I, j, max, min)
{
if(i==j)then
{
maxA[i]
minA[j]
}
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 (i+j)/2
max _ min_ val (i, mid, max ,min)
Max _ Min _Val(mid+1,max_new,min_new)
if(max<max_ new)then

Dept of CSE Page 23


MODULE-2

maxmax_ new
if(min>min_ new)then
minmin_ new
}

 MERGE SORT
Description:-
 Merge sort is a divide and conquer technique, & it is a sort algorithm that splits the items to be sorted in to two
groups, recursively sorts each group, and merges them into final sorted sequence.
 It sort a given array A[0……..n-1] by dividing into 2 equal i,e A[0…….n/2-1]and b[n/2……..n-1],sorting of
them recursively
 Merge fn is a sub fn under a merge sort for merging 2 smaller sorted arrays into a single sorted one

 THE STEPS OF ALGORITHM ARE:-


• steps are initialize to point to the 1st element of the array being merge
• The element are pointed compared is smaller of them is added to the resultant array being
constructed
• After that index of smaller element is incremented to point to its immediate successor in the array it
was copied from
• The operation is repeated until one of the 2 given array is exhausted and then the remaining
elements of the other array are copied to the end of the resultant array

Algorithm:
ALGORITHM Merge sort ( A[0… n-1] )
//sorts array A[0- -n-1] by recursive merge sort
//Input: An array A[0- -n-1]of orderable elements
//output: array A[0- -n-1] sorted in non decreasing order

if n > 1
copy A[0… (n/2 -1)] to B[0… (n/2 -1)]
copy A[n/2… n -1)] to C[0… (n/2 -1)]
Merge sort ( B[0… (n/2 -1)] )
Merge sort ( C[0… (n/2 -1)])
Merge ( B, C, A )

ALGORITHM Merge ( B[0… p-1], C[0… q-1], A[0… p+q-1] )


//merges two sorted arrays into one sorted array
//Input: arrays B[0..p-1], C[0..q-1], both sorted
//Output: Sorted array A[0…p+q-1] of the elements from B & C

i→0
j→0
k→0
while i < p and j < q do
if B[i] ≤ C[j]
Dept of CSE Page 24
MODULE-2

A[k]→B[i]
i<--i+1
else
A[k] →C[j]
J<--j + 1
K<--k +
1 if i = p
copy C [ j… q-1 ] to A [ k… (p+q-1) ]
else
copy B [ i… p-1 ] to A [ k… (p+q-1) ]

Example:
Apply merge sort for the following list of elements: 6, 3, 7, 8, 2, 4, 5, 1

TRACKING:
2 3 8 9
Take this array as p=4
i=0,j=0,k=0
B[0]<=C[0]
2<=1[false]
A[0]<-C[0]
A[0]<-1
J<-j+1
J<-0+1
J<-1
K<-0+1
K<-1
I=0,j=1,k=1
B[0]<=C[1]
2<=4[true]
A[1]<-B[0]
A[1]<=2
I=i+1=0+1
I<-1
K<-1+1
K<-2
I=1,j=1,k=2
B[1]<=C[1]
3<=4[true]
A[2]<-B[1]
A[2]<-3
I<-i+1<-1+1
I<-2
K<-2+1
K<-3
I=2,j=1,k=3
Dept of CSE Page 25
MODULE-2

B[2]<=C[1]
8<=4[false]
A[3]<-4
J<-j=1<-1=1
J<-2
K<-3+1
K<-4
I=2,j=2,k=4
B[2]<=C[2]8
8<=5[false]
A[4]<-5
J<-j+1<-2+1
J<-3
K<-4+1
K<-5
I=2,j=3,k=5
B[2]<=C[3]
8<=7[false]
A[5]<-7
J<-3+1
J<-4
K<-5+1<-6
I=2,j=4,k=6
I<p &j<q
2<4 4<4 false
Come out of the while loop
And the check,
I=p
2==4 false
Then copy the last two elements 8&9
Now the sorted array is

1 2 3 4 5 6 7 8 9

Dept of CSE Page 26


MODULE-2

Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Best, worst, average case exists:
Worst case: During key comparison, neither of the two arrays becomes empty
before the other one contains just one element.
• Let C(n) denotes the number of times basic operation is executed. Then
C(n) = 2C(n/2) + Cmerge(n) for n > 1
C(1) = 0
where, Cmerge(n) is the number of key comparison made during the merging stage.
In the worst case:
Cmerge(n) = 2 Cmerge(n/2) + n-1 for n > 1
Cmerge(1) = 0
• Solving the recurrence equation using master theorem:
C(n) = 2C(n/2) + n-1 for n > 1
C(1) = 0
Here a = 2
b=2
f(n) = n; d = 1
Therefore 2 = 21, case 2 holds
C(n) = Θ (ndlog n)
= Θ (n1log n)

Quick Sort and its performance:-


Definition:-
• It is a sorting technique under divide and conquer
• Its sort an array a[0….n-1] in non descending order
• Unlike merge sort and binary sort which divider a input array according to their position but in
quick sort divider
• Specially it rearrange the element of the given array to achieve its partition
• A situation where all the elements before some position[split position] are smaller then or equal
to a[s] and all the elements after position s are greater then or equal to a[s]
• After a partition has been achieved a[s] will be in its signal position in the sorted array
• And we continue sorting in to two sub arrays recursively

The algorithm steps are:-


• We select one element for point =a[0]=a[l]
• Initialize the values l and r, l=0,r-n-1
• Take 2 indices values i and j which are pointing to 2 that is i=0 and j=r+1
• Pre increment the value of I until a [i]>=p which stands the array from left to right
• Pre decrement the value of j by 1 until a[j]<=p
• Once both can stop we leave to find the value i and j and if i< j swap a[i] with a[j],continue the
scanning.

ALGORITHM:- Quick sort (A[ l …r ])


//sorts a sub array by quick sort
Dept of CSE Page 27
MODULE-2

//Input: A sub-array A[l..r] of A[0..n-1],defined by its left and right indices l and r
//Output: The sub-array A[l..r], sorted in non
decreasing order if l < r
S<--Partition (A[l..r]) // s is a split
position
Quick sort(A[l..s-1])
Quick sort(A[s+1..r]

ALGORITHM Partition (A[l ..r])


//Partitions a sub-array by using its first element as a pivot
//Input: A sub-array A[l..r] of A[0..n-1], defined by its left and right indices l and r (l < r)
//Output: A partition of A[l..r], with the split position returned as this function‘s
value
p<--A[l]
i<--l;
J<-r+1;
Repeat
repeat i<--i + 1 until A[i] >=p //left-right scan
repeat j<--j – 1 until A[j] < p //right-left scan

if (i < j) //need to continue with the scan

swap(A[i], a[j])

until i >= j
//no need to scan
swap(A[i], A[j])

swap(A[l], A[j])

return j

Analysis:
• Input size: Array size, n
• Basic operation: key comparison
• Best, worst, average case exists:
Best case: when partition happens in the middle of the array each time.
Worst case: When input is already sorted. During key comparison, one half is
empty, while remaining n-1 elements are on the other partition.
• Let C(n) denotes the number of times basic operation is executed in worst case:
Then
C(n) = C(n-1) + (n+1) for n > 1 (2 sub-problems of size 0 and n-1 respectively

C(1) = 1

Best case:
C(n) = 2C(n/2) + Θ(n) (2 sub-problems of size n/2 each)

Dept of CSE Page 28


MODULE-2

Solving the recurrence equation using backward substitution/ master theorem, we have:
C(n) = C(n-1) + (n+1) for n > 1; C(1) = 1
C(n) = Θ (n2)

C(n) = 2C(n/2) + Θ(n).


= Θ (n1log n)
= Θ (n log n)

NOTE:
The quick sort efficiency in average case is Θ( n log n) on random input.

 STRASSEN S MATRIX MULTIPLICATION

C11 c12 a11 a12 b11 b12


C21 c22 a21 a22 b21 b22

S1= (a11+a22) (b11+b22)


S2= (a21+a22) * b11
S3= a11 * (b12-b22)
S4= a22 * (b21-b11)
S5= (a11+a12) * b22
S6= (a21-a11) * (b11+b12)
Dept of CSE Page 29
MODULE-2

S7= |(a12-a22) * (b21+b22)


C11= s1 + s4 - s5 + s7
C12= s4 + s5
C21= s2 + s4
C22= s1+ s3 - s2 + s6

* strassen’s matrix multiplication is d and c method


* it used sever multi &18 add &sub in order to product matrices
* in divide process it divides matrix into sub matrix a0,a1,a2………….a8
* in conquer by using multiple equation, recursively multiply sub matrix and get final result. with less time

Dept of CSE Page 30


MODULE-2

Advantages of Divide & Conquer technique:


• For solving conceptually difficult problems like Tower Of Hanoi, divide &
conquer is a powerful tool
• Results in efficient algorithms
• Divide & Conquer algorithms are adapted foe execution in multi-processor
machines
• Results in algorithms that use memory cache efficiently.

Limitations of divide & conquer technique:


• Recursion is slow
• Very simple problem may be more complicated than an iterative approach.
Example: adding n numbers etc

 Decrease & Conquer Approach:


Decrease and conquer is an design technique based on exploit in the relation between a soln between
given instances of a problem and solution instance to a smaller same problem.
It works on recursive.
There are 3 types decrease and conquer
1.Decrease by constant.
2.Decrease by constant factor.
3.Variable size decrease.

 Decrease by constant:
In the size of an instance is reduced by same constant on each iterations of an algorithm.
Ex: Increase sort is an best example.

Decrease by conquer constant factor:


Dept of CSE Page 31
MODULE-2

The technique suggest reducing a problem instance by same constant factor on each iteration of an
algorithm, on or in most of constant factor is always is equal to 2.
Hence the technique works like D & C

Variable size decrease:


In this technique a size reduction pattern varies from 1 iteration to another euclits algorithm from
computing gcd of 2 nos is the best example.

TOPOLOGICAL SORTING:
Topological sorting is a sorting method to list the vertices of the graph in such an order that for
every edges in the graph , the vertex where the edges starts is listed before the vertex where the
edge end.

Topological sorting problem can be solved by using


1. DFS method
2. source removal method

1. DFS METHOD:
* it is d& c technique to travel a graph & to find topological ordering,
* stack is used for the structure of DFS.

EXAMPLE:
Apply DFS method to find topological sorting for graph below

Dept of CSE Page 32


MODULE-2

Vector 0=universal 1=visited


0 0 0 0
a b c d

Dept of CSE Page 33


MODULE-2

1st iteration = arbitrary we are choosing ‘a’ as source get all A


Adjacency nodes of source ‘a’. C
d
*{b , c}
0 0 A

a  b
nd
2 iteration =b is source get all Adjacency of nodes of source b.
Top=-1 stack
{a, c, d} D 1
1 0 0 C 2
B 3
{a, c, d} A 4
push pop

3rd iteration= c is source get all adjacency of nodes of source c.


{a , b , d}
1 1 0
cd
th
4 =d as source get all adjacency of nodes of source d
{ b , c} d
1 1 c
b

‘d’ be since there is unvisited .adjacency vertex of d becomes dead end hence pop d.

5th= c as source (c is stack source since stack stop is c).get all adjacency node are
{a, b, d}
1 1 1 C

B
A

csince there is unvisited A,V of C becomes dead end hence pop C.

Dept of CSE Page 34


MODULE-2

6TH ITRATION== b as source get all adjacency of source b. {a, c, d}

1 1 1

Here b becomes dead end hence pop b


7th iteration== a as source get all adjacency of source a
{b , c}

Here a become dead end hence pop a


The poping order is
dcba
hence the topological reverse order of poping is abcd.
a

2.SOURCE REMOVAL METHOD:


ALGORITHM:
STEP 1: from a given graph fixed a vertex with no incoming edges.
STEP 2: delete along with all the edges outgoing from it.
STEP 3: if there are more than one such vertices than break the randomly.
STEP 4: note vertices that are deleted in sorting,
STEP 5: all there recorded vertices (delete vertices) gives topological sorting.

Dept of CSE Page 35


MODULE-2

 Question paper problems:-


1. Write merge sort algorithm and discuss its efficiency. Sort the list E,X,A,M,P,L,E in alphabetical order
using the merge sort.
2. Design an algorithm for binary search, give an example. Show that the worst case efficiency of
binary search is Θ (log n)
3. Give the general plan for divide and conquer strategy and its recurrence statement
4. using divide and conquer strategy, multiply the two numbers 2301 and 1210 and analysis the
same.
5. consider the numbers given below. show how partitioning algorithm of quick sort will place 106
in its correct position, show all the steps clearly.
106 117 128 134 141 91 84 63 42
6. Assuming that n is a power of 2,solve the recurrence relation T(n)=2T(n/2)+2. Take T(2)=1 and
T(1)= 0.
7. Discuss how quick sort works to sort an array. Trace quick sort algorithm for the following data
set 65,70,75,80,85,60,55,50,45.also derive the worst case complexity of quick sort.
8. consider the following se of 14 elements in an array list,-15,-
6,0,7,9,23,54,82,101,112,125,131,142,151 when binary search is applied on these elements, find the
elements which required maximum number of comparisons for successful search and unsuccessful
search.
9. what are the three variations of decrease and conquer strategy? Apply DFS based algorithm and
source removal methods to solve the topological sorting problem for the following diagraph.

Dept of CSE Page 36


MODULE-2

Dept of CSE Page 37


MODULE-2

Dept of CSE Page 38


MODULE-2

Dept of CSE Page 39


MODULE-2

Dept of CSE Page 40


MODULE-2

Dept of CSE,SJBIT Page 41


.

You might also like