Lecture-2-Time-and-Space-Complexity-1
Lecture-2-Time-and-Space-Complexity-1
Time complexity of an algorithm is the amount of CPU time it needs to run completion.
TIME: SPACE:
- Operations - Variables
- Comparisons - Data structures
- Loops - Allocations
- Pointer references - Function call
- Function calls to outside
Time complexity of an algorithm is the amount of time (or number of steps)
needed by a program to complete its task (to execute a particular algorithm).
1. Compilation time
Solutions
Less More
Time
Space
Compile Time
Run Time
➢ Analyzing Loops
Time and Space are dependent
➢ Analyzing Nested Loops
on these analysis
1 1, 2, 8: Once
int s = 0;
3, 4, 5, 6, 7: Once per each iteration of for
loop, N iteration
for (int i = 0 ; i < N ; i++ )
4
2 Total: 5𝑁 + 3
s = s + A[i] ; 3
Space Complexity of a program is the amount of memory consumed by the algorithm until it completes
its execution.
1. A fixed amount of memory occupied by the space for the program i.e. data types
2. Code and space occupied by the variables used in the program.
3. A variable amount of memory occupied by the component variable whose size is dependent on the
problem being solved.
4. This space increases or decreases depending on whether the program uses iterative or recursive
procedures.
➢ Type 1: A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem.
For example, simple variables and constant used, program size, etc.
➢ Type 2: A variable part is a space required by variables, whose size depends on the size of the
problem.
Instruction Space: is the space in memory occupied by the complied version of the program. We
consider this space as a constant space for any value of n. The instruction space is independent of the
size of the problem.
Data Space: is the space in memory, which used to hold the variables, data structures, allocated
memory and other data elements. The data space is related to the size of the problem.
Environment Space: is the space in memory used on the run time stack for each function call. This is
related to the run time stack and holds the returning address of the previous function. Stored return value
and pointer on it.
Basic Example for Space Complexity
Method 1 Method 2
𝑛−2 𝑛−2
The second method is faster. That’s why time complexity is important. In real life we want
software to be fast & smooth.
Space complexity of a program is a simple measurement of how fast the space taken by a
program grows, if the input increases.
Method 2
Method 1
𝑂(1)
𝑂(𝑛)
The second method is better. There is no point in using more space to solve a problem if, we
can do the same with lesser space complexity.
Calculating Time
complexity of
Algorithms
Time complexity
𝑂(𝑛) 𝑂(𝑛2 )
for (𝑖 = 1 to 𝑐) for (𝑖 = 1 ; 𝑖 ≤ 𝑛; 𝑖 = 𝑖 + 𝑐)
{ {
x = y + z; x = y + z;
} }
int 𝑖 = 1; int 𝑖 = 1;
while (𝑖 ≤ 𝑐) while (𝑖 ≤ 𝑛)
{ {
x = y + z; i = i + c;
} }
𝑂(1) 𝑂(𝑛)
7. Loops running n times and
incrementing/decrementing by
constant factor
for (𝑖 = 1 ; 𝑖 ≤ 𝑛; 𝑖 = 𝑖 ∗ 𝑐)
{
x = y + z;
}
int 𝑖 = 1;
while (𝑖 ≤ 𝑛)
{
i = i / c;
}
𝑂(𝑛) 𝑂(𝑙𝑜𝑔𝑛)
8. Loops running n times and
incrementing by some constant
1→𝑖=2
power
2 → 𝑖 = 2𝑐
2
3 → 𝑖 = 2𝑐
for (𝑖 = 2 ; 𝑖 ≤ 𝑛; 𝑖 = 𝑝𝑜𝑤 (𝑖, 𝑐)) …
{ 𝑖
x = y + z; The loop will end when: 𝑛 = 2𝑐
} 𝑖
log2( 𝑛) = log 2 (2𝑐 )
𝑙𝑜𝑔𝑛 = 𝑐 𝑖
int 𝑖 = 2;
while (𝑖 ≤ 𝑛)
log 𝑐 𝑙𝑜𝑔𝑛 = 𝑙𝑜𝑔𝑐 𝑐 𝑖
{
𝑖 = 𝑙𝑜𝑔𝑐(𝑙𝑜𝑔𝑛)
i = pow(i , c);
}
𝑂(𝑙𝑜𝑔 𝑙𝑜𝑔𝑛 )
Time complexity
More Examples
Algo1 () Algo2 ()
{ {
int 𝑖; int 𝑖;
for (𝑖 = 1 to 𝑛) for (𝑖 = 1 to 𝑛)
//nested loop
print (“Hello World”); for (𝑗 = 1 to 𝑛)
} print (“Hello World”);
}
𝑂(𝑛)
𝑂(𝑛2 )
Time complexity
Algo3 () Algo4 ()
{ {
int 𝑖; int 𝑖;
for (𝑖 = 1; 𝑖 < 𝑛; 𝑖 = 𝑖 ∗ 2) for (𝑖 = 1; 𝑖 < 𝑛; 𝑖 = 𝑖/5)
print (“Hello World”); print (“Hello World”);
} }
𝑖 → 1, 2, 4, 8, 16, 32, … 𝑛
𝑂(log 5 𝑛)
20 , 21, 22 , 23, 24 , 25 … 2𝑘
Algo5 ()
𝑛 = 2𝑘 {
2𝑘 = 𝑛 int 𝑖;
for (𝑖 = 1; 𝑖 < 𝑛3 ; 𝑖 = 𝑖 ∗ 5)
𝑘 = log2 𝑛 print (“Hello World”);
}
𝑂(log 2 𝑛) 𝑂(log 5 𝑛3 )
Time complexity
Algo6 () Algo7 ()
{ {
int 𝑖; int 𝑖 = 1, 𝑘 = 1;
for (𝑖 = 1; 𝑖 2 <= 𝑛; 𝑖 + +) while (𝑘 <= 𝑛)
print (“Hello World”); {
} 𝑖 + +; // all operations
𝑘 = 𝑘 + 𝑖; inside any loop
𝑖 2 <= 𝑛 print (“Hello World”); considered
𝑖 2 <= 𝑛 }
𝑖 <= 𝑛 }
𝑘 1 3 6 10 15 … 𝑛
𝑖 1 2 3 4 5 … 𝑧
𝑂( 𝑛) 𝑧(𝑧 + 1)
=𝑛
2
𝑧 2 + 𝑧 = 2𝑛
𝑧 = 2𝑛 = 2 ∙ 𝑛 𝑂( 𝑛)
Time complexity
Algo8 () Algo9 ()
{ {
int 𝑖, 𝑗, 𝑘; int 𝑖 = 𝑛;
for (𝑖 = 𝑛/2; 𝑖 <= 𝑛; 𝑖 + +) // 𝑛/2 while (𝑖 > 1)
for (𝑗 = 1; 𝑗 <= 𝑛/2; 𝑗 + +) // 𝑛/2 {
for (𝑘 = 1; 𝑘 <= 𝑛; 𝑘 = 𝑘 ∗ 2) // log 2 𝑛 print (“Hello World”);
print (“Hello World”); 𝑖 = 𝑖/2; // log 2 𝑛
} }
}
𝑛 𝑛 𝑛2 log 2 𝑛
∙ ∙ log2 𝑛 =
2 2 4
Algo10 ()
{
int 𝑖, 𝑗, 𝑘;
for (𝑖 = 𝑛/2; 𝑖 < 𝑛; 𝑖 + +) // 𝑛/2
for (𝑗 = 1; 𝑗 <= 𝑛; 𝑗 = 2 ∗ 𝑗) // log 2 𝑛
for (𝑘 = 1; 𝑘 <= 𝑛; 𝑘 = 𝑘 ∗ 2) // log 2 𝑛
print (“Hello World”);
}
𝑛 𝑛 (log 2 𝑛)2
∙ log 2 𝑛 ∙ log 2 𝑛 =
2 2
Algo11 () Algo12 ()
𝑛 𝑛+1 𝑛2 + 𝑛
𝑇𝐶 = 1 + 2 + 3 … + 𝑛 = = ( )
2 2
𝑂(𝑛2 )
Time complexity
𝑛 𝑛
1 𝑑𝑥
≈න = 𝑙𝑜𝑔𝑛
Algo13 () 𝑘 1 𝑥
𝑘=1
for (𝑖 = 1; 𝑖 ≤ 𝑛; 𝑖 + +){
for (𝑗 = 1; 𝑗 <= 𝑛; 𝑗 = 𝑗 + 𝑖)
𝑥 = 𝑥 + 1;
}
𝑖 1 2 3 … 𝑛
𝑗 𝑛 𝑛/2 𝑛/3 … 𝑛/𝑛
1 1 1
𝑂(𝑛𝑙𝑜𝑔𝑛) 𝑇𝐶 = 𝑛 1 + + + ⋯+ = 𝑛𝑙𝑜𝑔𝑛
2 3 𝑛
Time complexity
𝑛(𝑛 + 1)(𝑛 + 2)
1+ 1+ 2 + 1 +2 + 3 + ⋯+ 1+ 2+ 3 +⋯+ 𝑛 =
Algo14 () 6
𝑥 = 0;
for (𝑖 = 1; 𝑖 ≤ 𝑛; 𝑖 + +){
for (𝑗 = 1; 𝑗 ≤ 𝑖; 𝑗 = 𝑗 + +)
for (k = 1; 𝑘 ≤ 𝑗; 𝑘 = 𝑘 + +)
𝑥 = 𝑥 + 1;
}
𝑖 1 2 3 … 𝑛
𝑗 1 1; 2 1; 2; 3 … 1, 2, 3, … , 𝑛
𝑘 1 1; 1, 2 1; 1, 2; 1, 2, 3 … 1; 1, 2; … 1,2, 3 … 𝑛
𝑂(𝑛3 )
int fun(int n) int a = 0, b = 0;
{ for (i = 0; i < N; i++) {
int count = 0; a = a + rand();
for (int i = n; i > 0; i /= 2) }
for (int j = 0; j < i; j++) for ( j = 0; j < M; j++) {
count += 1; b = b + rand();
return count; }
}
𝑂(𝑁 + 𝑀)
𝑛 𝑛
𝑇 𝑛 = 𝑛 + + + ⋯ + 1 = 𝑂(𝑛)
2 4
for (int i = n; i > 0; i = i / 2) int i, j, k = 0;
{ for (i = n / 2; i <= n; i++) {
for (int j = 1; j < n; j = j * 2) for ( j = 2; j <= n; j = j * 2)
{ {
for (int k = 0; k < n; k = k + 2) k = k + n / 2;
{ }
//some logic with complexity X }
}
} 𝑂(𝑛𝑙𝑜𝑔𝑛)
}
𝑂(𝑛 (log2 𝑛)2 )
int value = 0;
for(int i=0;i<n;i++)
for(int i=0;i<n;i++){ for(int j=0;j<i;j++)
i*=k; value += 1;
}
𝑂(𝑛2)
𝑂(log 𝑘 𝑛) int a = 0, i = N;
while (i > 0) {
a += i;
i /= 2;
}
𝑂(log 𝑛)
Calculating Space
complexity of
Algorithms
Space complexity
function sumOfNumbers(arr[], N)
{ Linear Space Complexity
sum = 0
for (i = 0 to N)
{
sum = sum + arr[i] arr – N * 4 bytes
} sum – 4 bytes
print (sum) i – 4 bytes
} Aux (initializing for loop,
function call, return) – 4 bytes
𝑂(𝑛)
Total (estimated): 4N + 12 bytes
Space complexity
int fact = 1;
Constant Space (behavior)
for (int i = 1; i <= n; i++)
{
fact *= i;
}
return fact; fact – 4 bytes
n – 4 bytes
i – 4 bytes
Aux (initializing for loop,
𝑂(1) function call, return) – 4 bytes
factorial(n){
if (n <= 1) { 𝑂(𝑛)
return 1;
} else { Linear Space Complexity
return (n*factorial(n-1));
} 1
} assume n = 5 2*f(1)
𝑓𝑛(5) 3*f(2)
n – 4 bytes 4*f(3)
Aux (function 120 5*f(4)
call) – 5 * 4 bytes
Call Stack
Total (estimated): 4 bytes + 4*n bytes
Space – Time Tradeoff and Efficiency
All efforts made by analyzing time and space complexity lead to the algorithm’s efficiency.
But, when we can say that an algorithm is efficient? The answer seems to be obvious: it should be
fast, and it should take the least amount of memory possible.
Unfortunately, in algorithmics, space and time are like two separate poles. Increasing speed will
most often lead to increased memory consumption and vice-versa.
On the one side, we have merge sort, which is extremely fast but requires a lot of memory. On the
other side, we have bubble sort, a slow algorithm but one that occupies minimal space. There are
also some balanced ones like in-place heap sort. Its speed and space usage are not the best, but
they’re acceptable.
Maximizing both the algorithm’s space and time complexity is impossible. We should adjust
those parameters according to our requirements and environment.
5 Basic Sequences and Their Sums
a) b) c)
What is the time and space complexity of the following codes:
a) b) c)
a)
b)
c)
What is the time complexity of the following codes:
a)
Time Complexity: O(log n)
b)
Time Complexity: O(log n)
c)
Recursive Method:
At Iteration 1:
Also, we know that after k iterations, the length of the
Length of array = n array becomes 1 Therefore, the Length of the array:
At Iteration 2: 𝑛
=1
𝑛
2𝑘
Length of array = 2 => 𝑛 =2𝑘
At Iteration 3: Applying log function on both sides:
𝑛
𝑛
Length of array = = 2
=> log 2 𝑛 = log 2 2𝑘
2 22
https://medium.com/@manishsakariya/time-complexity-examples-6a4877a1b923
https://www.youtube.com/watch?v=AWHi1-Xmd-Y
https://www.youtube.com/watch?v=yOb0BL-84h8&t=1107s
https://www.enjoyalgorithms.com/blog/time-complexity-analysis-of-loop-in-programming