MAT3514 - Algorithm Complexity Assignment
Algorithm Analysis - Tran Ba Tuan - HUS - VNU
(1) What is Algorithm Complexity
Algorithm complexity refers to the computational resources an algorithm requires to solve a problem, typically in terms of time and space. It is usually analyzed based on:
Time Complexity: This measures the amount of time an algorithm takes as a function of the input size (denoted as n). The time complexity is expressed using Big-O notation, which describes the upper bound of the running time. Common complexities include:
- Constant time
- Logarithmic time
- Linear time
- Linearithmic time
- Quadratic time
- Exponential time
- Factorial time
Space Complexity: This measures the amount of memory an algorithm uses relative to the input size. It is also expressed in Big-O notation, capturing how additional memory grows as input size increases.
Efficient algorithms strive for low time and space complexity, ensuring scalability and performance when dealing with larger datasets.
(2) Examples: Upper bound
Example 1. What is the Upper bound for:
a, We are given the function:
Block
- As increases, the linear term dominates the constant .
- The constant in front of can be ignored because Big-O notation focuses on the order of growth, not constant factors.
- Answer:
b, We are given the function:
Block
- As increases, the quadratic term dominates the constant .
- The coefficient can be ignored in Big-O notation.
- Answer:
c, We are given the function:
Block
- As increases, the term grows faster than and , so it dominates the function.
- The coefficients and are constants and can be ignored.
- Answer:
d, We are given the function:
Block
- As increases, the cubic term dominates the quadratic term .
- The coefficient can be ignored in Big-O notation.
- Answer:
e, We are given the function:
Block
- This is a constant function; it does not depend on and remains the same regardless of input size.
- For constant functions, the upper bound is , meaning the function has constant time complexity.
- Answer:
(2) Examples: Time complexity
General question: What is the time complexity?
Example 2. Calculate :
Block
- This is kind of a trick question. This is ONE independent operation, no loops involved, and the actual value of does not matter, so It has a time complexity of
- Time Complexity:
Example 3.
for i in range(0, n):
print('Current Number', i)
Python- The loop runs from to , so the number of iterations is , and there is a single print operation per iteration.
- Time Complexity:
Example 4.
for i in range(0, n):
print('Current Number', i)
break
Python- The loop seems to be similar to Example 3., but there is a break statement right after the very first operation. Therefore, the loop will be terminated on the very first iteration.
- Time Complexity:
Example 5.
def function(n):
i = 1
while i <= n:
i = i * 2
print(i)
function(100)
Python- In this function, there is a loop running from to . However, inside the loop itself, the value of doubles with each iteration, so the number of iterations is logarithmic with respect to .
- Time Complexity:
Example 6.
for i in range(0, n):
for j in range(0, n):
print("Value of i, j", i, j)
Python- In this case, we can clearly see that there are two loops, each going from to , so there are total iterations.
- Time Complexity:
Example 7.
public void function(int n) {
int i, j, k, count = 0;
for(i = n/2; i <= n; i++)
for(j = 1; j + n/2 <= n; j++)
for(k = 1; k <= n; k = k * 2)
count++;
}
Java- The outer loop runs from to , so it executes times, which is .
- The middle loop runs times.
- The innermost loop doubles each time, so it runs times.
- The total time complexity is the product of all three loops:
- Time Complexity:
Example 8.
public void function(int n) {
int i, j, k, count = 0;
for(i = n/2; i <= n; i++)
for(j = 1; j <= n; j = 2 * j)
for(k = 1; k <= n; k = k * 2)
count++;
}
Java- The outer loop runs from to , so it executes times, which is .
- The middle loop doubles each time, so it runs times.
- The innermost loop doubles each time, so it runs times.
- The total time complexity is the product of all three loops:
- Time Complexity:
(3) Practice: Time complexity
Exercise 1. What is the algorithm complexity?
a, We are given the function:
Block
- We have three terms: , , and .
- The dominant term is as it grows faster than for large values of .
- The constant terms and lower-order terms ($ 3n $ and $ 2 $) are dropped in Big O notation.
- Therefore, the complexity is .
- Answer:
b, We are given the function:
Block
- We have three terms here: , , and .
- Note that is (this is a mathematical fact).
- So, is actually .
- However, for very large , grows faster than .
- The constant terms (5 and 7) are dropped in Big O notation.
- Therefore, the overall time complexity is dominated by , giving us .
- Answer:
c, We are given the function:
Block
- We have two terms: and .
- Although has a larger coefficient, grows much faster for large values of .
- In Big O notation, we’re concerned with the growth rate for large , not the coefficients.
- Therefore, dominates, and the time complexity is .
- Answer:
d, We are given the function:
Block
- We have three terms: , , and .
- For large values of , grows much faster than both and .
- The constants (100) are dropped in Big O notation.
- Therefore, the time complexity is dominated by , giving us .
- Answer:
e, We are given the function:
Block
- We have two terms: and .
- grows faster than for large .
- The constant (0.01) is dropped in Big O notation.
- Therefore, the time complexity is .
- Answer:
Exercise 2. What is the time complexity?
a, Given method:
// Returns the sum of the integers in given array .
public static int example1(int[] arr) {
int n = arr.length, total = 0;
for (int j = 0; j < n; j++) // loop from 0 to n -1
total += arr[j];
return total;
}
Java- This function has a single loop that iterates from to .
- The number of iterations is directly proportional to the size of the input array .
- Each iteration performs a constant-time operation (addition).
- Therefore, the time complexity is linear, .
- Answer:
b, Given method:
// Returns the sum of the integers with even index in given array .
public static int example2(int[] arr) {
int n = arr.length, total = 0;
for (int j = 0; j < n; j += 2) // note the increment of 2
total += arr [j];
return total;
}
Java- This function has a loop that iterates over every other element of the array.
- Although it processes only half of the elements, in Big O notation, we still consider this linear time.
- The number of operations is still directly proportional to , just with a factor of (1/2).
- Constants are dropped in Big O notation, so simplifies to .
- Answer:
c, Given method:
// Returns the sum of the prefix sums of given array .
public static int example3(int[] arr) {
int n = arr.length, total = 0;
for (int j = 0; j < n ; j++) // loop from 0 to n -1
for (int k = 0; k <= j; k++) // loop from 0 to j
total += arr [j];
return total;
}
Java- This function has two nested loops.
- The outer loop runs times.
- For each iteration of the outer loop, the inner loop runs times, where goes from to .
- The total number of iterations is the sum of integers from to , which is .
- This simplifies to in Big O notation.
- Answer:
d, Given method:
// Returns the sum of the prefix sums of given array .
public static int example4(int[] arr) {
int n = arr.length, prefix = 0, total = 0;
for (int j = 0; j < n; j++) { // loop from 0 to n -1
prefix += arr [j];
total += prefix;
}
return total;
}
Java- This function has a single loop that iterates times.
- Inside the loop, all operations (addition and assignment) are constant time.
- Although it’s calculating prefix sums, it does so efficiently in a single pass through the array.
- Therefore, the time complexity is linear,
- Answer:
e, Given method:
// Returns the number of times second array stores sum of prefix sums from first
public static int example5(int[] first, int[] second) {
// assume equal - length arrays
int n = first.length, count = 0;
for (int i = 0; i < n; i++) { // loop from 0 to n -1
int total = 0;
for (int j = 0; j < n; j++) // loop from 0 to n -1
for (int k = 0; k <= j; k++) // loop from 0 to j
total += first [k];
if (second [i] == total) count++;
}
return count;
}
Java- This function has three nested loops.
- The outermost loop runs times.
- For each iteration of the outer loop, the middle loop runs times.
- For each iteration of the middle loop, the innermost loop runs times, where goes from to .
- The total number of iterations is approximately .
- In Big O notation, this simplifies to .
- Answer:
(4) Practice: Master theorem
(Master theorem) If is a power of , the solution to the recurrence:
Block
is denoted:
Block
Example 9. Determine algorithm complexity of this expression:
Block
\begin{flalign*} &\text{Let: } T(n-1) = 2T(n-2) - 1 \\ &\Rightarrow T(n) = 2(2T(n-2) - 1) - 1 \qquad (1) \quad\\ &= 4T(n-2) - 3 \\[10pt] &\text{Let: } T(n-2) = 2T(n-3) - 1 \\ &\Rightarrow T(n) = 8T(n-3) - 7 \qquad (2)\\[10pt] &\text{From (1), (2): } \\ &T(n) = 2^k T(n-k)-(2^k - 1) \\[10pt] &\text{Let k = n: } \\ &T(n) = 2^n T(0) - (2^n - 1) \\ &= 2^n . 1 - (2^n - 1) = 2^n - 2^n + 1 = 1 \\[10pt] &\Rightarrow T(n) = O(1) \\ \end{flalign*}Block
Example 10. Determine algorithm complexity of this expression:
Block
\begin{flalign*} &\text{Let: } T(n-1) = 3T(n-2) \\ &\Rightarrow T(n) = 3(3T(n-2)) = 3^2 T(n-2) \qquad (1) \quad \\[10pt] &\text{Let: } T(n-2) = 3T(n-3) \\ &\Rightarrow T(n) = 3^3 T(n-3) \qquad (2) \quad \\[10pt] &\text{From (1), (2): } \\ &T(n) = 3^k T(n-k) \\[10pt] &\text{Let k = n: } \\ &T(n) = 3^n T(0) = 3^n \\[10pt] &\Rightarrow T(n) = O(3^n) \\ \end{flalign*}Block
Example 11. Determine algorithm complexity:
1. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=2 ;\quad b=2 ;\quad d=1; \\ & \text{We see that } 2 = 2^1 \text{ or } a = b^d \\ & \text{Therefore this is case 2 of the Master Theorem, we conclude: } \quad \\ & T(n) = O(nlog(n)) \\ \end{flalign*}Block
2. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=4 ;\quad b=3 ;\quad d=1; \\ & \text{We see that } 4 > 3^1 \text{ or } a > b^d \\ & \text{Therefore this is case 3 of the Master Theorem, we conclude: } \quad \\ & T(n) = O(n^{\log_3 4}) \\ \end{flalign*}Block
3. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=3 ;\quad b=2 ;\quad d=2; \\ & \text{We see that } 3 < 2^2 \text{ or } a < b^d \\ & \text{Therefore this is case 1 of the Master Theorem, we conclude: } \quad \\ & T(n) = O(n^2) \\ \end{flalign*}Block
Exercise 3. Determine algorithm complexity:
1. Given expression:
Block
- Let
Block
- Let
Block
- Generalizing, let be the number of steps:
Block
The summation is a geometric series:
Block
Thus,
- Let :
Block
- Final result:
Block
Thus, .
2. Given expression:
Block
- Let
Block
- Let
Block
- Generalizing:
Block
Let’s compute the contributions from the summation:
Block
The series has terms:
Block
- Let :
Block
- Final result:
Block
Thus, .
3. Given expression:
Block
- Let
Block
- Let
Block
- Generalizing:
Block
This simplifies to:
Block
- Let :
Block
The sum contributes:
Block
- Final result:
Block
Thus, .
4. Given expression:
Block
- Let
Block
- Let
Block
- Generalizing:
Block
- Let :
Block
Expanding the summation:
Block
- Final result: Evaluating the terms yields:
Block
Simplifying,
Block
Thus,
5. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=2; \quad b=2; \quad d=1; \\ & \text{We see that } 2 = 2^1 \text{ or } a = b^d \\ & \text{By the Master Theorem, we conclude: } \quad \\ & T(n) = O(n \log n) \\ \end{flalign*}Block
6. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=6; \quad b=6; \quad d=1; \\ & \text{We see that } 6 = 6^1 \text{ or } a = b^d \\ & \text{By the Master Theorem, we conclude: } \quad \\ & T(n) = O(n \log n) \\ \end{flalign*}Block
7. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=6; \quad b=6; \quad d=1; \\ & \text{We see that } 6 = 6^1 \text{ or } a = b^d \\ & \text{By the Master Theorem, we conclude: } \quad \\ & T(n) = O(n \log n) \\ \end{flalign*}Block
8. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=4; \quad b=3; \quad d=1; \\ & \text{We see that } 4 > 3^1 \text{ or } a > b^d \\ & \text{By the Master Theorem, we conclude: } \quad \\ & T(n) = O(n^{\log_3 4}) \\ \end{flalign*}Block
9. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=3; \quad b=2; \quad d=2; \\ & \text{We see that } 3 < 2^2 \text{ or } a < b^d \\ & \text{By the Master Theorem, we conclude: } \quad \\ & T(n) = O(n^2) \\ \end{flalign*}Block
10. Given expression:
Block
\begin{flalign*} & \text{Determine a, b, d: } \\ & a=3; \quad b=2; \quad d=1; \\ & \text{We see that } 3 > 2^1 \text{ or } a > b^d \\ & \text{By the Master Theorem, we conclude: } \quad \\ & T(n) = O(n^{\log_2 3}) \\ \end{flalign*}Block