SlideShare a Scribd company logo
1 of 20
ALGORITHM ANALYSIS
NURJAHAN NIPA
LECTURER, DEPT OF ICT, BDU
Lecture Outlines
โ–  Algorithm Complexity of Algorithms
โ–  Time Complexity
โ–  Space Complexity
Algorithm Analysis
Analysis of efficiency of an algorithm can be performed at two different stages, before
implementation and after implementation, as
โ–  A priori analysis โˆ’ This is defined as theoretical analysis of an algorithm. Efficiency of
algorithm is measured by assuming that all other factors e.g. speed of processor, are
constant and have no effect on implementation.
โ–  A posterior analysis โˆ’ This is defined as empirical analysis of an algorithm. The chosen
algorithm is implemented using programming language. Next the chosen algorithm is
executed on target computer machine. In this analysis, actual statistics like running time
and space needed are collected.
Algorithm analysis is dealt with the execution or running time of various operations
involved. Running time of an operation can be defined as number of computer instructions
executed per operation.
The complexity of an algorithm is a function describing the efficiency of the algorithm in
terms of the amount of data the algorithm must process. There are two main complexity
measures of the efficiency of an algorithm:
โ–  Time complexity is a function describing the amount of time an algorithm takes in
terms of the amount of input to the algorithm.
โ–  Space complexity is a function describing the amount of memory (space) an algorithm
takes in terms of the amount of input to the algorithm.
Time Complexity
There are three types of time complexities, which can be found in the analysis of an
algorithm:
๏‚ง Best case time complexity
๏‚ง Average case time complexity
๏‚ง Worst case time complexity
Best-case time complexity
The best-case time complexity of an algorithm is a measure of the minimum time that the algorithm
will require. For example, the best case for a simple linear search on a list occurs when the desired
element is the first element of the list.
Worst-case time complexity
The worst-case time complexity of an algorithm is a measure of the maximum time that the
algorithm will require. A worst-case estimate is normally computed because it provides an upper
bound for all inputs including the extreme slowest case also. For example, the worst case for a simple
linear search on a list occurs when the desired element is found at the last position of the list or not
on the list.
Average-case time complexity
The average-case time complexity of an algorithm is a measure of average time of all instances taken
by an algorithm. Average case analysis does not provide the upper bound and sometimes it is difficult
to compute.
Average-case time complexity and worst-case time complexity are the most used in algorithm
analysis. Best-case time complexity is rarely found but is does not have any uses.
What is Asymptotic Notation?
โ–  Whenever we want to perform analysis of an algorithm, we need to calculate the
complexity of that algorithm. But when we calculate the complexity of an algorithm it
does not provide the exact amount of resource required. So instead of taking the exact
amount of resource, we represent that complexity in a general form (Notation) which
produces the basic nature of that algorithm. We use that general form (Notation) for
analysis process.
โ–  Asymptotic notation of an algorithm is a mathematical representation of its complexity.
Majorly, we use THREE types of Asymptotic Notations and those are as follows...
โ–  Big - Oh (O)
โ–  Big - Omega (ฮฉ)
โ–  Big - Theta (ฮ˜)
Big - Oh Notation (O)
โ–  Big - Oh notation is used to define the upper bound of
an algorithm in terms of Time Complexity.
โ–  That means Big - Oh notation always indicates the
maximum time required by an algorithm for all input
values. That means Big - Oh notation describes the
worst case of an algorithm time complexity.
Big - Oh Notation can be defined as follows...
โ–  Consider function f(n) as time complexity of an
algorithm and g(n) is the most significant term. If f(n)
<= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we
can represent f(n) as O(g(n)).
f(n) = O(g(n))
Big - Omega Notation (ฮฉ)
โ–  Big - Omega notation is used to define the lower bound
of an algorithm in terms of Time Complexity.
โ–  That means Big-Omega notation always indicates the
minimum time required by an algorithm for all input
values. That means Big-Omega notation describes the best
case of an algorithm time complexity.
Big - Omega Notation can be defined as follows...
โ–  Consider function f(n) as time complexity of an algorithm
and g(n) is the most significant term. If f(n) >= C g(n) for
all n >= n0, C > 0 and n0 >= 1. Then we can represent
f(n) as ฮฉ(g(n)).
f(n) = ฮฉ(g(n))
Big - Theta Notation (ฮ˜)
โ–  Big - Theta notation is used to define the average bound
of an algorithm in terms of Time Complexity.
โ–  That means Big - Theta notation always indicates the
average time required by an algorithm for all input values.
That means Big - Theta notation describes the average
case of an algorithm time complexity.
โ–  Consider function f(n) as time complexity of an
algorithm and g(n) is the most significant term. If
C1 g(n) <= f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 >
0 and n0 >= 1. Then we can represent f(n) as ฮ˜(g(n)).
f(n) = ฮ˜(g(n))
What effects run time of an algorithm?
Complexity of an algorithm is a measure of the amount of time and/or space
required by an algorithm for an input of a given size (n). computer used, the hardware
platform.
๏ฑ Representation of abstract data types (ADTโ€™s)
๏ฑ Efficiency of compiler
๏ฑ Competence of implementer (programming skills)
๏ฑ Complexity of underlying algorithm
๏ฑ Size of the input
Linear Loops
To calculate the efficiency of an algorithm that has a single loop, we need to first determine the
number of times the statements in the loop will be executed. This is because the number of
iterations is directly proportional to the loop factor. Greater the loop factor, more is the number of
iterations. For example, consider the loop given below:
Here, 100 is the loop factor. We have already said that efficiency is directly proportional to the
number of iterations. Hence, the general formula in the case of linear loops may be given as
However calculating efficiency is not as simple as is shown in the above example. Consider the
loop given below:
Here, the number of iterations is half the number of the loop factor. So, here the efficiency can be
given as
Logarithmic Loops
We have seen that in linear loops, the loop updation statement either adds or subtracts the loop-
controlling variable. However, in logarithmic loops, the loop-controlling variable is either multiplied or
divided during each iteration of the loop. For example, look at the loops given below:
โ–  Consider the first for loop in which the loop-controlling variable i is multiplied by 2. The loop will
be executed only 10 times and not 1000 times because in each iteration the value of I doubles.
Now, consider the second loop in which the loop-controlling variable i is divided by 2.
โ–  In this case also, the loop will be executed 10 times. Thus, the number of iterations is a function of
the number by which the loop-controlling variable is divided or multiplied. In the examples
discussed, it is 2. That is, when n = 1000, the number of iterations can be given by log 1000 which
is approximately equal to 10.
โ–  Therefore, putting this analysis in general terms, we can conclude that the efficiency of loops in
which iterations divide or multiply the loop-controlling variables can be given as
f(n) = log n
Linear logarithmic loop
โ–  Consider the following code in which the loop-controlling variable of the inner loop is
multiplied after each iteration. The number of iterations in the inner loop is log 10. This
inner loop is controlled by an outer loop which iterates 10 times. Therefore, according to
the formula, the number of iterations for this code can be given as 10 log 10.
โ–  In more general terms, the efficiency of such loops can be given as f(n) = n log n.
Quadratic loop
In a quadratic loop, the number of iterations in the inner loop is equal to the number of
iterations in the outer loop. Consider the following code in which the outer loop executes 10
times and for each iteration of the outer loop, the inner loop also executes 10 times. Therefore,
the efficiency here is 100.
The generalized formula for quadratic loop can be given as f(n) = n2
Dependent quadratic loop
In a dependent quadratic loop, the number of iterations in the inner loop is dependent on
the outer loop. Consider the code given below:
In this code, the inner loop will execute just once in the first iteration, twice in the second
iteration, thrice in the third iteration, so on and so forth. In this way, the number of
iterations can be calculated as
If we calculate the average of this loop (55/10 = 5.5), we will observe that it is equal to the
number of iterations in the outer loop (10) plus 1 divided by 2. In general terms, the inner
loop iterates (n + 1)/2 times. Therefore, the efficiency of such a code can be given as
Space complexity
Space complexity of an algorithm represents the amount of memory space needed the
algorithm in its life cycle. Space needed by an algorithm is equal to the sum of the
following two components
โ–  A fixed part that is a space required to store certain data and variables (i.e. simple
variables and constants, program size etc.), that are not dependent of the size of
the problem.
โ–  A variable part is a space required by variables, whose size is totally dependent on
the size of the problem. For example, recursion stack space, dynamic memory
allocation etc.
Example of Space Complexity
Space complexity S(p) of any algorithm p is S(p) = A + Sp(I) Where A is treated as the fixed
part and S(I) is treated as the variable part of the algorithm which depends on instance
characteristic I. Following is a simple example that tries to explain the concept
Algorithm
โ–  SUM(P, Q)
โ–  Step 1 - START
โ–  Step 2 - R โ† P + Q + 10
โ–  Step 3 - Stop
Here we have three variables P, Q and R and one constant. Hence S(p) = 1+3. Now space is
dependent on data types of given constant types and variables and it will be multiplied
accordingly.
THANK YOU!

