Professional Documents
Culture Documents
M.Raja Babu
Associate Professor
Dept of Information Technology
Aditya Engineering College(A)
Surampalem.
Aditya Engineering College (A)
Algorithm
• An algorithm is a finite set of instructions that, if followed, accomplishes a particular task.
• All algorithms must satisfy the following criteria:
• Input
-An algorithm has zero or more inputs, taken from a specified set of objects.
• Output
-An algorithm has one or more outputs, which have a specified relation to the inputs.
• Definiteness
-Each step must be precisely defined; Each instruction is clear and unambiguous.
• Finiteness
-The algorithm must always terminate after a finite number of steps.
• Effectiveness
-All operations to be performed must be sufficiently basic that they can be done exactly and in finite
length.
Algorithm specification
• Algorithms can be specified in the following three ways:
• 1.using a natural language like English
• 2.using pseudo code
• 3.flow chart
Algorithm Analysis
• Analysis of algorithms is important for two reasons :
• 1. To estimate the efficiency of the given algorithm
• 2. for comparing the algorithms for the given problem.
• Efficiency of an algorithm can be analyzed at two different stages,
before implementation and after implementation.
1. priori analysis − This is theoretical analysis of an algorithm.
2. posterior analysis − This is empirical analysis of an algorithm.
Algorithm Analysis
• Algorithm analysis deals with the execution or running time of various
operations involved.
• Running time of an operation can be defined as no. of computer
instructions executed per operation.
• Suppose X is an algorithm and n is the size of input data, the time and
space used by the Algorithm X are the two main factors which decide
the efficiency of X.
• Time Factor − The time is measured by counting the number of key
operations such as comparisons in sorting algorithm
• Space Factor − The space is measured by counting the maximum
memory space required by the algorithm.
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
Algorithm Analysis
• The complexity of an algorithm f(n) gives the running time and / or
storage space required by the algorithm in terms of n as the size of
input data.
• Space Complexity: Space complexity of an algorithm represents the
amount of memory space required by the algorithm in its life cycle.
Space required by an algorithm is equal to the sum of the following
two components :
• A fixed part
• A variable part
• Space complexity S(P) of any algorithm P is S(P) = C + SP(I)
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
• Algorithm: SUM(A, B)
• Step 1 - START
• Step 2 - C ← A + B + 10
• Step 3 – Stop
S(P) = 1+3.
Time complexity
• Time Complexity of an algorithm represents the amount of time
required by the algorithm to run to completion.
• Time requirements can be defined as a numerical function T(n).
• Methods for finding time complexity of an algorithm :
• 1.step count
• 2.operation count
• 3.asymptotic analysis
• 3.recurrence relations
• 5.amortized analysis
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
Step count
• In this method, we count number of times one instruction is executing.
From that we will try to find the complexity of the algorithm.
• Algorithm sum(a[], n)
•{
• sum = 0; // 1 step * 1
• for ( i = 0; i < n; i++) // 1 step * (n+1)
• sum += a[i]; // 1 step * N
• return sum; // 1 step * 1
•}
• T(n)=1+n+1+n+1=2n+3=O(n)
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
Operation count
• In this method, time complexity is computed based on the count of basic
operations.
• Time complexity is expressed as the number of times the basic operation is
executed.
• Algorithm sum(a[], n)
•{
• sum = 0;
• for ( i = 0; i < n; i++)
• sum += a[i];
• return sum;
•}
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
Asymptotic analysis
• Asymptotic analysis is the process of calculating the running time of an
algorithm by considering the rate of growth of an algorithm.
• In Asymptotic Analysis, we evaluate the performance of an algorithm in
terms of input size (we don’t measure the actual running time).
• We calculate, how the time taken by an algorithm increases with the
input size.
Asymptotic analysis
• Using asymptotic analysis, we can express the best case, average case
and worst case time complexity of an algorithm.
• We can have three cases to analyze an algorithm
1. Best Case − we calculate lower bound on running time of an
algorithm(Minimum time required for program execution)
2. Average Case − Average time required for program execution.
3. Worst Case − we calculate upper bound on running time of an
algorithm.(Maximum time required for program execution)
Asymptotic Notations
• Asymptotic notations are the mathematical notations used to
describe the running time of an algorithm.
• Following are commonly used asymptotic notations used in
calculating running time complexity of an algorithm.
• Big Oh Notation(Ο)
• Omega Notation(Ω)
• Theta Notation (θ)
Big Oh Notation(Ο)
• The Ο(n) is the way to express the upper bound of an algorithm's running time.
• It measures the worst case time complexity or longest amount of time an
algorithm can possibly take to complete.
Big Oh Notation(Ο)
• If f(n) and g(n) are two functions then
• f(n) = O(g(n)) if there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all
n > n0.
Example:
• 1. 3n+2=O(n)
as 3n+2≤4n for all n≥2
• 2. 3n+3=O(n)
as 3n+3≤4n for all n≥3
Omega Notation(Ω)
• The Ω(n) is the way to express the lower bound of an algorithm's
running time.
• It measures the best case time complexity or best amount of time an
algorithm can possibly take to complete.
Omega Notation(Ω)
• If f(n) and g(n) are two functions then
• f(n) = Ω(g(n)) if there exists c > 0 and n0 such that f(n) >=c.g(n) for all
n > n0.
Example:
f(n) = 3n + 2
g(n) = n
3n + 2 >= C n for all values of C = 1 and n >= 1.
3n + 2 = Ω(n)
Recursive algorithms
• A recursive algorithm is an algorithm which calls itself with smaller input
values.
• An iterative algorithm uses a looping structure while a recursive algorithm,
a function calls itself again and again until the base condition is satisfied.
Algorithm Sum(n ) Algorithm Sum(n )
{ {
if (n == 1) Sum=0;
return 1 for(i=1;i<n;i++)
sum=sum+i;
return n + Sum(n-1)
}
}
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
Recurrence relations
• This method is used to analyze recursive algorithms.
• When we analyze a recursive algorithm two steps are required:
• 1.forming a recurrence equation
• 2.solving the recurrence equation
• There are mainly two ways for solving recurrences.
1. Substitution Method
2. Master Theorem Method
Substitution method
• Here the solution can be obtained by repeated substitutions of the
RHS of the RE till a pattern is obtained.
Substitution method
• T (n) = T (n-1) +1 and T (1) = 1.
• Solution:
• T (n) = T (n-1) +1
= (T (n-2) +1) +1
= (T (n-3) +1) +1+1
= T (n-4) +4
= T (n-5) +1+4
= T (n-5) +5
= T (n-k) + k
Where k = n-1
T (n-k) = T (1) = 1
T (n) = 1+ (n-1) = 1+n-1=n= 0 (n).
Design&analysisof Algorithms M.Raja Babu 4/29/23
Aditya Engineering College (A)
• Algorithm Sum(n )
•{
• if (n == 1)
return 1
return n + Sum(n-1)
}
• recurrence relation is
• T(1) = 1
• T(n) = 1 + T(n-1), when n > 1.
2.solve the equation by using Substitution Method.O(2n)
T (n) = 1 if n=0
= 2T (n-1) if n>0