0% found this document useful (0 votes)
210 views73 pages

Subject Code: SCSA1403 Subject Name: Design and Analysis of Algorithms Unit I Faculty Name: Dr. P. AJITHA

This document discusses the key concepts covered in Unit 1 of the Design and Analysis of Algorithms subject. It defines algorithms and lists their characteristics such as being well-defined, finite, and efficient. It also describes the advantages of algorithms in terms of effective communication, easy debugging and coding. The document then provides an example algorithm to calculate the number of absent students in a class. It outlines the steps in designing algorithms such as understanding the problem, choosing appropriate data structures, and analyzing time and space complexity. Finally, it discusses measuring the performance of algorithms through analytical and experimental methods.

Uploaded by

Vivek Redhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
210 views73 pages

Subject Code: SCSA1403 Subject Name: Design and Analysis of Algorithms Unit I Faculty Name: Dr. P. AJITHA

This document discusses the key concepts covered in Unit 1 of the Design and Analysis of Algorithms subject. It defines algorithms and lists their characteristics such as being well-defined, finite, and efficient. It also describes the advantages of algorithms in terms of effective communication, easy debugging and coding. The document then provides an example algorithm to calculate the number of absent students in a class. It outlines the steps in designing algorithms such as understanding the problem, choosing appropriate data structures, and analyzing time and space complexity. Finally, it discusses measuring the performance of algorithms through analytical and experimental methods.

Uploaded by

Vivek Redhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Subject Code: SCSA1403

Subject Name: Design and Analysis


of Algorithms
UNIT I

Faculty Name: Dr. P. AJITHA

01/07/2022 SCSA1403 DAA-Unit I 1


01/07/2022 SCSA1403 DAA-Unit I 2
01/07/2022 SCSA1403 DAA-Unit I 3
01/07/2022 SCSA1403 DAA-Unit I 4
01/07/2022 SCSA1403 DAA-Unit I 5
Road Map to Unit I

01/07/2022 SCSA1403 DAA-Unit I 6


Algorithm
 An algorithm can be defined as a finite set of steps, which has to be followed while
carrying out a particular problem.
 It is nothing but a process of executing actions step by step.
 An algorithm is a distinct computational procedure that takes input as a set of values
and results in the output as a set of values by solving the problem.

01/07/2022 SCSA1403 DAA-Unit I 7


Characteristics of Algorithms

 Input: It should externally supply zero or more quantities.


 Output: It results in at least one quantity.
 Definiteness: Each instruction should be clear and ambiguous.
 Finiteness: An algorithm should terminate after executing a finite number of steps.
 Effectiveness: Every instruction should be fundamental to be carried out, in principle,
by a person using only pen and paper.
 Feasible: It must be feasible enough to produce each instruction.

01/07/2022 SCSA1403 DAA-Unit I 8


Characteristics of Algorithms

 Flexibility: It must be flexible enough to carry out desired changes with no efforts.
 Efficient: The term efficiency is measured in terms of time and space required by an
algorithm to implement. Thus, an algorithm must ensure that it takes little time and
less memory space meeting the acceptable limit of development time.
 Independent: An algorithm must be language independent, which means that it
should mainly focus on the input and the procedure required to derive the output
instead of depending upon the language.

01/07/2022 SCSA1403 DAA-Unit I 9


Advantages of Algorithms

 Effective Communication: Since it is written in a natural language like English, it


becomes easy to understand the step-by-step delineation of a solution to any
particular problem.
 Easy Debugging: A well-designed algorithm facilitates easy debugging to detect the
logical errors that occurred inside the program.
 Easy and Efficient Coding: An algorithm is nothing but a blueprint of a program that
helps develop a program.
 Independent of Programming Language: Since it is a language-independent, it can be
easily coded by incorporating any high-level language.

01/07/2022 SCSA1403 DAA-Unit I 10


Disadvantages of Algorithms

 Developing algorithms for complex problems would be time-consuming and difficult to


understand.
 It is a challenging task to understand complex logic through algorithms.

01/07/2022 SCSA1403 DAA-Unit I 11


Problem:

Suppose there are 60 students in the class. How will you calculate the number of
absentees in the class?

