SlideShare a Scribd company logo
Complexity Analysis
Presented
By:
Comparing Algorithms
• Given 2 or more algorithms to solve the same problem, how do we select the best one?
• Some criteria for selecting an algorithm
1) Is it easy to implement, understand, modify?
2) How long does it take to run it to completion?
3) How much of computer memory does it use?
• Software engineering is primarily concerned with the first criteria
• Time complexity
• The amount of time that an algorithm needs to run to completion
• Space complexity
• The amount of memory an algorithm needs to run
How to Calculate Running Time
• Best case running time is usually
useless
• Average case time is very useful
but often difficult to determine
• We focus on the worst case
running time
• Easier to analyze
• Crucial to applications such as
games, finance and robotics
0
20
40
60
80
100
120
RunningTime
1000 2000 3000 4000
I nput Size
best case
average case
worst case
Asymptotic Algorithm Analysis
• The asymptotic analysis of an algorithm determines the running
time in big-Oh notation
• To perform the asymptotic analysis
• We find the worst-case number of primitive operations executed as a
function of the input size
• We express this function with big-Oh notation
• Example:
• We determine that algorithm arrayMax executes at most 7n − 1 primitive
operations
• We say that algorithm arrayMax “runs in O(n) time”
• Since constant factors and lower-order terms are eventually
dropped anyhow, we can disregard them when counting primitive
operations
Big-Oh Notation
• asymptotic analysis
• The big-Oh notation is used widely to characterize
running times and space bounds
• The big-Oh notation allows us to ignore constant factors
and lower order terms and focus on the main components
of a function which affect its growth
• Given functions f(n) and g(n), we say that f(n) is O(g(n)) if
there are positive constants
c and n0 such that
f(n) ≤ cg(n) for n ≥ n0
Big-Oh Notation Example
Example: 2n + 10 is
O(n)
 2n + 10 ≤ cn
 (c − 2) n ≥ 10
 n ≥ 10/(c − 2)
 Pick c = 3 and n0= 10
0
10
20
30
40
50
60
70
80
0 5 10 15 20 25 30
n
3n
2n+10
n
Big-Oh and Growth Rate
• The big-Oh notation gives an upper bound on the growth rate of a
function
• The statement “f(n) is O(g(n))” means that the growth rate of f(n)
is no more than the growth rate of g(n)
• We can use the big-Oh notation to rank functions according to
their growth rate
f(n) is O(g(n)) g(n) is O(f(n))
g(n) grows more Yes No
f(n) grows more No Yes
Same growth Yes Yes
Relatives of Big-Oh
big-Omega
f(n) is Ω(g(n)) if there is a constant c > 0
and an integer constant n0 ≥ 1
such that
f(n) ≥ c•g(n)
for n ≥ n0
big-Theta
f(n) is Θ(g(n)) if there are constants c’ > 0 and c’’ > 0 and an integer constant n0 ≥ 1
such that c’•g(n) ≤ f(n) ≤ c’’•g(n)
 for n ≥ n0
Important Functions Growth Rates
n log(n) n nlog(n) n2
n3
2n
8 3 8 24 64 512 256
16 4 16 64 256 4096 65536
32 5 32 160 1024 32768 4.3x109
64 6 64 384 4096 262144 1.8x1019
128 7 128 896 16384 2097152 3.4x1038
256 8 256 2048 65536 1677721
8
1.2x1077
More Big-Oh Examples
7n-2
7n-2 is O(n)
need c > 0 and n0 ≥ 1 such that 7n-2 ≤ c•n for n ≥ n0
this is true for c = 7 and n0 = 1

3n3
+ 20n2
+ 5
3n3
+ 20n2
+ 5 is O(n3
)
need c > 0 and n0 ≥ 1 such that 3n3
+ 20n2
+ 5 ≤ c•n3
for n ≥ n0
this is true for c = 4 and n0 = 21

3 log n + 5
3 log n + 5 is O(log n)
need c > 0 and n0 ≥ 1 such that 3 log n + 5 ≤ c•log n for n ≥ n0
this is true for c = 8 and n0 = 2
Complexity analysis

More Related Content

PPTX
Python project2
PPTX
Python project
PDF
Implementation of dijsktra’s algorithm in parallel
PPTX
Analysis of algorithn class 3
PPTX
CS 151 Date time lecture
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
Python project2
Python project
Implementation of dijsktra’s algorithm in parallel
Analysis of algorithn class 3
CS 151 Date time lecture
Application of Integration
Application of Integration
Application of Integration

What's hot (20)

PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application+of+Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PDF
Application of Integration
PPTX
Lecture02
PPTX
Random numbers c++ class 11 and 12
PPTX
Python time
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application+of+Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Application of Integration
Lecture02
Random numbers c++ class 11 and 12
Python time
Ad

Similar to Complexity analysis (20)