More Related Content

What's hot

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
ย 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
ย 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
Haitham El-Ghareeb
ย 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
chidabdu
ย 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
Sri Prasanna
ย 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
Abbas Ali
ย 

What's hot (20)

Theory of algorithms final
Theory of algorithms final Theory of algorithms final
Theory of algorithms final
ย 
Lecture 4 asymptotic notations
Lecture 4   asymptotic notationsLecture 4   asymptotic notations
Lecture 4 asymptotic notations
ย 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
ย 
Complexity
ComplexityComplexity
Complexity
ย 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
ย 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
ย 
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
ย 
Unit i basic concepts of algorithms
Unit i basic concepts of algorithmsUnit i basic concepts of algorithms
Unit i basic concepts of algorithms
ย 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
ย 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
ย 
Asymptoptic notations
Asymptoptic notationsAsymptoptic notations
Asymptoptic notations
ย 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
ย 
Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1Anlysis and design of algorithms part 1
Anlysis and design of algorithms part 1
ย 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
ย 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
ย 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
ย 
Data Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study NotesData Structures - Lecture 8 - Study Notes
Data Structures - Lecture 8 - Study Notes
ย 
Algorithm chapter 2
Algorithm chapter 2Algorithm chapter 2
Algorithm chapter 2
ย 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
ย 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
ย 