Algorithm:

1.Initialize a variable called as Count to zero, absent to zero, total to 60


2.FOR EACH Student PRESENT DO the following:
Increase the Count by One
3.Then Subtract Count from total and store the result in absent
4.Display the number of absent students

01/07/2022 SCSA1403 DAA-Unit I 12


Steps in Design and Analysis of Algorithm:

01/07/2022 SCSA1403 DAA-Unit I 13


Steps in Design and Analysis of Algorithm:
1. Understanding the problem:
 The problem given should be understood completely.
 Check if it is similar to some standard problems and if a Known algorithm exists,
otherwise a new algorithm has to be devised.
2. Ascertain the capabilities of the computational device:
 Once a problem is understood we need to know the capabilities of the computing
device this can be done by knowing the type of the architecture, speed and memory
availability.
3. Exact /approximate solution: 
 Once algorithm is devised, it is necessary to show that it computes answer for all
the possible legal inputs.

01/07/2022 SCSA1403 DAA-Unit I 14


Steps in Design and Analysis of Algorithm:
4. Deciding data structures :
 Data structures play a vital role in designing and analyzing the algorithms.
 Some of the algorithm design techniques also depend on the structuring data
specifying a problem’s instance.
Algorithm + Data structure = Programs

5. Algorithm design techniques:


 Creating an algorithm is an art which may never be fully automated.
 By mastering these design strategies, it will become easier for you to devise new
and useful algorithms.

01/07/2022 SCSA1403 DAA-Unit I 15


Steps in Design and Analysis of Algorithm:
6. Prove correctness: 
 Correctness has to be proved for every algorithm.
 For some algorithms, a proof of correctness is quite easy; for others it can be quite
complex.
 A technique used for proving correctness is by mathematical induction because an
algorithm’s iterations provide a natural sequence of steps needed for such proofs.
 But we need one instance of its input for which the algorithm fails.
 If it is incorrect, redesign the algorithm, with the same decisions of data structures design
technique etc

01/07/2022 SCSA1403 DAA-Unit I 16


Steps in Design and Analysis of Algorithm:
7. Analyze the algorithm
 There are two kinds of algorithm efficiency: time and space efficiency.
 Time efficiency indicates how fast the algorithm runs
 space efficiency indicates how much extra memory the algorithm needs.
8. Coding
 Programming the algorithm by using some programming language.
 Formal verification is done for small programs.
 Validity is done by testing and debugging
 Some compilers allow code optimization which can speed up a program by a constant
factor whereas a better algorithm can make a difference in their running time.
01/07/2022 SCSA1403 DAA-Unit I 17
Performance of a program:

The performance of a program is measured based on the amount of computer memory and time
needed to run a program.
The two approaches which are used to measure the performance of the program are:
1. Analytical method  called the Performance Analysis.
2. Experimental method  called the Performance Measurement.

01/07/2022 SCSA1403 DAA-Unit I 18


SPACE COMPLEXITY

 The Space complexity of a program is defined as the amount of memory it needs to run to
completion.
 It is one of the factor which accounts for the performance of the program.
 The space complexity can be measured using experimental method, which is done by running
the program and then measuring the actual space occupied by the program during execution.
 But this is done very rarely.
 We estimate the space complexity of the program before running the program.

01/07/2022 SCSA1403 DAA-Unit I 19


SPACE COMPLEXITY

The reasons for estimating the space complexity before running the program even for the first
time are:
(1) We should know in advance, whether or not, sufficient memory is present in the computer. If
this is not known and the program is executed directly, there is possibility that the program
may consume more memory than the available during the execution of the program. This
leads to insufficient memory error and the system may crash, leading to severe damages if
that was a critical system.
(2) In Multi user systems, we prefer, the programs of lesser size, because multiple copies of the
program are run when multiple users access the system. Hence if the program occupies less
space during execution, then more number of users can be accommodated.
01/07/2022 SCSA1403 DAA-Unit I 20
SPACE COMPLEXITY
Space complexity is the sum of the following components:
(i) Instruction space:
The program which is written by the user is the source program. When this program is
compiled, a compiled version of the program is generated. For executing the program an executable
version of the program is generated. The space occupied by these three when the program is under
execution, will account for the instruction space.
The instruction space depends on the following factors:
 Compiler used – Some compiler generate optimized code which occupies less space.
 Compiler options – Optimization options may be set in the compiler options.
 Target computer – The executable code produced by the compiler is dependent on the processor
