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:

  • O(1):O(1): Constant time
  • O(logn):O(log n): Logarithmic time
  • O(n):O(n): Linear time
  • O(nlogn):O(n log n): Linearithmic time
  • O(n2):O(n²): Quadratic time
  • O(2n):O(2^n): Exponential time
  • O(n!):O(n!): 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:

f(n)=3n+5f(n) = 3n + 5
Block
  • As nn increases, the linear term 3n3n dominates the constant 55.
  • The constant 33 in front of nn can be ignored because Big-O notation focuses on the order of growth, not constant factors.
  • Answer: O(f(n))=O(n)O(f(n)) = O(n)

b, We are given the function:

f(n)=4n2+3f(n) = 4n^2 + 3
Block
  • As nn increases, the quadratic term 4n24n^2 dominates the constant 33.
  • The coefficient 44 can be ignored in Big-O notation.
  • Answer: O(f(n))=O(n2)O(f(n)) = O(n^2)

c, We are given the function:

f(n)=n4+100n2+80f(n) = n^4 + 100n^2 + 80
Block
  • As nn increases, the term n4n^4 grows faster than 100n2100n^2 and 8080, so it dominates the function.
  • The coefficients 100100 and 8080 are constants and can be ignored.
  • Answer: O(f(n))=O(n4)O(f(n)) = O(n^4)

d, We are given the function:

f(n)=5n35n2f(n) = 5n^3 - 5n^2
Block
  • As nn increases, the cubic term 5n35n^3 dominates the quadratic term 5n2-5n^2.
  • The coefficient 55 can be ignored in Big-O notation.
  • Answer: O(f(n))=O(n3)O(f(n)) = O(n^3)

e, We are given the function:

f(n)=502f(n) = 502
Block
  • This is a constant function; it does not depend on nn and remains the same regardless of input size.
  • For constant functions, the upper bound is O(1)O(1), meaning the function has constant time complexity.
  • Answer: O(f(n))=O(1)O(f(n)) = O(1)

(2) Examples: Time complexity

General question: What is the time complexity?

Example 2. Calculate SS:

S=n(n1)2S = \frac{n(n-1)}{2}
Block
  • This is kind of a trick question. This is ONE independent operation, no loops involved, and the actual value of nn does not matter, so It has a time complexity of O(1)O(1)
  • Time Complexity: O(1)O(1)

Example 3.

1
2
for i in range(0, n):
    print('Current Number', i)
Python
  • The loop runs from 00 to n1n - 1, so the number of iterations is nn, and there is a single print operation per iteration.
  • Time Complexity: O(n)O(n)

Example 4.

1
2
3
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: O(1)O(1)

Example 5.

1
2
3
4
5
6
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 i=1i = 1 to nn. However, inside the loop itself, the value of ii doubles with each iteration, so the number of iterations is logarithmic with respect to nn.
  • Time Complexity: O(log(n))O(log(n))

Example 6.

1
2
3
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 00 to n1n - 1, so there are n×n=n2n \times n = n^2 total iterations.
  • Time Complexity: O(n2)O(n^2)

Example 7.

1
2
3
4
5
6
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 i=n/2i = n/2 to nn, so it executes n/2n/2 times, which is O(n)O(n).
  • The middle loop runs n/2n/2 times.
  • The innermost loop doubles kk each time, so it runs O(log(n))O(log(n)) times.
  • The total time complexity is the product of all three loops:
O(n)×O(n/2)×O(log(n))=O(n2log(n))O(n) \times O(n/2) \times O(log(n)) \\ = O(n^2log(n))
  • Time Complexity: O(n2log(n))O(n^2log(n))

Example 8.

1
2
3
4
5
6
7
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 i=n/2i = n/2 to nn, so it executes n/2n/2 times, which is O(n)O(n).
  • The middle loop doubles jj each time, so it runs O(log(n))O(log(n)) times.
  • The innermost loop doubles kk each time, so it runs O(log(n))O(log(n)) times.
  • The total time complexity is the product of all three loops:
O(n)×O(log(n))×O(log(n))=O(nlog2(n))\\ O(n) \times O(log(n)) \times O(log(n)) \\ = O(n log^2(n))
  • Time Complexity: O(nlog2(n))O(n log^2(n))

(3) Practice: Time complexity

Exercise 1. What is the algorithm complexity?

a, We are given the function:

