PROJECTENGRAM

COMPLEXITY ANALYSIS

Tags: #◇Computer Science #◇CSALGCM #◇Algorithms and Complexity #◇Complexity Analysis


Complexity Analysis

◇Overview

To determine if an algorithm is practical for large-scale data, we must evaluate its efficiency mathematically. Complexity Analysis involves defining the theoretical limits of an algorithm's performance in terms of time and space constraints. By using Asymptotic Analysis, we can ignore machine-specific constants and lower-order terms to focus on the function's underlying order of growth relative to the input size nn.

◇Key Concepts

Asymptotic Analysis

Asymptotic analysis evaluates the performance of an algorithm as the input size nn approaches infinity. It characterizes how fast the computational requirements grow.

Loose Bounds vs. Tight Bounds

Bounds in asymptotic notation can be categorized into loose and tight bounds. Loose bounds might overestimate or underestimate the growth significantly but are mathematically valid, while tight bounds offer a strict definition of exact growth.

Loose Bounds:

  • Big-Oh (OO): Represents an upper bound. f(n)=O(g(n))f(n) = O(g(n)) implies that f(n)f(n) grows at worst as fast as g(n)g(n).
    Formal Definition: There exist constants c>0c > 0 and n0≥1n_0 \ge 1 such that 0≤f(n)≤cg(n)0 \le f(n) \le cg(n) for all n≥n0n \ge n_0.
  • Big-Omega (Ω\Omega): Represents a lower bound. f(n)=Ω(g(n))f(n) = \Omega(g(n)) implies that f(n)f(n) grows at least as fast as g(n)g(n).
    Formal Definition: There exist constants c>0c > 0 and n0≥1n_0 \ge 1 such that 0≤cg(n)≤f(n)0 \le cg(n) \le f(n) for all n≥n0n \ge n_0.
  • Big-Theta (Θ\Theta): Represents a tight bound (both upper and lower). f(n)=Θ(g(n))f(n) = \Theta(g(n)) means f(n)f(n) grows exactly at the same rate as g(n)g(n).
    Formal Definition: There exist constants c1,c2>0c_1, c_2 > 0 and n0≥1n_0 \ge 1 such that 0≤c1g(n)≤f(n)≤c2g(n)0 \le c_1g(n) \le f(n) \le c_2g(n) for all n≥n0n \ge n_0.

Strict/Tight Bounds:

  • Little-Oh (oo): A strictly upper bound. f(n)=o(g(n))f(n) = o(g(n)) means f(n)f(n) grows strictly slower than g(n)g(n).
    Formal Definition: For all constants c>0c > 0, there exists an n0≥1n_0 \ge 1 such that 0≤f(n)<cg(n)0 \le f(n) < cg(n) for all n≥n0n \ge n_0.
  • Little-Omega (ω\omega): A strictly lower bound. f(n)=ω(g(n))f(n) = \omega(g(n)) means f(n)f(n) grows strictly faster than g(n)g(n).
    Formal Definition: For all constants c>0c > 0, there exists an n0≥1n_0 \ge 1 such that 0≤cg(n)<f(n)0 \le cg(n) < f(n) for all n≥n0n \ge n_0.

Limit Definitions for Asymptotic Bounds

We can use mathematical limits as n→∞n \to \infty of the ratio f(n)/g(n)f(n)/g(n) to quickly find asymptotic relationships:

  • If lim⁡n→∞f(n)g(n)=0\lim_{n \to \infty} \frac{f(n)}{g(n)} = 0, then f(n)=o(g(n))f(n) = o(g(n)) (strictly slower).
  • If lim⁡n→∞f(n)g(n)=∞\lim_{n \to \infty} \frac{f(n)}{g(n)} = \infty, then f(n)=ω(g(n))f(n) = \omega(g(n)) (strictly faster).
  • If lim⁡n→∞f(n)g(n)=c\lim_{n \to \infty} \frac{f(n)}{g(n)} = c (0<c<∞0 < c < \infty), then f(n)=Θ(g(n))f(n) = \Theta(g(n)) (same rate).

◇Algorithms & Formulas

Orders of Growth