used.
01/07/2022 SCSA1403 DAA-Unit I 21
SPACE COMPLEXITY
Space complexity is the sum of the following components:
(ii) Data space:
The space needed by the constants, simple variables, arrays, structures and other data
structures will account for the data space.
The Data space depends on the following factors:
 Structure size – It is the sum of the size of component variables of the structure.
 Array size – Total size of the array is the product of the size of the data type and the number of
array locations.

01/07/2022 SCSA1403 DAA-Unit I 22


SPACE COMPLEXITY
Space complexity is the sum of the following components:
(iii) Environment stack space:
The Environment stack space is used for saving information needed to resume execution of
partially completed functions. That is whenever the control of the program is transferred from one
function to another during a function call, then the values of the local variable of that function and
return address are stored in the environment stack. This information is retrieved when the control
comes back to the same function.
The environment stack space depends on the following factors:
 Return address
 Values of all local variables and formal parameters.

01/07/2022 SCSA1403 DAA-Unit I 23


SPACE COMPLEXITY
The Total space occupied by the program during the execution of the program is the sum of the
fixed space and the variable space.
(i) Fixed space - The space occupied by the instruction space, simple variables and constants.
(ii) Variable space – The dynamically allocated space to the various data structures and the
environment stack space varies according to the input from the user.
Space complexity S(P) = c + Sp

c -- Fixed space or constant space


Sp -- Variable space

We will be interested in estimating only the variable space because that is the one which varies
according to the user input.
01/07/2022 SCSA1403 DAA-Unit I 24
SPACE COMPLEXITY

Consider the following piece of code...

int square(int a)
{
return a*a;
}

01/07/2022 SCSA1403 DAA-Unit I 25


SPACE COMPLEXITY

 Totally it requires 4 bytes of memory to complete its execution. And this 4 bytes of memory
is fixed for any input value of 'a'. This space complexity is said to be Constant Space
Complexity.
 If any algorithm requires a fixed amount of space for all input values then that space
complexity is said to be Constant Space Complexity
 Eg 2:
{
int z = a + b + c;
return(z);
}

01/07/2022 SCSA1403 DAA-Unit I 26


SPACE COMPLEXITY

 Eg.3

#include<stdio.h>
int main()
{
int a = 5, b = 5, c;
c = a + b;
printf("%d", c);
}

01/07/2022 SCSA1403 DAA-Unit I 27


SPACE COMPLEXITY
Consider the following piece of code...

int sum(int A[], int n)


{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}

01/07/2022 SCSA1403 DAA-Unit I 28


SPACE COMPLEXITY

In above piece of code it requires


 'n*2' bytes of memory to store array variable ‘a[].
 2 bytes of memory for integer parameter ‘n’.
 4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes each) .
 2 bytes of memory for return value.
 That means, totally it requires '2n+8' bytes of memory to complete its execution.
 Here, the amount of memory depends on the input value of 'n'. This space complexity is
said to be Linear Space Complexity.

01/07/2022 SCSA1403 DAA-Unit I 29


TIME COMPLEXITY

 Time complexity of the program is defined as the amount of computer time it needs to
run to completion.
 The time complexity can be measured, by measuring the time taken by the program
when it is executed. This is an experimental method. But this is done very rarely.
 We always try to estimate the time consumed by the program even before it is run for
the first time.

01/07/2022 SCSA1403 DAA-Unit I 30


TIME COMPLEXITY- Example

 Imagine a classroom of 100 students in which you gave your pen to one person. Now,
you want that pen. Here are some ways to find the pen and what the O order is.
 O(n): Going and asking each student individually is O(N).
O(n2): You go and ask the first person of the class, if he has the pen. Also, you ask this person
about other 99 people in the classroom if they have that pen and so on.
O(log n): Now I divide the class into two groups, then ask: “Is it on the left side, or the right
side of the classroom?” Then I take that group and divide it into two and ask again, and so on.
Repeat the process till you are left with one student who has your pen. This is what you mean
by O(log n). 

