MCS-208 · Unit 1

Analysis of Algorithms

Section 8 of 8

Block 1 · Unit 1Analysis of Algorithms10 min · Section 8 of 8

1.8Calculating Time Complexity: the Complexity Ladder

Covers IGNOU MCS-208 §1.5

This last page applies the tools to well-known algorithms and puts them on one “ladder” of growth rates. The tables show, with real numbers, why the step from polynomial to exponential is so dramatic.

Constant-time code

Statements such as x = 4 * y + 3;, z = z + 1; and p = 1; work on single variables. Each takes a fixed number of steps, whatever the values stored, so each is O(1)O(1), and a fixed sequence of them is also O(1)O(1). Accessing A[i] in an array is also O(1)O(1): the address is calculated directly from i.

Binary search: O(log n)

After each comparison the range shrinks from nn to at most n/2n/2: the sizes go n,n/2,n/4,…,1n, n/2, n/4, \dots, 1. After kk halvings the size is about n/2kn / 2^k, which reaches 1 when k=log⁡2nk = \log_2 n. Counting the comparison on the final range of size 1, the worst case is ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 comparisons: O(log⁡n)O(\log n).

The travelling salesman problem

The brute-force method tries every tour. How many are there? Fix the starting city (a tour is a cycle, so where it starts does not matter). The remaining n−1n - 1 cities can be ordered in (n−1)!(n - 1)! ways, and each tour is counted twice (once in each direction) when distances are the same both ways. So there are (n−1)!2\frac{(n-1)!}{2} different tours.

No algorithm is known that solves TSP exactly in polynomial time (it is NP-hard), so in practice people use heuristics that find good, though not always optimal, tours.

The complexity ladder

OrderNameTypical example
O(1)O(1)constantAccess A[i] in an array
O(log⁡n)O(\log n)logarithmicBinary search
O(n)O(n)linearLoop over all elements of an array (linear search)
O(nlog⁡n)O(n \log n)linearithmicMerge sort
O(n2)O(n^2)quadraticInsertion sort (worst case), selection sort
O(n3)O(n^3)cubicMultiplying two n×nn \times n matrices the standard way
O(nk)O(n^k)polynomialAny fixed power kk
O(cn)O(c^n), c>1c > 1exponentialTrying all 2n2^n subsets of nn items
O(n!)O(n!)factorialBrute-force TSP, trying all orderings

Two more facts from the study material are worth knowing:

  • Any sorting algorithm that only compares elements needs Ω(nlog⁡n)\Omega(n \log n) comparisons in the worst case, so merge sort is optimal among comparison sorts.
  • Exponential growth is the dangerous one: such algorithms are usable only for very small inputs.

Key takeaways

  • A fixed number of simple statements is O(1)O(1).
  • Binary search halves the range each step: at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 comparisons, O(log⁡n)O(\log n).
  • TSP has (n−1)!2\frac{(n-1)!}{2} different tours; brute force is factorial and no polynomial exact algorithm is known.
  • Ladder: 1<log⁡n<n<nlog⁡n<n2<n3<2n<n!1 < \log n < n < n\log n < n^2 < n^3 < 2^n < n!; standard matrix multiplication is O(n3)O(n^3).

Practice questions

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

Question 1 · multiple choice

In the worst case, how many comparisons does binary search need for a sorted array of 1000 elements?

Show answer

Answer: B. 10

⌊log⁡21000⌋+1=9+1=10\lfloor \log_2 1000 \rfloor + 1 = 9 + 1 = 10 (since 29=512≤1000<10242^9 = 512 \le 1000 < 1024). 500 would be an average for linear search, not binary search.

Question 2 · multiple choice

How many different tours are there for a TSP with 5 cities (symmetric distances)?

Show answer

Answer: A. 12

(5−1)!2=242=12\frac{(5-1)!}{2} = \frac{24}{2} = 12. 120 is 5!5!, which counts each tour 10 times (5 starting cities × 2 directions).

Question 3 · multiple choice

An O(n2)O(n^2) algorithm takes 4 seconds for n=1000n = 1000. About how long will it take for n=2000n = 2000?

Show answer

Answer: C. 16 seconds

Doubling nn multiplies n2n^2 by 4: 4×4=164 \times 4 = 16 seconds. 8 seconds would be the answer for a linear algorithm.

Question 4 · multiple choice

What does this program print?

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

int main(void)
{
    int count = 0;
    for (int i = 100; i > 0; i /= 2)
        count++;
    printf("%d\n", count);
    return 0;
}
Show answer

Answer: D. 7

With integer division i takes 100, 50, 25, 12, 6, 3, 1 (seven values), then becomes 0. This is ⌊log⁡2100⌋+1=6+1=7\lfloor \log_2 100 \rfloor + 1 = 6 + 1 = 7: a halving loop is O(log⁡n)O(\log n).

Question 5 · multiple choice

What is the time complexity of the standard algorithm for multiplying two n×nn \times n matrices?

Show answer

Answer: D. O(n3)O(n^3)

There are n2n^2 result entries and each needs nn multiplications. O(n2)O(n^2) is only the size of the output, not the work.

Question 6 · short answer

Arrange in increasing order of growth and name each: n!n!, nlog⁡nn \log n, 11, 2n2^n, n2n^2, log⁡n\log n, nn.

Show worked answer

11 (constant) < log⁡n\log n (logarithmic) < nn (linear) < nlog⁡nn \log n (linearithmic) < n2n^2 (quadratic) < 2n2^n (exponential) < n!n! (factorial).

↑ ↓ move↵ openTab switch type