SlideShare a Scribd company logo
Asymptotic Analysis of 
Algorithms 
SHYAMAL KEJRIWAL
Asymptotic Analysis of Algorithms 
An algorithm is any well-defined step-by-step 
procedure for solving a computational problem. 
Examples - 
Problem Input Output 
Checking if a number is prime A number Yes/No 
Finding a shortest path between your hostel and 
IITG Map, your hostel 
your department 
name, your dept name 
Well-defined shortest 
path 
Searching an element in an array of numbers An array of numbers Array index
Asymptotic Analysis of Algorithms 
An algorithm can be specified in English, as a computer 
program, or even as a hardware design. 
There can be many correct algorithms to solve a given 
problem. Is the correctness of an algorithm enough for 
its applicability? By the way, how do we know if an 
algorithm is correct in the first place?
Asymptotic Analysis of Algorithms 
The correctness of an algorithm is not enough. We 
need to analyze the algorithm for efficiency. 
Efficiency of an algorithm is measured in terms of: 
•Execution time (Time complexity) – How long does the 
algorithm take to produce the output? 
•The amount of memory required (Space complexity)
Asymptotic Analysis of Algorithms 
Let us analyze a few algorithms for space and time 
requirements. 
Problem: Given a number n (<=1) as an input, find the 
count of all numbers between 1 and n (both inclusive) 
whose factorials are divisible by 5.
Asymptotic Analysis of Algorithms 
ALGORITHM BY ALIA 
1. Initialize current = 1 and count = 0. 
2. Calculate fact = 1*2*…*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2. 
ALGORITHM BY BOB 
1. Initialize current = 1, fact = 1 and count = 0. 
2. Calculate fact = fact*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2.
Asymptotic Analysis of Algorithms 
ALGORITHM BY ALIA 
1. Initialize current = 1 and count = 0. 
2. Calculate fact = 1*2*…*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2. 
RUNNING TIME ANALYSIS 
First note that there are few basic operations 
that are used in this algorithm: 
1. Addition (+) 
2. Product (*) 
3. Modulus (%) 
4. Comparison (==) 
5. Assignment (=) 
We assume that each such operation takes 
constant time “c” to execute.
Asymptotic Analysis of Algorithms 
ALGORITHM BY ALIA 
1. Initialize current = 1 and count = 0. 
2. Calculate fact = 1*2*…*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2. 
RUNNING TIME ANALYSIS 
Note that different basic operations don’t take 
same time for execution. Also, the time taken by 
any single operation also varies from one 
computation system to other(processor to 
processor). For simplicity, we assume that each 
basic operation takes some constant time 
independent of its nature and computation system 
on which it is executed. 
After making the above simplified assumption to 
analyze running time, we need to calculate the 
number of times the basic operations are 
executed in this algorithm. Can you count them?
Asymptotic Analysis of Algorithms 
ALGORITHM BY ALIA 
1. Initialize current = 1 and count = 0. 
2. Calculate fact = 1*2*…*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2. 
RUNNING TIME ANALYSIS
Asymptotic Analysis of Algorithms 
ALGORITHM BY BOB 
1. Initialize current = 1, fact = 1 and count = 0. 
2. Calculate fact = fact*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2. 
RUNNING TIME ANALYSIS 
Now find the time complexity of the 
other algorithm for the same problem 
similarly and appreciate the difference.
Asymptotic Analysis of Algorithms 
ALGORITHM BY BOB 
1. Initialize current = 1, fact = 1 and count = 0. 
2. Calculate fact = fact*current. 
3. Check if fact%5 == 0. If yes, increment count 
by 1. 
4. Increment current by 1. 
5. If current == n+1, stop. Else, go to step 2. 
RUNNING TIME ANALYSIS
Asymptotic Analysis of Algorithms 
CAN WE DO BETTER 
Verify if the following algorithm is correct. 
If n < 5, count = 0. Else, count = n - 4. 
RUNNING TIME ANALYSIS 
A constant time algorithm, isn’t it? For 
such algorithms, we say that the time 
complexity is Θ(1). 
Algorithms whose solutions are 
independent of the size of the 
problem’s inputs are said to have 
constant time complexity. Constant 
time complexity is denoted as Θ(1).
Asymptotic Analysis of Algorithms 
The previous examples suggest that we need to know the 
following two things to calculate running time for an 
algorithm. 
1. What are the basic or constant time operations present 
in the computation system used to implement the 
algorithm? 
2. How many such basic operations are performed in the 
algorithm? We count the number of basic operations 
required in terms of input values or input size.
Asymptotic Analysis of Algorithms 
Similarly, to calculate space requirements for an algorithm, 
we need to answer the following. 
1. What are the basic or constant space data types present 
in the computation system used to implement the 
algorithm? 
2. How many instances of such basic data types are used in 
the algorithm?
Asymptotic Analysis of Algorithms 
Our computer systems generally support following constant 
time instructions: 
1. arithmetic (such as add, subtract, multiply, divide, 
remainder, floor, ceiling) 
2. data movement (load, store, copy) 
3. control (conditional and unconditional branch, 
subroutine call and return).
Asymptotic Analysis of Algorithms 
The basic data types generally supported in our computer 
systems are integer and floating point (for storing real 
numbers). Note that characters are also stored as 1 byte 
integers in our systems. 
So, if we use few arrays of ‘n’ integers in our algorithm, we 
can say that the space requirements of the algorithm is 
proportional to n. Or, the space complexity is Θ(n).
Asymptotic Analysis of Algorithms 
Now that you know the basic operations and data types present in modern 
systems, can you analyze the running time and space requirements of the 
following C++ code snippets. 
int x = 0; 
for ( int j = 1; j <= n/2; j++ ){ 
x = x + j; 
} 
int n; 
int *array; 
cin >> n; 
array = new int [n]; 
for ( int j = 0; j < n; j++ ){ 
array[j] = j + 1; 
}
Asymptotic Analysis of Algorithms
Asymptotic Analysis of Algorithms
Asymptotic Analysis of Algorithms 
Θ Notation 
To determine the time complexity of an algorithm: 
Express the amount of work done as a sum f1(n) + f2(n) + … + fk(n) 
Identify the dominant term: the fi such that fj is Θ(fi) and for k different from j 
fk(n) < fj(n) (for all sufficiently large n) 
Then the time complexity is Θ(fi)
Asymptotic Analysis of Algorithms
Asymptotic Analysis of Algorithms
Asymptotic Analysis of Algorithms 
Let us try analyze a few more C++ codes and express the time and 
space complexities in Θ notation. 
With independent nested loops: The number of iterations of the 
inner loop is independent of the number of iterations of the outer 
loop 
int x = 0; 
for ( int j = 1; j <= n/2; j++ ){ 
for ( int k = 1; k <= n*n; k++ ){ 
x = x + j + k; 
} 
}
Asymptotic Analysis of Algorithms 
Let us try analyze a few more C++ codes and express the time 
and space complexities in Θ notation. 
With dependent nested loops: Number of iterations of the 
inner loop depends on a value from the outer loop 
int x = 0; 
for ( int j = 1; j <= n; j++ ){ 
for ( int k = 1; k <= 3*j; k++ ){ 
x = x + j; 
} 
}
Asymptotic Analysis of Algorithms 
▪ C++ codes for algorithms discussed and running time analysis 
▪ Linear search vs binary search 
▪ Worst case time complexity

