SlideShare a Scribd company logo
1 of 13
Time Complexity
UC Berkeley
Fall 2004, E77
http://jagger.me.berkeley.edu/~pack/e77

Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Time complexity of algorithms
Dependency of
– the time it takes to solve a problem
– as a function of the problem dimension/size

Examples:
– Sorting a list of length n
– Searching a list of length n
– Multiplying a n×n matrix by an n×1 vector

Time to solve problem might depend on data
– Average-case time
– Best-case time
• data is well suited for algorithm (can’t be counted on)

– Worst-case time
• data is such that algorithm performs poorly (time-wise)

Worst-Case gives an upper bound as to how much time
will be needed to solve any instance of the problem.
Example: N-by-N matrix, N-by-1 vector, multiply

Total = c1N+c2N+N(c3+c2N+N(c4+c5)+c5)
= (c2+c4+c5)N2 + (c1+c2+c3+c5)N
= c6N2 + c7N

N times

Y = zeros(N,1);
initialize space, c1N
initialize “for” loop, c2N
for i=1:N
Scalar assignment, c3
Y(i) = 0.0;
initialize “for” loop, c2N
for j=1:N
c4
Y(i) = Y(i) + A(i,j)*x(j);
End of loop, return/exit, c5
end
End of loop, return/exit, c5
end

N times

(3 accesses, 1 add, 1 multiply)
Asymptotic Time complexity of algorithms
Dependency of
– the time it takes to solve a problem
– as a function of the problem dimension/size

but
– formula may only be valid for “large” problems

So, we keep track of “growth rates” of the computational
workload as a function of problem dimension
Order, called “big O” notation
Given two functions, f and g, say that “f is of order g” if
– there is a constant c, and
– a value x0

such that

f ( x ) ≤ c ⋅ g ( x ) for all x > x0

So, apart from a fixed multiplicative constant, the function
g is an
– upper bound on the function f
– valid for large values of its argument.

Notation: write f = O( g ) to mean “f is of order g”.
Sometimes write f ( x )
arguments are labled.

= O( g ( x ) ) to remind us what the
Not equality,but “belongs to a class”
Recall that f ( n ) = O( g ( n ) ) means that
– there is a constant c, and
– a value n0

such that f ( n ) ≤ c ⋅ g ( n ) for all n > n0
The = sign does not mean equality!
It means that f is an element of the set of functions which
are eventually bounded by (different) constant multiples of
g.
Therefore, it is correct/ok to write

3n = O( n ) ,

( )

3n = O n 2 ,

(

3n = O n 3 ⋅ log n

)
Big O: Examples
Example:

Note:

f ( n ) = 31
g ( n) = 1
for all n > 0, 31 ≤ 31
f ( n ) 31⋅ g (n)

For all n, f is bounded above by 31g
Write
or

f ( n ) = O( g ( n ) )
31 = O(1)
Big O: Examples
Example:

f ( n ) = 4n 2 + 7 n + 12
g ( n) = n 2

for n > 8, 4n 2 + 7 n + 12 < 5n 2
Note:
f ( n)
For large enough n
f is bounded above by 5g
Write f ( n ) = O( g ( n ) )
f = O( g )
or
4n 2 + 7n + 12 = O n 2
or

( )

5 g ( n)
1200

1000

800

600

400

200

0

0

5

10

15
Big O: Relationships
Suppose f1 and f2 satisfy: There is a value n0

f1 ( n ) ≤ f 2 ( n ) for all n > n0
Then f1 ( n ) + f 2 ( n ) ≤ 2 f 2 ( n ) for all n > n0
Hence

f1 + f 2 = O( f 2 )

Example 3:

27 log( n ) + 19 + n = O( n )
f1 ( n )

f 2 ( n)

Generalization: f1 = O( f 2 ) ⇒

f1 + f 2 = O( f 2 )
Big O: Relationships
Suppose positive functions f1, f2, g1, g2, satisfy

f1 = O( g1 ) and
Then f1 ⋅ f 2 = O( g1 ⋅ g 2 )

f 2 = O( g 2 )

Why? There exist c1, c2, n1, n2 such that

f i ( n ) ≤ ci ⋅ g i ( n ) for all n > ni

Take c0=c1c2, n0=max(n1,n2). Multiply to give

f1 ( n ) f ( n ) 2 ≤ c0 ⋅ g1 ( n ) g 2 ( n ) for all n > n0

Example:

(12n

2

)

( )

+ 2 log n ( 27 log( n ) + 19 + n ) = O n 3
f 2 ( n)
f1 ( n )
Asymptotic: Relationships
Obviously, for any positive function g

g = O( g )
Let β be a positive constant. Then β ⋅ g = O( g ) as well.

( )

Example 4: 13n = O n
3

3

Message: Bounding of growth rate. If n doubles, then the
bound grows by 8.
Example 5:

log n
= O( log n )
512
N times

