SlideShare a Scribd company logo
1 of 20
Download to read offline
Complexity Analysis
of Algorithms
Jordi Cortadella
Department of Computer Science
Estimating runtime
What is the runtime of g(n)?
Introduction to Programming © Dept. CS, UPC 2
void g(int n) {
for (int i = 0; i < n; ++i) f();
}
void g(int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) f();
}
Estimating runtime
What is the runtime of g(n)?
Introduction to Programming © Dept. CS, UPC 3
void g(int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j <= i; ++j) f();
}
Complexity analysis
• A technique to characterize the execution time of
an algorithm independently from the machine, the
language and the compiler.
• Useful for:
– evaluating the variations of execution time with regard
to the input data
– comparing algorithms
• We are typically interested in the execution time
of large instances of a problem, e.g., when 𝑛 → ∞,
(asymptotic complexity).
Introduction to Programming © Dept. CS, UPC 4
Big O
• A method to characterize the execution time of
an algorithm:
– Adding two square matrices is O(n2)
– Searching in a dictionary is O(log n)
– Sorting a vector is O(n log n)
– Solving Towers of Hanoi is O(2n)
– Multiplying two square matrices is O(n3)
– …
• The O notation only uses the dominating terms
of the execution time. Constants are disregarded.
Introduction to Programming © Dept. CS, UPC 5
Big O: formal definition
• Let T(n) be the execution time of an algorithm when
the size of input data is n.
• T(n) is O(f(n)) if there are positive constants c and n0
such that T(n)  cf(n) when n  n0.
Introduction to Programming © Dept. CS, UPC 6
n0 n
cf(n)
T(n)
Big O: example
• Let T(n) = 3n2 + 100n + 5, then T(n) = O(n2)
• Proof:
– Let c = 4 and n0 = 100.05
– For n  100.05, we have that 4n2  3n2 + 100n + 5
• T(n) is also O(n3), O(n4), etc.
Typically, the smallest complexity is used.
Introduction to Programming © Dept. CS, UPC 7
Big O: examples
Introduction to Programming © Dept. CS, UPC 8
Complexity ranking
Introduction to Programming © Dept. CS, UPC 9
Complexity analysis: examples
Let us assume that f() has complexity O(1)
Introduction to Programming © Dept. CS, UPC 10
for (int i = 0; i < n; ++i) f();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) f();
for (int i = 0; i < n; ++i)
for (int j = 0; j <= i; ++j) f();
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k) f();
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < p; ++k) f();
Complexity analysis: examples
Introduction to Programming © Dept. CS, UPC 11
if (condition) {
O(𝒏)
} else {
O(𝒏𝟐)
}
O(𝒏)
O(𝒏𝟐
)
Complexity analysis: recursion
void f(int n) {
if (n > 0) {
DoSomething(n); // O(n)
f(n/2);
}
}
Introduction to Programming © Dept. CS, UPC 12
Complexity analysis: recursion
void f(int n) {
if (n > 0) {
DoSomething(n); // O(n)
f(n/2); f(n/2);
}
}
Introduction to Programming © Dept. CS, UPC 13
n
n/2
n/4
…
1 1
…
1 1
n/4
…
1 1
…
1 1
n/2
n/4
…
1 1
…
1 1
n/4
…
1 1
…
1 1
Complexity analysis: recursion
void f(int n) {
if (n > 0) {
DoSomething(n); // O(n)
f(n-1);
}
}
Introduction to Programming © Dept. CS, UPC 14
Complexity analysis: recursion
void f(int n) {
if (n > 0) {
DoSomething(); // O(1)
f(n-1); f(n-1);
}
}
Introduction to Programming © Dept. CS, UPC 15
Asymptotic complexity (small values)
Introduction to Programming © Dept. CS, UPC 16
Asymptotic complexity (larger values)
Introduction to Programming © Dept. CS, UPC 17
Execution time: example
Let us consider that every operation can be
executed in 1 ns (10-9 s).
Introduction to Programming © Dept. CS, UPC 18
How about “big data”?
Algorithm Analysis © Dept. CS, UPC 19
Source: Jon Kleinberg and Éva Tardos, Algorithm Design, Addison Wesley 2006.
This is often the practical limit for big data
Summary
• Complexity analysis is a technique to analyze and compare
algorithms (not programs).
• It helps to have preliminary back-of-the-envelope
estimations of runtime (milliseconds, seconds, minutes,
days, years?).
• Worst-case analysis is sometimes overly pessimistic.
Average case is also interesting (not covered in this course).
• In many application domains (e.g., big data) quadratic
complexity, 𝑂 𝑛2
, is not acceptable.
• Recommendation: avoid last-minute surprises by doing
complexity analysis before writing code.
Introduction to Programming © Dept. CS, UPC 20

More Related Content

Similar to Complexity.pdf

Compilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVMCompilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVMLinaro
 
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 1Deepak John
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfssuser034ce1
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...Joe Andelija
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.pptebinazer1
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
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 CMeghaj Mallick
 
Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02mansab MIRZA
 
Class 18: Measuring Cost
Class 18: Measuring CostClass 18: Measuring Cost
Class 18: Measuring CostDavid Evans
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 
ALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.pptALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.pptsapnaverma97
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptxSunilWork1
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02Muhammad Aslam
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
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
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsMAHERMOHAMED27
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms ISri Prasanna
 

Similar to Complexity.pdf (20)

Compilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVMCompilation of COSMO for GPU using LLVM
Compilation of COSMO for GPU using LLVM
 
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
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
An Exact Exponential Branch-And-Merge Algorithm For The Single Machine Total ...
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
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
 
Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02Asymptotics 140510003721-phpapp02
Asymptotics 140510003721-phpapp02
 
