Computational
Complexity of
Algorithm
Group 5
Computational Complexity
Given some input data of size n,
how long will a function take to
run?
How does the number of
computations scale with n?
BACK NEXT
Ex:
EXAMPLE: COUNTING LETTERS
HELLO
HELLO, WELCOME TO DISCRETEMATH!
BACK NEXT
EX:
Runtimes Can Vary
Discretemath
This sentence has a D
I Like Discretemath!
-Focus on Worst Case!
BACK NEXT
BIG O
BIG 'O' NOTATION
DESCRIBES THE GROWTH BEHAVIOR OR
"ORDER" OF A FUNCTION.
EX: O(N) - N OPERATIONS (LINEAR)
ROUGH UPPER BOUND.
BACK NEXT
GRAPH
# COMPUTATIONS
Constant- 0 (1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Constant O(1)
Runtime does not depend on input size.
Example: Look up first item
[1, 2, 5]
[1, 3, 5, 6, 7, 8, 9, 11, 15, 19, 21]
BACK NEXT
GRAPH
# COMPUTATIONS
Logarithmic - 0 (log(n))
Constant- 0 (1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Logarithmic - O(log(n))
Runtime grows slower than input size.
Example: Binary Search
BACK NEXT
GRAPH
# COMPUTATIONS
Linear - 0(n)
Logarithmic - 0(log(n))
Constant- 0(1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Linear - O(n)
Runtime grows at the same rate as input.
Example: Unsorted list search
BACK NEXT
GRAPH
# COMPUTATIONS
Log-Linear - 0(n log(n))
Linear - 0(n)
Logarithmic - 0(log(n))
Constant- 0(1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Log-Linear - O(n * log(n))
An operation of log(n) complexity for
each input value.
Example: Efficient Sorting
BACK NEXT
GRAPH
Quadratic - O (n^2)
# COMPUTATIONS
Log-Linear - 0(n log(n))
Linear - 0(n)
Logarithmic - 0(log(n))
Constant- 0(1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Quadratic - O(n^2)
An operation of complexity O(n) is
carried out for each input.
Example: Check all pairs of input
values. (Double for loop)
BACK NEXT
GRAPH
Quadratic - O (n^2)
# COMPUTATIONS
Log-Linear - 0(n log(n))
..
)..
^3
Linear - 0(n)
(n
O
- l
ia
m
no
ly
Po
Logarithmic - 0(log(n))
Constant- 0(1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Polynomial - O(n^3)...
Computations with other exponents
(n^3, n^4, etc.)
Example: Check all triplets of input
values. (Triple for loop)
BACK NEXT
GRAPH
Exponential - O(2^n)
Quadratic - O pi(n^2)
# COMPUTATIONS
Log-Linear - O(n log(n))
..
)..
^3
Linear - O(n)
(n
O
- l
ia
m
no
ly
Po
Logarithmic - O(log(n))
Constant- O(1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Exponential - O(2^n)
Complexity is multiplied (doubled,
tripled, etc.) with each additional
input value.
Ex: Recursive algorithms
Traveling salesman
BACK NEXT
GRAPH
Worse:
O(n!)
Exponential - O(2^n)
Quadratic - O pi(n^2)
# COMPUTATIONS
Log-Linear - O(n log(n))
..
)..
^3
Linear - O(n)
(n
O
-l
ia
m
no
ly
Po
Logarithmic - O(log(n))
Constant- O(1)
INPUT SIZE N
BACK NEXT
DESCRIPTION
Worse: O(n!)
Runtimes can be worse than
exponential.
Ex: Permutations.
Runtimes can be infinite.
BACK NEXT
Multiple Input Dimensions
Inputs of size n and m. Complexity
may depend on both: O(m * n)
Example: 2 dimensional data (matrix)
operations
BACK NEXT
Complexity Implications
Code implementation can have a
practical impact.
Analyzing runtime and refactoring is
useful.
Some problems are hard (intractable).
BACK NEXT
Considerations
Practical vs theoretical implications
Suboptimal solutions (heuristics) may
be good enough.
Better technology helps.
Memory complexity
BACK NEXT