MCS-208 · Unit 1

Analysis of Algorithms

Section 1 of 8

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

1.1Algorithms and Complexity

Covers IGNOU MCS-208 §1.0, 1.2

Before we can analyse an algorithm, we must be clear about what an algorithm is and what we want to measure. This page defines both ideas and introduces the vocabulary used in the rest of the unit.

What an algorithm is

The five properties are usually listed like this (the names follow Donald Knuth’s classic list):

PropertyMeaningExample of breaking it
InputZero or more values are given before the algorithm starts— (zero inputs is allowed, e.g. “print the first 10 primes”)
OutputAt least one result is producedA procedure that calculates but never reports anything
DefinitenessEach step has exactly one meaning“Add a few numbers”
EffectivenessEach step is basic enough to be done exactly, in finite time“Write down the exact decimal value of π”
Termination (finiteness)It stops after a finite number of steps for every valid inputA loop whose condition never becomes false

An algorithm is an idea; a program is that idea written in a particular programming language (C, Python, Java) so a computer can run it. One algorithm can have many programs.

What complexity measures

To analyse an algorithm means to predict how much of a resource it needs. The two main resources are time (number of steps) and space (amount of memory).

The input size nn is whatever measures “how big” the input is: the number of elements in an array, the number of digits in a number, the number of cities on a map.

Why not just measure seconds? Because seconds depend on the processor, the compiler, the operating system and even other programs running at the same time. The same program can take 2 seconds on an old laptop and 0.2 seconds on a new one. What does not change is the shape of the growth: if one algorithm’s time grows like nn and another’s grows like n2n^2, then for large inputs the first wins on every machine. This “look only at the growth for large nn” approach is called asymptotic analysis.

Two related fields are easy to mix up:

Complexity theoryComputability theory
Main questionHow much time/memory does it take to solve the problem?Can the problem be solved by any algorithm at all?
Example resultSorting by comparisons needs about nlog⁡nn \log n comparisons in the worst caseNo algorithm can decide, for every program, whether it halts

Also note: we analyse the complexity of an algorithm, not of a problem. The problem “sort an array” can be solved by bubble sort (time grows like n2n^2) or merge sort (time grows like nlog⁡nn \log n). Same problem, different algorithms, different complexity.

Complexity classes P and NP

A decision problem is a problem whose answer is only “yes” or “no”, for example “Is there a route through all these cities of length at most 500 km?”

  • P is the class of decision problems that a normal (deterministic) algorithm can solve in polynomial time, that is, in at most c⋅nkc \cdot n^k steps for some constants cc and kk. Example: “Is this array sorted?”
  • NP is the class of decision problems for which a proposed “yes” answer (a certificate) can be checked in polynomial time. Equivalently, a non-deterministic machine can solve them in polynomial time. Examples: Boolean satisfiability (SAT), Hamiltonian path, vertex cover.

Every problem in P is also in NP (if you can solve it fast, you can check an answer fast). Whether P equals NP is the most famous open question in computer science.

The space–time trade-off

Sometimes you can make an algorithm faster by using more memory, or save memory by doing more work. Complexity analysis gives you numbers to base this choice on instead of guessing.

Key takeaways

  • An algorithm has input, output, definiteness, effectiveness and termination.
  • Complexity is the growth rate of time or space as the input size nn grows.
  • We analyse algorithms, not problems: one problem can have algorithms of different complexity.
  • P = solvable in polynomial time; NP = a “yes” answer can be checked in polynomial time.
  • Extra memory can often buy speed (and the reverse); analysis tells you the cost of each choice.

Practice questions

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

Question 1 · multiple choice

A procedure repeats “x = x − 1” starting from x = 10 while x ≠ 0.5, using whole numbers only. Which property of an algorithm does it break?

Show answer

Answer: C. Termination

x goes 10, 9, …, 1, 0, −1, … and never equals 0.5, so the loop never stops. Each step is still clear, so definiteness is not the problem.

Question 2 · multiple choice

Which question belongs to computability theory rather than complexity theory?

Show answer

Answer: B. Can any algorithm decide this problem at all?

Computability asks whether a problem can be solved at all, regardless of resources. The other three ask about resources, which is complexity theory.

Question 3 · multiple choice

Which of these is a decision problem?

Show answer

Answer: D. Does this graph contain a path through every vertex exactly once?

A decision problem has only a yes/no answer. “Find the shortest route” asks for an object (a route), so it is an optimisation problem, not a decision problem.

Question 4 · multiple choice

What does it mean that a problem is in class P?

Show answer

Answer: A. A deterministic algorithm can solve it in polynomial time.

P = solvable by an ordinary algorithm in at most c⋅nkc \cdot n^k steps. “Polynomial memory” describes PSPACE, a different class.

Question 5 · multiple choice

For A=[2,7,1,8,2,8]A = [2, 7, 1, 8, 2, 8] (positions 1 to 6) the prefix-sum array is P=[0,2,9,10,18,20,28]P = [0, 2, 9, 10, 18, 20, 28]. What is the sum of positions 3 to 6, computed as P[6]−P[2]P[6] - P[2]?

Show answer

Answer: C. 19

P[6]−P[2]=28−9=19P[6] - P[2] = 28 - 9 = 19, and directly 1+8+2+8=191 + 8 + 2 + 8 = 19. Using P[3]P[3] instead of P[2]P[2] gives 18, which wrongly drops position 3.

Question 6 · short answer

Define an algorithm and explain its five properties.

Show worked answer

An algorithm is a finite sequence of precise, executable instructions that, for every valid input, stops after a finite number of steps with the correct output. Properties: (1) Input – zero or more values are supplied; (2) Output – at least one result is produced; (3) Definiteness – every step is clear and has one meaning; (4) Effectiveness – every step is basic enough to be carried out exactly in finite time; (5) Termination – the algorithm stops after a finite number of steps for every valid input.

↑ ↓ move↵ openTab switch type