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

Similar to Data Structure Algorithm -Algorithm Complexity (20)

PPT
Basics of data structure types of data structures
kavita20193
 
PDF
Data Structure & Algorithms - Mathematical
babuk110
 
PPTX
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
PPTX
Searching Algorithms
Afaq Mansoor Khan
 
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PPT
Lec03 04-time complexity
Abbas Ali
 
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
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PDF
Algorithm Analysis.pdf
MemMem25
 
PPTX
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
PPTX
Unit ii algorithm
Tribhuvan University
 
PPTX
DAA-Unit1.pptx
NishaS88
 
PPTX
Unit i basic concepts of algorithms
sangeetha s
 
PPTX
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
2022cspaawan12556
 
PPT
Big-O notations, Algorithm and complexity analaysis
drsomya2019
 
PDF
Unit 1_final DESIGN AND ANALYSIS OF ALGORITHM.pdf
saiscount01
 
Basics of data structure types of data structures
kavita20193
 
Data Structure & Algorithms - Mathematical
babuk110
 
BCSE202Lkkljkljkbbbnbnghghjghghghghghghghgh
shivapatil54
 
Searching Algorithms
Afaq Mansoor Khan
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
Lec03 04-time complexity
Abbas Ali
 
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
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Algorithm Analysis.pdf
MemMem25
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Unit ii algorithm
Tribhuvan University
 
DAA-Unit1.pptx
NishaS88
 
Unit i basic concepts of algorithms
sangeetha s
 
DSA Complexity.pptx What is Complexity Analysis? What is the need for Compl...
2022cspaawan12556
 
Big-O notations, Algorithm and complexity analaysis
drsomya2019
 
Unit 1_final DESIGN AND ANALYSIS OF ALGORITHM.pdf
saiscount01
 

Recently uploaded (20)

PDF
Integrating Lifestyle Data into Personalized Health Solutions (www.kiu.ac.ug)
publication11
 
PPT
Experimental Design by Cary Willard v3.ppt
MohammadRezaNirooman1
 
PPTX
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
PDF
Carbonate formation and fluctuating habitability on Mars
Sérgio Sacani
 
PDF
20250603 Recycling 4.pdf . Rice flour, aluminium, hydrogen, paper, cardboard.
Sharon Liu
 
PDF
Service innovation with AI: Transformation of value proposition and market se...
Selcen Ozturkcan
 
PPTX
Cerebellum_ Parts_Structure_Function.pptx
muralinath2
 
PDF
oil and gas chemical injection system
Okeke Livinus
 
PDF
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PDF
soil and environmental microbiology.pdf
Divyaprabha67
 
PPTX
Ghent University Global Campus: Overview
Ghent University Global Campus
 
PDF
Rapid protoplanet formation in the outer Solar System recorded in a dunite fr...
Sérgio Sacani
 
DOCX
Analytical methods in CleaningValidation.docx
Markus Janssen
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
PPTX
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
PDF
Webinar: World's Smallest Pacemaker
Scintica Instrumentation
 
PDF
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
PDF
A Man of the Forest: The Contributions of Gifford Pinchot
RowanSales
 
PDF
A High-Caliber View of the Bullet Cluster through JWST Strong and Weak Lensin...
Sérgio Sacani
 
Integrating Lifestyle Data into Personalized Health Solutions (www.kiu.ac.ug)
publication11
 
Experimental Design by Cary Willard v3.ppt
MohammadRezaNirooman1
 
Phage Therapy and Bacteriophage Biology.pptx
Prachi Virat
 
Carbonate formation and fluctuating habitability on Mars
Sérgio Sacani
 
20250603 Recycling 4.pdf . Rice flour, aluminium, hydrogen, paper, cardboard.
Sharon Liu
 
Service innovation with AI: Transformation of value proposition and market se...
Selcen Ozturkcan
 
Cerebellum_ Parts_Structure_Function.pptx
muralinath2
 
oil and gas chemical injection system
Okeke Livinus
 
Adding Geochemistry To Understand Recharge Areas - Kinney County, Texas - Jim...
Texas Alliance of Groundwater Districts
 
Annual report 2024 - Inria - English version.pdf
Inria
 
soil and environmental microbiology.pdf
Divyaprabha67
 
Ghent University Global Campus: Overview
Ghent University Global Campus
 
Rapid protoplanet formation in the outer Solar System recorded in a dunite fr...
Sérgio Sacani
 
Analytical methods in CleaningValidation.docx
Markus Janssen
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
Webinar: World's Smallest Pacemaker
Scintica Instrumentation
 
The ALMA-CRISTAL survey: Gas, dust, and stars in star-forming galaxies when t...
Sérgio Sacani
 
A Man of the Forest: The Contributions of Gifford Pinchot
RowanSales
 
A High-Caliber View of the Bullet Cluster through JWST Strong and Weak Lensin...
Sérgio Sacani
 
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