SlideShare a Scribd company logo
Algorithm Analysis
Data Structures
 Data Structure is a way to store and
organize data in a computer so that it can
be used efficiently.
Algorithm
 An algorithm is a sequence of clear and
precise step-by-step instructions for
solving a problem in a finite amount of
time.
Good Algorithms?
 Run in less time
 Consume less memory
But computational resources (time
complexity) is usually more important
Algorithm Analysis
 It would seem that the most obvious way
to measure the efficiency of an algorithm
is to run it and measure how much
processor time is needed
 But is it correct???
Factors
 Hardware
 Operating System
 Compiler
 Size of input
 Nature of Input
 Algorithm
Which should be improved?
Time and Space Complexity
 Analyzing an algorithm means determining the
amount of resources (such as time and space)
needed to execute it.
 Time Complexity
◦ The time complexity of an algorithm is basically the
running time of a program as a function of the input
size.
 Space Complexity
◦ The space complexity of an algorithm is the amount
of computer memory that is required during the
program execution as a function of the input size.
Space Complexity
 Fixed part
◦ It varies from problem to problem. It includes
the space needed for storing instructions,
constants, variables, and other structures
variables like arrays
 Variable part
◦ It includes the space needed for recursion
stack, and for variables that are allocated
space dynamically during the runtime of a
program.
However, running time
requirement is more critical than
memory requirement.
Moving Beyond Experimental Analysis
Our goal is to develop an approach to analyze
the efficiency of algorithms that:
Allows us to evaluate the relative efficiency of
any two algorithms in a way that is independent
of the hardware and software environment.
 Is performed by studying a high-level
description of the algorithm without need for
implementation.
 Takes into account all possible inputs.
Running Time of an Algorithm
 Depends upon
 Input Size
 Nature of Input
 Generally time grows with size of input,
so running time of an algorithm is usually
measured as function of input size.
 Running time is measured in terms of
number of steps/primitive operations
performed
 Independent from machine, OS
Types of running time
 Worst case running time
◦ This denotes the behavior of an algorithm with respect to the
worst possible case of the input instance.
 Average case running time
◦ It is an estimate of the running time for an average input. It
specify the expected behavior of the algorithm when the input
is randomly drawn from a given distribution.
 Best case running time
◦ It is used to analyze an algorithm under optimal condition.
 Amortized running time
◦ Amortized running time refers to the time required to
perform a sequence of (related) operations averaged over all
the operations performed.
Time-Space Trade-off
 If space is a big constraint then one might
choose an algorithm that takes less space
at the cost of more CPU time.
 If time is major constraint, then one might