01/07/2022 SCSA1403 DAA-Unit I 31


TIME COMPLEXITY- Example

 I might need to do the O(n2) search if only one student knows on which
student the pen is hidden.
 I’d use the O(n) if one student had the pen and only they knew it.
 I’d use the O(log n) search if all the students knew, but would only tell me if I
guessed the right side. 

01/07/2022 SCSA1403 DAA-Unit I 32


TIME COMPLEXITY

The reasons for estimating the time complexity of the program even before running the
program for the first time are:
(1) We need real time response for many applications. That is a faster execution of the program is
required for many applications. If the time complexity is estimated beforehand, then
modifications can be done to the program to improve the performance before running it.
(2) It is used to specify the upper limit for time of execution for some programs. The purpose of this
is to avoid infinite loops.

01/07/2022 SCSA1403 DAA-Unit I 33


TIME COMPLEXITY

The time complexity of the program depends on the following factors:


 Compiler used – some compilers produce optimized code which consumes less time to get
executed.
 Compiler options – The optimization options can be set in the options of the compiler.
 Target computer – The speed of the computer or the number of instructions executed per
second differs from one computer to another

01/07/2022 SCSA1403 DAA-Unit I 34


TIME COMPLEXITY

The total time taken for the execution of the program is the sum of the compilation time and the
execution time.
(i) Compile time – The time taken for the compilation of the program to produce the intermediate
object code or the compiler version of the program. The compilation time is taken only once as it
is enough if the program is compiled once. If optimized code is to be generated, then the
compilation time will be higher.
(ii) Run time or Execution time - The time taken for the execution of the program. The optimized
code will take less time to get executed.

01/07/2022 SCSA1403 DAA-Unit I 35


TIME COMPLEXITY

Time complexity T(P) = c + Tp

c -- Compile time
Tp -- Run time or execution time

We will be interested in estimating only the execution time as this is the one which varies according
to the user input.

01/07/2022 SCSA1403 DAA-Unit I 36


TIME COMPLEXITY

Time Complexity of algorithm/code is not equal to the actual time required to execute a particular
code but the number of times a statement executes.
We can prove this by using time command.
For example, Write code in C/C++ or any other language to find maximum between N numbers,
where N varies from 10, 100, 1000, 10000.
And compile that code on Linux based operating system (Fedora or Ubuntu) with below command:

gcc program.c – o program


run it with time ./program

01/07/2022 SCSA1403 DAA-Unit I 37


TIME COMPLEXITY

 You will get surprising results i.e. for N = 10 you may get 0.5ms time and for N = 10, 000 you
may get 0.2 ms time.
 Also, you will get different timings on the different machine. So, we can say that actual time
requires to execute code is machine dependent (whether you are using pentium1 or pentiun5) and
also it considers network load if your machine is in LAN/WAN.
 Even you will not get the same timings on the same machine for the same code, the reason behind
that the current network load. 
Now, the question arises if time complexity is not the actual time require executing the code then
what is it? 

01/07/2022 SCSA1403 DAA-Unit I 38


TIME COMPLEXITY

 The answer is : Instead of measuring actual time required in executing each statement in the
code, we consider how many times each statement execute. 
For example: int main()
{
printf("Hello World");
}
Output
Hello World

01/07/2022 SCSA1403 DAA-Unit I 39


TIME COMPLEXITY

 The answer is : Instead of measuring actual time required in executing each statement in the
code, we consider how many times each statement execute. 
For example: int main()
{
printf("Hello World");
}
Output
Hello World

01/07/2022 SCSA1403 DAA-Unit I 40


TIME COMPLEXITY

In above code “Hello World!!!” print only once on a screen.


So, time complexity is constant: O(1) i.e. every time constant amount of time require
to execute code, no matter which operating system or which machine configurations
you are using. 

01/07/2022 SCSA1403 DAA-Unit I 41


TIME COMPLEXITY
void main()
{
int i, n = 8;
for (i = 1; i <= n; i++) {
printf("Hello Word !!!\n");
}
}

