Algorithms · Algorithm Analysis and Asymptotic Notation
Definition of an Algorithm
Overview
An algorithm is a finite, unambiguous, step-by-step procedure that takes input, produces output and terminates; it is usually sketched first as an informal English-like blueprint (pseudocode) before the program is written in a language such as C. Writing the algorithm first makes the solution easier to communicate before implementation.
A single problem can have many different algorithms.
Key concepts
- Algorithm: an informal description of the solution to a problem — a blueprint written in an English-like language, before any program is written in a programming language such as C.
- Blueprint before program: the algorithm is written first; only after the algorithm is written is the program written.
- Communication: the algorithm makes it easier to communicate the solution before implementation.
- Many solutions per problem: for a given problem P1 there may be many solutions; each solution is called an algorithm — A1, A2, A3, A4, and so on.
How it works
- Start with a problem to solve.
- Before writing the program in any programming language (e.g., C), write a blueprint — an informal, English-like description of what is to be written.
- This description makes it easier to communicate the solution before implementation.
- This description is called the algorithm.
- Only after the algorithm is written is the program written.
Revision summary
- An algorithm is a finite, unambiguous sequence of well-defined steps — it is not yet a program; the informal English-like form is its pseudocode sketch.
- The algorithm comes first; the program in a language like C comes after.
- The purpose of the blueprint is easier communication of the solution before implementation.
- One problem P1 can have many algorithms A1, A2, A3, A4, ... — there will be many solutions.
Previous-year exam insights
- Every algorithm must satisfy: finiteness (terminates after a finite number of steps), definiteness (each step precisely defined, no ambiguity), input (zero or more inputs), output (one or more outputs) and effectiveness (each step is basic enough to be carried out exactly).
Asked inGATE 1993 - An algorithm expressed in a programming language is a program; the same algorithm can be implemented in many languages, and one problem can have several correct algorithms differing in time/space complexity.
- Analysis of an algorithm measures the resources it uses (running time as number of basic operations, and extra memory) as a function of input size n, independent of machine, language or compiler; this is why asymptotic notation is used.
- Correctness and termination are separate from efficiency: an algorithm must be proved to halt and produce the required output for every valid input before its complexity is meaningful.
- A procedure/function that may not terminate or may not be definite is not an algorithm; a 'program' that loops forever on some input fails the finiteness requirement.
- Worst-case, best-case and average-case running times are defined over the set of inputs of a given size; average case requires an assumed probability distribution of inputs.