PDF
ESINF03-AlgAnalis.pdfESINF03-AlgAnalis.pdf
PPTX
Asymptotic Notations
PPT
Data Structures- Part2 analysis tools
PPTX
Analysis of algorithms
PPTX
04. Growth_Rate_AND_Asymptotic Notations_.pptx
PPT
Analysis Of Algorithms I
PPTX
Data Structure Asymptotic Notations.pptx
PPT
02 order of growth
PDF
PPTX
Algorithm Analysis
PDF
Algorithm Analysis.pdf
PDF
CS-102 DS-class_01_02 Lectures Data .pdf
PDF
The bog oh notation
PDF
Ch24 efficient algorithms
PPT
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
PPTX
Asymptotic_Notations_v2_1709293258280.pptx
PPT
lecture 1
PPT
Analysis of algo
PPTX
1_Asymptotic_Notation_pptx.pptx
PDF
Asymptotic Notation
ESINF03-AlgAnalis.pdfESINF03-AlgAnalis.pdf
Asymptotic Notations
Data Structures- Part2 analysis tools
Analysis of algorithms
04. Growth_Rate_AND_Asymptotic Notations_.pptx
Analysis Of Algorithms I
Data Structure Asymptotic Notations.pptx
02 order of growth
Algorithm Analysis
Algorithm Analysis.pdf
CS-102 DS-class_01_02 Lectures Data .pdf
The bog oh notation
Ch24 efficient algorithms
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Asymptotic_Notations_v2_1709293258280.pptx
lecture 1
Analysis of algo
1_Asymptotic_Notation_pptx.pptx
Asymptotic Notation
Ad

Recently uploaded (20)

PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
Foundation of Data Science unit number two notes
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PDF
annual-report-2024-2025 original latest.
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PDF
Lecture1 pattern recognition............
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Business Acumen Training GuidePresentation.pptx
Miokarditis (Inflamasi pada Otot Jantung)
Foundation of Data Science unit number two notes
Business Ppt On Nestle.pptx huunnnhhgfvu
Qualitative Qantitative and Mixed Methods.pptx
annual-report-2024-2025 original latest.
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Lecture1 pattern recognition............
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
climate analysis of Dhaka ,Banglades.pptx
1_Introduction to advance data techniques.pptx
ISS -ESG Data flows What is ESG and HowHow
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Introduction-to-Cloud-ComputingFinal.pptx
Reliability_Chapter_ presentation 1221.5784
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Acceptance and paychological effects of mandatory extra coach I classes.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Business Acumen Training GuidePresentation.pptx

Complexity analysis

  • 2. Comparing Algorithms • Given 2 or more algorithms to solve the same problem, how do we select the best one? • Some criteria for selecting an algorithm 1) Is it easy to implement, understand, modify? 2) How long does it take to run it to completion? 3) How much of computer memory does it use? • Software engineering is primarily concerned with the first criteria • Time complexity • The amount of time that an algorithm needs to run to completion • Space complexity • The amount of memory an algorithm needs to run
  • 3. How to Calculate Running Time • Best case running time is usually useless • Average case time is very useful but often difficult to determine • We focus on the worst case running time • Easier to analyze • Crucial to applications such as games, finance and robotics 0 20 40 60 80 100 120 RunningTime 1000 2000 3000 4000 I nput Size best case average case worst case
  • 4. Asymptotic Algorithm Analysis • The asymptotic analysis of an algorithm determines the running time in big-Oh notation • To perform the asymptotic analysis • We find the worst-case number of primitive operations executed as a function of the input size • We express this function with big-Oh notation • Example: • We determine that algorithm arrayMax executes at most 7n − 1 primitive operations • We say that algorithm arrayMax “runs in O(n) time” • Since constant factors and lower-order terms are eventually dropped anyhow, we can disregard them when counting primitive operations
  • 5. Big-Oh Notation • asymptotic analysis • The big-Oh notation is used widely to characterize running times and space bounds • The big-Oh notation allows us to ignore constant factors and lower order terms and focus on the main components of a function which affect its growth • Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n0 such that f(n) ≤ cg(n) for n ≥ n0
  • 6. Big-Oh Notation Example Example: 2n + 10 is O(n)  2n + 10 ≤ cn  (c − 2) n ≥ 10  n ≥ 10/(c − 2)  Pick c = 3 and n0= 10 0 10 20 30 40 50 60 70 80 0 5 10 15 20 25 30 n 3n 2n+10 n
  • 7. Big-Oh and Growth Rate • The big-Oh notation gives an upper bound on the growth rate of a function • The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than the growth rate of g(n) • We can use the big-Oh notation to rank functions according to their growth rate f(n) is O(g(n)) g(n) is O(f(n)) g(n) grows more Yes No f(n) grows more No Yes Same growth Yes Yes
  • 8. Relatives of Big-Oh big-Omega f(n) is Ω(g(n)) if there is a constant c > 0 and an integer constant n0 ≥ 1 such that f(n) ≥ c•g(n) for n ≥ n0 big-Theta f(n) is Θ(g(n)) if there are constants c’ > 0 and c’’ > 0 and an integer constant n0 ≥ 1 such that c’•g(n) ≤ f(n) ≤ c’’•g(n)  for n ≥ n0
  • 9. Important Functions Growth Rates n log(n) n nlog(n) n2 n3 2n 8 3 8 24 64 512 256 16 4 16 64 256 4096 65536 32 5 32 160 1024 32768 4.3x109 64 6 64 384 4096 262144 1.8x1019 128 7 128 896 16384 2097152 3.4x1038 256 8 256 2048 65536 1677721 8 1.2x1077
  • 10. More Big-Oh Examples 7n-2 7n-2 is O(n) need c > 0 and n0 ≥ 1 such that 7n-2 ≤ c•n for n ≥ n0 this is true for c = 7 and n0 = 1  3n3 + 20n2 + 5 3n3 + 20n2 + 5 is O(n3 ) need c > 0 and n0 ≥ 1 such that 3n3 + 20n2 + 5 ≤ c•n3 for n ≥ n0 this is true for c = 4 and n0 = 21  3 log n + 5 3 log n + 5 is O(log n) need c > 0 and n0 ≥ 1 such that 3 log n + 5 ≤ c•log n for n ≥ n0 this is true for c = 8 and n0 = 2