MCS-208 · Unit 1

Analysis of Algorithms

Section 5 of 8

Block 1 · Unit 1Analysis of Algorithms7 min · Section 5 of 8

1.5Exact Step Counting: the psum Function

Covers IGNOU MCS-208 §1.3

The rules on the previous page give the order quickly. Sometimes, though, an exam asks for an exact count first. This page counts every step of a small function, then shows how the exact count turns into Big-O, Ω and Θ.

The function

The function psum returns 12+22+⋯+n21^2 + 2^2 + \dots + n^2. With the lines numbered:

psum with line numbers
long psum(long n)
{
    long i, partial_sum;
    partial_sum = 0;                        /* Line 1 */
    for (i = 1; i <= n; i++)                /* Line 2 */
        partial_sum = partial_sum + i * i;  /* Line 3 */
    return partial_sum;                     /* Line 4 */
}

Counting line by line

Count each simple operation as one step (declarations cost nothing):

LineOperationsTimes executedSteps
1one assignment11
2initialise i = 111
2test i <= nn+1n + 1 (true nn times, false once)n+1n + 1
2increment i++nnnn
3multiply, add, assignnn3n3n
4return11
Total5n+45n + 4

From the exact count to O, Ω and Θ

Let T(n)=5n+4T(n) = 5n + 4.

  • Big-O. Choose c=6c = 6: 5n+4≤6n5n + 4 \le 6n when 4≤n4 \le n. So T(n)=O(n)T(n) = O(n) with c=6c = 6, n0=4n_0 = 4.
  • Omega. Choose c=5c = 5: 5n≤5n+45n \le 5n + 4 for every n≥1n \ge 1. So T(n)=Ω(n)T(n) = \Omega(n) with c=5c = 5, n0=1n_0 = 1.
  • Theta. Both hold, so T(n)=Θ(n)T(n) = \Theta(n) with c1=5c_1 = 5, c2=6c_2 = 6, n0=4n_0 = 4.

Big-O is only an upper bound, so T(n)T(n) is also O(nlog⁡n)O(n \log n) and O(n2)O(n^2), but O(n)O(n) is the tight, useful answer.

This is why, in practice, you may count each simple statement as one step without worrying whether it is really 1, 3 or 4 operations: any fixed number is a constant factor, and Θ ignores constant factors.

Key takeaways

  • A for loop running nn times costs 1 (init) + (n+1)(n + 1) (tests) + nn (increments), plus the body nn times.
  • psum performs exactly 5n+45n + 4 basic operations (3n+43n + 4 statements).
  • Any linear count an+ban + b with a>0a > 0 is Θ(n)\Theta(n).
  • Choose constants explicitly: 5n+4≤6n5n + 4 \le 6n for n≥4n \ge 4.

Practice questions

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

Question 1 · multiple choice

In for (i = 1; i <= n; i++), how many times is the test i <= n evaluated?

Show answer

Answer: C. n+1n + 1

It is true for i=1,…,ni = 1, \dots, n (nn times) and false once for i=n+1i = n + 1. Answering nn forgets the final test that stops the loop.

Question 2 · multiple choice

A loop runs nn times and its body has 2 basic operations. Counting initialise, tests, increments and body, how many operations does the loop perform?

Show answer

Answer: B. 4n+24n + 2

1+(n+1)+n+2n=4n+21 + (n + 1) + n + 2n = 4n + 2. The answer 2n+22n + 2 counts only the loop control and forgets the body.

Question 3 · multiple choice

Which constants prove 5n+4=O(n)5n + 4 = O(n)?

Show answer

Answer: D. c=6c = 6, n0=4n_0 = 4

5n+4≤6n5n + 4 \le 6n exactly when n≥4n \ge 4. With c=5c = 5 it never holds (5n+4>5n5n + 4 > 5n), and with n0=1n_0 = 1 the check fails at n=1n = 1 (9>69 > 6).

Question 4 · multiple choice

What does this program print?

C · q-step3.c
#include <stdio.h>

int main(void)
{
    int n = 10, count = 0;
    for (int i = 0; i < n; i += 3)
        count++;
    printf("%d\n", count);
    return 0;
}
Show answer

Answer: B. 4

i takes 0, 3, 6, 9 (four values); 12 is not less than 10. In general the loop runs ⌈n/3⌉\lceil n/3 \rceil times, which is still O(n)O(n).

Question 5 · multiple choice

Why may we count each simple statement as one step when finding the order?

Show answer

Answer: B. Because any fixed number of operations is a constant factor, which O, Ω and Θ ignore

A statement with 3 or 4 operations changes the count from 3n+43n + 4 to 5n+45n + 4, but not the order Θ(n)\Theta(n). Statements do not really take equal time.

Question 6 · short answer

Prove that if f(n)=n2+2n+5f(n) = n^2 + 2n + 5 and g(n)=n2g(n) = n^2, then f(n)=O(g(n))f(n) = O(g(n)) (Check Your Progress 2, question 4).

Show worked answer

We need cc and n0n_0 with n2+2n+5≤c n2n^2 + 2n + 5 \le c\,n^2 for all n≥n0n \ge n_0. For n≥1n \ge 1: 2n≤2n22n \le 2n^2 and 5≤5n25 \le 5n^2. So n2+2n+5≤n2+2n2+5n2=8n2n^2 + 2n + 5 \le n^2 + 2n^2 + 5n^2 = 8n^2. Hence c=8c = 8, n0=1n_0 = 1 works, and f(n)=O(n2)f(n) = O(n^2). (Another valid choice: c=2c = 2, n0=4n_0 = 4, since 2n+5≤n22n + 5 \le n^2 for n≥4n \ge 4: at n=4n = 4, 13≤1613 \le 16.)

↑ ↓ move↵ openTab switch type