More Related Content

What's hot

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Shakil Ahmed
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
SangeethaSasi1
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
Saravanan Natarajan
 
Big o notation
Big o notationBig o notation
Big o notation
hamza mushtaq
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
hodcsencet
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
Swapnil Agrawal
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
Mohamed Loey
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
YekoyeTigabuYeko
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
Mohamed Loey
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
Rajandeep Gill
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
Soujanya V
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.
mohanrathod18
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
Saranya Natarajan
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Lovely Professional University
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
Daffodil International University
 
Lec 2 algorithms efficiency complexity
Lec 2 algorithms efficiency  complexityLec 2 algorithms efficiency  complexity
Lec 2 algorithms efficiency complexity
Anaya Zafar
 

What's hot (20)

Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
 
Tsp branch and-bound
Tsp branch and-boundTsp branch and-bound
Tsp branch and-bound
 
Big o notation
Big o notationBig o notation
Big o notation
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Lec 2 algorithms efficiency complexity
Lec 2 algorithms efficiency  complexityLec 2 algorithms efficiency  complexity
Lec 2 algorithms efficiency complexity
 

Viewers also liked

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
Amrinder Arora
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
Arvind Krishnaa
 
Лекция 1: Введение в алгоритмы
Лекция 1: Введение в алгоритмыЛекция 1: Введение в алгоритмы
Лекция 1: Введение в алгоритмыMikhail Kurnosov
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm review
chidabdu
 
