SlideShare a Scribd company logo
1 of 17
ALGORITHM COMPLEXITY
(ITERATIVE)
Dr B R Ambedkar National Institute of Technology, Jalandhar
• What is algorithm complexity?
• What is the need to analyze the complexity of an algorithm?
• Execution Speed (depending on n)
• Some common time complexities
• Analysis of Algorithms
Contents
Algorithm Complexity
•Algorithm complexity is a measure which evaluates the order
of the count of operations, performed by a given algorithm as a
function of the size of the input data.
•Complexity of algorithm is usually evaluated in three different
cases:
• Best Case
• Average Case
• Worst Case
• In simple language, algorithm complexity is rough estimation
of number of steps performed by given computation depending
on the size of the input data.
• Measured through asymptotic notation
O(g(n)) where g(n) is a function of the input data size.
• Examples:-
 Linear Complexity O(n),
 Quadratic complexity O(n2),
 Logarithmic complexity O(log n),
 Cubic complexity O(n3)
Algorithm Complexity(continued)
• It takes a lot of time for a program to process large inputs and
complete the execution of the program.
• Sometimes, it is important to analyze the execution time of
given algorithm so that it is more user efficient and user does
not need to wait a long time for the program to execute.
• So, to analyze how much time a user will require if he enters a
value, we analyze the complexity of the algorithms.
• The better the time complexity of an algorithm is, the faster the
program will be executed.
Why analyzing Algorithm complexity?
Execution Speed (depending on n)
Algorithm 10 50 100 1000 10000 100000
O(log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
O(n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
O(n*log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
O(n2) < 1 sec. < 1 sec. < 1 sec. < 1 sec. 2 sec.
4 min.
(approx.)
O(n3) < 1 sec. < 1 sec. < 1 sec. 20 sec.
5 hours
(approx.)
231 days
(approx.)
O(2n) < 1 sec.
260 days
(approx.)
hang hang hang hang
O(n!) hang hang hang hang hang hang
Some Important Points
NOTE:-
• Time complexity is always measured in worst case, i.e.,
O(g(n)).
• The time complexity is always written in the form of the
largest degree of the polynomial as a function of n
obtained. For example, if g(n) = 6n2 + 4n +7, then the time
complexity of the algorithm is O(n2).
• If there are O(1) statements under If Else statements, then
the time complexity of If Else block will always be O(1).
Some Important Points (Continued)
• For example:-
if (condition 1) {
Some O(1) statements
} else if (condition 2) {
Some O(1) statements
} else if (condition 3) {
Some O(1) statements
} else {
Some O(1) statements
}
Some Complexities
Complexity Notation Description
Constant O(1)
Constant number of operations, not
depending on the input data size,
e.g. n=10000 1-2 operations.
Logarithmic O(log n)
Number of operations proportional
to log2n where n is the size of the
input, e.g. n=10000 14
operations
Linear O(n)
Number of operations proportional
to the input data size, e.g. n =
10000 5000 operations
Analysis of Algorithms
O(1) : Time complexity of a function (or set of statements) is
considered as O(1) if it doesn’t contain loop, recursion and call
to any other non-constant time function.
• A loop or recursion that runs a constant number of times is
also considered as O(1).
Example:-
// Here c is a constant
for( i = 0 ; i < c ; i++) {
cout<<“Hello”;
}
Analysis of Algorithms
O(n): Time Complexity of a loop is considered as O(n) if the
loop variables is incremented or decremented by a constant
amount.
Example:-
for( i = 1 ; i <= n ; i++) {
cout<<“Hello”;
}
The loop will execute at most n times in the worst case. So the
time complexity of the above algorithm is O(n).
Analysis of Algorithms
O(nk): Time complexity of nested loops is equal to the number
of times the innermost statement is executed.
Example:-
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
cout<<“Good”;
}
}
The inner loop runs from j=1 to j=n, so it will run n times. This
inner loop will run each of n times for i=1 to i=n, so the total
running time will be O(n*n) which is O(n2).
Analysis of Algorithms
O(log n): Time Complexity of a loop is considered as O(log n) if
the loop variables is divided or multiplied by a constant amount.
Example:-
for (int i = 1; i <= n; i = i*2) {
cout<<“Great”;
}
The value of i will be 1, 2, 4, 8,.…..,2k until 2k <= n.
Therefore, k = log2n.
Hence, the loop will run maximum log2n times, so the time
complexity is O(log2n).
Analysis of Algorithms
Example:
int i = 1,s = 0;
while(s <=n)
{
i++;
s = s + i;
}
Therefore, s=(i*(i+1))/2
Execution will end when s > n.
Hence, n = (i*(i+1))/2
i 1 2 3 4 5
s 1 3 6 10 15
Analysis of Algorithms
Ignoring constant terms, the equation becomes
n = i2
Hence, loop will exit when i = √n.
Therefore, time complexity of the algorithm is O(√n).
Conclusion
• Different programs require execute in different time
limits.
• But to make the programs most effective and make them
execute in the shortest time, we need to find optimize
algorithm to a problem.
• Analyzing the time complexity of algorithms help us
analyze and find out the best algorithm that will solve the
problem in the least possible time.
References
Websites:-
• https://www.geeksforgeeks.org/anaylsis-of-algorithm-
set-4-analysis-of-loops/
• https://www.leda-tutorial.org/en/official/ch02s02s03.html
• https://introprogramming.info/english-intro-csharp-
book/read-online/chapter-19-data-structures-and-
algorithm-complexity/

More Related Content

What's hot

Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Dr. Pankaj Agarwal
 
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 algortihmSajid Marwat
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysisDr. Rajdeep Chatterjee
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
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.Tariq Khan
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O NotationJawad Khan
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
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
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithmPratik Mota
 
Sample presentation slides
Sample presentation slidesSample presentation slides
Sample presentation slidesvahid baghi
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3Kumar
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructureKumar
 

What's hot (20)

Algorithmic Notations
Algorithmic NotationsAlgorithmic Notations
Algorithmic Notations
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Parallel algorithms
Parallel algorithms Parallel algorithms
Parallel algorithms
 
Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis Introduction to Algorithms Complexity Analysis
Introduction to Algorithms Complexity Analysis
 
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
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
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.
 
Big o
Big oBig o
Big o
 
Complexity analysis - The Big O Notation
Complexity analysis - The Big O NotationComplexity analysis - The Big O Notation
Complexity analysis - The Big O Notation
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
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
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
Introduction to datastructure and algorithm
Introduction to datastructure and algorithmIntroduction to datastructure and algorithm
Introduction to datastructure and algorithm
 
Sample presentation slides
Sample presentation slidesSample presentation slides
Sample presentation slides
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Analysis of algorithn class 3
Analysis of algorithn class 3Analysis of algorithn class 3
Analysis of algorithn class 3
 
Algorithm big o
Algorithm big oAlgorithm big o
Algorithm big o
 
Parallel quicksort cz. 1
Parallel quicksort cz. 1Parallel quicksort cz. 1
Parallel quicksort cz. 1
 
Algorithem complexity in data sructure
Algorithem complexity in data sructureAlgorithem complexity in data sructure
Algorithem complexity in data sructure
 

Similar to ALGO COMPLEXITY ANALYSIS

19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexityashishtinku
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptxSunilWork1
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexityshowkat27
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptxabcdefgh690537
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxChSreenivasuluReddy
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..KarthikeyaLanka1
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptxEnosSalar
 
Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfzoric99
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptxpallavidhade2
 
Brief introduction to Algorithm analysis
Brief introduction to Algorithm analysis Brief introduction to Algorithm analysis
Brief introduction to Algorithm analysis Anantha Ramu
 

Similar to ALGO COMPLEXITY ANALYSIS (20)

19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptx
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
19. algorithms and-complexity
19. algorithms and-complexity19. algorithms and-complexity
19. algorithms and-complexity
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptx
 
algorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptxalgorithmanalysis and effciency.pptx
algorithmanalysis and effciency.pptx
 
DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..DS Unit-1.pptx very easy to understand..
DS Unit-1.pptx very easy to understand..
 
Computational Complexity.pptx
Computational Complexity.pptxComputational Complexity.pptx
Computational Complexity.pptx
 
Ch1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdfCh1. Analysis of Algorithms.pdf
Ch1. Analysis of Algorithms.pdf
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx1_Asymptotic_Notation_pptx.pptx
1_Asymptotic_Notation_pptx.pptx
 
Brief introduction to Algorithm analysis
Brief introduction to Algorithm analysis Brief introduction to Algorithm analysis
Brief introduction to Algorithm analysis
 

Recently uploaded

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringJuanCarlosMorales19600
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineering
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 

ALGO COMPLEXITY ANALYSIS

  • 1. ALGORITHM COMPLEXITY (ITERATIVE) Dr B R Ambedkar National Institute of Technology, Jalandhar
  • 2. • What is algorithm complexity? • What is the need to analyze the complexity of an algorithm? • Execution Speed (depending on n) • Some common time complexities • Analysis of Algorithms Contents
  • 3. Algorithm Complexity •Algorithm complexity is a measure which evaluates the order of the count of operations, performed by a given algorithm as a function of the size of the input data. •Complexity of algorithm is usually evaluated in three different cases: • Best Case • Average Case • Worst Case
  • 4. • In simple language, algorithm complexity is rough estimation of number of steps performed by given computation depending on the size of the input data. • Measured through asymptotic notation O(g(n)) where g(n) is a function of the input data size. • Examples:-  Linear Complexity O(n),  Quadratic complexity O(n2),  Logarithmic complexity O(log n),  Cubic complexity O(n3) Algorithm Complexity(continued)
  • 5. • It takes a lot of time for a program to process large inputs and complete the execution of the program. • Sometimes, it is important to analyze the execution time of given algorithm so that it is more user efficient and user does not need to wait a long time for the program to execute. • So, to analyze how much time a user will require if he enters a value, we analyze the complexity of the algorithms. • The better the time complexity of an algorithm is, the faster the program will be executed. Why analyzing Algorithm complexity?
  • 6. Execution Speed (depending on n) Algorithm 10 50 100 1000 10000 100000 O(log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. O(n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. O(n*log2n) < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. O(n2) < 1 sec. < 1 sec. < 1 sec. < 1 sec. 2 sec. 4 min. (approx.) O(n3) < 1 sec. < 1 sec. < 1 sec. 20 sec. 5 hours (approx.) 231 days (approx.) O(2n) < 1 sec. 260 days (approx.) hang hang hang hang O(n!) hang hang hang hang hang hang
  • 7. Some Important Points NOTE:- • Time complexity is always measured in worst case, i.e., O(g(n)). • The time complexity is always written in the form of the largest degree of the polynomial as a function of n obtained. For example, if g(n) = 6n2 + 4n +7, then the time complexity of the algorithm is O(n2). • If there are O(1) statements under If Else statements, then the time complexity of If Else block will always be O(1).
  • 8. Some Important Points (Continued) • For example:- if (condition 1) { Some O(1) statements } else if (condition 2) { Some O(1) statements } else if (condition 3) { Some O(1) statements } else { Some O(1) statements }
  • 9. Some Complexities Complexity Notation Description Constant O(1) Constant number of operations, not depending on the input data size, e.g. n=10000 1-2 operations. Logarithmic O(log n) Number of operations proportional to log2n where n is the size of the input, e.g. n=10000 14 operations Linear O(n) Number of operations proportional to the input data size, e.g. n = 10000 5000 operations
  • 10. Analysis of Algorithms O(1) : Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. • A loop or recursion that runs a constant number of times is also considered as O(1). Example:- // Here c is a constant for( i = 0 ; i < c ; i++) { cout<<“Hello”; }
  • 11. Analysis of Algorithms O(n): Time Complexity of a loop is considered as O(n) if the loop variables is incremented or decremented by a constant amount. Example:- for( i = 1 ; i <= n ; i++) { cout<<“Hello”; } The loop will execute at most n times in the worst case. So the time complexity of the above algorithm is O(n).
  • 12. Analysis of Algorithms O(nk): Time complexity of nested loops is equal to the number of times the innermost statement is executed. Example:- for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { cout<<“Good”; } } The inner loop runs from j=1 to j=n, so it will run n times. This inner loop will run each of n times for i=1 to i=n, so the total running time will be O(n*n) which is O(n2).
  • 13. Analysis of Algorithms O(log n): Time Complexity of a loop is considered as O(log n) if the loop variables is divided or multiplied by a constant amount. Example:- for (int i = 1; i <= n; i = i*2) { cout<<“Great”; } The value of i will be 1, 2, 4, 8,.…..,2k until 2k <= n. Therefore, k = log2n. Hence, the loop will run maximum log2n times, so the time complexity is O(log2n).
  • 14. Analysis of Algorithms Example: int i = 1,s = 0; while(s <=n) { i++; s = s + i; } Therefore, s=(i*(i+1))/2 Execution will end when s > n. Hence, n = (i*(i+1))/2 i 1 2 3 4 5 s 1 3 6 10 15
  • 15. Analysis of Algorithms Ignoring constant terms, the equation becomes n = i2 Hence, loop will exit when i = √n. Therefore, time complexity of the algorithm is O(√n).
  • 16. Conclusion • Different programs require execute in different time limits. • But to make the programs most effective and make them execute in the shortest time, we need to find optimize algorithm to a problem. • Analyzing the time complexity of algorithms help us analyze and find out the best algorithm that will solve the problem in the least possible time.
  • 17. References Websites:- • https://www.geeksforgeeks.org/anaylsis-of-algorithm- set-4-analysis-of-loops/ • https://www.leda-tutorial.org/en/official/ch02s02s03.html • https://introprogramming.info/english-intro-csharp- book/read-online/chapter-19-data-structures-and- algorithm-complexity/