Class 18: Measuring Cost
Class 18: Measuring CostClass 18: Measuring Cost
Class 18: Measuring Cost
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 
ALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.pptALGORITHM-ANALYSIS.ppt
ALGORITHM-ANALYSIS.ppt
 
Asymptotic Notations.pptx
Asymptotic Notations.pptxAsymptotic Notations.pptx
Asymptotic Notations.pptx
 
19 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp0219 algorithms-and-complexity-110627100203-phpapp02
19 algorithms-and-complexity-110627100203-phpapp02
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Lec1
Lec1Lec1
Lec1
 
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..
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elements
 
Analysis Of Algorithms I
Analysis Of Algorithms IAnalysis Of Algorithms I
Analysis Of Algorithms I
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
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
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
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🔝
 
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 🔝✔️✔️
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

Complexity.pdf

  • 1. Complexity Analysis of Algorithms Jordi Cortadella Department of Computer Science
  • 2. Estimating runtime What is the runtime of g(n)? Introduction to Programming © Dept. CS, UPC 2 void g(int n) { for (int i = 0; i < n; ++i) f(); } void g(int n) { for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) f(); }
  • 3. Estimating runtime What is the runtime of g(n)? Introduction to Programming © Dept. CS, UPC 3 void g(int n) { for (int i = 0; i < n; ++i) for (int j = 0; j <= i; ++j) f(); }
  • 4. Complexity analysis • A technique to characterize the execution time of an algorithm independently from the machine, the language and the compiler. • Useful for: – evaluating the variations of execution time with regard to the input data – comparing algorithms • We are typically interested in the execution time of large instances of a problem, e.g., when 𝑛 → ∞, (asymptotic complexity). Introduction to Programming © Dept. CS, UPC 4
  • 5. Big O • A method to characterize the execution time of an algorithm: – Adding two square matrices is O(n2) – Searching in a dictionary is O(log n) – Sorting a vector is O(n log n) – Solving Towers of Hanoi is O(2n) – Multiplying two square matrices is O(n3) – … • The O notation only uses the dominating terms of the execution time. Constants are disregarded. Introduction to Programming © Dept. CS, UPC 5
  • 6. Big O: formal definition • Let T(n) be the execution time of an algorithm when the size of input data is n. • T(n) is O(f(n)) if there are positive constants c and n0 such that T(n)  cf(n) when n  n0. Introduction to Programming © Dept. CS, UPC 6 n0 n cf(n) T(n)
  • 7. Big O: example • Let T(n) = 3n2 + 100n + 5, then T(n) = O(n2) • Proof: – Let c = 4 and n0 = 100.05 – For n  100.05, we have that 4n2  3n2 + 100n + 5 • T(n) is also O(n3), O(n4), etc. Typically, the smallest complexity is used. Introduction to Programming © Dept. CS, UPC 7
  • 8. Big O: examples Introduction to Programming © Dept. CS, UPC 8
  • 9. Complexity ranking Introduction to Programming © Dept. CS, UPC 9
  • 10. Complexity analysis: examples Let us assume that f() has complexity O(1) Introduction to Programming © Dept. CS, UPC 10 for (int i = 0; i < n; ++i) f(); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) f(); for (int i = 0; i < n; ++i) for (int j = 0; j <= i; ++j) f(); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) f(); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < p; ++k) f();
  • 11. Complexity analysis: examples Introduction to Programming © Dept. CS, UPC 11 if (condition) { O(𝒏) } else { O(𝒏𝟐) } O(𝒏) O(𝒏𝟐 )
  • 12. Complexity analysis: recursion void f(int n) { if (n > 0) { DoSomething(n); // O(n) f(n/2); } } Introduction to Programming © Dept. CS, UPC 12
  • 13. Complexity analysis: recursion void f(int n) { if (n > 0) { DoSomething(n); // O(n) f(n/2); f(n/2); } } Introduction to Programming © Dept. CS, UPC 13 n n/2 n/4 … 1 1 … 1 1 n/4 … 1 1 … 1 1 n/2 n/4 … 1 1 … 1 1 n/4 … 1 1 … 1 1
  • 14. Complexity analysis: recursion void f(int n) { if (n > 0) { DoSomething(n); // O(n) f(n-1); } } Introduction to Programming © Dept. CS, UPC 14
  • 15. Complexity analysis: recursion void f(int n) { if (n > 0) { DoSomething(); // O(1) f(n-1); f(n-1); } } Introduction to Programming © Dept. CS, UPC 15
  • 16. Asymptotic complexity (small values) Introduction to Programming © Dept. CS, UPC 16
  • 17. Asymptotic complexity (larger values) Introduction to Programming © Dept. CS, UPC 17
  • 18. Execution time: example Let us consider that every operation can be executed in 1 ns (10-9 s). Introduction to Programming © Dept. CS, UPC 18
  • 19. How about “big data”? Algorithm Analysis © Dept. CS, UPC 19 Source: Jon Kleinberg and Éva Tardos, Algorithm Design, Addison Wesley 2006. This is often the practical limit for big data
  • 20. Summary • Complexity analysis is a technique to analyze and compare algorithms (not programs). • It helps to have preliminary back-of-the-envelope estimations of runtime (milliseconds, seconds, minutes, days, years?). • Worst-case analysis is sometimes overly pessimistic. Average case is also interesting (not covered in this course). • In many application domains (e.g., big data) quadratic complexity, 𝑂 𝑛2 , is not acceptable. • Recommendation: avoid last-minute surprises by doing complexity analysis before writing code. Introduction to Programming © Dept. CS, UPC 20