SlideShare a Scribd company logo
1 of 19
1
Data Structures
Performance Analysis
2
Fundamental Concepts
Some fundamental concepts that you should
know:
– Dynamic memory allocation.
– Recursion.
– Performance analysis.
3
Performance Analysis
• There are problems and algorithms to solve them.
• Problems and problem instances.
• Example: Sorting data in ascending order.
– Problem: Sorting
– Problem Instance: e.g. sorting data (2 3 9 5 6 8)
– Algorithms: Bubble sort, Merge sort, Quick sort,
Selection sort, etc.
• Which is the best algorithm for the problem? How
do we judge?
4
Performance Analysis
• Two criteria are used to judge algorithms:
(i) time complexity (ii) space complexity.
• Space Complexity of an algorithm is the
amount of memory it needs to run to
completion.
• Time Complexity of an algorithm is the
amount of CPU time it needs to run to
completion.
5
Space Complexity
• Memory space S(P) needed by a program P,
consists of two components:
– A fixed part: needed for instruction space (byte
code), simple variable space, constants space
etc.  c
– A variable part: dependent on a particular
instance of input and output data. 
Sp(instance)
• S(P) = c + Sp(instance)
6
Space Complexity: Example 1
1. Algorithm abc (a, b, c)
2. {
3. return a+b+b*c+(a+b-c)/(a+b)+4.0;
4. }
For every instance 3 computer words
required to store variables: a, b, and c.
Therefore Sp()= 3. S(P) = 3.
7
Space Complexity: Example 2
1. Algorithm Sum(a[], n)
2. {
3. s:= 0.0;
4. for i = 1 to n do
5. s := s + a[i];
6. return s;
7. }
8
Space Complexity: Example 2.
• Every instance needs to store array a[] & n.
– Space needed to store n = 1 word.
– Space needed to store a[ ] = n floating point
words (or at least n
words)
– Space needed to store i and s = 2 words
• Sp(n) = (n + 3). Hence S(P) = (n + 3).
9
Time Complexity
• Time required T(P) to run a program P also
consists of two components:
– A fixed part: compile time which is
independent of the problem instance  c.
– A variable part: run time which depends on the
problem instance  tp(instance)
• T(P) = c + tp(instance)
10
Time Complexity
• How to measure T(P)?
– Measure experimentally, using a “stop watch”
 T(P) obtained in secs, msecs.
