SlideShare a Scribd company logo
Design and Analysis of
Algorithms
Complexity theory
Department of Computer Science
Complexity theory
❖The complexity of an algorithm is simply the
amount of work the algorithm performs to complete
its task.
❖Complexity theory is the study of the cost of
solving interesting problems. It measures the
amount of resources needed.
 Time
 Space
❖Two aspects
 Upper bounds: give a fast algorithm
 Lower bounds: no algorithm is faster.
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 2
What is Algorithm Analysis?
❖How to estimate the time required for an
algorithm
❖Techniques that drastically reduce the
running time of an algorithm
❖A mathemactical framework that more
rigorously describes the running time of an
algorithm
Monday, March 6, 2023 3
Design and Analysis of Computer Algorithm
Algorithm Analysis(1/5)
❖Measures the efficiency of an algorithm or its
implementation as a program as the input size
becomes very large
❖We evaluate a new algorithm by comparing its
performance with that of previous approaches
 Comparisons are asymptotic analyses of classes of
algorithms
❖We usually analyze the time required for an
algorithm and the space required for a
datastructure
Monday, March 6, 2023 4
Design and Analysis of Computer Algorithm
Algorithm Analysis (2/5)
❖Many criteria affect the running time of an
algorithm, including
 speed of CPU, bus and peripheral hardware
 design think time, programming time and
debugging time
 language used and coding efficiency of the
programmer
 quality of input (good, bad or average)
Monday, March 6, 2023 5
Design and Analysis of Computer Algorithm
Algorithm Analysis (3/5)
❖Programs derived from two algorithms for solving
the same problem should both be
 Machine independent
 Language independent
 Environment independent (load on the
system,...)
 Amenable to mathematical study
 Realistic