Similar to Lecture 03 algorithm analysis

Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
MemMem25
ย 
Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
vijipersonal2012
ย 
2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx2. Introduction to Algorithm.pptx
2. Introduction to Algorithm.pptx
RahikAhmed1
ย 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
Adelina Ahadova
ย 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
babuk110
ย 
Aad introduction
Aad introductionAad introduction
Aad introduction
Mr SMAK
ย 

Similar to Lecture 03 algorithm analysis (20)

DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...DSA Complexity.pptx   What is Complexity Analysis? What is the need for Compl...
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
ย 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
ย 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
ย 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
ย 
Algorithm.pptx
Algorithm.pptxAlgorithm.pptx
Algorithm.pptx
ย 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
ย 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
ย 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
ย 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
ย 
Module 1 notes of data warehousing and data
Module 1 notes of data warehousing and dataModule 1 notes of data warehousing and data
Module 1 notes of data warehousing and data
ย 
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptxASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ASYMTOTIC NOTATIONS BIG O OEMGA THETE NOTATION.pptx
ย 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using C
ย 
Measuring algorithm performance
Measuring algorithm performanceMeasuring algorithm performance
Measuring algorithm performance
ย 
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
ย 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
ย 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
ย 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
ย 
Aad introduction
Aad introductionAad introduction
Aad introduction
ย 
Python algorithm
Python algorithmPython algorithm
Python algorithm
ย 

Recently uploaded

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
SUHANI PANDEY
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
KreezheaRecto
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
ย 

Recently uploaded (20)

notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
ย 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
ย 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ย 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
ย 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
ย 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
ย 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
ย 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
ย 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
ย 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
ย 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
ย 