– Count program steps  T(P) obtained as a step
count.
• Fixed part is usually ignored; only the
variable part tp() is measured.
11
Time Complexity
• What is a program step?
– a+b+b*c+(a+b)/(a-b)  one step;
– comments  zero steps;
–while (<expr>) do  step count equal to
the number of times <expr> is executed.
–for i=<expr> to <expr1> do  step count
equal to number of times <expr1> is checked.
12
Time Complexity: Example 1
Statements S/E Freq. Total
1 Algorithm Sum(a[],n) 0 - 0
2 { 0 - 0
3 S = 0.0; 1 1 1
4 for i=1 to n do 1 n+1 n+1
5 s = s+a[i]; 1 n n
6 return s; 1 1 1
7 } 0 - 0
2n+3
13
Time Complexity: Example 2
Statements S/E Freq. Total
1 Algorithm Sum(a[],n,m) 0 - 0
2 { 0 - 0
3 for i=1 to n do; 1 n+1 n+1
4 for j=1 to m do 1 n(m+1) n(m+1)
5 s = s+a[i][j]; 1 nm nm
6 return s; 1 1 1
2nm+2n+2
14
Performance Measurement
• Which is better?
– T(P1) = (n+1) or T(P2) = (n2 + 5).
– T(P1) = log (n2 + 1)/n! or T(P2) = nn(nlogn)/n2.
• Complex step count functions are difficult
to compare.
• For comparing, ‘rate of growth’ of time and
space complexity functions is easy and
sufficient.
15
Big O Notation
• Big O of a function gives us ‘rate of growth’ of the
step count function f(n), in terms of a simple
function g(n), which is easy to compare.
• Definition: [Big O] The function f(n) = O(g(n))
(big ‘oh’ of g of n) iff there exist positive
constants c and n0 such that f(n) <= c*g(n) for all
n, n>=n0. See graph on next slide.
• Example: f(n) = 3n+2 is O(n) because 3n+2 <= 4n
for all n >= 2. c = 4, n0 = 2. Here g(n) = n.
16
Big O Notation
= n0
17
Big O Notation
• Example: f(n) = 10n2+4n+2 is O(n2)
because 10n2+4n+2 <= 11n2 for all n >=5.
• Example: f(n) = 6*2n+n2 is O(2n) because
6*2n+n2 <=7*2n for all n>=4.
• Algorithms can be: O(1)  constant; O(log
n)  logrithmic; O(nlogn); O(n) linear;
O(n2)  quadratic; O(n3)  cubic; O(2n) 
exponential.
18
Big O Notation
• Now it is easy to compare time or space
complexities of algorithms. Which
algorithm complexity is better?
– T(P1) = O(n) or T(P2) = O(n2)
– T(P1) = O(1) or T(P2) = O(log n)
– T(P1) = O(2n) or T(P2) = O(n10)
19
Some Results
Sum of two functions: If f(n) = f1(n) + f2(n), and
f1(n) is O(g1(n)) and f2(n) is O(g2(n)), then f(n) =
O(max(|g1(n)|, |g2(n)|)).
Product of two functions: If f(n) = f1(n)* f2(n), and
f1(n) is O(g1(n)) and f2(n) is O(g2(n)), then f(n) =
O(g1(n)* g2(n)).

More Related Content

Similar to ALGORITHM-ANALYSIS.ppt

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysissumitbardhan
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
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
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaPriyanka Rana
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingMvenkatarao
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...DebiPrasadSen
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm AnalysisMegha V
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.pptebinazer1
 
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
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexityAbbas Ali
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisAnindita Kundu
 
C++ Notes PPT.ppt
C++ Notes PPT.pptC++ Notes PPT.ppt
C++ Notes PPT.pptAlpha474815
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.pptSagarDR5
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisFerdin Joe John Joseph PhD
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfVinayNassa3
 

Similar to ALGORITHM-ANALYSIS.ppt (20)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
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.
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/ThetaAlgorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
Algorithm analysis basics - Seven Functions/Big-Oh/Omega/Theta
 
Introduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searchingIntroduction to Data Structures Sorting and searching
Introduction to Data Structures Sorting and searching
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
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..
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
asymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysisasymptotic analysis and insertion sort analysis
asymptotic analysis and insertion sort analysis
 
C++ Notes PPT.ppt
C++ Notes PPT.pptC++ Notes PPT.ppt
C++ Notes PPT.ppt
 
lecture1.ppt
lecture1.pptlecture1.ppt
lecture1.ppt
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
 
Iare ds ppt_3
Iare ds ppt_3Iare ds ppt_3
Iare ds ppt_3
 
jn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdfjn;lm;lkm';m';;lmppt of data structure.pdf
jn;lm;lkm';m';;lmppt of data structure.pdf
 
Complexity analysis
Complexity analysisComplexity analysis
Complexity analysis
 

Recently uploaded

Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightDelhi Call girls
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlkumarajju5765
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxMohammedJunaid861692
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023ymrp368
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFxolyaivanovalion
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxolyaivanovalion
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionfulawalesam
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% SecurePooja Nehwal
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Onlineanilsa9823
 

Recently uploaded (20)

Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girlCall Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
Call Girls 🫤 Dwarka ➡️ 9711199171 ➡️ Delhi 🫦 Two shot with one girl
 
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptxBPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
BPAC WITH UFSBI GENERAL PRESENTATION 18_05_2017-1.pptx
 
Data-Analysis for Chicago Crime Data 2023
Data-Analysis for Chicago Crime Data  2023Data-Analysis for Chicago Crime Data  2023
Data-Analysis for Chicago Crime Data 2023
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Halmar dropshipping via API with DroFx
Halmar  dropshipping  via API with DroFxHalmar  dropshipping  via API with DroFx
Halmar dropshipping via API with DroFx
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service Online
 

ALGORITHM-ANALYSIS.ppt

  • 2. 2 Fundamental Concepts Some fundamental concepts that you should know: – Dynamic memory allocation. – Recursion. – Performance analysis.
  • 3. 3 Performance Analysis • There are problems and algorithms to solve them. • Problems and problem instances. • Example: Sorting data in ascending order. – Problem: Sorting – Problem Instance: e.g. sorting data (2 3 9 5 6 8) – Algorithms: Bubble sort, Merge sort, Quick sort, Selection sort, etc. • Which is the best algorithm for the problem? How do we judge?
  • 4. 4 Performance Analysis • Two criteria are used to judge algorithms: (i) time complexity (ii) space complexity. • Space Complexity of an algorithm is the amount of memory it needs to run to completion. • Time Complexity of an algorithm is the amount of CPU time it needs to run to completion.
  • 5. 5 Space Complexity • Memory space S(P) needed by a program P, consists of two components: – A fixed part: needed for instruction space (byte code), simple variable space, constants space etc.  c – A variable part: dependent on a particular instance of input and output data.  Sp(instance) • S(P) = c + Sp(instance)
  • 6. 6 Space Complexity: Example 1 1. Algorithm abc (a, b, c) 2. { 3. return a+b+b*c+(a+b-c)/(a+b)+4.0; 4. } For every instance 3 computer words required to store variables: a, b, and c. Therefore Sp()= 3. S(P) = 3.
  • 7. 7 Space Complexity: Example 2 1. Algorithm Sum(a[], n) 2. { 3. s:= 0.0; 4. for i = 1 to n do 5. s := s + a[i]; 6. return s; 7. }
  • 8. 8 Space Complexity: Example 2. • Every instance needs to store array a[] & n. – Space needed to store n = 1 word. – Space needed to store a[ ] = n floating point words (or at least n words) – Space needed to store i and s = 2 words • Sp(n) = (n + 3). Hence S(P) = (n + 3).
  • 9. 9 Time Complexity • Time required T(P) to run a program P also consists of two components: – A fixed part: compile time which is independent of the problem instance  c. – A variable part: run time which depends on the problem instance  tp(instance) • T(P) = c + tp(instance)
  • 10. 10 Time Complexity • How to measure T(P)? – Measure experimentally, using a “stop watch”  T(P) obtained in secs, msecs. – Count program steps  T(P) obtained as a step count. • Fixed part is usually ignored; only the variable part tp() is measured.
  • 11. 11 Time Complexity • What is a program step? – a+b+b*c+(a+b)/(a-b)  one step; – comments  zero steps; –while (<expr>) do  step count equal to the number of times <expr> is executed. –for i=<expr> to <expr1> do  step count equal to number of times <expr1> is checked.
  • 12. 12 Time Complexity: Example 1 Statements S/E Freq. Total 1 Algorithm Sum(a[],n) 0 - 0 2 { 0 - 0 3 S = 0.0; 1 1 1 4 for i=1 to n do 1 n+1 n+1 5 s = s+a[i]; 1 n n 6 return s; 1 1 1 7 } 0 - 0 2n+3
  • 13. 13 Time Complexity: Example 2 Statements S/E Freq. Total 1 Algorithm Sum(a[],n,m) 0 - 0 2 { 0 - 0 3 for i=1 to n do; 1 n+1 n+1 4 for j=1 to m do 1 n(m+1) n(m+1) 5 s = s+a[i][j]; 1 nm nm 6 return s; 1 1 1 2nm+2n+2
  • 14. 14 Performance Measurement • Which is better? – T(P1) = (n+1) or T(P2) = (n2 + 5). – T(P1) = log (n2 + 1)/n! or T(P2) = nn(nlogn)/n2. • Complex step count functions are difficult to compare. • For comparing, ‘rate of growth’ of time and space complexity functions is easy and sufficient.
  • 15. 15 Big O Notation • Big O of a function gives us ‘rate of growth’ of the step count function f(n), in terms of a simple function g(n), which is easy to compare. • Definition: [Big O] The function f(n) = O(g(n)) (big ‘oh’ of g of n) iff there exist positive constants c and n0 such that f(n) <= c*g(n) for all n, n>=n0. See graph on next slide. • Example: f(n) = 3n+2 is O(n) because 3n+2 <= 4n for all n >= 2. c = 4, n0 = 2. Here g(n) = n.
  • 17. 17 Big O Notation • Example: f(n) = 10n2+4n+2 is O(n2) because 10n2+4n+2 <= 11n2 for all n >=5. • Example: f(n) = 6*2n+n2 is O(2n) because 6*2n+n2 <=7*2n for all n>=4. • Algorithms can be: O(1)  constant; O(log n)  logrithmic; O(nlogn); O(n) linear; O(n2)  quadratic; O(n3)  cubic; O(2n)  exponential.
  • 18. 18 Big O Notation • Now it is easy to compare time or space complexities of algorithms. Which algorithm complexity is better? – T(P1) = O(n) or T(P2) = O(n2) – T(P1) = O(1) or T(P2) = O(log n) – T(P1) = O(2n) or T(P2) = O(n10)
  • 19. 19 Some Results Sum of two functions: If f(n) = f1(n) + f2(n), and f1(n) is O(g1(n)) and f2(n) is O(g2(n)), then f(n) = O(max(|g1(n)|, |g2(n)|)). Product of two functions: If f(n) = f1(n)* f2(n), and f1(n) is O(g1(n)) and f2(n) is O(g2(n)), then f(n) = O(g1(n)* g2(n)).