01/07/2022 SCSA1403 DAA-Unit I 42


TIME COMPLEXITY
Output
Hello Word !!!
Hello Word !!!
Hello Word !!!
Hello Word !!!
Hello Word !!!
Hello Word !!!
Hello Word !!!
Hello Word !!!
In above code “Hello World!!!” will print N times. So, time complexity of above code is O(N).
01/07/2022 SCSA1403 DAA-Unit I 43
TIME COMPLEXITY

01/07/2022 SCSA1403 DAA-Unit I 44


TIME COMPLEXITY

Pseudocode:
Sum(a,b){
return a+b //Takes 2 unit of time(constant) one for arithmetic operation and one for
return.(as per above conventions) cost=2 no of times=1
}

Tsum= 2 = C =O(1)

01/07/2022 SCSA1403 DAA-Unit I 45


TIME COMPLEXITY

Table 1:

Statement s/e frequency Total steps


Algorithm Sum (a,n) 0 ------ 0
{ 0 ------ 0
s: =0.0; 1 1 1
for i = 1 to n do 1 (n + 1) (n + 1)
s = s + a[i]; 2 n 2n
return s; 1 1 1
} 0 ------ 0
Total (3n + 3)

01/07/2022 SCSA1403 DAA-Unit I 46


TIME COMPLEXITY

Pseudocode:
list_Sum(A,n){//A->array and n->number of elements in the array
total =0 // cost=1 no of times=1
for i=0 to n-1 // cost=2 no of times=n+1 (+1 for the end false condition)
sum = sum + A[i] // cost=2 no of times=n
return sum // cost=1 no of times=1
}

Tsum=1 + 2 * (n+1) + 2 * n + 1 = 4n + 4 =C1 * n + C2 = O(n)

01/07/2022 SCSA1403 DAA-Unit I 47


TIME COMPLEXITY

How to Compare Algorithms?

To compare algorithms, let us define a few objective measures:

Execution times: Not a good measure as execution times are specific to a particular
computer.
A number of statements executed: Not a good measure, since the number of statements
varies with the programming language as well as the style of the individual programmer.
Ideal solution: Let us assume that we express the running time of a given algorithm as a
function of the input size n (i.e., f(n)) and compare these different functions corresponding
to running times. This kind of comparison is independent of machine time, programming
style, etc.

01/07/2022 SCSA1403 DAA-Unit I 48


Asymptotic Notation
Asymptotic Notations are the expressions that are used to represent the complexity of an algorithm.

 There are three types of analysis that we perform on a particular algorithm.

 Best Case: In which we analyse the performance of an algorithm for the input, for which the
algorithm takes less time or space.

 Worst Case: In which we analyse the performance of an algorithm for the input, for which the
algorithm takes long time or space.

 Average Case: In which we analyse the performance of an algorithm for the input, for which the
algorithm takes time or space that lies between best and worst case.
01/07/2022 SCSA1403 DAA-Unit I 49
Growth of Functions and Asymptotic Notation

• When we study algorithms, we are interested in characterizing them


according to their efficiency.
• We are usually interesting in the order of growth of the running time
of an algorithm, not in the exact running time. This is also referred
to as the asymptotic running time.
• We need to develop a way to talk about rate of growth of
functions so that we can compare algorithms.
• Asymptotic notation gives us a method for classifying functions
according to their rate of growth.
01/07/2022 SCSA1403 DAA-Unit I 50
Groth of Functions and Asymptotic Notation

Types of Data Structure Asymptotic Notation


1. Big-O Notation (Ο) – Big O notation specifically describes worst case scenario.

2. Omega Notation (Ω) – Omega(Ω) notation specifically describes best case


scenario.

3. Theta Notation (θ) – This notation represents the average complexity of an


algorithm.

01/07/2022 SCSA1403 DAA-Unit I 51


Big-O Notation (Ο)

Big O notation specifically describes worst case scenario. It represents the upper bound running time
complexity of an algorithm. Lets take few examples to understand how we represent the time and
space complexity using Big O notation.
O(1)
Big O notation O(1) represents the complexity of an algorithm that always execute in same time or
space regardless of the input data.
O(1) example
The following step will always execute in same time(or space) regardless of the size of input data.

