MCS-208 · Unit 1

Analysis of Algorithms

Section 4 of 8

Block 1 · Unit 1Analysis of Algorithms11 min · Section 4 of 8

1.4The Process of Analysis and the RAM Model

Covers IGNOU MCS-208 §1.3

How do we actually find the complexity of a piece of code? We agree on a simple model of a computer, decide what one “step” costs, and then add up the steps statement by statement. This page gives the model and the rules for the common statement types.

Performance versus complexity

An algorithm can use several resources: CPU time (time complexity), memory (space complexity), disk input/output and network bandwidth. Two different questions can be asked about them:

PerformanceComplexity
QuestionHow much time/memory does this program use in this run?How does the requirement grow as the input grows?
Depends onAlgorithm, machine, compiler, inputOnly the algorithm
UnitSeconds, megabytesA growth function such as O(n2)O(n^2)
Measured byRunning and timing the programAnalysing the algorithm

For example, if the working memory is O(n2)O(n^2), doubling nn makes it roughly four times larger for large nn, because (2n)2=4n2(2n)^2 = 4n^2.

The RAM model

Common orders and what doubling n does

OrderNameIf n doubles, time …
O(1)O(1)constantstays the same
O(log⁡n)O(\log n)logarithmicgrows by one step
O(n)O(n)lineardoubles
O(n2)O(n^2)quadraticbecomes 4 times
O(n3)O(n^3)cubicbecomes 8 times

Rules for statements

1. Sequence. The time of statements one after another is the sum of their times. If each is O(1)O(1), a fixed number kk of them is still O(1)O(1).

2. If-then-else. Only one branch runs, but in the worst case it is the slower one:

Pattern: if-then-else
if (condition)        -> O(1) for the test
    sequence 1        -> say O(n^2)
else
    sequence 2        -> say O(1)
worst case = test + slower branch = O(n^2)

3. Simple loop. Time = (number of iterations) × (time of the body). A loop that runs nn times with an O(1)O(1) body is O(n)O(n). How many statements are in the body does not matter, as long as the number is fixed.

4. Nested loops. Work from the inside out and multiply. An outer loop that runs nn times around an inner loop that runs mm times executes the inner body n⋅mn \cdot m times: O(n⋅m)O(n \cdot m), or O(n2)O(n^2) when both run nn times.

Loops that double or halve

Not every loop counts up by 1. If the loop variable is multiplied by 2 each time, it reaches nn very quickly.

Key takeaways

  • Performance is measured on a machine; complexity is a property of the algorithm.
  • In the RAM model every simple operation and memory access costs one step.
  • Sequence: add; if-else: take the worse branch; loop: iterations × body; nested loops: multiply.
  • A doubling or halving loop runs ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 times: O(log⁡n)O(\log n).

Practice questions

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

Question 1 · multiple choice

What does this program print?

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

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

Answer: B. 10

The inner loop runs 0, 1, 2, 3, 4 times for i = 0 to 4, so count = 0 + 1 + 2 + 3 + 4 = 10. 25 would be the count if the inner loop ran n times every time. In general this pattern gives n(n−1)/2=O(n2)n(n-1)/2 = O(n^2).

Question 2 · multiple choice

In the RAM model, how many steps does a single memory access cost?

Show answer

Answer: A. One step, whatever the address

“Random access” means every cell can be reached in one step. Real machines have caches, but the model ignores them.

Question 3 · multiple choice

An if-statement has a then-branch that costs O(n2)O(n^2) and an else-branch that costs O(1)O(1). What is its worst-case cost?

Show answer

Answer: C. O(n2)O(n^2)

In the worst case the slower branch runs. Multiplying the branches (n2⋅1n^2 \cdot 1) is wrong because only one branch executes.

Question 4 · multiple choice

How many times does the body of for (i = 1; i <= 10; i *= 2) run?

Show answer

Answer: B. 4

i takes the values 1, 2, 4, 8; the next value 16 is greater than 10. That is ⌊log⁡210⌋+1=3+1=4\lfloor \log_2 10 \rfloor + 1 = 3 + 1 = 4.

Question 5 · multiple choice

“All algorithms of order O(1) run in the same time.” This statement is:

Show answer

Answer: D. False, because each O(1) algorithm has its own constant

O(1) means the time does not grow with n, but the constant can be 1 step for one algorithm and a million for another. O(1) algorithms are not slow for large n; that option confuses the reason.

Question 6 · short answer

Find the time complexity of for (i = 1; i <= n; i *= 2) j = 1; and justify it.

Show worked answer

The assignment j = 1 is O(1). The loop variable takes the values 1,2,4,…,2k1, 2, 4, \dots, 2^k while 2k≤n2^k \le n, so kk runs from 0 to ⌊log⁡2n⌋\lfloor \log_2 n \rfloor: the body executes ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 times. Total time =(⌊log⁡2n⌋+1)⋅O(1)=O(log⁡n)= (\lfloor \log_2 n \rfloor + 1) \cdot O(1) = O(\log n).

↑ ↓ move↵ openTab switch type