2 Algorithm Analysis and Time Complexity
2 Algorithm Analysis and Time Complexity
1
Algorithms Analysis
If you run the same program on a computer, cellphone, or even a smartwatch, will it take
same time or different time?
Wouldn’t it be great if we can compare algorithms regardless of the hardware where we run
them?
That’s what time complexity is for!
But, why stop with the running time?
We could also compare the memory "used" by different algorithms, and we call that space
complexity.
2
What is an Algorithm?
An Algorithm is a set of step by step instructions to be followed to solve a particular
problem.
Characteristics of an Algorithm
Finding if a
< 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
number is odd
6
Relationship between algorithm input size and time taken to
complete
As you can see in the table, most algorithms on the table are affected by the
input size.
This is not for every case. Line 5 executed only if line 4 condition is TRUE.
So, we need the big picture and get rid of smaller terms to compare algorithms
easier.
9
Running Time
best case
Most algorithms transform input average case
objects into output objects. worst case
120
The running time of an algorithm
typically grows with the input size. 100
Running Time
Average-case running time is often 80
difficult to determine. 60
10
All algorithms have three scenarios:
Best-case scenario: the most favorable input arrangement where the program will
take the least amount of operations to complete.
Worst-case scenario: the inputs are arranged in such a way that causes the program
to take the longest to complete.
E.g., array items in reversed order for some sorting algorithm will take the longest
to run.
11
Average Case vs. Worst Case
The average case running time is
harder to analyze because you
need to know the probability
distribution of the input.
12
Experimental Approach
9000
Write a program implementing the 8000
algorithm 7000
6000
Run the program with inputs of
Time (ms)
5000
varying size and composition
4000
1000
Plot the results
0
0 50 100
Input Size
13
Limitations of Experiments
Restrictions
14
Theoretical Analysis
Uses a high-level description of the algorithm instead
of an implementation
You can express it in Big O notation as O(n3). The other terms (3n2 + 5) will become
less significant as the input grows bigger.
Big O notation only cares about the “biggest” terms in the time/space complexity.
16
17
18
19
How long an algorithm takes to run based on their
time complexity and input size
Input Size O(1) O(n) O(n log n) O(n2) O(2n) O(n!)
1 < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec.
10 < 1 sec. < 1 sec. < 1 sec. < 1 sec. < 1 sec. 4 seconds
This is just an illustration since, in different hardware, the times will be distinct. These times are under the assumption
of running on 1 GHz CPU, and it can execute on average one instruction in 1 nanosecond (usually takes more time).
Note
Also, keep in mind that each line might be translated into dozens of CPU instructions depending on the programming
language.
20
Space Complexity
Space complexity is similar to time complexity.
Instead of the count of operations executed, it will account for the amount of
memory used additionally to the input.
For calculating the space complexity, we keep track of the “variables” and memory
used.
On other algorithms, If we have to use an auxiliary array that holds the same
number of elements as the input, then the space complexity would be n.
21
Pseudocode
High-level description of Example: find the max
an algorithm element of an array
More structured than Algorithm arrayMax(A, n)
english prose Input array A of n integers
Less detailed than a Output maximum element of A
program currentMax A[0]
Preferred notation for for i 1 to n 1 do
describing algorithms if A[i] currentMax then
currentMax A[i]
Hides program design return currentMax
issues
22
Pseudocode Details
Control flow
Method call
if … then … [else …] var.method (arg [, arg…])
Return value
while … do …
return expression
repeat … until … Expressions
for … do … Assignment (like in C, C++)
Equality testing
Indentation replaces braces (like in C, C++)
n2 Superscripts and other
Method declaration mathematical formatting
allowed
Algorithm method (arg [, arg…])
Input …
Output …
23