Lecture 03 algorithm analysis

  • 2. Lecture Outlines โ–  Algorithm Complexity of Algorithms โ–  Time Complexity โ–  Space Complexity
  • 3. Algorithm Analysis Analysis of efficiency of an algorithm can be performed at two different stages, before implementation and after implementation, as โ–  A priori analysis โˆ’ This is defined as theoretical analysis of an algorithm. Efficiency of algorithm is measured by assuming that all other factors e.g. speed of processor, are constant and have no effect on implementation. โ–  A posterior analysis โˆ’ This is defined as empirical analysis of an algorithm. The chosen algorithm is implemented using programming language. Next the chosen algorithm is executed on target computer machine. In this analysis, actual statistics like running time and space needed are collected. Algorithm analysis is dealt with the execution or running time of various operations involved. Running time of an operation can be defined as number of computer instructions executed per operation.
  • 4. The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. There are two main complexity measures of the efficiency of an algorithm: โ–  Time complexity is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm. โ–  Space complexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm.
  • 5. Time Complexity There are three types of time complexities, which can be found in the analysis of an algorithm: ๏‚ง Best case time complexity ๏‚ง Average case time complexity ๏‚ง Worst case time complexity
  • 6. Best-case time complexity The best-case time complexity of an algorithm is a measure of the minimum time that the algorithm will require. For example, the best case for a simple linear search on a list occurs when the desired element is the first element of the list. Worst-case time complexity The worst-case time complexity of an algorithm is a measure of the maximum time that the algorithm will require. A worst-case estimate is normally computed because it provides an upper bound for all inputs including the extreme slowest case also. For example, the worst case for a simple linear search on a list occurs when the desired element is found at the last position of the list or not on the list. Average-case time complexity The average-case time complexity of an algorithm is a measure of average time of all instances taken by an algorithm. Average case analysis does not provide the upper bound and sometimes it is difficult to compute. Average-case time complexity and worst-case time complexity are the most used in algorithm analysis. Best-case time complexity is rarely found but is does not have any uses.
  • 7. What is Asymptotic Notation? โ–  Whenever we want to perform analysis of an algorithm, we need to calculate the complexity of that algorithm. But when we calculate the complexity of an algorithm it does not provide the exact amount of resource required. So instead of taking the exact amount of resource, we represent that complexity in a general form (Notation) which produces the basic nature of that algorithm. We use that general form (Notation) for analysis process. โ–  Asymptotic notation of an algorithm is a mathematical representation of its complexity. Majorly, we use THREE types of Asymptotic Notations and those are as follows... โ–  Big - Oh (O) โ–  Big - Omega (ฮฉ) โ–  Big - Theta (ฮ˜)
  • 8. Big - Oh Notation (O) โ–  Big - Oh notation is used to define the upper bound of an algorithm in terms of Time Complexity. โ–  That means Big - Oh notation always indicates the maximum time required by an algorithm for all input values. That means Big - Oh notation describes the worst case of an algorithm time complexity. Big - Oh Notation can be defined as follows... โ–  Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If f(n) <= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as O(g(n)). f(n) = O(g(n))
  • 9. Big - Omega Notation (ฮฉ) โ–  Big - Omega notation is used to define the lower bound of an algorithm in terms of Time Complexity. โ–  That means Big-Omega notation always indicates the minimum time required by an algorithm for all input values. That means Big-Omega notation describes the best case of an algorithm time complexity. Big - Omega Notation can be defined as follows... โ–  Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If f(n) >= C g(n) for all n >= n0, C > 0 and n0 >= 1. Then we can represent f(n) as ฮฉ(g(n)). f(n) = ฮฉ(g(n))
  • 10. Big - Theta Notation (ฮ˜) โ–  Big - Theta notation is used to define the average bound of an algorithm in terms of Time Complexity. โ–  That means Big - Theta notation always indicates the average time required by an algorithm for all input values. That means Big - Theta notation describes the average case of an algorithm time complexity. โ–  Consider function f(n) as time complexity of an algorithm and g(n) is the most significant term. If C1 g(n) <= f(n) <= C2 g(n) for all n >= n0, C1 > 0, C2 > 0 and n0 >= 1. Then we can represent f(n) as ฮ˜(g(n)). f(n) = ฮ˜(g(n))
  • 11. What effects run time of an algorithm? Complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size (n). computer used, the hardware platform. ๏ฑ Representation of abstract data types (ADTโ€™s) ๏ฑ Efficiency of compiler ๏ฑ Competence of implementer (programming skills) ๏ฑ Complexity of underlying algorithm ๏ฑ Size of the input
  • 12. Linear Loops To calculate the efficiency of an algorithm that has a single loop, we need to first determine the number of times the statements in the loop will be executed. This is because the number of iterations is directly proportional to the loop factor. Greater the loop factor, more is the number of iterations. For example, consider the loop given below: Here, 100 is the loop factor. We have already said that efficiency is directly proportional to the number of iterations. Hence, the general formula in the case of linear loops may be given as However calculating efficiency is not as simple as is shown in the above example. Consider the loop given below: Here, the number of iterations is half the number of the loop factor. So, here the efficiency can be given as
  • 13. Logarithmic Loops We have seen that in linear loops, the loop updation statement either adds or subtracts the loop- controlling variable. However, in logarithmic loops, the loop-controlling variable is either multiplied or divided during each iteration of the loop. For example, look at the loops given below: โ–  Consider the first for loop in which the loop-controlling variable i is multiplied by 2. The loop will be executed only 10 times and not 1000 times because in each iteration the value of I doubles. Now, consider the second loop in which the loop-controlling variable i is divided by 2. โ–  In this case also, the loop will be executed 10 times. Thus, the number of iterations is a function of the number by which the loop-controlling variable is divided or multiplied. In the examples discussed, it is 2. That is, when n = 1000, the number of iterations can be given by log 1000 which is approximately equal to 10. โ–  Therefore, putting this analysis in general terms, we can conclude that the efficiency of loops in which iterations divide or multiply the loop-controlling variables can be given as f(n) = log n
  • 14. Linear logarithmic loop โ–  Consider the following code in which the loop-controlling variable of the inner loop is multiplied after each iteration. The number of iterations in the inner loop is log 10. This inner loop is controlled by an outer loop which iterates 10 times. Therefore, according to the formula, the number of iterations for this code can be given as 10 log 10. โ–  In more general terms, the efficiency of such loops can be given as f(n) = n log n.
  • 15. Quadratic loop In a quadratic loop, the number of iterations in the inner loop is equal to the number of iterations in the outer loop. Consider the following code in which the outer loop executes 10 times and for each iteration of the outer loop, the inner loop also executes 10 times. Therefore, the efficiency here is 100. The generalized formula for quadratic loop can be given as f(n) = n2
  • 16. Dependent quadratic loop In a dependent quadratic loop, the number of iterations in the inner loop is dependent on the outer loop. Consider the code given below: In this code, the inner loop will execute just once in the first iteration, twice in the second iteration, thrice in the third iteration, so on and so forth. In this way, the number of iterations can be calculated as If we calculate the average of this loop (55/10 = 5.5), we will observe that it is equal to the number of iterations in the outer loop (10) plus 1 divided by 2. In general terms, the inner loop iterates (n + 1)/2 times. Therefore, the efficiency of such a code can be given as
  • 17.
  • 18. Space complexity Space complexity of an algorithm represents the amount of memory space needed the algorithm in its life cycle. Space needed by an algorithm is equal to the sum of the following two components โ–  A fixed part that is a space required to store certain data and variables (i.e. simple variables and constants, program size etc.), that are not dependent of the size of the problem. โ–  A variable part is a space required by variables, whose size is totally dependent on the size of the problem. For example, recursion stack space, dynamic memory allocation etc.
  • 19. Example of Space Complexity Space complexity S(p) of any algorithm p is S(p) = A + Sp(I) Where A is treated as the fixed part and S(I) is treated as the variable part of the algorithm which depends on instance characteristic I. Following is a simple example that tries to explain the concept Algorithm โ–  SUM(P, Q) โ–  Step 1 - START โ–  Step 2 - R โ† P + Q + 10 โ–  Step 3 - Stop Here we have three variables P, Q and R and one constant. Hence S(p) = 1+3. Now space is dependent on data types of given constant types and variables and it will be multiplied accordingly.

Editor's Notes

  1. http://www.btechsmartclass.com/data_structures/asymptotic-notations.html