Accessing array index(int num = arr[5])

01/07/2022 SCSA1403 DAA-Unit I 52


Big-O Notation (Ο)

O(n)
Big O notation O(N) represents the complexity of an algorithm, whose performance will grow
linearly (in direct proportion) to the size of the input data.

O(n) example
The execution time will depend on the size of array. When the size of the array increases, the
execution time will also increase in the same proportion (linearly)

Traversing an array

01/07/2022 SCSA1403 DAA-Unit I 53


Big-O Notation (Ο)

O(n^2)
Big O notation O(n^2) represents the complexity of an algorithm, whose performance is directly
proportional to the square of the size of the input data.

O(n^2) example

Traversing a 2D array

Similarly there are other Big O notations such as:


logarithmic growth O(log n), log-linear growth O(n log n), exponential growth O(2^n) and factorial
growth O(n!).

01/07/2022 SCSA1403 DAA-Unit I 54


Big-O Notation (Ο)

01/07/2022 SCSA1403 DAA-Unit I 55


Omega Notation (Ω)

Omega notation specifically describes best case scenario. It represents the lower bound running time
complexity of an algorithm. So if we represent a complexity of an algorithm in Omega notation, it
means that the algorithm cannot be completed in less time than this, it would at-least take the time
represented by Omega notation or it can take more (when not in best case scenario).

01/07/2022 SCSA1403 DAA-Unit I 56


Omega Notation (Ω)

01/07/2022 SCSA1403 DAA-Unit I 57


Theta Notation (θ)

This notation describes both upper bound and lower bound of an algorithm so we can say that it
defines exact asymptotic behaviour. In the real case scenario the algorithm not always run on best and
worst cases, the average running time lies between best and worst and can be represented by the theta
notation.

01/07/2022 SCSA1403 DAA-Unit I 58


Theta Notation (θ)

01/07/2022 SCSA1403 DAA-Unit I 59


Big-O Notation

• Definition: f (n) = O(g(n)) iff there are two positive constants c


and n0 such that
|f (n)| ≤ c |g(n)| for all n ≥ n0
• If f (n) is nonnegative, we can simplify the last condition to
0 ≤ f (n) ≤ c g(n) for all n ≥ n0
• We say that “ f (n) is big-O of 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).
01/07/2022 SCSA1403 DAA-Unit I 60
Big-O Notation (Ο)

01/07/2022 SCSA1403 DAA-Unit I 61


Big-O Notation (Ο)

01/07/2022 SCSA1403 DAA-Unit I 62


Ω notation

• Definition: f (n) = Ω(g(n)) iff there are two positive constants


c and n0 such that
|f (n)| ≥ c |g(n)| for all n ≥ n0
• If f (n) is nonnegative, we can simplify the last condition to
0 ≤ c g(n) ≤ f (n) for all n ≥ n0
• We say that “ f (n) is omega of g(n).”

• As n increases, f (n) grows no slower than g(n). In other words, g(n) is


an asymptotic lower bound on f (n).

01/07/2022 SCSA1403 DAA-Unit I 63


Omega Notation (Ω)

Examples:

1. f(n) = 3n + 2

Let us take g(n) = n


c = 3
n0 = 0

Let us check the above condition

3n + 1 ≥ 3n for all n ≥ 0

The condition is satisfied. Hence f(n) = Ω(n).

01/07/2022 SCSA1403 DAA-Unit I 64


Omega Notation (Ω)

1. f(n) = 10n2 + 4n + 2

Let us take g(n) = n2


c = 10
n0 = 0

Let us check the above condition

10n2 + 4n + 2 ≥ 10n for all n ≥ 0

The condition is satisfied. Hence f(n) = Ω(n2).

01/07/2022 SCSA1403 DAA-Unit I 65


Θ notation

• Definition: f (n) = Θ(g(n)) iff there are three positive constants c1,
c2 and n0 such that
c1|g(n)| ≤ |f (n)| ≤ c2|g(n)| for all n ≥ n0
• If f (n) is nonnegative, we can simplify the last condition to
0 ≤ c1 g(n) ≤ f (n) ≤ c2 g(n) for all n ≥ n0
• We say that “ f (n) is theta of g(n).”

