SlideShare a Scribd company logo
1 of 25
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
your department
IITG Map, your hostel
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
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;
}
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.
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

01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: IntroductionAndres Mendez-Vazquez
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solvingPrabhakaran V M
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basicsayeshasafdar8
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithmssangeetha s
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIMohamed Loey
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysisjayavignesh86
 
Lec 2 algorithms efficiency complexity
Lec 2 algorithms efficiency  complexityLec 2 algorithms efficiency  complexity
Lec 2 algorithms efficiency complexityAnaya Zafar
 
Daa presentation 97
Daa presentation 97Daa presentation 97
Daa presentation 97Garima Verma
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsMohamed Loey
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introductionssuseraf8b2f
 
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
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2Kumar
 

What's hot (20)

01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction01 Analysis of Algorithms: Introduction
01 Analysis of Algorithms: Introduction
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solving
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basics
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
 
Algorithm Introduction
Algorithm IntroductionAlgorithm Introduction
Algorithm Introduction
 
Algorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms IIAlgorithms Lecture 3: Analysis of Algorithms II
Algorithms Lecture 3: Analysis of Algorithms II
 
Lecture 3 insertion sort and complexity analysis
Lecture 3   insertion sort and complexity analysisLecture 3   insertion sort and complexity analysis
Lecture 3 insertion sort and complexity analysis
 
Lec 2 algorithms efficiency complexity
Lec 2 algorithms efficiency  complexityLec 2 algorithms efficiency  complexity
Lec 2 algorithms efficiency complexity
 
Model and Design
Model and Design Model and Design
Model and Design
 
Daa presentation 97
Daa presentation 97Daa presentation 97
Daa presentation 97
 
Fundamental of Algorithms
Fundamental of Algorithms Fundamental of Algorithms
Fundamental of Algorithms
 
Algorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern AlgorithmsAlgorithms Lecture 8: Pattern Algorithms
Algorithms Lecture 8: Pattern Algorithms
 
Analysis and Algorithms: basic Introduction
Analysis and Algorithms: basic IntroductionAnalysis and Algorithms: basic Introduction
Analysis and Algorithms: basic Introduction
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Day 3 chapter 2 unit 1
Day 3 chapter 2 unit 1Day 3 chapter 2 unit 1
Day 3 chapter 2 unit 1
 
Analysis of algorithn class 2
Analysis of algorithn class 2Analysis of algorithn class 2
Analysis of algorithn class 2
 
#1 designandanalysis of algo
#1 designandanalysis of algo#1 designandanalysis of algo
#1 designandanalysis of algo
 

Viewers also liked (20)

Tangencies
TangenciesTangencies
Tangencies
 
Lines make up images
Lines make up imagesLines make up images
Lines make up images
 
Triangles in works of art
Triangles in works of artTriangles in works of art
Triangles in works of art
 
Drawing three dimensional objects
Drawing three dimensional objectsDrawing three dimensional objects
Drawing three dimensional objects
 
Representation systems
Representation systemsRepresentation systems
Representation systems
 
Tik vania
Tik vaniaTik vania
Tik vania
 
Barry McGee
Barry McGeeBarry McGee
Barry McGee
 
Business Concepts for Mobile Applications
Business Concepts for Mobile ApplicationsBusiness Concepts for Mobile Applications
Business Concepts for Mobile Applications
 
2by978azh8hj5
2by978azh8hj52by978azh8hj5
2by978azh8hj5
 
Technology Concepts for Mobile Applications
Technology Concepts for Mobile ApplicationsTechnology Concepts for Mobile Applications
Technology Concepts for Mobile Applications
 
Halloween
HalloweenHalloween
Halloween
 
Tik vania
Tik vaniaTik vania
Tik vania
 
Vocabulary
VocabularyVocabulary
Vocabulary
 
Pruebaaa2
Pruebaaa2Pruebaaa2
Pruebaaa2
 
Parc güell
Parc güellParc güell
Parc güell
 
Introduction to Google Analytics
Introduction to Google AnalyticsIntroduction to Google Analytics
Introduction to Google Analytics
 
和聲的魅力
和聲的魅力和聲的魅力
和聲的魅力
 
Creativity
CreativityCreativity
Creativity
 
Agile Data Mining with Data Vault 2.0 (english)
Agile Data Mining with Data Vault 2.0 (english)Agile Data Mining with Data Vault 2.0 (english)
Agile Data Mining with Data Vault 2.0 (english)
 
Regular star polygons
Regular star polygonsRegular star polygons
Regular star polygons
 

Similar to Asymptotic analysis of algorithms.pptx

DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptxSayanSen36
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
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.pdfishan743441
 
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 EfficiencySaranya Natarajan
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptxRahikAhmed1
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdfpasinduneshan
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdfASMAALWADEE2
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptxwondmhunegn
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 

Similar to Asymptotic analysis of algorithms.pptx (20)

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
 
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
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
 
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
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
 
Analysis algorithm
Analysis algorithmAnalysis algorithm
Analysis algorithm
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
3 analysis.gtm
3 analysis.gtm3 analysis.gtm
3 analysis.gtm
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptx
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 

Asymptotic analysis of algorithms.pptx

  • 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 your department IITG Map, your hostel 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 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; } 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.
  • 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