Big oh Representation Used in Time complexities
Big oh Representation Used in Time complexitiesBig oh Representation Used in Time complexities
Big oh Representation Used in Time complexities
LAKSHMITHARUN PONNAM
 
lecture 26
lecture 26lecture 26
lecture 26
sajinsc
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
Kumar
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
Rajendra Dangwal
 
Stack and Hash Table
Stack and Hash TableStack and Hash Table
Stack and Hash Table
Umma Khatuna Jannat
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
fika sweety
 
Sorting
SortingSorting
Sorting
surya pandian
 
Asymptomatic bacteriuria
Asymptomatic bacteriuriaAsymptomatic bacteriuria
Asymptomatic bacteriuria
Sabita Paudel
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3
Kumar
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
Sumita Das
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notations
Rajendran
 
Emotion Economy: Ethnography as Corporate Strategy
Emotion Economy: Ethnography as Corporate StrategyEmotion Economy: Ethnography as Corporate Strategy
Emotion Economy: Ethnography as Corporate Strategy
Kelly Goto
 
Activity selection problem class 12
Activity selection problem class 12Activity selection problem class 12
Activity selection problem class 12
Kumar
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 

Viewers also liked (20)

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Лекция 1: Введение в алгоритмы
Лекция 1: Введение в алгоритмыЛекция 1: Введение в алгоритмы
Лекция 1: Введение в алгоритмы
 
Algorithm review
Algorithm reviewAlgorithm review
Algorithm review
 
Big oh Representation Used in Time complexities
Big oh Representation Used in Time complexitiesBig oh Representation Used in Time complexities
Big oh Representation Used in Time complexities
 
lecture 26
lecture 26lecture 26
lecture 26
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Stack and Hash Table
Stack and Hash TableStack and Hash Table
Stack and Hash Table
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Sorting
SortingSorting
Sorting
 
Asymptomatic bacteriuria
Asymptomatic bacteriuriaAsymptomatic bacteriuria
Asymptomatic bacteriuria
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
 
Basics & asymptotic notations
Basics & asymptotic notationsBasics & asymptotic notations
Basics & asymptotic notations
 
Emotion Economy: Ethnography as Corporate Strategy
Emotion Economy: Ethnography as Corporate StrategyEmotion Economy: Ethnography as Corporate Strategy
Emotion Economy: Ethnography as Corporate Strategy
 
Activity selection problem class 12
Activity selection problem class 12Activity selection problem class 12
Activity selection problem class 12
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 

Similar to Lecture 5: Asymptotic analysis of algorithms

Asymptotic analysis of algorithms.pptx
Asymptotic analysis of algorithms.pptxAsymptotic analysis of algorithms.pptx
Asymptotic analysis of algorithms.pptx
Rachit Jain
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
sangeetha s
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
Tribhuvan University
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
SayanSen36
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
MemMem25
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
Koteswari Kasireddy
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
Koteswari Kasireddy
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
Saranya Natarajan
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
ishan743441
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
ssuseraf8b2f
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
DeepakYadav656387
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
RahikAhmed1
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
smruti sarangi
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
iqbalphy1
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
irdginfo
 
DSA
DSADSA
DSA
rrupa2
 
Analysis algorithm
Analysis algorithmAnalysis algorithm
Analysis algorithm
renukarenuka9
 

Similar to Lecture 5: Asymptotic analysis of algorithms (20)

Asymptotic analysis of algorithms.pptx
Asymptotic analysis of algorithms.pptxAsymptotic analysis of algorithms.pptx
Asymptotic analysis of algorithms.pptx
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptxData Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
DSA
DSADSA
DSA
 
Analysis algorithm
Analysis algorithmAnalysis algorithm
Analysis algorithm
 

