Computational Complexity
Computational Complexity
Chnoor M. Rahman
Spring 2023
A priori analysis and A posteriori analysis
• Algorithms
• Independent of language
• Hardware independent
• Time and Space function
• Results do not change
Posteriori Testing
• Program
• Language dependent
• Hardware dependent
• Results might not be the same
Analyzing algorithms
To analyze algorithms, their complexity should be calculates.
7
Big O notation
8
Big O notation
10
Eliminate all excluding the highest order terms
Regular Big-O
11
Common Time complexities
1. O(1) — Constant Time: Given an input of size n, it only takes a single step for
the algorithm to accomplish the task.
3. O(n) — Linear Time: Given an input of size n, the number of of steps required
is directly related (1 to 1)
12
Common Time complexities cont..
13
14
Example:
let n = 16;
O(2^n) = 65,536 steps "(...)“ (an n increases by 1 -> count doubles roughly by 2)
15
Big O Analysis
Required time
No. of inputs
16
Example
algorithm change_Position (X , Y){
Store:=X; (1)
X:=Y; (1)
Y:=Store; (1)
}
F(n) =3
Time Complexity = O(1)
O(1) – Example
//If I know the persons name, I only have to take one step to check:
function isFriend(name){ //similar to knowing the index in an Array
return friends[name]; (1)
}
function add(num1,num2){ // I have two numbers, takes one step to return the value
return num1 + num2; (1)
} Time Complexity: O(1)
18
O(1) – Example
void constantTimeComplexity(int arr[])
{
printf("First element of array = %d",arr[0]);
}
Answer: O(1)
Here, the input array could be 1 item or 1,000 items, but this function 8istill just
require one step.
19
Example
//You decrease the amount of work you have to do with each step
When the input is divided with each iteration, it’s O(log n). Example: Binary Search
21
O(n) – Example
//The number of steps you take is directly correlated to the input size
function addAges(array){
var sum = 0;
for (let i=0 ; i < array.length; i++){ //has to go through each value
sum += array[i]
}
return sum;
}
22
O(n) – Example
void linearTimeComplexity(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d\n", arr[i]);
}
}
Answer: O(n)
This function runs in O(n) time (or "linear time"), where n is the number of items in the
array. If the array has 10 items, we have to print 10 times. If it has 1000 items, we have to
print 1000 times.
23
O(n²) – Example 1 Here we're nesting two loops. If
our array has n items, our outer
function addedAges(array){ loop runs n times, and our inner
var addedAge = 0; loop runs n times for each iteration
for (let i=0 ; i < array.length; i++){ of the outer loop, giving us n^2
for(let j=0 ; j < array.length ; j++){ total prints. If the array has 10
addedAge += array[i][j];
items, we have to print 100 times.
If it has 1000 items, we have to
}
print 1000000 times. Thus this
} function runs in O(n^2) time (or
return addedAge; "quadratic time").
}
Note: If one for loop is linear time (n) Then two nested for loops are (n * n) or (n^2)
Quadratic!
24
O(n²) – Example 2
void quadraticTimeComplexity(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d = %d\n", arr[i], arr[j]);
}
}
}
Answer: O(n^2)
25
O(2^n) – Example 1
The number of steps it takes to accomplish a task is a constant to the n power. For
example, trying to find every combination of letters for a password of length n.
26
O(2^n) – Example 2
int fibonacci(int num)
{
if (num <= 1) return num;
return fibonacci(num - 2) + fibonacci(num - 1);
}
Answer: O(2^n)
An example of an O(2^n) function is the recursive calculation of Fibonacci
numbers. O(2^n) denotes an algorithm whose growth doubles with each addition to
the input data set. The growth curve of an O(2^n) function is exponential - starting
off very shallow, then rising meteorically.
27
Example Since
A is two dimension array, B is two
Algorithm Sum(X,Y,n) dimension array and C is two dimension
array
{
Then
for (i=0;i<n;i++) X n^2
{ Y n^2
for(j=0;j<n;j++) Z n^2
{ n 1
i 1
Z[i, j]=X[I,j]+Y[I,j];
j 1
}
} f(n)=3 n^2+3
} O(n^2)
Example
f(n)=n/20
Example
Algorithm Sum(X,Y,n)
{
for (i=0;i<n;i++) n+1
{
for(j=0;j<n;j++) n * (n+1)
{
Z[i, j]=X[I,j]+Y[I,j]; n * n
} ---------
} 2n^2+ 2n+1
} f(n)=(n^2)
O(n^2)
Example
} 1
return a;
f(n)=2n +3
O(n)=n
}
Example
34
More Examples:
35
Example - HW
int f(int n){
If(n==1)
return 1;
Else
return f(n-1) + f(n-1);
}
36
References
• https://www.freecodecamp.org/news/time-is-complex-but-priceless-f0abd01506
3c/#:~:text=O(n%C2%B2)%20%E2%80%94%20Quadratic%20Time%3A%20The%2
0number%20of%20steps,power%20(pretty%20large%20number)
.
• https://www.vegaitglobal.com/media-center/knowledge-base/fundamental-data
-structures-computational-complexity
• Data Structures and algorithms – 4th Edition – Chapter 4
37