choose a program that takes minimum
time to execute at the cost of more
space.
Complexity Analysis - 1
'''Input: int A[N], array of N integers
Output: Sum of all numbers in array A''‘
def Sum(intList):
s=0
for i in range(len(intList)):
s = s + intList[i]
return s
Sum([5,6,7,8])
 How should we analyse this?
Count the instructions
def Sum(intList):
s=0
for i in range(len(intList)):
s = s + intList[i]
return s
Sum([5,6,7,8])
1
2
3
4 5
6
7 1,2,7,8: Once
3,4,5,6: Once per each
iteration of for loop, N
iteration
Total: 4N + 4
The complexity function of
the algorithm is : f(N) =
4N +4
8
Growth of function - 4n+4
Estimated running time for different values of N:
N = 10 => 44 steps
N = 100 => 404 steps
N = 1,000 => 4004 steps
N = 1,000,000 => 4,000,004 steps
As N grows, the number of steps grow in linear
proportion to N for this function “Sum”
What Dominates in Previous Example?
What about the +4 and 4 in 4N+4?
◦ As N gets large, the +4 becomes insignificant
◦ 4 is inaccurate, as different operations require varying
amounts of time and also does not have any significant
importance
What is fundamental is that the time is linear in N.
Asymptotic Complexity:As N gets large, concentrate
on the highest order term:
 Drop lower order terms such as +4
 Drop the constant coefficient of the highest order
term i.e. N
Asymptotic Complexity
 The 4N+4 time bound is said to "grow
asymptotically" like N
 This gives us an approximation of the
complexity of the algorithm
 Ignores lots of (machine dependent)
details, concentrate on the bigger picture
Big Oh Notation – Worst Case Analysis
If f(N) and g(N) are two complexity functions, we
say
f(N) = O(g(N))
(read "f(N) is order g(N)", or "f(N) is big-O of g(N)")
If g is an upper bound on f and if there are
constants c and N0 such that for N > N0,
f(N) ≤ c * g(N)
for all sufficiently large N.
Asymptotic graph of f(n)
Example
Consider
f(n)=2n2
+3
and g(n)=n2
Is f(n)=O(g(n))? i.e. Is 2n2
+3 = O(n2
)?
Proof:
2n2
+3 ≤ c * n2
Assume N0 =1 and c=1?
Assume N0 =1 and c=2?
Assume N0 =1 and c=3?
 If true for one pair of N0 and c, then there exists infinite set of
such pairs of N0 and c
Standard Analysis Techniques
 Simple statement
 Sequence of statements
 Block statement
 Selection
 Loop
 Non-recursive subprogram call
 Recursive subprogram call
Counting Primitive Operations/Simple
Statements
 Assigning an identifier to an object
 Determining the object associated with an
identifier
 Performing an arithmetic operation (for example,
adding two numbers)
 Comparing two numbers
 Accessing a single element of a Python list by index
 Calling a function (excluding operations executed
within the function)
 Returning from a function.
Execution Time of an algorithm
Execution Time of an algorithm
Execution Time of an algorithm
Execution Time of an algorithm
Performance Classification
f(n) Classification
1 Constant: run time is fixed, and does not depend upon n. Most instructions are
executed once, or only a few times, regardless of the amount of information being
processed
log n Logarithmic: when n increases, so does run time, but much slower. Common in
programs which solve large problems by transforming them into smaller problems.
n Linear: run time varies directly with n. Typically, a small amount of processing is
done on each element.
n log n When n doubles, run time slightly more than doubles. Common in programs which
break a problem down into smaller sub-problems, solves them independently, then
combines solutions
n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small
problems; typically the program processes all pairs of input (e.g. in a double nested
loop).
n3 Cubic: when n doubles, runtime increases eightfold
2n Exponential: when n doubles, run time squares. This is often the result of a natural,
“brute force” solution.
How Input Size effect?
Big O Calculation
 Simple statement Big O = O(1)
 Sequence of statements
◦ statement1 // O(n2)
◦ statement2 // O(1)
◦ statement3 // O(n)
◦ statement4 // O(n)
◦ The time complexity of the sequence will be:
O( 2) + (1) + ( ) + ( )
𝑛 𝑂 𝑂 𝑛 𝑂 𝑛
=max( ( 2) , (1) , ( ) , ( )) = ( 2)
𝑂 𝑛 𝑂 𝑂 𝑛 𝑂 𝑛 𝑂 𝑛
Big O Calculation
 Selection
Analyzing Loops-Uniform step size
 Any loop has two parts:
◦ How many iterations are performed?
◦ How many steps per iteration?
sum = 0
for j in range(N)
sum = sum +j
◦ Loop executes N times (0..N-1)
◦ O(1) steps per iteration
 Total time is N * O(1) = O(N*1) = O(N)
Analyzing Loops – Deceptive case
 What about this for loop?
sum =0
for j in range(100)
sum = sum +j
 Loop executes 100 times
 O(1) steps per iteration
 Total time is 100 * O(1) = O(100 * 1) = O(100)
= O(1)
Analyzing loops –Variable step size
Data Structure Algorithm -Algorithm Complexity
Analyzing loops –Variable step size
Analyzing Nested Loops – Independent
loops
 Treat just like a single loop and evaluate each
level of nesting as needed:
for j in range(N)
for k in range(N,-1,-1)
sum = k+j;
 Start with outer loop:
◦ How many iterations? N
◦ How much time per iteration? Need to evaluate
inner loop
 Inner loop uses O(N) time
 Total time is N * O(N) = O(N*N) = O(N2
)
Analyzing Nested Loops – dependent
loop
 What if the number of iterations of one loop
depends on the counter of the other?
for i in range( N)
for k in range(i, N)
sum += k+i;
 Analyze inner and outer loop together
Analyzing Nested Loops – dependent
loop
 Arithmetic Series
Non-recursive subprogram calls/function
calls
def foo(n):
count = 0
for j in range(n):
count=+1
return count
def bar (n):
temp = 0
i = 1
while i < n:
temp1 = foo(i)
temp = temp1+temp
i = i * 2
bar(16)
Analyzing Nested Loops – Linear
logarithmic loop
i = 1
N = 16 (check for different N)
while i < N:
j = 1
while j < N:
j*=2
i+=1

More Related Content

PPT
How to calculate complexity in Data Structure
debasisdas225831
 
PPT
Time complexity.ppt
YekoyeTigabuYeko
 
PPT
Time complexity.pptr56435 erfgegr t 45t 35
DickyNsjg1
 
PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PDF
Data Structure & Algorithms - Introduction
babuk110
 
PDF
Chapter One.pdf
abay golla
 
How to calculate complexity in Data Structure
debasisdas225831
 
Time complexity.ppt
YekoyeTigabuYeko
 
Time complexity.pptr56435 erfgegr t 45t 35
DickyNsjg1
 
how to calclute time complexity of algortihm
Sajid Marwat
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
Data Structure & Algorithms - Introduction
babuk110
 
Chapter One.pdf
abay golla
 

Similar to Data Structure Algorithm -Algorithm Complexity (20)

PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
PPT
Lec03 04-time complexity
Abbas Ali
 
PPTX
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
PPTX
algorithmanalysisinfundamentalsofdatastructure-190810085243.pptx
ShirishaBuduputi
 
PDF
Annotations.pdf
GauravKumar295392
 
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPTX
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
PPTX
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
shesnasuneer
 
PDF
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
PPT
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
PPT
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
PPTX
Unit i basic concepts of algorithms
sangeetha s
 
PPT
Chapter1.1 Introduction.ppt
Tekle12
 
PPT
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Tekle12
 
PDF
Daa notes 1
smruti sarangi
 
PPTX
02 Introduction to Data Structures & Algorithms.pptx
mettlehenry573
 
PDF
Algorithm Analysis.pdf
MemMem25
 
PDF
Design Analysis and Algorithm Module1.pdf
Shana799280
 
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
Lec03 04-time complexity
Abbas Ali
 
Algorithm for the DAA agscsnak javausmagagah
RaviPandey598038
 
algorithmanalysisinfundamentalsofdatastructure-190810085243.pptx
ShirishaBuduputi
 
Annotations.pdf
GauravKumar295392
 
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
RashidFaridChishti
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
shesnasuneer
 
Advanced Datastructures and algorithms CP4151unit1b.pdf
Sheba41
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
Lecture 1 and 2 of Data Structures & Algorithms
haseebanjum2611
 
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Unit i basic concepts of algorithms
sangeetha s
 
Chapter1.1 Introduction.ppt
Tekle12
 
Chapter1.1 Introduction to design and analysis of algorithm.ppt
Tekle12
 
Daa notes 1
smruti sarangi
 
02 Introduction to Data Structures & Algorithms.pptx
mettlehenry573
 
Algorithm Analysis.pdf
MemMem25
 
Design Analysis and Algorithm Module1.pdf
Shana799280
 
Ad

Recently uploaded (20)

PPTX
Qualification of.UV visible spectrophotometer pptx
shrutipandit17
 
PDF
Approximating manifold orbits by means of Machine Learning Techniques
Esther Barrabés Vera
 
PDF
Sujay Rao Mandavilli Multi-barreled appraoch to educational reform FINAL FINA...
Sujay Rao Mandavilli
 
PPTX
Hepatopulmonary syndrome power point presentation
raknasivar1997
 
PPTX
RED ROT DISEASE OF SUGARCANE.pptx
BikramjitDeuri
 
PPTX
fghvqwhfugqaifbiqufbiquvbfuqvfuqyvfqvfouiqvfq
PERMISONJERWIN
 
PPTX
Cell Structure and Organelles Slides PPT
JesusNeyra8
 
PPTX
Quality control test for plastic & metal.pptx
shrutipandit17
 
PPTX
Hericium erinaceus, also known as lion's mane mushroom
TinaDadkhah1
 
PDF
The Cosmic Symphony: How Photons Shape the Universe and Our Place Within It
kutatomoshi
 
PPTX
Brain_stem_Medulla oblongata_functions of pons_mid brain
muralinath2
 
PPTX
Sleep_pysilogy_types_REM_NREM_duration_Sleep center
muralinath2
 
PPTX
Modifications in RuBisCO system to enhance photosynthesis .pptx
raghumolbiotech
 
PPTX
Laboratory design and safe microbiological practices
Akanksha Divkar
 
PDF
A deep Search for Ethylene Glycol and Glycolonitrile in the V883 Ori Protopla...
Sérgio Sacani
 
PPT
Grade_9_Science_Atomic_S_t_r_u_cture.ppt
QuintReynoldDoble
 
PPTX
Internal Capsule_Divisions_fibres_lesions
muralinath2
 
PPTX
General Characters and Classification of Su class Apterygota.pptx
Dr Showkat Ahmad Wani
 
PPTX
METABOLIC_SYNDROME Dr Shadab- kgmu lucknow pptx
ShadabAlam169087
 
PPTX
INTERNATIONAL CLASSIFICATION OF DISEASES ji.pptx
46JaybhayAshwiniHari
 
Qualification of.UV visible spectrophotometer pptx
shrutipandit17
 
Approximating manifold orbits by means of Machine Learning Techniques
Esther Barrabés Vera
 
Sujay Rao Mandavilli Multi-barreled appraoch to educational reform FINAL FINA...
Sujay Rao Mandavilli
 
Hepatopulmonary syndrome power point presentation
raknasivar1997
 
RED ROT DISEASE OF SUGARCANE.pptx
BikramjitDeuri
 
fghvqwhfugqaifbiqufbiquvbfuqvfuqyvfqvfouiqvfq
PERMISONJERWIN
 
Cell Structure and Organelles Slides PPT
JesusNeyra8
 
Quality control test for plastic & metal.pptx
shrutipandit17
 
Hericium erinaceus, also known as lion's mane mushroom
TinaDadkhah1
 
The Cosmic Symphony: How Photons Shape the Universe and Our Place Within It
kutatomoshi
 
Brain_stem_Medulla oblongata_functions of pons_mid brain
muralinath2
 
Sleep_pysilogy_types_REM_NREM_duration_Sleep center
muralinath2
 
Modifications in RuBisCO system to enhance photosynthesis .pptx
raghumolbiotech
 
Laboratory design and safe microbiological practices
Akanksha Divkar
 
A deep Search for Ethylene Glycol and Glycolonitrile in the V883 Ori Protopla...
Sérgio Sacani
 
Grade_9_Science_Atomic_S_t_r_u_cture.ppt
QuintReynoldDoble
 
Internal Capsule_Divisions_fibres_lesions
muralinath2
 
General Characters and Classification of Su class Apterygota.pptx
Dr Showkat Ahmad Wani
 
METABOLIC_SYNDROME Dr Shadab- kgmu lucknow pptx
ShadabAlam169087
 
INTERNATIONAL CLASSIFICATION OF DISEASES ji.pptx
46JaybhayAshwiniHari
 
Ad

Data Structure Algorithm -Algorithm Complexity

  • 2. Data Structures  Data Structure is a way to store and organize data in a computer so that it can be used efficiently.
  • 3. Algorithm  An algorithm is a sequence of clear and precise step-by-step instructions for solving a problem in a finite amount of time.
  • 4. Good Algorithms?  Run in less time  Consume less memory But computational resources (time complexity) is usually more important
  • 5. Algorithm Analysis  It would seem that the most obvious way to measure the efficiency of an algorithm is to run it and measure how much processor time is needed  But is it correct???
  • 6. Factors  Hardware  Operating System  Compiler  Size of input  Nature of Input  Algorithm Which should be improved?
  • 7. Time and Space Complexity  Analyzing an algorithm means determining the amount of resources (such as time and space) needed to execute it.  Time Complexity ◦ The time complexity of an algorithm is basically the running time of a program as a function of the input size.  Space Complexity ◦ The space complexity of an algorithm is the amount of computer memory that is required during the program execution as a function of the input size.
  • 8. Space Complexity  Fixed part ◦ It varies from problem to problem. It includes the space needed for storing instructions, constants, variables, and other structures variables like arrays  Variable part ◦ It includes the space needed for recursion stack, and for variables that are allocated space dynamically during the runtime of a program.
  • 9. However, running time requirement is more critical than memory requirement.
  • 10. Moving Beyond Experimental Analysis Our goal is to develop an approach to analyze the efficiency of algorithms that: Allows us to evaluate the relative efficiency of any two algorithms in a way that is independent of the hardware and software environment.  Is performed by studying a high-level description of the algorithm without need for implementation.  Takes into account all possible inputs.
  • 11. Running Time of an Algorithm  Depends upon  Input Size  Nature of Input  Generally time grows with size of input, so running time of an algorithm is usually measured as function of input size.  Running time is measured in terms of number of steps/primitive operations performed  Independent from machine, OS
  • 12. Types of running time  Worst case running time ◦ This denotes the behavior of an algorithm with respect to the worst possible case of the input instance.  Average case running time ◦ It is an estimate of the running time for an average input. It specify the expected behavior of the algorithm when the input is randomly drawn from a given distribution.  Best case running time ◦ It is used to analyze an algorithm under optimal condition.  Amortized running time ◦ Amortized running time refers to the time required to perform a sequence of (related) operations averaged over all the operations performed.
  • 13. Time-Space Trade-off  If space is a big constraint then one might choose an algorithm that takes less space at the cost of more CPU time.  If time is major constraint, then one might choose a program that takes minimum time to execute at the cost of more space.
  • 14. Complexity Analysis - 1 '''Input: int A[N], array of N integers Output: Sum of all numbers in array A''‘ def Sum(intList): s=0 for i in range(len(intList)): s = s + intList[i] return s Sum([5,6,7,8])  How should we analyse this?
  • 15. Count the instructions def Sum(intList): s=0 for i in range(len(intList)): s = s + intList[i] return s Sum([5,6,7,8]) 1 2 3 4 5 6 7 1,2,7,8: Once 3,4,5,6: Once per each iteration of for loop, N iteration Total: 4N + 4 The complexity function of the algorithm is : f(N) = 4N +4 8
  • 16. Growth of function - 4n+4 Estimated running time for different values of N: N = 10 => 44 steps N = 100 => 404 steps N = 1,000 => 4004 steps N = 1,000,000 => 4,000,004 steps As N grows, the number of steps grow in linear proportion to N for this function “Sum”
  • 17. What Dominates in Previous Example? What about the +4 and 4 in 4N+4? ◦ As N gets large, the +4 becomes insignificant ◦ 4 is inaccurate, as different operations require varying amounts of time and also does not have any significant importance What is fundamental is that the time is linear in N. Asymptotic Complexity:As N gets large, concentrate on the highest order term:  Drop lower order terms such as +4  Drop the constant coefficient of the highest order term i.e. N
  • 18. Asymptotic Complexity  The 4N+4 time bound is said to "grow asymptotically" like N  This gives us an approximation of the complexity of the algorithm  Ignores lots of (machine dependent) details, concentrate on the bigger picture
  • 19. Big Oh Notation – Worst Case Analysis If f(N) and g(N) are two complexity functions, we say f(N) = O(g(N)) (read "f(N) is order g(N)", or "f(N) is big-O of g(N)") If g is an upper bound on f and if there are constants c and N0 such that for N > N0, f(N) ≤ c * g(N) for all sufficiently large N.
  • 21. Example Consider f(n)=2n2 +3 and g(n)=n2 Is f(n)=O(g(n))? i.e. Is 2n2 +3 = O(n2 )? Proof: 2n2 +3 ≤ c * n2 Assume N0 =1 and c=1? Assume N0 =1 and c=2? Assume N0 =1 and c=3?  If true for one pair of N0 and c, then there exists infinite set of such pairs of N0 and c
  • 22. Standard Analysis Techniques  Simple statement  Sequence of statements  Block statement  Selection  Loop  Non-recursive subprogram call  Recursive subprogram call
  • 23. Counting Primitive Operations/Simple Statements  Assigning an identifier to an object  Determining the object associated with an identifier  Performing an arithmetic operation (for example, adding two numbers)  Comparing two numbers  Accessing a single element of a Python list by index  Calling a function (excluding operations executed within the function)  Returning from a function.
  • 24. Execution Time of an algorithm
  • 25. Execution Time of an algorithm
  • 26. Execution Time of an algorithm
  • 27. Execution Time of an algorithm
  • 28. Performance Classification f(n) Classification 1 Constant: run time is fixed, and does not depend upon n. Most instructions are executed once, or only a few times, regardless of the amount of information being processed log n Logarithmic: when n increases, so does run time, but much slower. Common in programs which solve large problems by transforming them into smaller problems. n Linear: run time varies directly with n. Typically, a small amount of processing is done on each element. n log n When n doubles, run time slightly more than doubles. Common in programs which break a problem down into smaller sub-problems, solves them independently, then combines solutions n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small problems; typically the program processes all pairs of input (e.g. in a double nested loop). n3 Cubic: when n doubles, runtime increases eightfold 2n Exponential: when n doubles, run time squares. This is often the result of a natural, “brute force” solution.
  • 29. How Input Size effect?
  • 30. Big O Calculation  Simple statement Big O = O(1)  Sequence of statements ◦ statement1 // O(n2) ◦ statement2 // O(1) ◦ statement3 // O(n) ◦ statement4 // O(n) ◦ The time complexity of the sequence will be: O( 2) + (1) + ( ) + ( ) 𝑛 𝑂 𝑂 𝑛 𝑂 𝑛 =max( ( 2) , (1) , ( ) , ( )) = ( 2) 𝑂 𝑛 𝑂 𝑂 𝑛 𝑂 𝑛 𝑂 𝑛
  • 32. Analyzing Loops-Uniform step size  Any loop has two parts: ◦ How many iterations are performed? ◦ How many steps per iteration? sum = 0 for j in range(N) sum = sum +j ◦ Loop executes N times (0..N-1) ◦ O(1) steps per iteration  Total time is N * O(1) = O(N*1) = O(N)
  • 33. Analyzing Loops – Deceptive case  What about this for loop? sum =0 for j in range(100) sum = sum +j  Loop executes 100 times  O(1) steps per iteration  Total time is 100 * O(1) = O(100 * 1) = O(100) = O(1)
  • 37. Analyzing Nested Loops – Independent loops  Treat just like a single loop and evaluate each level of nesting as needed: for j in range(N) for k in range(N,-1,-1) sum = k+j;  Start with outer loop: ◦ How many iterations? N ◦ How much time per iteration? Need to evaluate inner loop  Inner loop uses O(N) time  Total time is N * O(N) = O(N*N) = O(N2 )
  • 38. Analyzing Nested Loops – dependent loop  What if the number of iterations of one loop depends on the counter of the other? for i in range( N) for k in range(i, N) sum += k+i;  Analyze inner and outer loop together
  • 39. Analyzing Nested Loops – dependent loop  Arithmetic Series
  • 40. Non-recursive subprogram calls/function calls def foo(n): count = 0 for j in range(n): count=+1 return count def bar (n): temp = 0 i = 1 while i < n: temp1 = foo(i) temp = temp1+temp i = i * 2 bar(16)
  • 41. Analyzing Nested Loops – Linear logarithmic loop i = 1 N = 16 (check for different N) while i < N: j = 1 while j < N: j*=2 i+=1

Editor's Notes

  • #41: Outer loop = N Inner loop = step size ? Log n N . logn