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
Ad

More Related Content

What's hot (20)

Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
Sumit Satam
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
subhashchandra197
 
Time complexity
Time complexityTime complexity
Time complexity
Katang Isip
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
SangeethaSasi1
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
String matching, naive,
String matching, naive,String matching, naive,
String matching, naive,
Amit Kumar Rathi
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Daa
DaaDaa
Daa
Dhananjay Singh
 
List in Python
List in PythonList in Python
List in Python
Siddique Ibrahim
 
Top 100 Python Interview Questions And Answers
Top 100 Python Interview Questions And AnswersTop 100 Python Interview Questions And Answers
Top 100 Python Interview Questions And Answers
ProBytes
 
Lecture#02, building blocks of uml ASE
Lecture#02, building blocks of uml ASELecture#02, building blocks of uml ASE
Lecture#02, building blocks of uml ASE
babak danyal
 
Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
Abdul Rahman Sherzad
 
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
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
Kamran Ashraf
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
daimk2020
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
Sumit Satam
 
Divide and conquer - Quick sort
Divide and conquer - Quick sortDivide and conquer - Quick sort
Divide and conquer - Quick sort
Madhu Bala
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Linear Search Data Structure
Linear Search Data StructureLinear Search Data Structure
Linear Search Data Structure
Talha Shaikh
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Top 100 Python Interview Questions And Answers
Top 100 Python Interview Questions And AnswersTop 100 Python Interview Questions And Answers
Top 100 Python Interview Questions And Answers
ProBytes
 
Lecture#02, building blocks of uml ASE
Lecture#02, building blocks of uml ASELecture#02, building blocks of uml ASE
Lecture#02, building blocks of uml ASE
babak danyal
 
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
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
The Maximum Subarray Problem
The Maximum Subarray ProblemThe Maximum Subarray Problem
The Maximum Subarray Problem
Kamran Ashraf
 
Control Flow Graphs
Control Flow GraphsControl Flow Graphs
Control Flow Graphs
daimk2020
 

Viewers also liked (20)

Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
Rishabh Soni
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 
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
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
Rishabh Soni
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Nikhil Sharma
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 
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
 
Activity selection problem
Activity selection problemActivity selection problem
Activity selection problem
fika sweety
 
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
 
Ad

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
Rachit Jain
 
Algorithm description in data structures
Algorithm description in data structuresAlgorithm description in data structures
Algorithm description in data structures
ananya195642
 
Performance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic NotationsPerformance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic Notations
DrSMeenakshiSundaram1
 
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
 
design analysis of algorithmaa unit 1.pptx
design analysis of algorithmaa unit 1.pptxdesign analysis of algorithmaa unit 1.pptx
design analysis of algorithmaa unit 1.pptx
rajesshs31r
 
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
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjcModule-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer ScienceDesign and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
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
 
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
 
Algo analysis for computational programmingpptx
Algo analysis for computational programmingpptxAlgo analysis for computational programmingpptx
Algo analysis for computational programmingpptx
ashima967262
 
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
 
Asymptotic analysis of algorithms.pptx
Asymptotic analysis of algorithms.pptxAsymptotic analysis of algorithms.pptx
Asymptotic analysis of algorithms.pptx
Rachit Jain
 
Algorithm description in data structures
Algorithm description in data structuresAlgorithm description in data structures
Algorithm description in data structures
ananya195642
 
Performance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic NotationsPerformance Analysis,Time complexity, Asymptotic Notations
Performance Analysis,Time complexity, Asymptotic Notations
DrSMeenakshiSundaram1
 
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
 
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
 
design analysis of algorithmaa unit 1.pptx
design analysis of algorithmaa unit 1.pptxdesign analysis of algorithmaa unit 1.pptx
design analysis of algorithmaa unit 1.pptx
rajesshs31r
 
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
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjcModule-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer ScienceDesign and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
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
 
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
 
Algo analysis for computational programmingpptx
Algo analysis for computational programmingpptxAlgo analysis for computational programmingpptx
Algo analysis for computational programmingpptx
ashima967262
 
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
 
Ad

More from Vivek Bhargav (10)

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
 
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
 

Recently uploaded (20)

Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Construction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil EngineeringConstruction Materials (Paints) in Civil Engineering
Construction Materials (Paints) in Civil Engineering
Lavish Kashyap
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 

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
  翻译: