Module_1_Part1
Module_1_Part1
Data
Structures &
Structures &
Algorithms
Algorithms
MODULE 1: GROWTH OF FUNCTIONS
7/29/2024
INTRODUCTION
➢The word algorithm comes from the name of the author
-Abu Jafar Mohammed Ibn Musa Al khowarizmi who wrote
A text book entitled-”Algorithmi de numero indorum”
Now term ”Algorithmi” in the title of the book led to the term
Algorithm.
Input
Output
Definiteness
Finiteness
Effectiveness
1.INPUT: The Algorithm should be given zero or more input.
7/29/2024
ALGORITHM (CONTD…)
3. Pseudo-code Method:
In this method, we should typically describe algorithms as program,
which resembles language like Pascal & Algol(Algorithmic Language).
4. Programming Language:
we have to use programming language to write algorithms like
C, C++, JAVA etc.
PSEUDO-CODE CONVENTIONS
3. An identifier begins with a letter. The data types of variables are not
explicitly declared.
node= record
{
data type 1 data 1;
data type n data n;
node *link;
}
4. There are two Boolean values TRUE and FALSE.
Logical Operators
AND, OR, NOT
Relational Operators
<, <=,>,>=, =, !=
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
Here link is a pointer to the record type node. Individual data items of
a record can be accessed with → and period.
Contd…
7. The following looping statements are employed.
For, while and repeat-until While Loop:
While < condition > do
{
<statement-1>
..
..
<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
Case
{
: <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
9. Input and output are done using the instructions read & write. No
format is used to specify the size of input or output quantities
Contd…
10. There is only one type of procedure: Algorithm, the heading takes the
form,
Algorithm Name (Parameter lists)
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for i:= 2 to n do
6. if A[i] > Result then
7. Result :=A[i];
8. return Result;
9. }
Factorial of a number
Issue in the study of algorithm
1. How to create an algorithm.
2. How to validate an algorithm.
3. How to analyses an algorithm
4. How to test a program.
7/29/2024
PERFORMANCE ANALYSIS
b) Linear(variable)space complexity
1.Constant space complexity: A fixed amount of space
for all the input values.
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for i=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
Examples to solve
7/29/2024
Examples to solve
Algorithm Multiply(A,B,n)
{
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
c[i,j] = 0;
for (k=0; k<n; k++) {
c[i,j] = c[i,j] + A[i,k] + B[k,j]
}
}
}
return c
}
s(n) = ?
f(n) = ?
7/29/2024
Example
For (i=0; i<n; i++) = O(n)
For (i=0; i<n; i=i+2) = n/2 = O(n)
For (i=n;i>n; i--) = O(n)
For (i=1; i<n; i=i*2) = O(log2 n)
for (i=1; i<n; i=i*3) = O(log3 n)
For (i=n; i>1; i = i/2) = O(log2 n)
7/29/2024
Types of Time functions
O(1) - Constant
O(log n) - Logarithmic
O(n) - Linear
O(n2) - Quadratic
O (n3) - Cubic
O(2n) - Exponential
7/29/2024
KINDS OF ANALYSIS
1.Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of size n.
2.Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of size n.
• Need assumption of statistical distribution of inputs.
3.Best-case:
• T(n) = minimum time of algorithm on any input of size n.
COMPLEXITY:
Complexity refers to the rate at which the storage time grows as a
function of the problem size
Analysis of an Algorithm
➢The goal of analysis of an algorithm is to compare algorithm in running
time and also Memory management.
➢Running time of an algorithm depends on how long it takes a computer
to run the lines of code of the algorithm.
Running time of an algorithm depends on
1.Speed of computer
2.Programming language
3.Compiler and translator
Examples: binary search, linear search
Linear vs Binary search
7/29/2024
ASYMPTOTIC NOTATION
Problem:-Find upper bond ,lower bond & tight bond range for
functions: f(n)= 2n+5
Solution:-Let us given that f(n)= 2n+5 , now g(n)= n
lower bond=2n, upper bond =3n, tight bond=2n
For Big –oh notation(O):- according to definition
f(n)<=cg(n) for Big oh we use upper bond so
f(n)=2n+5, g(n)=n and c=3 according to definition
2n+5<=3n
Put n=1 7<=3 false Put n=2 9<=6 false Put n=3 14<=9 false Put
n=4 13<=12 false Put n=5 15<=15 true
now for all value of n>=5 above condition is satisfied. C=3 n>=5
2. Big - omega notation :- f(n)>=c.g(n) we know that this
Notation is lower bond notation so c=2
Let f(n)=2n+5 & g(n)=2.n
Now 2n+5>=c.g(n);
2n+5>=2n put n=1
We get 7>=2 true for all value of n>=1,c=2 condition is satisfied.
3. Theta notation :- according to definition
c1.g(n)<=f(n)<=c2.g
Thank You