• As n increases, f (n) grows at the same rate as g(n). In other words,


g(n) is an asymptotically tight bound on f (n).
01/07/2022 SCSA1403 DAA-Unit I 66
Properties of Big Oh Notation
Following are some important properties of big oh natations:

1. If there are two functions f1(n) and f2(n) such that f1(n)=O(g1(n)) and f2(n)=O(g2(n))
then
f1(n)+f2(n)=max(O(g1(n)) ,O(g2(n))).
2. If there are two functions f1(n) and f2(n) such that f1(n)=O(g1(n)) and f2(n)=O(g2(n))
then
f1(n) * f2(n)=O(g1(n)) *(g2(n)).
3. If there exists a function f1 such that f1=f2*c where c is the constant then, f1 and f2 are
equivalent. That means O(f1+f2)=O(f1)=O(f2).
4. If f(n)=O(g(n)) and g(n)=O(h(n)) then f(n)=O(h(n)).
5. In a polynomial the highest power term dominates other terms.
For example if we obtain 3n3+2n2+10 then its time complexity is O(n3).

Any constant value leads to O(1) time complexity. That is if f(n)=c then it Ɛ O(1) time
complexity.

01/07/2022 SCSA1403 DAA-Unit I 67


Order of Growth
 Measuring the performance of an algorithm in relation with the input size ‘n’ is called order of
growth.
 It is clear that logarithmic function is the slowest growing function and Exponential function 2 n is
the fastest function. n logn nlogn n 2
2 n

1 0 0 1 2

2 1 2 4 4

4 2 8 16 16

8 3 24 64 256

16 4 64 256 65536

32 5 160 1024 4294967296

01/07/2022 SCSA1403 DAA-Unit I 68


Basic Efficiency Classes:
Different efficiency classes and each class possessing certain characteristic.
Name of efficiency Order of Description Example
class growth
Constant 1 As the input size grows then we get constant Scanning array
running time. elements
Logarithmic logn When we get logarithmic running time then it is Perform binary search
sure that the algorithm does not consider all its operation.
input rather the problem is divided into smaller
parts on each iteration

Linear n The running time of algorithm depends on the Performing sequential


input size n search operation.

Nlogn nlogn Some instance of input is considered for the list Sorting the elements
of size n. using merge sort or
quick sort.

01/07/2022 SCSA1403 DAA-Unit I 69


Basic Efficiency Classes:
Different efficiency classes and each class possessing certain characteristic.
Name of efficiency Order of Description Example
class growth
Quadratic n2 When the algorithm has two nested loops then Scanning matrix
this type of efficiency occurs. elements.
Cube n3 When the algorithm has three nested loops then Performing matrix
this type of efficiency occurs. multiplication.

Exponential 2n When the algorithm has very faster rate of Generating all subsets
growth then this type of efficiency occurs. of n elements.

Factorial nǃ When the algorithm is computing all the Generating all


permutations then this type of efficiency occurs permutations.

01/07/2022 SCSA1403 DAA-Unit I 70


Examples:

Linear
for ( i=0 ; i<n ; i++ )
m += i;
Time Complexity O(n)

Quadratic
for ( i=0 ; i<n ; i++ )
for( j=0 ; j<n ; j++ )
sum[i] += entry[i][j];
Time Complexity O(n2)

01/07/2022 SCSA1403 DAA-Unit I 71


Examples:

Cubic
For(i=1;i<=n;i++)
For(j=1;j<=n;j++)
For(k=1;k<=n;k++)
Printf(“AAA”);
Time Complexity is O(n3)

Logarithmic
For(i=1;i<n;i=i*2)
Printf(“AAA”)
Time Complexity O(log n)

01/07/2022 SCSA1403 DAA-Unit I 72


Examples:

Linear Logarithmic (Nlogn)


For(i=1;i<n;i=i*2)
For(j=1;j<=n,j++)
Printf(“AAA”)

01/07/2022 SCSA1403 DAA-Unit I 73

You might also like