Y = zeros(N,1); initialize space, c1N
initialize “for” loop, c2N
for i=1:N
Scalar assignment, c3
Y(i) = 0.0;
initialize “for” loop, c2N
for j=1:N
c4
Y(i) = Y(i) + A(i,j)*x(j);
End of loop, return/exit, c5
end
End of loop, return/exit, c5
end

N times

Example: N-by-N matrix, N-by-1 vector, multiply

Total = c6N2 + c7N
•Problem size affects (is, in fact) N
•Processor speed, processor and language
architecture, ie., technology, affect c6 and c7
Hence, “this algorithm of matrix-vector multiply has O(N2)
complexity.”
Time complexity familiar tasks
Task
Matrix/vector multiply
Getting a specific element from a list
Dividing a list in half, dividing one halve in half, etc
Binary Search
Scanning (brute force search) a list
Nested for loops (k levels)
MergeSort
BubbleSort
Generate all subsets of a set of data
Generate all permutations of a set of data

Growth rate
O(N2)
O(1)
O(log2N)
O(log2N)
O(N)
O(Nk)
O(N log2N)
O(N2)
O(2N)
O(N!)

More Related Content

What's hot (20)

EULER AND FERMAT THEOREM
EULER AND FERMAT THEOREMEULER AND FERMAT THEOREM
EULER AND FERMAT THEOREM
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Desktop
DesktopDesktop
Desktop
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Computing F-blowups
Computing F-blowupsComputing F-blowups
Computing F-blowups
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Signals Processing Assignment Help
Signals Processing Assignment HelpSignals Processing Assignment Help
Signals Processing Assignment Help
 
The wild McKay correspondence
The wild McKay correspondenceThe wild McKay correspondence
The wild McKay correspondence
 
Euler phi
Euler phiEuler phi
Euler phi
 
Eulers totient
Eulers totientEulers totient
Eulers totient
 
Local Volatility 1
Local Volatility 1Local Volatility 1
Local Volatility 1
 
Emat 213 study guide
Emat 213 study guideEmat 213 study guide
Emat 213 study guide
 
Clase3 Notacion
Clase3 NotacionClase3 Notacion
Clase3 Notacion
 
Solution of ps02 cmth03apr13
Solution of ps02 cmth03apr13Solution of ps02 cmth03apr13
Solution of ps02 cmth03apr13
 
Time complexity
Time complexityTime complexity
Time complexity
 
Bch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domainBch and reed solomon codes generation in frequency domain
Bch and reed solomon codes generation in frequency domain
 
02 asymp
02 asymp02 asymp
02 asymp
 
Lesson 10: The Chain Rule (slides)
Lesson 10: The Chain Rule (slides)Lesson 10: The Chain Rule (slides)
Lesson 10: The Chain Rule (slides)
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 

Similar to Time complexity

Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptsrushtiivp
 
Weekends with Competitive Programming
Weekends with Competitive ProgrammingWeekends with Competitive Programming
Weekends with Competitive ProgrammingNiharikaSingh839269
 
Lecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfLecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfShaistaRiaz4
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 
asymptotic notations i
asymptotic notations iasymptotic notations i
asymptotic notations iAli mahmood
 
Prime numbers boundary
Prime numbers boundary Prime numbers boundary
Prime numbers boundary Camilo Ulloa
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
lecture 1
lecture 1lecture 1
lecture 1sajinsc
 

Similar to Time complexity (20)

Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
big_oh
big_ohbig_oh
big_oh
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Design and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt pptDesign and analysis of algorithm ppt ppt
Design and analysis of algorithm ppt ppt
 
Weekends with Competitive Programming
Weekends with Competitive ProgrammingWeekends with Competitive Programming
Weekends with Competitive Programming
 
Lecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfLecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdf
 
Lecture3(b).pdf
Lecture3(b).pdfLecture3(b).pdf
Lecture3(b).pdf
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
asymptotic notations i
asymptotic notations iasymptotic notations i
asymptotic notations i
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
DAA_LECT_2.pdf
DAA_LECT_2.pdfDAA_LECT_2.pdf
DAA_LECT_2.pdf
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
Prime numbers boundary
Prime numbers boundary Prime numbers boundary
Prime numbers boundary
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
lecture 1
lecture 1lecture 1
lecture 1
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 

