Studocu DAA Unit 1 Notes
Studocu DAA Unit 1 Notes
Studocu DAA Unit 1 Notes
UNIT- I
Informal Definition:
An Algorithm is any well-defined computational procedure that takes some value or set of
values as Input and produces a set of values or some value as output. Thus algorithm is a sequence of
computational steps that transforms the I/P into the O/P.
1 : Input : The algorithm should produce zero or more number of quantities, as an input.
2 : Output : The algorithm should produce at least one quantity, as an output.
3 : Definiteness : Each instruction is clear and unambiguous.
4 : Finiteness : If we trace out the instructions of an algorithm, then for all cases, the algorithm
terminates after a finite number of steps.
5 : Effectiveness : Every step of algorithm should be effective. Every operation can be roughly by pen
and paper. So tracing of each step of algorithm should be done.
Issues ( or ) Design of Algorithm
2 : Decision Making : After finding the required input set for the given problem we have
Sequential algorithm : specifically runs on the machine in which the instruction are executed one after
another. Such machine is called as Random Acess Machine (RAM ).
Parallel algorithm : are run on the machine in which the instruction are executed in parallel.
B ) Choice for either exact or approximate problem solving method : The next important decision is
to decide whether the problem is to be solved exactly or approximately. If the problem needs to be
solved correctly then we need exact algorithm. Otherwise if the problem is so complex that we won’t get
the exact solution then we need to choose approximation algorithm.
C ) Algorithm Design Techniques : In this we will use different design techniques.
1 : Brute – force.
3 : Dynamic programming.
4 : Greedy technique.
5 : Back tracking.
1 : Natural language.
2 : Pseudo code.
3 : Flowchart.
1 : Natural language : It is very simple to specify an algorithm using natural language. But many times
specification of algorithm by using natural language is not clear, and there by we get brief specification.
2
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Step 3 : Add the two numbers and store the result in a variable c.
Such a specification creates diffculty while actually implementing it. Hence many programmers
prefer to have specification of alogorithm by means of pseudo code.
2 : pseudo code : pseudo code is nothing but a combination of natural language and programming
language constructs. A pseudo code is usually more precise than a natural language.
Algorithm sum(a,b)
{
// Problem description : This algorithm performs addition of two integers.
// Input : Two integers a and b.
// Output : addition of two integers.
c <- a + b;
write ( c );
}
3
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Flowchart Symbols
4
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Example :
Start
Input a and b
values
c=a+b
Display the
value of c
Stop
1 : Time Complexity.
2 : Space Complexity.
3 : Simplicity.
4 : Generality.
5 : Range of input.
5
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
1 : Time Complexity : The Time Complexity can be defined as amount of computer time required by an
algorithm to run to completion. By computing time complexity we can analyze whether an algorithm
requires slow or fast.
2 : Space Complexity: The Space Complexity can be defined as amount of space required by an
algorithm to run to completion. By computing space complexity we can analyze whether an algorithm
requires more or less space.
3 : Simplicity : Simplicity of an algorithm means generating sequence of instructions which are easy to
understand. This is an important characterstic of an algorithm. because simple algorithms can be
understood quickly and one can then write simpler programes for such algorithms.
4 : Generality : Generality shows that sometimes it becomes easier to design an algorithm in more
general way rather than designing it for particular set of input. Hence we should write general
algorithms always.
5 : Range of input : Range of inputs comes in picture when we execute an algorithm. The design of
algorithm should be handle the range of input which is the most natural to corresponding problem.
Analysis of algorithm means checking the characterstics such as : Time complexity, Space
complexity, simplicity, generality and range of input. If these factors are not satisfactory then we must
redesign the algorithm.
6 : Implementation or coding of algorithm : The implementation of an algorithm is done by suitable
programming language. For example : if an algorithm consists of object and related methods then it will
be better to implement such algorithm using some object-oriented programming language like C++ and
JAVA. While writing a program for given algorithm it is essential to write an optimized code. This will
reduce the burden on computer.
7 : Testing a program : Testing a program is an activity carried out to expose as many errors as possible
and to correct them. There are two phases for testing a program.
1 : Debugging.
2 : Profiling.
1 : Debugging : Debugging is a technique in which a sample set of data is tested to see whether faulty
results occur or not. If any faulty result occurs then those results are corrected.
But in debugging technique only presence of error is pointed out. Any hidden error can not be
identified. Thus in debugging we cannot verify correctness of output on sample data. Hence profiling
concept is introduced.
2 : Profiling : Profiling is the process of executing a correct program on a sample set of data. Then the
time and space required by the program to execute is measured.
6
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
the algorithm is called pseudo code. It is not a programming language because it is not having any
The following rules are involved to represent the algorithm by using pseudo code
1 : Algorithm is a procedure which consisting of heading and body. The heading consisting of
// Problem Description :
// Input :
// Output :
3 : The body of an algorithm is written, in which various programming constructs like if, for,
4 : The beginning and end of block should be indicaed by { and } respectively. The compound
alphanumeric string.
represented by the context itself. Basic data types used are integer, float, char, Boolean and so on.
7
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Ex : a := 10;
11 : The array indices are stored with in square brackets ‘[‘ ‘]’. The index of array usually start at
12 : The conditional statements such as if-then or if-then-else are written in following form :
if the if-then statement is of compound type then { and } should be used for enclosing block.
while(condition) do
{
statement 1
statement 2
:
:
statement n
}
While the condition is true the block enclosed with { } gets executed otherwise statement
Sometime a keyword step is used to denote increment or decrement the value of variable.
Ex : for i 1 to n step 1
{
Write(i);
}
repeat
{
statement 1;
statement 2;
:
:
statement n;
}
until(condition)
16 : The break statement is used to exit fom inner loop. The rerurn statement is used to return
control from one point to another. Generally used while exiting from function.
Here link is a pointer to the record type node. Individual data items of a record can be accessed with
and period.
Note that statements in an algorithm executes in sequential order i.e, in the same order as they
9
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
PERFORMANCE ANALYSIS : Performance Analysis refers to the task of determining the computing time
and storage space requirements of an algorithm. It is also known as performance analysis.
The main purpose of algorithm analysis is to design most efficient algorithms. The efficiency of the
algorithm mainly depends on two factors
1 : Space Complexity
2 : Time Complexity
1 : Space Complexity : The Space Complexity can be defined as amount of space required by an
algorithm to run to completion. By computing space complexity we can analysis the algorithm requires
less or more space.
1 : The fixed static part that is independent of the characterstics ( example number size ) of the input
and outputs. This part includes the instruction space, space for simple variable and fixed size component
variables, space for constants etc..
2 : A variable dynamic part, that consists of the space needed by component variable whose size is
dependent on the particular problem instance at run time being solved, the space needed by referenced
variables and the recursion stack space.
S(p) = C + Sp
Where C is a constant i.e, fixed part. It denotes amount of space required for inputs, outputs,
variables and instructions. And Sp is a instance characterstics. This is a variable part amount of space
required for problem is depends on particular problem instance.
Ex 1 : Algorithm add(a,b,c)
{
// Problem Description : This algorithm computes the addition of 3 numbers
// Input : a, b and c are of floting type.
// Output : The addition is returned.
return a+b+c;
}
10
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
S(p) = C
If we assume that a, b and c occupy one word size then total size comes to be 3.
2 : Time Complexity : The Time Complexity can be defined as amount of computer time required by an
algorithm to run to completion.
1 : compile time.
2 : run time.
It is diffcult to compute the time complexity in terms of physically clocked time. Because runtime
depends on many factors such as system load, number of other programs running and processor speed.
For avoiding this, we are using frequency count is basically a count denoting number of times of
execution of statement.
The method to determine the step count of an algorithm is to build a table in which we list the
total number of steps contributes by each statement.
First determine the number of steps per execution (s/e) of the statement and the total number of
times (ie., frequency) each statement is executed.
By combining these two quantities, the total contribution of all statements, the step count for the
entire algorithm is obtained.
Example 1 :
11
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Algorithm sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
return s;
count=count+1;
}
If the count is zero to start with, then it will be 2n+3 on termination. So each invocation of sum execute
a total of 2n+3 steps.
12
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
ASYMPTOTIC NOTATIONS
Expressing the complexity in terms of its relationship to know function. This type of Notation is
called Asymptotic Notations.
1 : Big Oh Notation ( ‘ O ‘ )
2 : Omega Notation ( ‘ Ω ‘ )
3 : Theta Notation ( ‘ θ ’ )
1 : Big Oh Notation ( ‘ O ‘ ) :
Definition : Let f(n) and g(n) be two non-negative functions. f(n) is said to be O(g(n)) if and only if
there exists positive constants ‘c’ and ‘n0’ such that f(n) ≤ c * g(n) for all non- negative values of n.
where n ≥ n0.
c * g(n)
f(n)
.
.
.
.
.
.
n
n0
f(n) = O(g(n))
This notation provides an upper bound for the function f i.e, the function g(n)
13
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
To Prove.
f(n) = 8n + 128 = O (n2)
Proof : If f(n) = O(g(n)). We have there exists positive constants ‘c’ and ‘n0’ such that f(n) ≤ c * g(n) for
all non-negative values of n. where n ≥ n0.
Here
f(n) = 8n + 128
g(n) = n2
Now 8n + 128 ≤ 3* n2
Now N = 1.
There exists two positive constants c = 3, n0 = 8 such that 8n + 128 ≤ 3 * n2 for all n>=8.
To Prove.
Proof : If f(n) = O(g(n)). We have there exists positive constants ‘c’ and ‘n0’ such that f(n) ≤ c * g(n) for
all non-negative values of n. where n ≥ n0.
Here ,
f(n) = 12n2 + 6n
g(n) = n3
Now N = 1.
Now N = 2.
Now N = 3.
There exists two positive constants c = 4, n0 = 3 such that 12n2 + 6n ≤ 4 * n3 for all n>=3.
Proof : If f(n) = O(g(n)). We have there exists positive constants ‘c’ and ‘n0’ such that f(n) ≤ c * g(n) for all non-
negative values of n. where n ≥ n0.
Given,
f(n) = 2n + 2.
g(n) = n2
Now f(n) = 2n + 2 ≤ 2 * n2
15
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Now Take N = 1.
2.1 + 2 ≤ 2 * 1 ( false )
Now Take N = 2.
2.2 + 2 ≤ 2 * 4 ( True ).
There exists two positive constants c = 2, n0 = 2 such that 2n + 2 ≤ 2 * n2 for all n>=2.
2 : Omega Notation ( Ω ) :
Definition : Let f(n) and g(n) be two non-negative functions. f(n) is said to be Ω(g(n)) if and
only if there exists positive constants ‘c’ and ‘n0’ such that f(n) ≥ c * g(n) for all non-negative
f(n)
c * g(n)
.
.
.
.
.
.
n
n0
f(n) = Ω (g(n))
This notation provides an upper bound for the function f i.e, the function g(n) is an
16
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
To Prove.
Proof : If f(n) = O(g(n)). We have there exists positive constants ‘c’ and ‘n0’ such that f(n) ≥ c * g(n) for
all non-negative values of n. where n ≥ n0.
Here
f(n) = 8n + 128
g(n) = n2
Now 8n + 128 ≥ 1 * n2
Now N = 1.
There exists two positive constants c = 1, n0 = 1 such that 8n + 128 ≤ 1 * n2 for all n>1.
3 : Theta Notation ( θ ) :
Definition : Let f(n) and g(n) be two non-negative functions. f(n) is said to be θ(g(n)) if and
only if there exists positive constants ‘c1’ , ‘c2’ and ‘n0’ such that c1 * g(n) ≤ f(n) ≤ c2 * g(n)
c2 * g(n)
f(n)
. c1 * g(n)
.
.
.
.
.
n
n0
f(n) = θ(g(n))
17
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
This notation provides both lower bound and upper bound for the function f(n)i.e,
g(n) is both lower and upper bound on the value of f(n), for large n.
To Prove.
Proof :
If f(n) = O(g(n)). We have there exists positive constants ‘c1’ , ‘c2’ and ‘n0’
such that c1g(n) ≤ f(n) ≤ c2g(n) for all non-negative values of n. where n >_ no.
Here
f(n) = 8n + 128
g(n) = n2
Now,
1* n2 ≤ 8n + 128 ≤ 3* n2
Now Take N = 1
Now Take N = 2
Now Take N = 3
Now Take N = 4
Now Take N = 5
Now Take N = 6
18
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Now Take N = 7
Now Take N = 8
1 * 64 ≤ 64 + 128 ≤ 3 * 64 (true).
for all n ≥ 8
4 : Little Oh Notation : The Little Oh Notation is denoted by ‘ o ‘. The function f(n) = o(g(n)) iff
lim f(n)
n --∞ ------ = 0
g(n)
Example : While searching a particular element by using sequential search. We get the
2 : Worst Case : If the algorithm takes maximum amount of time to run to completion for a
Example : While searching an element by using linear search. If we get the desired element
3 : Average Case : If the algorithm takes average amount of time to run to completion for a
Example : BinarySearch.
19
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
UNIT-I
DIVIDE AND CONQUER METHOD
Divide Conquer Method: In divide and conquer method, if the given problem is divided into smaller sub
problems than we find the solution directly.
If the given problem is larger than divide the problem into sub problems and again sub
problem is large enough then divide the sub problem into smaller sub problems. Find the solution of the
sub problems finally we get the solution of the given problems. By combining the solution of sub
problem.
Given Problem P
P1 P2
P3 P4
. .
. .
. .
. .
Solution of P3 Solution of P4
Solution of P1 Solution of P2
Given Problem P
1
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Control Abstraction for Divide Conquer Strategy: Control abstraction is flow of control of the
procedure. It gives the basic function of the method. So it represents the logic code for divide and
conquer strategy.
Algorithm DAndC(p)
{
if small(p) then
return s(p);
else
{
divide p into smaller instance p1, p2, p3, p4, ……, pk;
P->Problem
Here small(P)is a Boolean value function. If it is true, then the function S is invoked . Otherwise the
problem P is divided into smaller subproblems. These problems p1, p2, p3, p4,...., pk are solved by
recursive applications of DAndC. Combine is a function that determines the solution to p using the
solutions to the k sub problems.
Calculate time for DAndC:
If the problem P of size is n and K sub problems size is n1,n2,n3 nk then
T(n) = g(n)
T(n1)+T(n2)+ --- +T(nk)+f(n)
T(n) = Time for DAndC of any Input size n;
g(n) =Time to compute the answer directly for small inputs
T(n1) = Time for DAndC of small input size n1;
T(n2) =Time for DAndC of small input size n2;
*
*
T(nk) = Time for DAndC of small input size nk;
f(n) =Time for dividing P and combining the solution of subproblems.
2
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Time complexity of Divide and Conquer algorithms is given by recurences of the form
Here a and b are known constants. This is called the general divide and-conquer recurrence.
One of the methods for solving any such recurence relation is called the substitution method. This
method repeatedly makes substitution for each occurence of the function T in the right hand side until all
such occurrences disappear.
Example : Consider the case in which a =2 and b=2. Let T(1) = 2 and f(n ) = n.
T(n) = aT(n/b)+f(n)
T(n) =2T(n/2) + n
=2[ 2T(n/2/2)+n/2]+n
=2[2T(n/4)+n/2]+n
=4T(n/4)+2n/2+n
=4T(n/4)+n+n
=4T(n/4)+2n
=4[ 2T(n/4/2)+n/4]+2n
=4[2T(n/8)+n/4]+2n
=8T(n/8)+4n/4 +2n
=8T(n/8)+n+2n
=8T(n/8) + 3n
= 23 T ( n / 23 ) + 3n
. In particular
. n = 2i
. i = log n 2
.
= 2 T ( 2i / 2i ) + in
i
3
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
2 : Quick Sort.
3 : Merge Sort.
1 : BINARY SEARCH : In binary search method we are using the divide and conquer strategy. In this
method first sort the elements in ascending order. An element which is to be searched from the list of
elements stored in array A[0 . . . n-1] is called KEY element.
Let A[mid] be the mid element of array A. Then there are three conditions that need to be tested
while searching the array using this method.
1 : if KEY = = A[mid] then desired element is present in the list.
2 : Otherwise if KEY < A[mid] then search the left sub list.
3 : Otherwise if KEY > A[mid] then search the right sub list.
4
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Solution:
0 1 2 3 4 5 6
10 20 30 40 50 60 70
Low High
0 1 2 3 4 5 6
10 20 30 4 0 50 60 70
…… 50 60 70
5
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Now we will again divide this list and check the mid element.
4 5 6
…… 50 60 70
.
. . i.e,The number is present in the list.
6
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Time Complexity of Binary Search Method : Time complexity of Binary Search method is described by
the recurence relation is
T(1) n=1
T(n) = T(n/2)+1 n>1
T(n/2) = T(n/4) +1
T(n/4) = T (n/8) +1
T(1) =1
T(n) = T(n/2)+1
=[ T(n/2/2)+1]+1
=T(n/4)+1+1
=T(n/4)+2
=[T(n/4/2)+1]+2
=T(n/8)+1+2
=T(n/8)+3
= T ( n / 23 ) + 3
= T (n /2i) +i
. In particular
. n = 2i
. i = log n 2
.
= T ( 2i / 2i ) + i
= T( 1 ) + log n 2
= 1 + log n 2
T(n) = O ( log n 2 )
7
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Quick Sort: Quick Sort is a sorting algorithm that uses the divide and conquers strategy. They are three steps
of quick sort are as follows.
1 : Split the array into two sub arrays that each element in the left sub array is less than or equal to the
middle element and each element in the right sub array is greater than or equal to the middle element.
The splitting of the array into two sub arrays is based on pivot element. All the elements that are less
than pivot should be in left sub array and all the elements that are greater than pivot should be in right
sub array.
2: recursively sort the two sub arrays.
3: combine all the sorted elements in a group to form a list of sorted element.
Algorithm for QuickSort :
m : = partition(A[ low…… high]; //Split the array into two sub arrays.
i = i + 1;
j = j - 1;
if ( i <= j) then
return j;
}
8
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Step 1 : split the array in two parts. The left sublist will contain the elements less than pivot and
Low High
50 30 10 90 80 20 40 70
i / Pivot j
Step 2 : if A[i] <= pivot && (i<j)( 50 <= 50 )&&(0<7)– condition true, we will increment i.
Low High
50 30 10 90 80 20 40 70
pivot i j
Step 3 : if A[i] <= pivot && (i<j) ( 30 <= 50 ) &&(1<7)– condition true, we will increment i.
Low High
50 30 10 90 80 20 40 70
Pivot i j
Step 4 : if A[i] <= pivot && (i<j)( 10 <= 50 ) &&(2<7)– – condition true, we will increment i.
Low High
50 30 10 90 80 20 40 70
Pivot i j
9
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Step 5 : if A[i] <= pivot && (i<j) ( 90 <= 50 ) &&(3<7)– – condition false, if A[j] >= pivot ( 70 >= 50 ) – condition
Low High
50 30 10 90 80 20 40 70
Pivot i j
Step 6 : if A[j] >= pivot ( 40 >= 50 ) – condition false, if i<=j ( 3<=6) – true, we will swap A[i]
Low High
50 30 10 40 80 20 90 70
Pivot i j
Step 7 : if A[i]<=pivot && (i<j) (40 <= 50)- &&(3<6)– condition true, we will increment i.
Low High
50 30 10 40 80 20 90 70
Pivot i j
Step 8 : if A[i] <= pivot && (i<j)( 80 <= 50 ) &&(4<6)– – condition false, if A[j] >= pivot ( 90 >= 50 ) – condition
Low High
50 30 10 40 80 20 90 70
Pivot i j
10
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Step 9 : if A[j] >= pivot ( 20 >= 50 ) – condition false, if (i<=j) (4<=5)- condition true, we will
Low High
50 30 10 40 20 80 90 70
Pivot i j
Step 10 : if A[i] <= pivot && (i<j) ( 20 <= 50 ) &&(4<5)– condition true, we will increment i.
Low High
50 30 10 40 20 80 90 70
Pivot ij
Step 11 : if A[i] <= pivot ( 80 <= 50 ) &&(5<5)– – condition false, if A[j] >= pivot ( 80 >= 50 ) – condition
Low High
50 30 10 40 20 80 90 70
Pivot j i
Step 12 : if A[j] >= pivot ( 20 >= 50 ) – condition false, if (i<=j) (5<=4)- condition false, we will
Low High
20 30 10 40 50 80 90 70
Pivot j i
Left sublist Pivot element
is correct Right sublist
position
11
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Low High
20 30 10 40 50 80 90 70
i / Pivot j
Low High
20 30 10 40 50 80 90 70
Pivot i j
Step 15 : If A[i]<=pivot && (i<j), (30<=20) &&(1<3)– - condition false, A[j]>=pivot, (40>=20)- condition true, we
will decrement j.
Low High
20 30 10 40 50 80 90 70
Pivot i j
Step 16 : A[j]>=pivot, ( 10>=20)- condition false, if (i<=j), (1<=2)- condition true, we will swap
Low High
20 10 30 40 50 80 90 70
Pivot i j
12
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Low High
20 10 30 40 50 80 90 70
Pivot ij
Step 18 : A[i]<=pivot && (i<j)( 30<=20) &&(2<2)– - condition false, A[j]>=pivot, ( 30>=20)- condition true, we will
decrement j.
Low High
20 10 30 40 50 80 90 70
Pivot j i
Step 19 : A[j]>=pivot ( 10>=20)- condition fale, if(i<=j), (2<=1)- condition false , we will swap A[low] and A[j], i.e,
20 and 10.
Low High
10 20 30 40 50 80 90 70
Step 20 : There is one element in left sublist hence we will start Right sub list.
Low
High
10 20 30 40 50 80 90 70
i/ pivot j
13
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Low High
10 20 30 40 50 80 90 70
pivot i j
Low High
10 20 30 40 50 80 70 90
pivot i j
Low High
10 20 30 40 50 80 70 90
pivot ij
Step 24 : if A[i]<=pivot&&(i<j) (90<=80) )&&(7<7) –condition false, if A[j]>=pivot, (90>=80) –condition true, we
will decrement i.
Low High
10 20 30 40 50 80 70 90
pivot j i
Step 25 : if A[j]>=pivot, (70>=80) –condition false, if(i<=j), (7<=6)-condition false, we will swap
A[low] and A[j], 80 and 70.
10 20 30 40 50 70 80 90
pivot
10 20 30 40 50 60 70 80 90
14
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Merge Sort : Merge Sort is one of the most popular sorting algorithms that is based on the principle
of Divide and Conquer Algorithm.
The concept of Divide and Conquer involves three steps:
15
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
16
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
17
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Time Complexity of Merge Sort : The time complexity of merge sort is given by recurrence relation is
T(1) = a
T(n) =2T(n/2) + cn
T(n) =2T(n/2) + cn
=2[ 2T(n/2/2)+cn/2]+cn
=2[2T(n/4)+cn/2]+cn
=4T(n/4)+2cn/2+cn
=4T(n/4)+cn+cn
=4T(n/4)+2cn
= 22T(n/22)+2cn
=4[ 2T(n/4/2)+cn/4]+2cn
=4[2T(n/8)+cn/4]+2cn
=8T(n/8)+4cn/4 +2cn
=8T(n/8)+cn+2cn
=8T(n/8) + 3cn
= 23 T ( n / 23 ) + 3cn
=24 T(n/24) +4cn
18
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Divide and conquer method suggest another way to compute the product of n*n matrix.
We assume that N is a power of 2 .In the case N is not a power of 2 ,then enough rows and
columns of zero can be added to both A and B .SO that the resulting dimension are the powers of
two.
If n=2 then the following formula as a computed using a matrix multiplication operation for the
elements of A & B.
If n>2,Then the elements are partitioned into sub matrix n/2*n/2..since ‘n’ is a power of 2 these
product can be recursively computed using the same formula .This Algorithm will continue
applying itself to smaller sub matrix until ‘N” become suitable small(n=2) so that the product is
computed directly .
b n<=2
T( n ) = 8T(n/2) + cn2 n>2
Strassen’s Formulas :
C11 = P + S – T + V
C12 = R + T
C21 = Q + S
C22 = P + R – Q + U
20
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
Example : 2 * 2 matix
1 1 0 0
Ex : if A =
, B=
1 1 1 1
Let
A11 = 1 B11 = 0
A12 = 1 B12 = 0
A21= 1 B21= 1
A22= 1 B22= 1
Now,
21
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
C11 = P + S – T + V
= 2+1–2+0
= 3-2 = 1
C12 = R + T
= -1 + 2 = 1
C21 = Q + S
= 0+1=1
C22 = P + R – Q + U
= 2 + -1 – 0 + 0
= 2-1-0+0=1
C11 C12 1 1
=
C21 C22 1 1
Example 2 : 4 * 4 matrices
22
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
23
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
24
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
25
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
26
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
27
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
28
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
29
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
30
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
31
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM
32
Dr.M.GIRI _Design & Analysis of Algorithms NOTES – III B.Tech II SEM