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):
Property
Meaning
Example of breaking it
Input
Zero or more values are given before the algorithm starts
— (zero inputs is allowed, e.g. “print the first 10 primes”)
Output
At least one result is produced
A procedure that calculates but never reports anything
Definiteness
Each step has exactly one meaning
“Add a few numbers”
Effectiveness
Each 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 input
A 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 n 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 n and another’s grows like n2, then for large inputs the first wins on every machine. This “look only at the growth for large n” approach is called asymptotic analysis.
Two related fields are easy to mix up:
Complexity theory
Computability theory
Main question
How much time/memory does it take to solve the problem?
Can the problem be solved by any algorithm at all?
Example result
Sorting by comparisons needs about nlogn comparisons in the worst case
No 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 n2) or merge sort (time grows like nlogn). 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⋅nk steps for some constants c and k. 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 n 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 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.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/analysis-of-algorithms/algorithms-and-complexity/ to read this page.