Monday, March 6, 2023 6
Design and Analysis of Computer Algorithm
Algorithm Analysis (4/5)
❖We estimate the algorithm's performance based on
the number of key and basic operations it requires
to process an input of a given size
❖For a given input size n we express the time T to
run the algorithm as a function T(n)
❖Concept of growth rate allows us to compare
running time of two algorithms without writing two
programs and running them on the same computer
Monday, March 6, 2023 7
Design and Analysis of Computer Algorithm
Algorithm Analysis (5/5)
❖Formally, let T(A,L,M) be total run time for
algorithm A if it were implemented with language L
on machine M. Then the complexity class of
algorithm A is
O(T(A,L1,M1) U O(T(A,L2,M2)) U O(T(A,L3,M3)) U ...
❖Call the complexity class V; then the complexity of
A is said to be f if V = O(f)
❖The class of algorithms to which A belongs is said
to be of at most linear/quadratic/ etc.
Monday, March 6, 2023 8
Design and Analysis of Computer Algorithm
Performance Analysis
❖Predicting the resources which are required by
an algorithm to perform its task.
❖An algorithm is said to be efficient and fast, if it
takes less time to execute and consumes less
memory space. The performance of an
algorithm is measured on the basis of following
properties :
 Space Complexity
 Time Complexity
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 9
Input Size
❖Time and space complexity
 This is generally a function of the input size
▪ E.g., sorting, multiplication
 How we characterize input size depends:
▪ Sorting: number of input items
▪ Multiplication: total number of bits
▪ Graph algorithms: number of nodes & edges
▪ Etc
Monday, March 6, 2023 10
Design and Analysis of Computer Algorithm
11
Running Time
❖ Most algorithms transform input
objects into output objects.
❖ The running time of an
algorithm typically grows with
the input size.
❖ Average case time is often
difficult to determine.
❖ We focus on the worst case
running time.
 Easier to analyze
 Crucial to applications such as
games, finance and robotics
0
20
40
60
80
100
120
Running
Time 1000 2000 3000 4000
Input Size
best case
average case
worst case
Design and Analysis of Computer Algorithm Monday, March 6, 2023
Running Time
❖Number of primitive steps that are executed
 Except for time of executing a function call, most
statements roughly require the same amount of
time
▪ y = m * x + b
▪ c = 5 / 9 * (t - 32 )
▪ z = f(x) + g(y)
❖We can be more exact if need be
Monday, March 6, 2023 12
Design and Analysis of Computer Algorithm
Space Complexity
❖ Its the amount of memory space required by the algorithm,
during the course of its execution.
❖ It must be taken seriously for multi-user systems and in
situations where limited memory is available.
❖ An algorithm generally requires space for following
components :
❖ Instruction Space: Its the space required to store the executable version
of the program. This space is fixed, but varies depending upon the
number of lines of code in the program.
❖ Data Space: Its the space required to store all the constants and
variables value.
❖ Environment Space : Its the space required to store the environment
information needed to resume the suspended function.
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 13
Constant Space Complexity
int square (int a)
{
return a*a;
}
❖If any algorithm requires a fixed amount of
space for all input values then that space
complexity is said to be Constant Space
Complexity
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 14
Linear Space Complexity
int sum (int A[ ], int n)
{
int sum = 0, i;
for (i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
❖If the amount of space required by an algorithm is
increased with the increase of input value, then that
space complexity is said to be Linear Space
Complexity
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 15
Time Complexity
❖The time complexity of an algorithm is the total
amount of time required by an algorithm to
complete its execution.
❖If any program requires fixed amount of time for
all input values then its time complexity is said
to be Constant Time Complexity
int sum (int a, int b)
{
return a+b;
}
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 16
Linear Time Complexity
❖If the amount of time required by an algorithm is
increased with the increase of input value then
that time complexity is said to be Linear Time
Complexity
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 17
Asymptotic Performance
❖How does the algorithm behave as the
problem size gets very large?
 Running time
 Memory/storage requirements
 Bandwidth/power requirements/logic gates/etc.
Monday, March 6, 2023 18
Design and Analysis of Computer Algorithm
Order of Growth
❖This refers to the rate of growth of the running time
of an algorithm
❖Only the leading term of a formula that matters
since the lower-order terms are relatively
insignificant for large n
❖E.g. given an2+bn+c for some constants a, b and
c, the leading term is an2
❖We also ignore the leading term’s constant
coefficient since it has less significant than the rate
of growth. Hence we write Ɵn2(“theta of n-
squared”)
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 19
Rate of Growth
❖Consider the example of buying elephants and
goldfish:
Cost: cost_of_elephants + cost_of_goldfish
Cost ~ cost_of_elephants (approximation)
❖The low order terms in a function are relatively
insignificant for large n
n4 + 100n2 + 10n + 50 ~ n4
i.e., we say that n4 + 100n2 + 10n + 50 and n4
have the same rate of growth
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 20
Function of Growth rate
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 21
Asymptotic notation
❖Asymptotic efficiency of algorithms is when we
look at input sizes large enough to make only
the order of growth of the running time relevant
❖Studying how the running time of an algorithm
increases with the size of input in the limit as
the size of input increases without bound
❖Asymptotically more efficient algorithm is best
for all but very small inputs
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 22
Big O Notation
❖Big O notation: asymptotic “less than”:
❖f(n)  O(g(n)) if and only if  c > 0, and n0 , so that
0 ≤ f(n) ≤ cg(n) for all n ≥ n0
❖f(n) = O(g(n)) means f(n)  O(g(n)) (i.e., at most)
 We say that g(n) is an asymptotic upper bound
for f(n).
❖It also means:
❖E.g. f(n) = O(n2) if there exists c, n0 > 0 such that
f(n) ≤ cn2, for all n ≥ n0.
lim ≤ c
n→
f(n)
g(n)
Monday, March 6, 2023 23
Design and Analysis of Computer Algorithm
Big-Oh Rules
❖If f(n) is a polynomial of degree d, then f(n) is O(nd),
i.e.,
1. Drop lower-order terms
2. Drop constant factors
❖Use the smallest possible class of functions
 Say “2n is O(n)” instead of “2n is O(n2)”
❖Use the simplest expression of the class
 Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 24
When to use – big Oh
❖ Big “oh” - asymptotic upper bound on the growth of
an algorithm
❖ When do we use Big Oh?
1. Theory of NP-completeness
2. To provide information on the maximum number of
operations that an algorithm performs
 Insertion sort is O(n2) in the worst case
▪ This means that in the worst case it performs at
most cn2 operations
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 25
Omega  Notation
❖  notation: asymptotic “greater than”:
❖f(n)   (g(n)) if and only if  c > 0, and n0 ,
so that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0
❖f(n) =  (g(n)) means f(n)   (g(n)) (i.e, at
least)
 We say g(n) is an asymptotic lower bound of
f(n).
❖It also means: lim ≥ c
n→
f(n)
g(n)
Monday, March 6, 2023 26
Design and Analysis of Computer Algorithm
When to use – Omega
Omega - asymptotic lower bound on the growth of an
algorithm or a problem*
When do we use Omega?
1. To provide information on the minimum number of
operations that an algorithm performs
 Insertion sort is (n) in the best case
▪ This means that in the best case its instruction
count is at least cn,
 It is (n2) in the worst case
▪ This means that in the worst case its instruction
count is at least cn2
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 27
When to use – Omega cont.
2.To provide information on a class of algorithms
that solve a problem
 Sort algorithms based on comparison of keys are
(nlgn) in the worst case
▪ This means that all sort algorithms based only on
comparison of keys have to do at least cnlgn
operations
 Any algorithm based only on comparison of keys
to find the maximum of n elements is (n) in
every case
▪ This means that all algorithms based only on
comparison of keys to find maximum have to do at
least cn operations
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 28
Theta  Notation
❖Θ notation: asymptotic “equality”:
❖Combine lower and upper bound
❖Means tight: of the same order
❖𝑓 𝑛 ∈ Θ 𝑔 𝑛 if and only if ∃ 𝑐1, 𝑐2 > 0, and 𝑛0 ,
such that 𝑐1𝑔 𝑛 ≤ 𝑓 𝑛 ≤ 𝑐2𝑔 𝑛 for any 𝑛 ≥ 𝑛0
❖𝑓 𝑛 = 𝛩 𝑔 𝑛 means 𝑓 𝑛 ∈ 𝛩 𝑔 𝑛
 We say g(n) is an asymptotically tight bound for
f(n).
❖It also means:
 𝑐1 ≤ lim
𝑛→∞
𝑓(𝑛)
𝑔(𝑛)
≤ 𝑐2
Monday, March 6, 2023 29
Design and Analysis of Computer Algorithm
When to use - Theta
❖Theta - asymptotic tight bound on the growth rate
 Insertion sort is (n2) in the worst and average
cases
▪ The means that in the worst case and average
cases insertion sort performs cn2 operations
 Binary search is (lg n) in the worst and average
cases
▪ The means that in the worst case and average
cases binary search performs clgn operations
❖Note: We want to classify an algorithm using Theta.
❖Little “oh” - used to denote an upper bound that is not
asymptotically tight. n is in o(n3). n is not in o(n)
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 30
Illustration
𝑓 𝑛 = 𝑂(𝑔 𝑛 ) 𝑓 𝑛 = Ω(𝑔 𝑛 ) 𝑓 𝑛 = Θ(𝑔 𝑛 )
Monday, March 6, 2023 31
Design and Analysis of Computer Algorithm
Does 5n+2 O(n)?
Proof: From the definition of Big Oh, there must exist c>0
and integer N>0 such that 0  5n+2cn for all nN.
Dividing both sides of the inequality by n>0 we get:
0  5+2/nc.
2/n  2, 2/n>0 becomes smaller when n increases
There are many choices here for c and N.
If we choose N=1 then c  5+2/1= 7.
If we choose c=6, then 0  5+2/n6. So N  2.
In either case (we only need one!) we have a c>o and N>0
such that 0  5n+2cn for all n  N. So the definition is
satisfied and 5n+2 O(n)
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 32
Is 5n-20  (n)?
Proof: From the definition of Omega, there must exist c>0 and
integer N>0 such that 0  cn  5n-20 for all nN
Dividing the inequality by n>0 we get: 0  c  5-20/n for all nN.
20/n  20, and 20/n becomes smaller as n grows.
There are many choices here for c and N.
Since c > 0, 5 – 20/n >0 and N >4
For example, if we choose c=4, then 5 – 20/n  4 and N 
20
In this case we have a c>o and N>0 such that 0  cn  5n-20
for all n  N. So the definition is satisfied and 5n-20   (n)
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 33
Is 1/2n2-3n O(n2)?
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 34
.
and
So
.
all
for
1/4.
c
Choose
finite
for
0
Since
6.
Since
.
that
such
and
exist
must
There
12
4
1
12
3
2
1
4
1
.
2
/
1
,
3
and
3
2
1
0
0
3
2
1
0
get
we
0
2
by
Dividing
all
for
3
2
2
1
2
0
0
0
=
=

−

=



−


−




−




N
/
c
n
n
c
n
/n
N
,
c
n
c
n
N
n
n
n
cn
N
c
N
Is 1/2n2-3n (n2)?
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 35
Kinds of Analysis
❖Worst case (usually)
 Provides an upper bound on running time
 An absolute guarantee
❖Average case (sometimes)
 Provides the expected running time
 Very useful, but treat with care: what is “average”?
▪ Random (equally likely) inputs
▪ Real-life inputs
❖Best-case: (rarely)
 Cheat with a slow algorithm that works fast on
some input.
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 36
Empirical Analysis of Algorithms
❖In practice, we will often need to resort to empirical
rather than theoretical analysis to compare algorithms.
 We may want to know something about performance of
the algorithm “on average” for real instances.
 Our model of computation may not capture important
effects of the hardware architecture that arise in
practice.
 There may be implementation details that affect
constant factors and are not captured by asymptotic
analysis.
❖For this purpose, we need a methodology for comparing
algorithms based on real-world performance.
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 37
Issues to Consider
❖Empirical analysis introduces many more factors
that need to be controlled in some way.
 Test platform (hardware, language, compiler)
 Measures of performance (what to compare)
 Benchmark test set (what instances to test on)
 Algorithmic parameters
 Implementation details
❖It is much less obvious how to perform a rigorous
analysis in the presence of so many factors.
❖Practical considerations prevent complete testing.
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 38
Measures of Performance
❖For the time being, we focus on sequential
algorithms.
❖What is an appropriate measure of performance?
❖What is the goal?
 Compare two algorithms.
 Improve the implementation of a single algorithm.
❖Possible measures
 Empirical running time (CPU time, wallclock)
 Representative operation counts
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 39
Measuring Time
❖There are three relevant measures of time taken by a
process.
 User time measures the amount of time (number of
cycles taken by a process in “user mode.”
 System time the time taken by the kernel executing on
behalf of the process.
 Wallclock time is the total “real” time taken to execute
the process.
❖Generally speaking, user time is the most relevant,
though it ignores some important operations (I/O, etc.).
❖Wallclock time should be used cautiously/sparingly, but
may be necessary for assessment of parallel codes,
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 40
Representative Operation Counts
❖In some cases, we may want to count operations, rather
than time
 Identify bottlenecks
 Counterpart to theoretical analysis
❖What operations should we count?
 Profilers can count function calls and executions of
individual lines of code to identify bottlenecks.
 We may know a priori what operations we want to
measure
(example: comparisons and swaps in sorting).
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 41
Test Sets
❖It is crucial to choose your test set well.
❖The instances must be chosen carefully in order to allow
proper conclusions to be drawn.
❖We must pay close attention to
 their size,
 inherent difficulty,
 and other important structural properties.
❖This is especially important if we are trying to distinguish
among multiple algorithms.
❖Example: Sorting
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 42
Comparing Algorithms
❖Given a performance measure and a test set, the
question still arises how to decide which algorithm is
“better.”
❖We can do the comparison using some sort of summary
statistic.
 Arithmetic mean
 Geometric mean
 Variance
❖ Performance profiles allow comparison of algorithms
across an entire test set without loss of information.
❖ They provide a visual summary of how algorithms
compare on a performance measure across a test set
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 43
Accounting for Stochasticity
❖In empirical analysis, we must take account of the fact
that running times are inherently stochastic.
❖If we are measuring wallclock time, this may vary
substantially for seemingly identical executions.
❖In the case of parallel processing, stochasticity may also
arise due to asynchronism (order of operations).
❖In such case, multiple identical runs may be used to
estimate the affect of this randomness.
❖If necessary, statistical analysis may be used to analyze
the results, but this is beyond the scope of this course.
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 44
Empirical versus Theoretical Analysis
❖For sequential algorithms, asymptotic analysis is often
good enough for choosing between algorithms.
❖It is less ideal with respect to tuning of implementation
details.
❖For parallel algorithms, asymptotic analysis is far more
problematic.
❖The details not captured by the model of computation
can matter much more.
❖There is an additional dimension on which we must
compare algorithms: scalability
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 45

More Related Content

Similar to CCS 3102 Lecture 3_ Complexity theory.pdf

Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...
bekidea
 
Chapter two
Chapter twoChapter two
Chapter two
mihiretu kassaye
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
Afaq Mansoor Khan
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
AntareepMajumder
 
Cs 331 Data Structures
Cs 331 Data StructuresCs 331 Data Structures
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
Mvenkatarao
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
Abhimanyu Mishra
 
DATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESDATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTES
Aniruddha Paul
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
Arumugam90
 
complexity.pptx
complexity.pptxcomplexity.pptx
complexity.pptx
Dr.Shweta
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Shaista Qadir
 
Measuring algorithm performance
Measuring algorithm performanceMeasuring algorithm performance
Measuring algorithm performance
HabitamuAsimare
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
Tanya Makkar
 
Aad introduction
Aad introductionAad introduction
Aad introduction
Mr SMAK
 
Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01
Prashanth Shivakumar
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
Trupti Agrawal
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
pasinduneshan
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
NishaS88
 
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
MAHERMOHAMED27
 

Similar to CCS 3102 Lecture 3_ Complexity theory.pdf (20)

Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...Data Structure and Algorithm chapter two, This material is for Data Structure...
Data Structure and Algorithm chapter two, This material is for Data Structure...
 
Chapter two
Chapter twoChapter two
Chapter two
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_25-07-2022_Fu...
 
Cs 331 Data Structures
Cs 331 Data StructuresCs 331 Data Structures
Cs 331 Data Structures
 
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
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
DATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTESDATA STRUCTURE AND ALGORITHM FULL NOTES
DATA STRUCTURE AND ALGORITHM FULL NOTES
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
complexity.pptx
complexity.pptxcomplexity.pptx
complexity.pptx
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
Measuring algorithm performance
Measuring algorithm performanceMeasuring algorithm performance
Measuring algorithm performance
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 
Aad introduction
Aad introductionAad introduction
Aad introduction
 
Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
 
DAA-Unit1.pptx
DAA-Unit1.pptxDAA-Unit1.pptx
DAA-Unit1.pptx
 
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
 

Recently uploaded

Eukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptxEukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptx
RitabrataSarkar3
 
Deep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless ReproducibilityDeep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless Reproducibility
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf
by6843629
 
What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.
moosaasad1975
 
Nucleophilic Addition of carbonyl compounds.pptx
Nucleophilic Addition of carbonyl  compounds.pptxNucleophilic Addition of carbonyl  compounds.pptx
Nucleophilic Addition of carbonyl compounds.pptx
SSR02
 
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
Abdul Wali Khan University Mardan,kP,Pakistan
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills MN
 
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdfTopic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
TinyAnderson
 
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero WaterSharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Texas Alliance of Groundwater Districts
 
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
Sérgio Sacani
 
Shallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptxShallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptx
Gokturk Mehmet Dilci
 
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptxThe use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
MAGOTI ERNEST
 
Medical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptxMedical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptx
terusbelajar5
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
kejapriya1
 
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
David Osipyan
 
Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
University of Hertfordshire
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
pablovgd
 
Cytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptxCytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptx
Hitesh Sikarwar
 
Equivariant neural networks and representation theory
Equivariant neural networks and representation theoryEquivariant neural networks and representation theory
Equivariant neural networks and representation theory
Daniel Tubbenhauer
 
Phenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvementPhenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvement
IshaGoswami9
 

Recently uploaded (20)

Eukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptxEukaryotic Transcription Presentation.pptx
Eukaryotic Transcription Presentation.pptx
 
Deep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless ReproducibilityDeep Software Variability and Frictionless Reproducibility
Deep Software Variability and Frictionless Reproducibility
 
8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf8.Isolation of pure cultures and preservation of cultures.pdf
8.Isolation of pure cultures and preservation of cultures.pdf
 
What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.What is greenhouse gasses and how many gasses are there to affect the Earth.
What is greenhouse gasses and how many gasses are there to affect the Earth.
 
Nucleophilic Addition of carbonyl compounds.pptx
Nucleophilic Addition of carbonyl  compounds.pptxNucleophilic Addition of carbonyl  compounds.pptx
Nucleophilic Addition of carbonyl compounds.pptx
 
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...THEMATIC  APPERCEPTION  TEST(TAT) cognitive abilities, creativity, and critic...
THEMATIC APPERCEPTION TEST(TAT) cognitive abilities, creativity, and critic...
 
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
Travis Hills' Endeavors in Minnesota: Fostering Environmental and Economic Pr...
 
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdfTopic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
Topic: SICKLE CELL DISEASE IN CHILDREN-3.pdf
 
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero WaterSharlene Leurig - Enabling Onsite Water Use with Net Zero Water
Sharlene Leurig - Enabling Onsite Water Use with Net Zero Water
 
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
EWOCS-I: The catalog of X-ray sources in Westerlund 1 from the Extended Weste...
 
Shallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptxShallowest Oil Discovery of Turkiye.pptx
Shallowest Oil Discovery of Turkiye.pptx
 
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptxThe use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
The use of Nauplii and metanauplii artemia in aquaculture (brine shrimp).pptx
 
Medical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptxMedical Orthopedic PowerPoint Templates.pptx
Medical Orthopedic PowerPoint Templates.pptx
 
bordetella pertussis.................................ppt
bordetella pertussis.................................pptbordetella pertussis.................................ppt
bordetella pertussis.................................ppt
 
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
3D Hybrid PIC simulation of the plasma expansion (ISSS-14)
 
Applied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdfApplied Science: Thermodynamics, Laws & Methodology.pdf
Applied Science: Thermodynamics, Laws & Methodology.pdf
 
NuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyerNuGOweek 2024 Ghent programme overview flyer
NuGOweek 2024 Ghent programme overview flyer
 
Cytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptxCytokines and their role in immune regulation.pptx
Cytokines and their role in immune regulation.pptx
 
Equivariant neural networks and representation theory
Equivariant neural networks and representation theoryEquivariant neural networks and representation theory
Equivariant neural networks and representation theory
 
Phenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvementPhenomics assisted breeding in crop improvement
Phenomics assisted breeding in crop improvement
 

CCS 3102 Lecture 3_ Complexity theory.pdf

  • 1. Design and Analysis of Algorithms Complexity theory Department of Computer Science
  • 2. Complexity theory ❖The complexity of an algorithm is simply the amount of work the algorithm performs to complete its task. ❖Complexity theory is the study of the cost of solving interesting problems. It measures the amount of resources needed.  Time  Space ❖Two aspects  Upper bounds: give a fast algorithm  Lower bounds: no algorithm is faster. Monday, March 6, 2023 Design and Analysis of Computer Algorithm 2
  • 3. What is Algorithm Analysis? ❖How to estimate the time required for an algorithm ❖Techniques that drastically reduce the running time of an algorithm ❖A mathemactical framework that more rigorously describes the running time of an algorithm Monday, March 6, 2023 3 Design and Analysis of Computer Algorithm
  • 4. Algorithm Analysis(1/5) ❖Measures the efficiency of an algorithm or its implementation as a program as the input size becomes very large ❖We evaluate a new algorithm by comparing its performance with that of previous approaches  Comparisons are asymptotic analyses of classes of algorithms ❖We usually analyze the time required for an algorithm and the space required for a datastructure Monday, March 6, 2023 4 Design and Analysis of Computer Algorithm
  • 5. Algorithm Analysis (2/5) ❖Many criteria affect the running time of an algorithm, including  speed of CPU, bus and peripheral hardware  design think time, programming time and debugging time  language used and coding efficiency of the programmer  quality of input (good, bad or average) Monday, March 6, 2023 5 Design and Analysis of Computer Algorithm
  • 6. Algorithm Analysis (3/5) ❖Programs derived from two algorithms for solving the same problem should both be  Machine independent  Language independent  Environment independent (load on the system,...)  Amenable to mathematical study  Realistic Monday, March 6, 2023 6 Design and Analysis of Computer Algorithm
  • 7. Algorithm Analysis (4/5) ❖We estimate the algorithm's performance based on the number of key and basic operations it requires to process an input of a given size ❖For a given input size n we express the time T to run the algorithm as a function T(n) ❖Concept of growth rate allows us to compare running time of two algorithms without writing two programs and running them on the same computer Monday, March 6, 2023 7 Design and Analysis of Computer Algorithm
  • 8. Algorithm Analysis (5/5) ❖Formally, let T(A,L,M) be total run time for algorithm A if it were implemented with language L on machine M. Then the complexity class of algorithm A is O(T(A,L1,M1) U O(T(A,L2,M2)) U O(T(A,L3,M3)) U ... ❖Call the complexity class V; then the complexity of A is said to be f if V = O(f) ❖The class of algorithms to which A belongs is said to be of at most linear/quadratic/ etc. Monday, March 6, 2023 8 Design and Analysis of Computer Algorithm
  • 9. Performance Analysis ❖Predicting the resources which are required by an algorithm to perform its task. ❖An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less memory space. The performance of an algorithm is measured on the basis of following properties :  Space Complexity  Time Complexity Monday, March 6, 2023 Design and Analysis of Computer Algorithm 9
  • 10. Input Size ❖Time and space complexity  This is generally a function of the input size ▪ E.g., sorting, multiplication  How we characterize input size depends: ▪ Sorting: number of input items ▪ Multiplication: total number of bits ▪ Graph algorithms: number of nodes & edges ▪ Etc Monday, March 6, 2023 10 Design and Analysis of Computer Algorithm
  • 11. 11 Running Time ❖ Most algorithms transform input objects into output objects. ❖ The running time of an algorithm typically grows with the input size. ❖ Average case time is often difficult to determine. ❖ We focus on the worst case running time.  Easier to analyze  Crucial to applications such as games, finance and robotics 0 20 40 60 80 100 120 Running Time 1000 2000 3000 4000 Input Size best case average case worst case Design and Analysis of Computer Algorithm Monday, March 6, 2023
  • 12. Running Time ❖Number of primitive steps that are executed  Except for time of executing a function call, most statements roughly require the same amount of time ▪ y = m * x + b ▪ c = 5 / 9 * (t - 32 ) ▪ z = f(x) + g(y) ❖We can be more exact if need be Monday, March 6, 2023 12 Design and Analysis of Computer Algorithm
  • 13. Space Complexity ❖ Its the amount of memory space required by the algorithm, during the course of its execution. ❖ It must be taken seriously for multi-user systems and in situations where limited memory is available. ❖ An algorithm generally requires space for following components : ❖ Instruction Space: Its the space required to store the executable version of the program. This space is fixed, but varies depending upon the number of lines of code in the program. ❖ Data Space: Its the space required to store all the constants and variables value. ❖ Environment Space : Its the space required to store the environment information needed to resume the suspended function. Monday, March 6, 2023 Design and Analysis of Computer Algorithm 13
  • 14. Constant Space Complexity int square (int a) { return a*a; } ❖If any algorithm requires a fixed amount of space for all input values then that space complexity is said to be Constant Space Complexity Monday, March 6, 2023 Design and Analysis of Computer Algorithm 14
  • 15. Linear Space Complexity int sum (int A[ ], int n) { int sum = 0, i; for (i = 0; i < n; i++) sum = sum + A[i]; return sum; } ❖If the amount of space required by an algorithm is increased with the increase of input value, then that space complexity is said to be Linear Space Complexity Monday, March 6, 2023 Design and Analysis of Computer Algorithm 15
  • 16. Time Complexity ❖The time complexity of an algorithm is the total amount of time required by an algorithm to complete its execution. ❖If any program requires fixed amount of time for all input values then its time complexity is said to be Constant Time Complexity int sum (int a, int b) { return a+b; } Monday, March 6, 2023 Design and Analysis of Computer Algorithm 16
  • 17. Linear Time Complexity ❖If the amount of time required by an algorithm is increased with the increase of input value then that time complexity is said to be Linear Time Complexity Monday, March 6, 2023 Design and Analysis of Computer Algorithm 17
  • 18. Asymptotic Performance ❖How does the algorithm behave as the problem size gets very large?  Running time  Memory/storage requirements  Bandwidth/power requirements/logic gates/etc. Monday, March 6, 2023 18 Design and Analysis of Computer Algorithm
  • 19. Order of Growth ❖This refers to the rate of growth of the running time of an algorithm ❖Only the leading term of a formula that matters since the lower-order terms are relatively insignificant for large n ❖E.g. given an2+bn+c for some constants a, b and c, the leading term is an2 ❖We also ignore the leading term’s constant coefficient since it has less significant than the rate of growth. Hence we write Ɵn2(“theta of n- squared”) Monday, March 6, 2023 Design and Analysis of Computer Algorithm 19
  • 20. Rate of Growth ❖Consider the example of buying elephants and goldfish: Cost: cost_of_elephants + cost_of_goldfish Cost ~ cost_of_elephants (approximation) ❖The low order terms in a function are relatively insignificant for large n n4 + 100n2 + 10n + 50 ~ n4 i.e., we say that n4 + 100n2 + 10n + 50 and n4 have the same rate of growth Monday, March 6, 2023 Design and Analysis of Computer Algorithm 20
  • 21. Function of Growth rate Monday, March 6, 2023 Design and Analysis of Computer Algorithm 21
  • 22. Asymptotic notation ❖Asymptotic efficiency of algorithms is when we look at input sizes large enough to make only the order of growth of the running time relevant ❖Studying how the running time of an algorithm increases with the size of input in the limit as the size of input increases without bound ❖Asymptotically more efficient algorithm is best for all but very small inputs Monday, March 6, 2023 Design and Analysis of Computer Algorithm 22
  • 23. Big O Notation ❖Big O notation: asymptotic “less than”: ❖f(n)  O(g(n)) if and only if  c > 0, and n0 , so that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 ❖f(n) = O(g(n)) means f(n)  O(g(n)) (i.e., at most)  We say that g(n) is an asymptotic upper bound for f(n). ❖It also means: ❖E.g. f(n) = O(n2) if there exists c, n0 > 0 such that f(n) ≤ cn2, for all n ≥ n0. lim ≤ c n→ f(n) g(n) Monday, March 6, 2023 23 Design and Analysis of Computer Algorithm
  • 24. Big-Oh Rules ❖If f(n) is a polynomial of degree d, then f(n) is O(nd), i.e., 1. Drop lower-order terms 2. Drop constant factors ❖Use the smallest possible class of functions  Say “2n is O(n)” instead of “2n is O(n2)” ❖Use the simplest expression of the class  Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)” Monday, March 6, 2023 Design and Analysis of Computer Algorithm 24
  • 25. When to use – big Oh ❖ Big “oh” - asymptotic upper bound on the growth of an algorithm ❖ When do we use Big Oh? 1. Theory of NP-completeness 2. To provide information on the maximum number of operations that an algorithm performs  Insertion sort is O(n2) in the worst case ▪ This means that in the worst case it performs at most cn2 operations Monday, March 6, 2023 Design and Analysis of Computer Algorithm 25
  • 26. Omega  Notation ❖  notation: asymptotic “greater than”: ❖f(n)   (g(n)) if and only if  c > 0, and n0 , so that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 ❖f(n) =  (g(n)) means f(n)   (g(n)) (i.e, at least)  We say g(n) is an asymptotic lower bound of f(n). ❖It also means: lim ≥ c n→ f(n) g(n) Monday, March 6, 2023 26 Design and Analysis of Computer Algorithm
  • 27. When to use – Omega Omega - asymptotic lower bound on the growth of an algorithm or a problem* When do we use Omega? 1. To provide information on the minimum number of operations that an algorithm performs  Insertion sort is (n) in the best case ▪ This means that in the best case its instruction count is at least cn,  It is (n2) in the worst case ▪ This means that in the worst case its instruction count is at least cn2 Monday, March 6, 2023 Design and Analysis of Computer Algorithm 27
  • 28. When to use – Omega cont. 2.To provide information on a class of algorithms that solve a problem  Sort algorithms based on comparison of keys are (nlgn) in the worst case ▪ This means that all sort algorithms based only on comparison of keys have to do at least cnlgn operations  Any algorithm based only on comparison of keys to find the maximum of n elements is (n) in every case ▪ This means that all algorithms based only on comparison of keys to find maximum have to do at least cn operations Monday, March 6, 2023 Design and Analysis of Computer Algorithm 28
  • 29. Theta  Notation ❖Θ notation: asymptotic “equality”: ❖Combine lower and upper bound ❖Means tight: of the same order ❖𝑓 𝑛 ∈ Θ 𝑔 𝑛 if and only if ∃ 𝑐1, 𝑐2 > 0, and 𝑛0 , such that 𝑐1𝑔 𝑛 ≤ 𝑓 𝑛 ≤ 𝑐2𝑔 𝑛 for any 𝑛 ≥ 𝑛0 ❖𝑓 𝑛 = 𝛩 𝑔 𝑛 means 𝑓 𝑛 ∈ 𝛩 𝑔 𝑛  We say g(n) is an asymptotically tight bound for f(n). ❖It also means:  𝑐1 ≤ lim 𝑛→∞ 𝑓(𝑛) 𝑔(𝑛) ≤ 𝑐2 Monday, March 6, 2023 29 Design and Analysis of Computer Algorithm
  • 30. When to use - Theta ❖Theta - asymptotic tight bound on the growth rate  Insertion sort is (n2) in the worst and average cases ▪ The means that in the worst case and average cases insertion sort performs cn2 operations  Binary search is (lg n) in the worst and average cases ▪ The means that in the worst case and average cases binary search performs clgn operations ❖Note: We want to classify an algorithm using Theta. ❖Little “oh” - used to denote an upper bound that is not asymptotically tight. n is in o(n3). n is not in o(n) Monday, March 6, 2023 Design and Analysis of Computer Algorithm 30
  • 31. Illustration 𝑓 𝑛 = 𝑂(𝑔 𝑛 ) 𝑓 𝑛 = Ω(𝑔 𝑛 ) 𝑓 𝑛 = Θ(𝑔 𝑛 ) Monday, March 6, 2023 31 Design and Analysis of Computer Algorithm
  • 32. Does 5n+2 O(n)? Proof: From the definition of Big Oh, there must exist c>0 and integer N>0 such that 0  5n+2cn for all nN. Dividing both sides of the inequality by n>0 we get: 0  5+2/nc. 2/n  2, 2/n>0 becomes smaller when n increases There are many choices here for c and N. If we choose N=1 then c  5+2/1= 7. If we choose c=6, then 0  5+2/n6. So N  2. In either case (we only need one!) we have a c>o and N>0 such that 0  5n+2cn for all n  N. So the definition is satisfied and 5n+2 O(n) Monday, March 6, 2023 Design and Analysis of Computer Algorithm 32
  • 33. Is 5n-20  (n)? Proof: From the definition of Omega, there must exist c>0 and integer N>0 such that 0  cn  5n-20 for all nN Dividing the inequality by n>0 we get: 0  c  5-20/n for all nN. 20/n  20, and 20/n becomes smaller as n grows. There are many choices here for c and N. Since c > 0, 5 – 20/n >0 and N >4 For example, if we choose c=4, then 5 – 20/n  4 and N  20 In this case we have a c>o and N>0 such that 0  cn  5n-20 for all n  N. So the definition is satisfied and 5n-20   (n) Monday, March 6, 2023 Design and Analysis of Computer Algorithm 33
  • 34. Is 1/2n2-3n O(n2)? Monday, March 6, 2023 Design and Analysis of Computer Algorithm 34
  • 36. Kinds of Analysis ❖Worst case (usually)  Provides an upper bound on running time  An absolute guarantee ❖Average case (sometimes)  Provides the expected running time  Very useful, but treat with care: what is “average”? ▪ Random (equally likely) inputs ▪ Real-life inputs ❖Best-case: (rarely)  Cheat with a slow algorithm that works fast on some input. Monday, March 6, 2023 Design and Analysis of Computer Algorithm 36
  • 37. Empirical Analysis of Algorithms ❖In practice, we will often need to resort to empirical rather than theoretical analysis to compare algorithms.  We may want to know something about performance of the algorithm “on average” for real instances.  Our model of computation may not capture important effects of the hardware architecture that arise in practice.  There may be implementation details that affect constant factors and are not captured by asymptotic analysis. ❖For this purpose, we need a methodology for comparing algorithms based on real-world performance. Monday, March 6, 2023 Design and Analysis of Computer Algorithm 37
  • 38. Issues to Consider ❖Empirical analysis introduces many more factors that need to be controlled in some way.  Test platform (hardware, language, compiler)  Measures of performance (what to compare)  Benchmark test set (what instances to test on)  Algorithmic parameters  Implementation details ❖It is much less obvious how to perform a rigorous analysis in the presence of so many factors. ❖Practical considerations prevent complete testing. Monday, March 6, 2023 Design and Analysis of Computer Algorithm 38
  • 39. Measures of Performance ❖For the time being, we focus on sequential algorithms. ❖What is an appropriate measure of performance? ❖What is the goal?  Compare two algorithms.  Improve the implementation of a single algorithm. ❖Possible measures  Empirical running time (CPU time, wallclock)  Representative operation counts Monday, March 6, 2023 Design and Analysis of Computer Algorithm 39
  • 40. Measuring Time ❖There are three relevant measures of time taken by a process.  User time measures the amount of time (number of cycles taken by a process in “user mode.”  System time the time taken by the kernel executing on behalf of the process.  Wallclock time is the total “real” time taken to execute the process. ❖Generally speaking, user time is the most relevant, though it ignores some important operations (I/O, etc.). ❖Wallclock time should be used cautiously/sparingly, but may be necessary for assessment of parallel codes, Monday, March 6, 2023 Design and Analysis of Computer Algorithm 40
  • 41. Representative Operation Counts ❖In some cases, we may want to count operations, rather than time  Identify bottlenecks  Counterpart to theoretical analysis ❖What operations should we count?  Profilers can count function calls and executions of individual lines of code to identify bottlenecks.  We may know a priori what operations we want to measure (example: comparisons and swaps in sorting). Monday, March 6, 2023 Design and Analysis of Computer Algorithm 41
  • 42. Test Sets ❖It is crucial to choose your test set well. ❖The instances must be chosen carefully in order to allow proper conclusions to be drawn. ❖We must pay close attention to  their size,  inherent difficulty,  and other important structural properties. ❖This is especially important if we are trying to distinguish among multiple algorithms. ❖Example: Sorting Monday, March 6, 2023 Design and Analysis of Computer Algorithm 42
  • 43. Comparing Algorithms ❖Given a performance measure and a test set, the question still arises how to decide which algorithm is “better.” ❖We can do the comparison using some sort of summary statistic.  Arithmetic mean  Geometric mean  Variance ❖ Performance profiles allow comparison of algorithms across an entire test set without loss of information. ❖ They provide a visual summary of how algorithms compare on a performance measure across a test set Monday, March 6, 2023 Design and Analysis of Computer Algorithm 43
  • 44. Accounting for Stochasticity ❖In empirical analysis, we must take account of the fact that running times are inherently stochastic. ❖If we are measuring wallclock time, this may vary substantially for seemingly identical executions. ❖In the case of parallel processing, stochasticity may also arise due to asynchronism (order of operations). ❖In such case, multiple identical runs may be used to estimate the affect of this randomness. ❖If necessary, statistical analysis may be used to analyze the results, but this is beyond the scope of this course. Monday, March 6, 2023 Design and Analysis of Computer Algorithm 44
  • 45. Empirical versus Theoretical Analysis ❖For sequential algorithms, asymptotic analysis is often good enough for choosing between algorithms. ❖It is less ideal with respect to tuning of implementation details. ❖For parallel algorithms, asymptotic analysis is far more problematic. ❖The details not captured by the model of computation can matter much more. ❖There is an additional dimension on which we must compare algorithms: scalability Monday, March 6, 2023 Design and Analysis of Computer Algorithm 45