MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 2 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms9 min · Section 2 of 8

3.2Counting Steps: The Summation Algorithm

Covers IGNOU MCS-211 §3.4, 3.4.1

Before simplifying to big-O, it helps to count steps exactly at least once. This page counts every unit of work in a small summation algorithm, checks the count with a program, and then states the general rules for simplifying.

The algorithm

Compute ∑i=1ni3=13+23+⋯+n3\sum_{i=1}^{n} i^3 = 1^3 + 2^3 + \dots + n^3:

Sum of cubes pseudocode
SumOfCubes(n)
1  declare total, i
2  total ← 0
3  for i ← 1 to n do
4      total ← total + i * i * i
5  return total

The cost model

Each basic operation costs 1 unit:

LineWorkUnits
1Declaration (no run-time work)0
2One assignment1
3Initialise i (1) + test i ≤ n (n+1n + 1 times, the last test fails) + increment i (nn times)2n+22n + 2
42 multiplications + 1 addition + 1 assignment, nn times4n4n
5Return1
Total6n+46n + 4
C · sum_cubes.c
#include <stdio.h>

/* Sum of cubes 1^3 + ... + n^3, counting "units" the way the
   step-count model does: assignment 1, each loop test 1,
   each increment 1, body 4 (2 mult + 1 add + 1 assign), return 1. */
long long sum_of_cubes(int n, long *units)
{
    long long total;
    int i;
    *units = 0;

    total = 0;                     *units += 1;   /* line 2           */
    i = 1;                         *units += 1;   /* loop: initialise */
    while (1) {
        *units += 1;                              /* loop: test i<=n  */
        if (!(i <= n))
            break;
        total = total + (long long)i * i * i;     /* line 4           */
        *units += 4;
        i++;                       *units += 1;   /* loop: increment  */
    }
    *units += 1;                                  /* return           */
    return total;
}

int main(void)
{
    int sizes[] = {1, 10, 100};
    for (int t = 0; t < 3; t++) {
        long units;
        int n = sizes[t];
        long long s = sum_of_cubes(n, &units);
        printf("n = %3d: sum = %10lld, units = %4ld, 6n + 4 = %4d\n", n, s, units, 6 * n + 4);
    }
    return 0;
}
Output
n =   1: sum =          1, units =   10, 6n + 4 =   10
n =  10: sum =       3025, units =   64, 6n + 4 =   64
n = 100: sum =   25502500, units =  604, 6n + 4 =  604

The sums agree with the formula (n(n+1)2)2\left(\frac{n(n+1)}{2}\right)^2 from Unit 2: for n=10n = 10, 552=302555^2 = 3025.

From the count to the order

The general rules used in this unit:

  1. Report the worst-case running time unless told otherwise.
  2. Keep only the fastest-growing term (drop lower-order terms).
  3. Drop constant factors.

So T(n)=6n+4T(n) = 6n + 4 becomes Θ(n)\Theta(n). A different cost model (for example, charging 2 units for a multiplication) changes the constants — perhaps 8n+58n + 5 — but never the order Θ(n)\Theta(n). This is why big-O is the right level of detail: it does not depend on such choices.

Key takeaways

  • Line-by-line counting gives an exact T(n)T(n), e.g. 6n+46n + 4 for the sum of cubes.
  • A loop of nn iterations has n+1n + 1 tests and nn increments.
  • Drop lower-order terms and constants: 6n+4=Θ(n)6n + 4 = \Theta(n).
  • The order of growth does not depend on the exact unit costs.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In the sum-of-cubes algorithm, how many times is the test i <= n evaluated?

Show answer

Answer: B. n+1n + 1

It is true n times and false once at the end, n + 1 tests in total.

Question 2 · multiple choice

What is T(5)T(5) for T(n)=6n+4T(n) = 6n + 4?

Show answer

Answer: C. 34

6 × 5 + 4 = 34. 64 is T(10).

Question 3 · multiple choice

If each multiplication cost 2 units instead of 1, the sum-of-cubes count would become 8n+48n + 4. Its order is:

Show answer

Answer: D. Θ(n)\Theta(n)

Constant factors are dropped, so 8n + 4 = Θ(n), the same class as 6n + 4.

Question 4 · multiple choice

A loop body with 3 units runs nn times, plus a loop control of 2n+22n + 2 and 2 other units. T(n)T(n) is:

Show answer

Answer: A. 5n+45n + 4

3n + (2n + 2) + 2 = 5n + 4.

Question 5 · multiple choice

What is ∑i=14i3\sum_{i=1}^{4} i^3?

Show answer

Answer: D. 100

1 + 8 + 27 + 64 = 100 = (4 × 5 / 2)² = 10². 30 is the sum of squares.

Question 6 · short answer

Count the units of p ← 1; for i ← 1 to n do p ← p * 2; return p (assignment, test, increment, multiplication and return cost 1 unit each) and give the order.

Show worked answer

p ← 1: 1. Loop control: initialise 1 + tests (n + 1) + increments n = 2n + 2. Body: 1 multiplication + 1 assignment = 2 units, n times = 2n. Return: 1. Total T(n) = 1 + 2n + 2 + 2n + 1 = 4n + 4 = Θ(n).

↑ ↓ move↵ openTab switch type