More from Vivek Bhargav

Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
Vivek Bhargav
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heaps
Vivek Bhargav
 
Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2
Vivek Bhargav
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
Vivek Bhargav
 
Lecture 7 & 8: Stack & queue
Lecture 7 & 8: Stack  & queueLecture 7 & 8: Stack  & queue
Lecture 7 & 8: Stack & queue
Vivek Bhargav
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
Vivek Bhargav
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
Vivek Bhargav
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
Vivek Bhargav
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
Vivek Bhargav
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntax
Vivek Bhargav
 

More from Vivek Bhargav (10)

Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
Lecture 11.1 : heaps
Lecture 11.1 :  heapsLecture 11.1 :  heaps
Lecture 11.1 : heaps
 
Lecture 10 : trees - 2
Lecture 10 : trees - 2Lecture 10 : trees - 2
Lecture 10 : trees - 2
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
Lecture 7 & 8: Stack & queue
Lecture 7 & 8: Stack  & queueLecture 7 & 8: Stack  & queue
Lecture 7 & 8: Stack & queue
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Lecture 4: Functions
Lecture 4: FunctionsLecture 4: Functions
Lecture 4: Functions
 
Lecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory AllocationLecture 3: Strings and Dynamic Memory Allocation
Lecture 3: Strings and Dynamic Memory Allocation
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Lecture 1: basic syntax
Lecture 1: basic syntaxLecture 1: basic syntax
Lecture 1: basic syntax
 

Recently uploaded

CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
PauloRodrigues104553
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 

Recently uploaded (20)

CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
Series of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.pptSeries of visio cisco devices Cisco_Icons.ppt
Series of visio cisco devices Cisco_Icons.ppt
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 

