MODULE 1design and Analysis of Algorithm
MODULE 1design and Analysis of Algorithm
Algorithm
Thara Chakkingal
Introduction
● An algorithm is a set of steps of operations to solve a problem
performing calculation, data processing, and automated reasoning
tasks.
● It takes set of values as input and results in the output as a set of
values by solving the problem.
● An algorithm design include creating an efficient algorithm to solve
a problem in an efficient way using minimum time and space.
Algorithms must satisfy the following
criteria:
● Input: Zero or more quantities are externally supplied.
● Finiteness: If we trace out the instructions of an algorithm, then for all cases, the
● Effectiveness: Every instruction must be very basic so that it can be carried out, in
principle, by a person using only pencil and paper. It is not enough that each operation
efficiency = f(n)
● n = number of instructions
Algorithmic efficiency
● Linear loops
for(i=0;i<1000;i++)
{
code
}
○ n = Loop factor =1000
○ Number of iterations are directly proportional to loop factor
○ f(n) = n
Algorithmic efficiency
● Linear loops
for(i=0;i<1000;i=i+2)
{
code
}
○ n = Loop factor =1000
○ Number of iterations are directly proportional to half the loop factor
○ f(n) = n/2
Algorithmic efficiency
● Nested loops
● Iterations = outer loop iterations * inner loop iterations
● Quadratic loop
for(i=1; i <= n; i++)
{
for(j=1; j <= n; j++)
{
code
}
}
● f(n) = n2
Algorithmic efficiency
● Nested loops
● Dependent Quadratic loop
for(i=1; i <= n; i++)
{
for(j=1; j <= i; j++)
{
code
}
}
● f(n) = n (n+1) / 2
Steps
Problem Types
Analysis of Algorithm
● Space Complexity
● Time Complexity
Space Complexity(Ref. DS by Salaria)
● Amount of memory needed by program upto completion of
execution.
● Space needed by program has following components:
○ Instruction Space-
■ Space needed to store the compiled version of the program instruction
○ Data space-
■ Space needed by constants,
■ variables,
■ fixed sized structured variables,
■ dynamically allocated space
○ Environmental stack space
■ Used to save information needed to resume execution of partially completed functions and
methods
■ Each time function is invoked the following data are saved on environment stack :
● Return address
algo sum()
{ s=0 --------- 1
for i= 1 to n --------- n + 1
s=s+a[i] ---------- n
return s ---------- 1
} 2n + 3
Asymptotic Analysis
● Asymptotic analysis of an algorithm refers to defining the mathematical
boundation/framing of its run-time performance.
● It is used to find the best case, average case, and worst case scenario of
an algorithm.
● Other than the "input" all other factors are considered constant.
● Asymptotic analysis refers to computing the running time of any
operation in mathematical units of computation.
● Def - Given functions f(n) and g(n), we say that f(n) is O(g(n) ) if and only if there are positive constants c and n0 such that
f(n) = O(g(n))
● As n increases, f(n) grows no faster than g(n). In other words, g(n) is an asymptotic upper bound on f(n).
● Examples:
f(n) = Ω(g(n))
● Example
● Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n
If we want to represent f(n) as Ω(g(n)) then it must satisfy f(n) >= C
g(n) for all values of C > 0 and n0>= 1
f(n) = Θ(g(n))
● Example
● Consider the following f(n) and g(n)...
f(n) = 3n + 2
g(n) = n