Time complexity

  • 1. Time Complexity UC Berkeley Fall 2004, E77 http://jagger.me.berkeley.edu/~pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  • 2. Time complexity of algorithms Dependency of – the time it takes to solve a problem – as a function of the problem dimension/size Examples: – Sorting a list of length n – Searching a list of length n – Multiplying a n×n matrix by an n×1 vector Time to solve problem might depend on data – Average-case time – Best-case time • data is well suited for algorithm (can’t be counted on) – Worst-case time • data is such that algorithm performs poorly (time-wise) Worst-Case gives an upper bound as to how much time will be needed to solve any instance of the problem.
  • 3. Example: N-by-N matrix, N-by-1 vector, multiply Total = c1N+c2N+N(c3+c2N+N(c4+c5)+c5) = (c2+c4+c5)N2 + (c1+c2+c3+c5)N = c6N2 + c7N N times Y = zeros(N,1); initialize space, c1N initialize “for” loop, c2N for i=1:N Scalar assignment, c3 Y(i) = 0.0; initialize “for” loop, c2N for j=1:N c4 Y(i) = Y(i) + A(i,j)*x(j); End of loop, return/exit, c5 end End of loop, return/exit, c5 end N times (3 accesses, 1 add, 1 multiply)
  • 4. Asymptotic Time complexity of algorithms Dependency of – the time it takes to solve a problem – as a function of the problem dimension/size but – formula may only be valid for “large” problems So, we keep track of “growth rates” of the computational workload as a function of problem dimension
  • 5. Order, called “big O” notation Given two functions, f and g, say that “f is of order g” if – there is a constant c, and – a value x0 such that f ( x ) ≤ c ⋅ g ( x ) for all x > x0 So, apart from a fixed multiplicative constant, the function g is an – upper bound on the function f – valid for large values of its argument. Notation: write f = O( g ) to mean “f is of order g”. Sometimes write f ( x ) arguments are labled. = O( g ( x ) ) to remind us what the
  • 6. Not equality,but “belongs to a class” Recall that f ( n ) = O( g ( n ) ) means that – there is a constant c, and – a value n0 such that f ( n ) ≤ c ⋅ g ( n ) for all n > n0 The = sign does not mean equality! It means that f is an element of the set of functions which are eventually bounded by (different) constant multiples of g. Therefore, it is correct/ok to write 3n = O( n ) , ( ) 3n = O n 2 , ( 3n = O n 3 ⋅ log n )
  • 7. Big O: Examples Example: Note: f ( n ) = 31 g ( n) = 1 for all n > 0, 31 ≤ 31 f ( n ) 31⋅ g (n) For all n, f is bounded above by 31g Write or f ( n ) = O( g ( n ) ) 31 = O(1)
  • 8. Big O: Examples Example: f ( n ) = 4n 2 + 7 n + 12 g ( n) = n 2 for n > 8, 4n 2 + 7 n + 12 < 5n 2 Note: f ( n) For large enough n f is bounded above by 5g Write f ( n ) = O( g ( n ) ) f = O( g ) or 4n 2 + 7n + 12 = O n 2 or ( ) 5 g ( n) 1200 1000 800 600 400 200 0 0 5 10 15
  • 9. Big O: Relationships Suppose f1 and f2 satisfy: There is a value n0 f1 ( n ) ≤ f 2 ( n ) for all n > n0 Then f1 ( n ) + f 2 ( n ) ≤ 2 f 2 ( n ) for all n > n0 Hence f1 + f 2 = O( f 2 ) Example 3: 27 log( n ) + 19 + n = O( n ) f1 ( n ) f 2 ( n) Generalization: f1 = O( f 2 ) ⇒ f1 + f 2 = O( f 2 )
  • 10. Big O: Relationships Suppose positive functions f1, f2, g1, g2, satisfy f1 = O( g1 ) and Then f1 ⋅ f 2 = O( g1 ⋅ g 2 ) f 2 = O( g 2 ) Why? There exist c1, c2, n1, n2 such that f i ( n ) ≤ ci ⋅ g i ( n ) for all n > ni Take c0=c1c2, n0=max(n1,n2). Multiply to give f1 ( n ) f ( n ) 2 ≤ c0 ⋅ g1 ( n ) g 2 ( n ) for all n > n0 Example: (12n 2 ) ( ) + 2 log n ( 27 log( n ) + 19 + n ) = O n 3 f 2 ( n) f1 ( n )
  • 11. Asymptotic: Relationships Obviously, for any positive function g g = O( g ) Let β be a positive constant. Then β ⋅ g = O( g ) as well. ( ) Example 4: 13n = O n 3 3 Message: Bounding of growth rate. If n doubles, then the bound grows by 8. Example 5: log n = O( log n ) 512
  • 12. N times Y = zeros(N,1); initialize space, c1N initialize “for” loop, c2N for i=1:N Scalar assignment, c3 Y(i) = 0.0; initialize “for” loop, c2N for j=1:N c4 Y(i) = Y(i) + A(i,j)*x(j); End of loop, return/exit, c5 end End of loop, return/exit, c5 end N times Example: N-by-N matrix, N-by-1 vector, multiply Total = c6N2 + c7N •Problem size affects (is, in fact) N •Processor speed, processor and language architecture, ie., technology, affect c6 and c7 Hence, “this algorithm of matrix-vector multiply has O(N2) complexity.”
  • 13. Time complexity familiar tasks Task Matrix/vector multiply Getting a specific element from a list Dividing a list in half, dividing one halve in half, etc Binary Search Scanning (brute force search) a list Nested for loops (k levels) MergeSort BubbleSort Generate all subsets of a set of data Generate all permutations of a set of data Growth rate O(N2) O(1) O(log2N) O(log2N) O(N) O(Nk) O(N log2N) O(N2) O(2N) O(N!)