T(n)=nlogn+3n+2T(n) = nlogn + 3n + 2
Block
  • We have three terms: nlognnlogn, 3n3n, and 22.
  • The dominant term is nlognnlogn as it grows faster than nn for large values of nn.
  • The constant terms and lower-order terms ($ 3n $ and $ 2 $) are dropped in Big O notation.
  • Therefore, the complexity is O(nlog(n))O(n log(n)).
  • Answer: O(nlog(n))O(n log(n))

b, We are given the function:

T(n)=nlog(n!)+5n2+7T(n) = nlog(n!) + 5n^2 + 7
Block
  • We have three terms here: nlog(n!)nlog(n!), 5n25n^2, and 77.
  • Note that log(n!)log(n!) is O(nlogn)O(n log n) (this is a mathematical fact).
  • So, nlog(n!)nlog(n!) is actually O(n2logn)O(n^2 log n).
  • However, for very large nn, n2n^2 grows faster than n2lognn^2 log n.
  • The constant terms (5 and 7) are dropped in Big O notation.
  • Therefore, the overall time complexity is dominated by n2n^2, giving us O(n2)O(n^2).
  • Answer: O(n2)O(n^2)

c, We are given the function:

T(n)=1000n+0.01n2T(n) = 1000n + 0.01n^2
Block
  • We have two terms: 1000n1000n and 0.01n20.01n^2.
  • Although 1000n1000n has a larger coefficient, n2n^2 grows much faster for large values of nn.
  • In Big O notation, we’re concerned with the growth rate for large nn, not the coefficients.
  • Therefore, n2n^2 dominates, and the time complexity is O(n2)O(n^2).
  • Answer: O(n2)O(n^2)

d, We are given the function:

T(n)=100nlogn+n3+100nT(n) = 100nlogn + n^3 + 100n
Block
  • We have three terms: 100nlogn100nlogn, n3n^3, and 100n100n.
  • For large values of nn, n3n^3 grows much faster than both nlognnlogn and nn.
  • The constants (100) are dropped in Big O notation.
  • Therefore, the time complexity is dominated by n3n^3, giving us O(n3)O(n^3).
  • Answer: O(n3)O(n^3)

e, We are given the function:

T(n)=0.01nlogn+n(logn)2T(n) = 0.01nlogn + n(logn)^2
Block
  • We have two terms: 0.01nlogn0.01nlogn and n(logn)2n(logn)^2.
  • n(logn)2n(logn)^2 grows faster than nlognnlogn for large nn.
  • The constant (0.01) is dropped in Big O notation.
  • Therefore, the time complexity is O(nlog2n)O(n log^2 n).
  • Answer: O(nlog2n)O(n log^2 n)

Exercise 2. What is the time complexity?

a, Given method:

1
2
3
4
5
6
7
// 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 00 to n1n-1.
  • The number of iterations is directly proportional to the size of the input array (n)(n).
  • Each iteration performs a constant-time operation (addition).
  • Therefore, the time complexity is linear, O(n)O(n).
  • Answer: O(n)O(n)

b, Given method:

1
2
3
4
5
6
7
// 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 nn, just with a factor of (1/2).
  • Constants are dropped in Big O notation, so O(n/2)O(n/2) simplifies to O(n)O(n).
  • Answer: O(n)O(n)

c, Given method:

1
2
3
4
5
6
7
8
// 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 nn times.
  • For each iteration of the outer loop, the inner loop runs j+1j + 1 times, where jj goes from 00 to n1n-1.
  • The total number of iterations is the sum of integers from 11 to nn, which is n(n+1)/2n(n+1)/2.
  • This simplifies to O(n2)O(n^2) in Big O notation.
  • Answer: O(n2)O(n^2)

d, Given method:

1
2
3
4
5
6
7
8
9
// 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 nn 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, O(n)O(n)
  • Answer: O(n)O(n)

e, Given method:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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 nn times.
  • For each iteration of the outer loop, the middle loop runs nn times.
  • For each iteration of the middle loop, the innermost loop runs j+1j + 1 times, where jj goes from 00 to n1n-1.
  • The total number of iterations is approximately nn(n/2)=n3/2n * n * (n/2) = n^3/2.
  • In Big O notation, this simplifies to O(n3)O(n^3).
  • Answer: O(n3)O(n^3)

(4) Practice: Master theorem

(Master theorem) If nn is a power of bb, the solution to the recurrence:

