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 1 2 + 2 2 + ⋯ + n 2 1^2 + 2^2 + \dots + n^2 1 2 + 2 2 + ⋯ + n 2 . With the lines numbered:
psum with line numbers Copy 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
Intuition
A for loop hides three jobs: it sets i once, tests i <= n before every iteration (plus one last failing test), and increments i after every iteration. Each job has to be counted separately, just like counting how many times you check the clock, not only how many tasks you do.
Count each simple operation as one step (declarations cost nothing):
Line Operations Times executed Steps 1 one assignment 1 1 2 initialise i = 1 1 1 2 test i <= n n + 1 n + 1 n + 1 (true n n n times, false once)n + 1 n + 1 n + 1 2 increment i++ n n n n n n 3 multiply, add, assign n n n 3 n 3n 3 n 4 return 1 1 Total 5 n + 4 5n + 4 5 n + 4
Example 1.20 Verify the count 5n + 4 by running the code
Goal. Check that psum performs exactly 5 n + 4 5n + 4 5 n + 4 basic operations.
Add a counter. Write the for loop as a while loop so the initialise, test and increment can each be counted, and add 1, 3 or 1 to a counter exactly where the table says.
C · psum_count.c Copy #include <stdio.h>
static long ops = 0 ; /* counts the basic operations performed */
/* psum(n) = 1*1 + 2*2 + ... + n*n, with every basic operation counted.
The for loop is written out as a while loop so each hidden step
(initialise, test, increment) can be counted separately. */
long psum( long n)
{
long i, partial_sum;
partial_sum = 0 ; ops += 1 ; /* Line 1: assignment */
i = 1 ; ops += 1 ; /* Line 2: initialise i */
while ( 1 ) {
ops += 1 ; /* Line 2: test i <= n */
if ( ! (i <= n))
break ;
partial_sum = partial_sum + i * i;
ops += 3 ; /* Line 3: multiply, add, assign */
i ++ ; ops += 1 ; /* Line 2: increment i */
}
ops += 1 ; /* Line 4: return */
return partial_sum;
}
int main( void )
{
long sizes [] = { 1 , 5 , 10 , 100 };
printf( " n psum(n) operations 5n + 4 \n " );
for ( int k = 0 ; k < 4 ; k ++ ) {
long n = sizes[k];
ops = 0 ;
long result = psum(n);
printf( "%4ld %12ld %12ld %8ld \n " , n, result, ops, 5 * n + 4 );
}
return 0 ;
}
Run it.
Output Copy n psum(n) operations 5n + 4
1 1 9 9
5 55 29 29
10 385 54 54
100 338350 504 504
Result. The counted operations equal 5 n + 4 5n + 4 5 n + 4 for every n n n tried, and p s u m ( 10 ) = 385 = 1 + 4 + 9 + ⋯ + 100 psum(10) = 385 = 1 + 4 + 9 + \dots + 100 p s u m ( 10 ) = 385 = 1 + 4 + 9 + ⋯ + 100 .
Correction · Step count of psum
Study material says: §1.3 counts the loop of line 2 as 2 n + 2 2n + 2 2 n + 2 statements, but then adds “1 + (n+1) + n + 1 = 2n + 3” as the total for the whole function, without counting line 3 at all.
Correct version: Line 3 runs n n n times and must be counted. Counting each executed statement as one step gives 1 + ( 2 n + 2 ) + n + 1 = 3 n + 4 1 + (2n + 2) + n + 1 = 3n + 4 1 + ( 2 n + 2 ) + n + 1 = 3 n + 4 . Counting each basic operation (line 3 has a multiplication, an addition and an assignment) gives 5 n + 4 5n + 4 5 n + 4 . Both are Θ ( n ) \Theta(n) Θ ( n ) , so the final answer O ( n ) O(n) O ( n ) is still right.
Why: Line 3 is the loop body; it is executed once per iteration. Weiss, Data Structures and Algorithm Analysis in C++ , 4th ed., §2.4.1, counts the same kind of function the same way (body: 4 units × N; loop control: 2N + 2; lines 1 and 4: 1 each). The count 5 n + 4 5n + 4 5 n + 4 was confirmed by the instrumented program above.
From the exact count to O, Ω and Θ
Let T ( n ) = 5 n + 4 T(n) = 5n + 4 T ( n ) = 5 n + 4 .
Big-O. Choose c = 6 c = 6 c = 6 : 5 n + 4 ≤ 6 n 5n + 4 \le 6n 5 n + 4 ≤ 6 n when 4 ≤ n 4 \le n 4 ≤ n . So T ( n ) = O ( n ) T(n) = O(n) T ( n ) = O ( n ) with c = 6 c = 6 c = 6 , n 0 = 4 n_0 = 4 n 0 = 4 .
Omega. Choose c = 5 c = 5 c = 5 : 5 n ≤ 5 n + 4 5n \le 5n + 4 5 n ≤ 5 n + 4 for every n ≥ 1 n \ge 1 n ≥ 1 . So T ( n ) = Ω ( n ) T(n) = \Omega(n) T ( n ) = Ω ( n ) with c = 5 c = 5 c = 5 , n 0 = 1 n_0 = 1 n 0 = 1 .
Theta. Both hold, so T ( n ) = Θ ( n ) T(n) = \Theta(n) T ( n ) = Θ ( n ) with c 1 = 5 c_1 = 5 c 1 = 5 , c 2 = 6 c_2 = 6 c 2 = 6 , n 0 = 4 n_0 = 4 n 0 = 4 .
Big-O is only an upper bound, so T ( n ) T(n) T ( n ) is also O ( n log n ) O(n \log n) O ( n log n ) and O ( n 2 ) O(n^2) O ( n 2 ) , but O ( n ) O(n) O ( n ) is the tight, useful answer.
Example 1.21 Constants do not change the order: 4n + 6
Goal. Show that f ( n ) = 4 n + 6 f(n) = 4n + 6 f ( n ) = 4 n + 6 has the same order as 5 n + 4 5n + 4 5 n + 4 .
Upper bound. 4 n + 6 ≤ 5 n 4n + 6 \le 5n 4 n + 6 ≤ 5 n when 6 ≤ n 6 \le n 6 ≤ n : c = 5 c = 5 c = 5 , n 0 = 6 n_0 = 6 n 0 = 6 , so f ( n ) = O ( n ) f(n) = O(n) f ( n ) = O ( n ) .
Lower bound. 4 n ≤ 4 n + 6 4n \le 4n + 6 4 n ≤ 4 n + 6 for all n ≥ 1 n \ge 1 n ≥ 1 : c = 4 c = 4 c = 4 , so f ( n ) = Ω ( n ) f(n) = \Omega(n) f ( n ) = Ω ( n ) .
Combine. Both bounds hold from n 0 = 6 n_0 = 6 n 0 = 6 .
Result. 4 n + 6 = Θ ( n ) 4n + 6 = \Theta(n) 4 n + 6 = Θ ( n ) , the same order as 5 n + 4 5n + 4 5 n + 4 and 3 n + 4 3n + 4 3 n + 4 .
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.
Note
The same counting method works for while loops: count the tests (iterations + 1), the body (iterations) and anything before and after the loop.
Key idea
An exact step count such as 5 n + 4 5n + 4 5 n + 4 becomes an order by choosing constants; the coefficients (5, 4) only affect c c c and n 0 n_0 n 0 , never the order Θ ( n ) \Theta(n) Θ ( n ) .
TEE Exam note
For “count the number of steps / statements executed” questions, draw the table (line, cost, times executed), remember the loop test runs n + 1 n + 1 n + 1 times, add the column, then state O, Ω and Θ with explicit c c c and n 0 n_0 n 0 . If the question does not fix the counting convention, say which one you use (statements or operations).
Common mistake
Counting the loop test only n n n times. The test runs once more than the body: the final, failing test is what ends the loop.
Common mistake
Giving c c c without n 0 n_0 n 0 . "5 n + 4 ≤ 6 n 5n + 4 \le 6n 5 n + 4 ≤ 6 n " is false for n = 1 , 2 , 3 n = 1, 2, 3 n = 1 , 2 , 3 ; you must state that it holds from n 0 = 4 n_0 = 4 n 0 = 4 .
Key takeaways
A for loop running n n n times costs 1 (init) + ( n + 1 ) (n + 1) ( n + 1 ) (tests) + n n n (increments), plus the body n n n times.
psum performs exactly 5 n + 4 5n + 4 5 n + 4 basic operations (3 n + 4 3n + 4 3 n + 4 statements).
Any linear count a n + b an + b an + b with a > 0 a > 0 a > 0 is Θ ( n ) \Theta(n) Θ ( n ) .
Choose constants explicitly: 5 n + 4 ≤ 6 n 5n + 4 \le 6n 5 n + 4 ≤ 6 n for n ≥ 4 n \ge 4 n ≥ 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?
Check answer
Show answer Answer: C. n + 1 n + 1 n + 1
It is true for i = 1 , … , n i = 1, \dots, n i = 1 , … , n (n n n times) and false once for i = n + 1 i = n + 1 i = n + 1 . Answering n n n forgets the final test that stops the loop.
Question 2 · multiple choice A loop runs n n n times and its body has 2 basic operations. Counting initialise, tests, increments and body, how many operations does the loop perform?
Check answer
Show answer Answer: B. 4 n + 2 4n + 2 4 n + 2
1 + ( n + 1 ) + n + 2 n = 4 n + 2 1 + (n + 1) + n + 2n = 4n + 2 1 + ( n + 1 ) + n + 2 n = 4 n + 2 . The answer 2 n + 2 2n + 2 2 n + 2 counts only the loop control and forgets the body.
Question 3 · multiple choice Which constants prove 5 n + 4 = O ( n ) 5n + 4 = O(n) 5 n + 4 = O ( n ) ?
Check answer
Show answer Answer: D. c = 6 c = 6 c = 6 , n 0 = 4 n_0 = 4 n 0 = 4
5 n + 4 ≤ 6 n 5n + 4 \le 6n 5 n + 4 ≤ 6 n exactly when n ≥ 4 n \ge 4 n ≥ 4 . With c = 5 c = 5 c = 5 it never holds (5 n + 4 > 5 n 5n + 4 > 5n 5 n + 4 > 5 n ), and with n 0 = 1 n_0 = 1 n 0 = 1 the check fails at n = 1 n = 1 n = 1 (9 > 6 9 > 6 9 > 6 ).
Question 4 · multiple choice What does this program print?
C · q-step3.c Copy #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 ;
} A 3 B 4 C 10 D 30
Check answer
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 ⌈ n /3 ⌉ times, which is still O ( n ) O(n) O ( n ) .
Question 5 · multiple choice Why may we count each simple statement as one step when finding the order?
A Because every statement really takes the same time B Because any fixed number of operations is a constant factor, which O, Ω and Θ ignore C Because the RAM model forbids statements with several operations D Because only loops are counted
Check answer
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 3 n + 4 3n + 4 3 n + 4 to 5 n + 4 5n + 4 5 n + 4 , but not the order Θ ( n ) \Theta(n) Θ ( n ) . Statements do not really take equal time.
Question 6 · short answer
Prove that if f ( n ) = n 2 + 2 n + 5 f(n) = n^2 + 2n + 5 f ( n ) = n 2 + 2 n + 5 and g ( n ) = n 2 g(n) = n^2 g ( n ) = n 2 , then f ( n ) = O ( g ( n ) ) f(n) = O(g(n)) f ( n ) = O ( g ( n )) (Check Your Progress 2, question 4).
Show worked answer We need c c c and n 0 n_0 n 0 with n 2 + 2 n + 5 ≤ c n 2 n^2 + 2n + 5 \le c\,n^2 n 2 + 2 n + 5 ≤ c n 2 for all n ≥ n 0 n \ge n_0 n ≥ n 0 . For n ≥ 1 n \ge 1 n ≥ 1 : 2 n ≤ 2 n 2 2n \le 2n^2 2 n ≤ 2 n 2 and 5 ≤ 5 n 2 5 \le 5n^2 5 ≤ 5 n 2 . So n 2 + 2 n + 5 ≤ n 2 + 2 n 2 + 5 n 2 = 8 n 2 n^2 + 2n + 5 \le n^2 + 2n^2 + 5n^2 = 8n^2 n 2 + 2 n + 5 ≤ n 2 + 2 n 2 + 5 n 2 = 8 n 2 . Hence c = 8 c = 8 c = 8 , n 0 = 1 n_0 = 1 n 0 = 1 works, and f ( n ) = O ( n 2 ) f(n) = O(n^2) f ( n ) = O ( n 2 ) . (Another valid choice: c = 2 c = 2 c = 2 , n 0 = 4 n_0 = 4 n 0 = 4 , since 2 n + 5 ≤ n 2 2n + 5 \le n^2 2 n + 5 ≤ n 2 for n ≥ 4 n \ge 4 n ≥ 4 : at n = 4 n = 4 n = 4 , 13 ≤ 16 13 \le 16 13 ≤ 16 .)