Algorithm complexities generally fall into these common classes (ordered from most to least efficient):

  1. O(1)O(1): Constant time. The operation doesn't depend on nn.
  2. O(log⁡n)O(\log n): Logarithmic time. The search space is divided by a constant factor in each step (e.g., Binary Search).
  3. O(n)O(n): Linear time. Processing each element once.
  4. O(nlog⁡n)O(n \log n): Linearithmic time. Typical of optimal comparison-based sorts (e.g., Merge Sort).
  5. O(n2)O(n^2): Quadratic time. Typical of nested loops iterating over the data (e.g., Bubble Sort).
  6. O(n3)O(n^3): Cubic time. Three nested loops (e.g., naive Matrix Multiplication).
  7. O(2n)O(2^n): Exponential time. Trying all subsets.
  8. O(n!)O(n!): Factorial time. Trying all permutations.

Asymptotic Bound Enclosures

◇Code Examples

1. Constant Time Geometry

『』
C
float b, h, area;
scanf("%f %f", &b, &h);
area = b * h / 2;
printf("Area = %f\n", area);

Analysis: There are no loops or recursive calls. Execution takes a constant number of steps.
Complexity: O(1)O(1)

2. Linear Search / Summation

『』
C
int sum = 0, n;
scanf("%d", &n);
for(int i = 0; i <= n; i++){
    sum += i;
}
printf("sum = %d\n", sum);

Analysis: The loop runs exactly n+1n+1 times, performing a constant amount of work each time.
Complexity: Θ(n)\Theta(n)

3. Early Exit / Worst Case Linearity

『』
C
bool isComposite = false;
int n;
scanf("%d", &n);
if(n < 2) {
    printf("Neither prime nor composite");
} else {
    for(int i = 2; !isComposite && i < n; i++) {
        if(n % i == 0){
            isComposite = true;
        }
    }
    printf("%s\n", isComposite ? "Composite" : "Prime");
}

Analysis: In the best case (e.g., nn is even), the loop terminates at i=2i=2, taking O(1)O(1) time. In the worst case (e.g., nn is a prime number), the loop runs n−2n-2 times. Therefore, the worst-case time complexity is bounded by O(n)O(n).

4. Quadratic Nested Loops

『』
C
int sum = 0, n;
scanf("%d", &n);
for(int i = 1; i < n; i++) {
    for(int j = 1; j < n; j++) {
        sum += i * j;
    }
}

Analysis: Both the outer and inner loops iterate n−1n-1 times independently. Total iterations: (n−1)×(n−1)=n2−2n+1(n-1) \times (n-1) = n^2 - 2n + 1.
Complexity: Θ(n2)\Theta(n^2)

5. Dependent Nested Loops

『』
C
int sum = 0, n;
scanf("%d", &n);
for(int i = 1; i < n; i++) {
    for(int j = 1; j <= i; j++) {
        sum += i * j;
    }
}

Analysis: The inner loop limit depends on i. The total iterations are ∑i=1n−1i=(n−1)n2=n2−n2\sum_{i=1}^{n-1} i = \frac{(n-1)n}{2} = \frac{n^2 - n}{2}. Despite the constant factor of 1/21/2, the highest order term is still quadratic.
Complexity: Θ(n2)\Theta(n^2)

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

What asymptotic notation represents a strictly upper bound?

Q2

If lim⁡n→∞f(n)g(n)=∞\lim_{n \to \infty} \frac{f(n)}{g(n)} = \infty, which of the following is true?

Q3

An algorithm iterates over an array of size nn, and for each element, it performs a binary search on another array of size nn. What is the time complexity?

Q4

Which of the following correctly describes Big-Theta (Θ\Theta) notation?

Q5

In the limit definition, if lim⁡n→∞f(n)g(n)=5\lim_{n \to \infty} \frac{f(n)}{g(n)} = 5, what can we conclude?

Q6

Consider the code snippet:

『』
C
for(int i = 1; i <= n; i *= 2) {
    printf("%d", i);
}

What is the time complexity?

Q7

Rank the following complexities from most efficient to least efficient: O(n2)O(n^2), O(nlog⁡n)O(n \log n), O(2n)O(2^n), O(log⁡n)O(\log n).

Q8

What is the complexity of evaluating a mathematical formula like the area of a triangle (b×h/2b \times h / 2)?

Q9

If an algorithm takes time proportional to n!n!, which bound describes it?

Q10

Which condition must hold true for f(n)=O(g(n))f(n) = O(g(n)) to be correct?