T(n)={aT(n/b)+ndif n>1 and a1,b>1,d01if n1T(n) = \begin{cases} aT(n/b) + n^d & \text{if } n > 1 \text{ and } a \geq 1, b > 1, d \geq 0 \quad \\ 1 & \text{if } n \leq 1\end{cases}
Block

is denoted:

T(n)={O(nd)if a<bdO(ndlog(n))if a=bdO(nlogb(a))if a>bdT(n) = \begin{cases} O(n^d) & \text{if } a < b^d \\ O(n^d \log(n)) & \text{if } a = b^d \\ O(n^{\log_b(a)}) & \text{if } a > b^d \end{cases}
Block

Example 9. Determine algorithm complexity of this expression:

T(n)={2T(n1)1if n>01otherwiseT(n) = \begin{cases} 2T(n-1) - 1 & \text{if } n > 0 \\ 1 & \text{otherwise} \end{cases}
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:

T(n)={3T(n1)if n>01otherwiseT(n) = \begin{cases} 3T(n-1) & \text{if } n > 0 \\ 1 & \text{otherwise} \end{cases}
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:

T(1)=1,and for all n2 a power of 2 ,T(n)=2T(n/2)+6n1T(1)=1, \text{and for all } n≥2 \text{ a power of 2 }, T(n)=2T(n/2)+6n−1 \quad
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:

T(1)=2,and for all n2 a power of 3 ,T(n)=4T(n/3)+3n5T(1)=2, \text{and for all } n≥2 \text{ a power of 3 }, T(n)=4T(n/3)+3n−5 \quad
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:

T(1)=1,and for all n2 a power of 2 ,T(n)=3T(n/2)+n2nT(1)=1, \text{and for all } n≥2 \text{ a power of 2 }, T(n)=3T(n/2)+n^2−n \quad
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:

T(1)=1,and for all n2,T(n)=3T(n1)+2T(1)=1, \text{and for all } n≥2, T(n)=3T(n-1)+2 \quad
Block
  1. Let T(n1)=3T(n2)+2T(n-1)=3 T(n-2)+2
    T(n)=3(3T(n2)+2)+2=32T(n2)+32+2=32T(n2)+6+2=32T(n2)+8\begin{aligned} T(n)= & 3(3 T(n-2)+2)+2=3^2 T(n-2)+3 \cdot 2+2 \quad\\ &=3^2 T(n-2)+6+2=3^2 T(n-2)+8 \end{aligned}
    Block
  2. Let T(n2)=3T(n3)+2T(n-2)=3 T(n-3)+2
    T(n)=32(3T(n3)+2)+8=33T(n3)+322+8=33T(n3)+18+8=33T(n3)+26\begin{aligned} T(n)= & 3^2(3 T(n-3)+2)+8=3^3 T(n-3)+3^2 \cdot 2+8 \quad\\ & =3^3 T(n-3)+18+8=3^3 T(n-3)+26 \end{aligned}
    Block
  3. Generalizing, let kk be the number of steps:
    T(n)=3kT(nk)+2(3k1+3k2++1)T(n)=3^k T(n-k)+2 \cdot\left(3^{k-1}+3^{k-2}+\ldots+1\right) \quad
    Block

The summation 2(3k1+3k2++1)2 \cdot\left(3^{k-1}+3^{k-2}+\ldots+1\right) is a geometric series:

=23k131=23k12=3k1=2 \cdot \frac{3^k-1}{3-1}=2 \cdot \frac{3^k-1}{2}=3^k-1 \quad
Block

Thus, T(n)=3kT(nk)+(3k1)T(n)=3^k T(n-k)+\left(3^k-1\right)

  1. Let k=n1k=n-1 :
    T(n)=3n1T(1)+(3n11)=3n11+(3n11)=3n1+3n11=23n11\begin{gathered} T(n)=3^{n-1} T(1)+\left(3^{n-1}-1\right)=3^{n-1} \cdot 1+\left(3^{n-1}-1\right) \quad \\ =3^{n-1}+3^{n-1}-1=2 \cdot 3^{n-1}-1 \end{gathered}
    Block
  2. Final result:
    T(n)=23n11T(n)=2 \cdot 3^{n-1}-1
    Block

Thus, T(n)=O(3n)T(n)=O\left(3^n\right).

2. Given expression:

T(1)=3,and for all n2,T(n)=T(n1)+2n3T(1)=3, \text{and for all } n≥2, T(n)=T(n-1)+2n−3 \quad
Block
  1. Let T(n1)=T(n2)+2(n1)3T(n-1)=T(n-2)+2(n-1)-3
    T(n)=T(n2)+2(n1)3+2n3=T(n2)+2n2T(n)=T(n-2)+2(n-1)-3+2 n-3=T(n-2)+2 n-2 \quad
    Block
  2. Let T(n2)=T(n3)+2(n2)3T(n-2)=T(n-3)+2(n-2)-3
    T(n)=T(n3)+2(n2)3+2n2=T(n3)+2n2+2(n2)3=T(n3)+2n2+2n43=T(n3)+4n9\begin{gathered} T(n)=T(n-3)+2(n-2)-3+2 n-2 \\ =T(n-3)+2 n-2+2(n-2)-3=T(n-3)+2 n-2+2 n-4-3=T(n-3)+4 n-9 \quad \end{gathered}
    Block
  3. Generalizing:
    T(n)=T(nk)+2n2+2(n1)3+2(n2)3++2(nk+1)3T(n)=T(n-k)+2 n-2+2(n-1)-3+2(n-2)-3+\ldots+2(n-k+1)-3 \quad
    Block

Let’s compute the contributions from the summation:

T(n)=T(nk)+(2n2)+(2n4)+T(n)=T(n-k)+(2 n-2)+(2 n-4)+\ldots \quad
Block

The series has kk terms:

=T(nk)+2nk3k2=T(nk)+2nk3k2=T(n-k)+2 n \cdot k-3 k-2=T(n-k)+2 n k-3 k-2 \quad
Block
  1. Let k=n1k=n-1 :
    T(n)=T(1)+2(n1)(n1)3(n1)2=3+2(n1)23(n1)2=3+2(n22n+1)3n+32=2n27n+4\begin{gathered} T(n)=T(1)+2(n-1)(n-1)-3(n-1)-2 \\ =3+2(n-1)^2-3(n-1)-2 \\ =3+2\left(n^2-2 n+1\right)-3 n+3-2=2 n^2-7 n+4 \quad \end{gathered}
    Block
  2. Final result:
    T(n)=2n27n+4T(n)=2 n^2-7 n+4
    Block

Thus, T(n)=O(n2)T(n)=O\left(n^2\right).

3. Given expression:

T(1)=1,and for all n2,T(n)=2T(n1)+n1T(1)=1, \text{and for all } n≥2, T(n)=2T(n-1)+n−1 \quad
Block
  1. Let T(n1)=2T(n2)+(n2)T(n-1)=2 T(n-2)+(n-2)
    T(n)=2(2T(n2)+(n2))+(n1)=22T(n2)+2(n2)+n1=22T(n2)+2n4+n1=22T(n2)+3n5\begin{gathered} T(n)=2(2 T(n-2)+(n-2))+(n-1)=2^2 T(n-2)+2(n-2)+n-1 \quad \\ =2^2 T(n-2)+2 n-4+n-1=2^2 T(n-2)+3 n-5 \end{gathered}
    Block
  2. Let T(n2)=2T(n3)+(n3)T(n-2)=2 T(n-3)+(n-3)
    T(n)=22(2T(n3)+(n3))+3n5=23T(n3)+22(n3)+3n5=23T(n3)+4(n3)+3n5=23T(n3)+4n12+3n5=23T(n3)+7n17\begin{gathered} T(n)=2^2(2 T(n-3)+(n-3))+3 n-5 \\ =2^3 T(n-3)+2^2(n-3)+3 n-5=2^3 T(n-3)+4(n-3)+3 n-5 \quad\\ =2^3 T(n-3)+4 n-12+3 n-5=2^3 T(n-3)+7 n-17 \end{gathered}
    Block
  3. Generalizing:
    T(n)=2kT(nk)+j=0k12j(nj1)T(n)=2^k T(n-k)+\sum_{j=0}^{k-1} 2^j \cdot(n-j-1) \quad
    Block

This simplifies to:

T(n)=2kT(nk)+( a summation )T(n)=2^k T(n-k)+(\text { a summation }) \quad
Block
  1. Let k=n1k=n-1 :
    T(n)=2n1T(1)+j=0n22j(nj1)T(n)=2^{n-1} T(1)+\sum_{j=0}^{n-2} 2^j(n-j-1) \quad
    Block

The sum contributes:

=2n11+n(2n11)(2n11)=2n1+n2n12n1+1=(n1)2n1+1=2^{n-1} \cdot 1+n\left(2^{n-1}-1\right)-\left(2^{n-1}-1\right)=2^{n-1}+n \cdot 2^{n-1}-2^{n-1}+1=(n-1) \cdot 2^{n-1}+1 \quad
Block
  1. Final result:
    T(n)=(n1)2n1+1T(n)=(n-1) \cdot 2^{n-1}+1
    Block

Thus, T(n)=O(2n)T(n)=O\left(2^n\right).

4. Given expression:

T(1)=5,and for all n2,T(n)=2T(n1)+3n+1T(1)=5, \text{and for all } n≥2, T(n)=2T(n-1)+3n+1 \quad
Block
  1. Let T(n1)=2T(n2)+3(n1)+1T(n-1)=2 T(n-2)+3(n-1)+1
    T(n)=2(2T(n2)+3(n1)+1)+3n+1=22T(n2)+2(3(n1)+1)+3n+1=22T(n2)+6n6+2+3n+1=22T(n2)+9n3\begin{gathered} T(n)=2(2 T(n-2)+3(n-1)+1)+3 n+1 \\ =2^2 T(n-2)+2(3(n-1)+1)+3 n+1=2^2 T(n-2)+6 n-6+2+3 n+1 \quad \\ =2^2 T(n-2)+9 n-3 \end{gathered}
    Block
  2. Let T(n2)=2T(n3)+3(n2)+1T(n-2)=2 T(n-3)+3(n-2)+1
    T(n)=22(2T(n3)+3(n2)+1)+9n3=23T(n3)+22(3(n2)+1)+9n3=23T(n3)+12n24+4+9n3=23T(n3)+21n23\begin{gathered} T(n)=2^2(2 T(n-3)+3(n-2)+1)+9 n-3 \\ =2^3 T(n-3)+2^2(3(n-2)+1)+9 n-3 \\ =2^3 T(n-3)+12 n-24+4+9 n-3=2^3 T(n-3)+21 n-23 \quad \end{gathered}
    Block
  3. Generalizing:
    T(n)=2kT(nk)+j=0k12j(3(nj)+1)T(n)=2^k T(n-k)+\sum_{j=0}^{k-1} 2^j(3(n-j)+1) \quad
    Block
  4. Let k=n1k=n-1 :
    T(n)=2n1T(1)+j=0n22j(3(nj)+1)T(n)=2^{n-1} T(1)+\sum_{j=0}^{n-2} 2^j(3(n-j)+1) \quad
    Block

Expanding the summation:

=2n15+j=0n2(3n2j3j2j+2j)=2^{n-1} \cdot 5+\sum_{j=0}^{n-2}\left(3 n \cdot 2^j-3 j \cdot 2^j+2^j\right) \quad
Block
  1. Final result: Evaluating the terms yields:
    T(n)=52n1+3n(2n11)+(2n11)=52n1+3n2n13n+2n11T(n)=5 \cdot 2^{n-1}+3 n\left(2^{n-1}-1\right)+\left(2^{n-1}-1\right)=5 \cdot 2^{n-1}+3 n \cdot 2^{n-1}-3 n+2^{n-1}-1 \quad
    Block

Simplifying,

=(5+3n+1)2n13n1=(3n+6)2n13n1=(5+3 n+1) 2^{n-1}-3 n-1=(3 n+6) 2^{n-1}-3 n-1 \quad
Block

Thus, T(n)=O(2n)T(n)=O\left(2^n\right)

5. Given expression:

T(1)=4,and for all n2 a power of 2 ,T(n)=2T(n/2)+3n+2T(1)=4, \text{and for all } n≥2 \text{ a power of 2 }, T(n)=2T(n/2)+3n+2 \quad
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:

T(1)=1,and for all n2 a power of 6 ,T(n)=6T(n/6)+2n+3T(1)=1, \text{and for all } n≥2 \text{ a power of 6 }, T(n)=6T(n/6)+2n+3 \quad
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:

T(1)=3,and for all n2 a power of 6 ,T(n)=6T(n/6)+3n1T(1)=3, \text{and for all } n≥2 \text{ a power of 6 }, T(n)=6T(n/6)+3n−1 \quad
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:

T(1)=3,and for all n2 a power of 3 ,T(n)=4T(n/3)+2n1T(1)=3, \text{and for all } n≥2 \text{ a power of 3 }, T(n)=4T(n/3)+2n−1 \quad
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:

T(1)=4,and for all n2 a power of 2 ,T(n)=3T(n/2)+n22n+1T(1)=4, \text{and for all } n≥2 \text{ a power of 2 }, T(n)=3T(n/2)+n^2−2n+1 \quad
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:

T(1)=1,and for all n2 a power of 2 ,T(n)=3T(n/2)+n2T(1)=1, \text{and for all } n≥2 \text{ a power of 2 }, T(n)=3T(n/2)+n−2 \quad
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