Module 1 - 2021 Scheme
Module 1 - 2021 Scheme
CONTENTS
What is an Algorithm?
Algorithm Specification
Analysis Framework
Performance Analysis
Space complexity
Time complexity
Asymptotic Notations
Big-Oh notation (O)
Omega notation (Ω)
Theta notation (θ)
Mathematical analysis of Non-Recursive and recursive Algorithms with
Examples
Brute force design technique
Selection sort
Sequential search
03/18/2024
String matching algorithm with complexity Analysis
K. Balakrishnan, 18CS42 2
WHAT IS AN ALGORITHM?
“An Algorithm is a finite set of instructions that, if followed,
accomplishes a particular task. In addition, all algorithms must satisfy
the following criteria,
1. Input : Zero or more quantities are externally
supplied.
2. Output : At least one quantity is produced.
3. Definiteness : Each instruction is clear and
unambiguous.
4. Finiteness : The algorithm must terminate
after a finite number of steps for all
cases.
5. Effectiveness : Every instruction must be very
basic so that it can be carried out in principle by a person
using only
03/18/2024 pencil18CS42
K. Balakrishnan, and paper. ” 3
ALGORITHM SPECIFICATION
Pseudo code Conventions
An algorithm can be written as
A set of English statements
Flowchart
Pseudo code
Looping statements
while <condition> do repeat
{ {
<statement 1> <statement 1>
. .
. .
<statement n> <statement n>
03/18/2024 } } until <condition>
K. Balakrishnan, 18CS42 5
ALGORITHM SPECIFICATION
for variable := value1 to value2 Step step_value do
{
<statement 1>
.
.
<statement n>
}
Conditional statements
if <condition> then <statement >
case
{
:<condition 1>: <statement 1>
:<condition 2>: <statement 2>
:else: <statement n>
}
03/18/2024 K. Balakrishnan, 18CS42 6
ALGORITHM SPECIFICATION
read and write keywords are the input and output statements.
10 20 30 40 50
03/18/2024 K. Balakrishnan, 18CS42 7
ALGORITHM SPECIFICATION
We will now understand how a problem statement gets translated into an
algorithm taking Selection Sort as a case study.
SortedSorted
list Sorted
list list Sorted
Unsorted
list Unsorted
List
Unsorted
List List
We can now say that there are 2 important tasks in this problem:
Finding the smallest element in the unsorted list.
Bringing this smallest element to its appropriate position in the list
by swapping.
03/18/2024 K. Balakrishnan, 18CS42 8
ALGORITHM SPECIFICATION
Phase 2 – Converting identified tasks into basic pseudo code
For a list of n elements, the identified tasks must be performed on all n
elements.
With this, the basic pseudo code for Selection Sort is,
for i :=1 to n do
{
Examine a[i] to a[n] and suppose the smallest Task 1
element is at a[j];
Interchange a[i] and a[j]; Task 2
}
The above however is not the final pseudo code as it still has some
English statements.
03/18/2024
20
10 10
30 K. Balakrishnan,
20 2018CS42
30
25 30
25
11
ALGORITHM SPECIFICATION
Recursive algorithms
An algorithm is said to be Recursive if the same algorithm is invoked in
the body.
where,
- execution time of the basic operation op
03/18/2024
C(n) - number of times the basic operation executes
K. Balakrishnan, 18CS42 16
ANALYSIS FRAMEWORK
Orders of growth
These are the functions that represent the run time of an algorithm.
However, it is not only the input size that influences the performance of
an algorithm.
There are other factors as well. We will understand this with the help of
the following example.
Algorithm SequentialSearch(A[0…n-1], k){
i := 0; i i i
while i < n and A[i] ≠ k do 0 1 2
i := i + 1;
10 20 30
if i < n then return i;
else return -1;
}
03/18/2024 K. Balakrishnan, 18CS42 18
ANALYSIS FRAMEWORK
Linear search is a classical example that shows that input size alone
does not determine the algorithms performance.
Linear search algorithm runs the fastest when the key to be searched
is the first element in the list.
Hence,
03/18/2024 K. Balakrishnan, 18CS42 19
ANALYSIS FRAMEWORK
Worst – Case efficiency
“It is the efficiency for the worst case input of size n, which is an
input for which the algorithm runs the longest.”
Linear search algorithm runs the longest when the key to be searched
is the either the last element in the list or not present in the list.
Hence,
An unsuccessful search occurs only after all the list elements are
compared and no match is found.
= [ 1 + 2 + .. + i + .. + n] + n(1 - p)
= + n(1 - p)
Space Complexity
To determine the space complexity of an algorithm, the algorithm has to
be viewed in 2 parts,
Fixed part and Variable part
03/18/2024 K. Balakrishnan, 18CS42 25
PERFORMANCE ANALYSIS
Fixed part corresponds to the section of the algorithm whose
memory requirement is fixed irrespective of the problem instance.
This space requirement is constant and does not change with the
problem instance.
In one instance, the algorithm can be executed for a[] with size 5 and
in another instance the size of a[] can be 100.
where
n – problem instance
, , - time needed for addition, subtraction, multiplication
ADD, SUB, MUL – number of additions, subtractions, multiplications
For instance, the term corresponds to the product of time taken for
an addition operation and the total number of additions in the
algorithm.
COUNT variable
This is a global variable with initial value 0.
n=3 RSum(a,0) + 10
0 + 10 = 10
We have,
.
.
.
All functions that have the same or smaller order of growth than
g(n) are said to belong to Big-Oh of g(n) denoted by O(g(n)).
Let g(n) = .
For a function g(n), all functions having the same or greater order of
growth than g(n) are said to belong to Big-Omega of g(n) denoted by
Ω(g(n)).
For g(n) = , we can say that all quadratic functions belong to θ(g(n)).
Proof
In the given problem
t(n) = 100n + 5, g(n) =
According to O definition,
t(n) ≤ c * g(n), for all n ≥
Next we need to find an appropriate value of . For this we use the trial
and error method.
Proof
In the given example,
t(n) = , g(n) =
According to Ω definition,
t(n) ≥ c * g(n), for all n ≥
Proof
In the given example,
t(n) = , g(n) =
According to θ definition,
* g(n) ≤ t(n) ≤ * g(n), for all n ≥
Hence, we break the θ expression into two halves and find the value
of & separately.
Let = 0.
1. We have,
2.
3.
4.
≤≤ ---TRUE
FALSE
TRUE
TRUE
Hence,
* ≤ , for and all n ≥
03/18/2024 K. Balakrishnan, 18CS42 59
ASYMPTOTIC NOTATIONS
We now find . We have,
≤ * , for all n ≥
Hence,
≤ * , for all n ≥
We also have,
* ≤ , for and all n ≥
Proof
To prove the theorem, we take help of the following mathematical
property.
if ≤ and ≤ , then, ≤ 2 * max{, } (1)
𝒕 (𝒏) 𝒄𝒐𝒏𝒔𝒕𝒂𝒏𝒕 𝒈 ( 𝒏)
The above expression can be rewritten as,
𝒕 𝟏 ( 𝒏) + 𝒕 𝟐 ( 𝒏 ) ∈ 𝑶( 𝒎𝒂𝒙 { 𝒈 𝟏 ( 𝒏 ) , 𝒈𝟐 ( 𝒏 ) })
Hence proved.
Solution
1. n(n + 1)/2 ∈ O() – TRUE
2. n(n + 1)/2 ∈ O() – TRUE
3. (n + 1)/2 ∈ θ() – FALSE
4. n(n + 1)/2 ∈ Ω(n) – TRUE
03/18/2024 K. Balakrishnan, 18CS42 66
ASYMPTOTIC NOTATIONS
Example 2
List the following functions according to their order of growth from the
lowest to the highest:
, , , , ,,
Solution
Function Order of Growth function Rank
n! (Factorial) 7
log n (Logarithmic) 1
(Exponential) 6
4
log n (Logarithmic) 2
3
(Exponential) 5
03/18/2024 K. Balakrishnan, 18CS42 67
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS
There are some algorithms that have the same Best case, Worst case
and Average case efficiencies.K. Balakrishnan, 18CS42
03/18/2024 68
MATHEMATICAL ANALYSIS OF NON-RECURSIVE ALGORITHMS
Consider the following example,
Algorithm ADD(a, b)
// Input: Two non-negative integers a & b.
//Output: The value of the sum of a & b.
c := a + b;
return c;
Analysis
STEP 1 : Input size = n.
i ij ij j
0 1 2 3
5 6 7 7
Analysis
STEP 1 – Input size is n.
This algorithm does not execute for the entire input size for all
instances.
For one instance the algorithm may execute for entire input size while
for another instance the algorithm stops after the very first iteration.
Therefore this algorithm has different best, worst and average case
efficiencies.
In both these instances, the algorithm executes for the entire input
size.
( 𝒏−( 𝒏− 𝟏𝟏 𝟐 𝒏 −𝟐
) [𝟐)𝟐[ 𝟐
(−(𝒏
𝒏
𝒏−𝟏𝒏−𝟐)(𝒏−
(𝒏−𝟐(𝒏−
(𝒏 𝟐)(𝒏−𝟏)
𝒏− 𝟐
(𝒏−𝟐)(𝒏−
)− 𝟐𝟏)
(𝒏 −𝟐
−𝒏+𝟐]
− 𝟏 𝟏)
))]
𝑪
𝑪
𝑪
𝑪
𝑪 𝑪 (𝒏
(𝒏
(()𝒏
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
𝒘𝒐𝒓𝒔𝒕
(𝒏
𝒘𝒐𝒓𝒔𝒕 )=
=𝒏())𝒏−𝟏
=
=((=
)=(𝒏
𝒏
𝟐
𝒏−𝟏(𝒏 ) =𝑶 (𝒏 )
() =
𝒏
(𝒏−𝟏)
)( 𝒏−)()𝒏−
−𝟏)
( 𝒏−𝟏𝟐−
=𝟎+𝟏 𝟏) ∑
−
𝒊=𝟎𝟐𝟐 𝟐
−𝟐)(𝒏−
) − ) −𝟏 − ∑ 𝒊
𝟐𝟐𝟐
𝒊=𝟎
𝟏)
Matrix Multiplication
ALGORITHM MatrixMultiplication(A[0..n − 1, 0..n − 1], B[0..n − 1, 0..n − 1])
//Multiplies two square matrices of order n by the definition-based algorithm
//Input: Two n × n matrices A and B
//Output: Matrix C = AB
for i := 0 to n − 1 do
for j := 0 to n − 1 do
C[i, j ] := 0;
for k := 0 to n − 1 do
C[i, j ] := C[i, j ]+ A[i, k] ∗ B[k, j];
return C;
Analysis
STEP 1 – Input size is n X n.
Let be the time required to perform one multiplication and be the time
required to perform one addition.
Then the total time spent in performing the basic operation, denoted by
T(n), is given by,
n=8 n = 4128
ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count := 1;
while n > 1 do
count := count + 1;
n := ;
return count;
Analysis
STEP 1 – Input size is n.
ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count := 1;
while n > 1 do
count := count + 1;
n := ;
return count;
STEP 4 –
Setting up a summation for this algorithm is not possible.
Based on this, we can conclude that the run time of this algorithm
denoted by T(n) is,
T(n) =
STEP 3 - Check whether the number of times the basic operation is executed can
vary on different inputs of the same size; if it can, the worst-case, average-case,
and best-case efficiencies must be investigated separately.
STEP 4 - Set up a recurrence relation, with an appropriate initial condition, for the
number of times the basic operation is executed.
STEP 5 - Solve the recurrence or, at least, ascertain the order of growth of its
solution.
1F(1)
* 2 *= 2
1F(0)
* 1 *= 11
03/18/2024 K. Balakrishnan, 18CS42 91
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
Analysis
ALGORITHM F(n)
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 then return 1;
else return F(n − 1) ∗ n;
Tower of Hanoi
Step 1 − Move n-1 disks from source to temp
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from temp to dest
Algorithm T(n, S, D, A)
if n = 1 then
move disk from source to dest
else {
T(n - 1, S, A, D);
move disk from source to dest
T(n - 1, A, D, S);
}
03/18/2024 K. Balakrishnan, 18CS42 96
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
T(3,A,C,B) T(1,A,C,
AC
B)
T(2,A,B,
AB
C)
T(1,C,B,
CB
A)
T(3,A,C, AC
B)
T(1,B,A,
BA
C)
T(2,B,C,
BC
A)
T(1,A,C,
AC
03/18/2024 B)K. Balakrishnan, 18CS42 97
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
The sequence of moves are,
A C, A B, C B, A C, B A, B C, A C
A B C
03/18/2024 K. Balakrishnan, 18CS42 98
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
Analysis
Algorithm T(n, S, D, A)
if n = 1 then
move disk from source to dest
else {
T(n - 1, S, A, D);
move disk from source to dest
T(n - 1, A, D, S);
}
Algorithm T(n, S, D, A)
if n = 1 then
move disk from source to dest
else {
T(n - 1, S, A, D);
move disk from source to dest
T(n - 1, A, D, S);
}
STEP 5
M(n) =
M(n) =
M(n) =
M(n) =
M(n) =
M(n) =
M(n) =
M(n) =
M(n) =
03/18/2024
M(n) =
K. Balakrishnan, 18CS42 101
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
M(n) =
03/18/2024 K. Balakrishnan, 18CS42 102
MATHEMATICAL ANALYSIS OF RECURSIVE ALGORITHMS
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 then return 1;
else return BinRec() + 1;
Analysis
STEP 1 – Input size is n.
ALGORITHM BinRec(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
if n = 1 then return 1;
else return BinRec() + 1;
STEP 5
One issue with the recurrence is the presence of the fraction n/2.
Hence, we have
n=
.
.
Substituting i = k, we get,