Lecture 5: Asymptotic analysis of algorithms

  • 1. Asymptotic Analysis of Algorithms SHYAMAL KEJRIWAL
  • 2. Asymptotic Analysis of Algorithms An algorithm is any well-defined step-by-step procedure for solving a computational problem. Examples - Problem Input Output Checking if a number is prime A number Yes/No Finding a shortest path between your hostel and IITG Map, your hostel your department name, your dept name Well-defined shortest path Searching an element in an array of numbers An array of numbers Array index
  • 3. Asymptotic Analysis of Algorithms An algorithm can be specified in English, as a computer program, or even as a hardware design. There can be many correct algorithms to solve a given problem. Is the correctness of an algorithm enough for its applicability? By the way, how do we know if an algorithm is correct in the first place?
  • 4. Asymptotic Analysis of Algorithms The correctness of an algorithm is not enough. We need to analyze the algorithm for efficiency. Efficiency of an algorithm is measured in terms of: •Execution time (Time complexity) – How long does the algorithm take to produce the output? •The amount of memory required (Space complexity)
  • 5. Asymptotic Analysis of Algorithms Let us analyze a few algorithms for space and time requirements. Problem: Given a number n (<=1) as an input, find the count of all numbers between 1 and n (both inclusive) whose factorials are divisible by 5.
  • 6. Asymptotic Analysis of Algorithms ALGORITHM BY ALIA 1. Initialize current = 1 and count = 0. 2. Calculate fact = 1*2*…*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2. ALGORITHM BY BOB 1. Initialize current = 1, fact = 1 and count = 0. 2. Calculate fact = fact*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2.
  • 7. Asymptotic Analysis of Algorithms ALGORITHM BY ALIA 1. Initialize current = 1 and count = 0. 2. Calculate fact = 1*2*…*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2. RUNNING TIME ANALYSIS First note that there are few basic operations that are used in this algorithm: 1. Addition (+) 2. Product (*) 3. Modulus (%) 4. Comparison (==) 5. Assignment (=) We assume that each such operation takes constant time “c” to execute.
  • 8. Asymptotic Analysis of Algorithms ALGORITHM BY ALIA 1. Initialize current = 1 and count = 0. 2. Calculate fact = 1*2*…*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2. RUNNING TIME ANALYSIS Note that different basic operations don’t take same time for execution. Also, the time taken by any single operation also varies from one computation system to other(processor to processor). For simplicity, we assume that each basic operation takes some constant time independent of its nature and computation system on which it is executed. After making the above simplified assumption to analyze running time, we need to calculate the number of times the basic operations are executed in this algorithm. Can you count them?
  • 9. Asymptotic Analysis of Algorithms ALGORITHM BY ALIA 1. Initialize current = 1 and count = 0. 2. Calculate fact = 1*2*…*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2. RUNNING TIME ANALYSIS
  • 10. Asymptotic Analysis of Algorithms ALGORITHM BY BOB 1. Initialize current = 1, fact = 1 and count = 0. 2. Calculate fact = fact*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2. RUNNING TIME ANALYSIS Now find the time complexity of the other algorithm for the same problem similarly and appreciate the difference.
  • 11. Asymptotic Analysis of Algorithms ALGORITHM BY BOB 1. Initialize current = 1, fact = 1 and count = 0. 2. Calculate fact = fact*current. 3. Check if fact%5 == 0. If yes, increment count by 1. 4. Increment current by 1. 5. If current == n+1, stop. Else, go to step 2. RUNNING TIME ANALYSIS
  • 12. Asymptotic Analysis of Algorithms CAN WE DO BETTER Verify if the following algorithm is correct. If n < 5, count = 0. Else, count = n - 4. RUNNING TIME ANALYSIS A constant time algorithm, isn’t it? For such algorithms, we say that the time complexity is Θ(1). Algorithms whose solutions are independent of the size of the problem’s inputs are said to have constant time complexity. Constant time complexity is denoted as Θ(1).
  • 13. Asymptotic Analysis of Algorithms The previous examples suggest that we need to know the following two things to calculate running time for an algorithm. 1. What are the basic or constant time operations present in the computation system used to implement the algorithm? 2. How many such basic operations are performed in the algorithm? We count the number of basic operations required in terms of input values or input size.
  • 14. Asymptotic Analysis of Algorithms Similarly, to calculate space requirements for an algorithm, we need to answer the following. 1. What are the basic or constant space data types present in the computation system used to implement the algorithm? 2. How many instances of such basic data types are used in the algorithm?
  • 15. Asymptotic Analysis of Algorithms Our computer systems generally support following constant time instructions: 1. arithmetic (such as add, subtract, multiply, divide, remainder, floor, ceiling) 2. data movement (load, store, copy) 3. control (conditional and unconditional branch, subroutine call and return).
  • 16. Asymptotic Analysis of Algorithms The basic data types generally supported in our computer systems are integer and floating point (for storing real numbers). Note that characters are also stored as 1 byte integers in our systems. So, if we use few arrays of ‘n’ integers in our algorithm, we can say that the space requirements of the algorithm is proportional to n. Or, the space complexity is Θ(n).
  • 17. Asymptotic Analysis of Algorithms Now that you know the basic operations and data types present in modern systems, can you analyze the running time and space requirements of the following C++ code snippets. int x = 0; for ( int j = 1; j <= n/2; j++ ){ x = x + j; } int n; int *array; cin >> n; array = new int [n]; for ( int j = 0; j < n; j++ ){ array[j] = j + 1; }
  • 20. Asymptotic Analysis of Algorithms Θ Notation To determine the time complexity of an algorithm: Express the amount of work done as a sum f1(n) + f2(n) + … + fk(n) Identify the dominant term: the fi such that fj is Θ(fi) and for k different from j fk(n) < fj(n) (for all sufficiently large n) Then the time complexity is Θ(fi)
  • 23. Asymptotic Analysis of Algorithms Let us try analyze a few more C++ codes and express the time and space complexities in Θ notation. With independent nested loops: The number of iterations of the inner loop is independent of the number of iterations of the outer loop int x = 0; for ( int j = 1; j <= n/2; j++ ){ for ( int k = 1; k <= n*n; k++ ){ x = x + j + k; } }
  • 24. Asymptotic Analysis of Algorithms Let us try analyze a few more C++ codes and express the time and space complexities in Θ notation. With dependent nested loops: Number of iterations of the inner loop depends on a value from the outer loop int x = 0; for ( int j = 1; j <= n; j++ ){ for ( int k = 1; k <= 3*j; k++ ){ x = x + j; } }
  • 25. Asymptotic Analysis of Algorithms ▪ C++ codes for algorithms discussed and running time analysis ▪ Linear search vs binary search ▪ Worst case time complexity