A problem can be viewed as a sequence of decisions to be made.
A Brute Force algorithm solves a problem by exhaustively enumerating
all the possibilities.
Example: brute force string matching
A Greedy algorithm always make the (seemingly) best choice
in each decision. Once a decision has been made, that decision
is never reconsidered.
Example: hill climbing; Huffman encoding; Dijkstra's shortest path
A Backtracking algorithm systematically considers
all possible outcomes for each decision, just like the brute force
algorithms. But backtracking algorithms can detect when an
exhaustive search is unnecessary. Therefore, backtracking algorithms
can perform much better than brute force ones when solving the same problem.
Example: eight queen; depth first traversal
A Divide And Conquer algorithm divides a problem into several
(usually smaller) sub-problems, solves each sub-problem independently,
then combines the solutions to obtain the solution to the original problem.
Divide and conquer algorithms are often implemented using recursion.
And generally the sub-problems are non-overlapping.
Example: quick sort; merge sort
A Dynamic Programming algorithm solves a series of sub-problems
to solve the original problem. The series of sub-problems is devised
carefully in such a way that each subsequent solution is obtained
by combining the solutions to one or more of the sub-problems
that have already been solved. All intermediate solutions are kept
in a table in order to prevent unnecessary duplication of efforts.
Example: matrix chain product, all pairs shortest path
(by Warshall, Floyd and Ingerman)
Dynamic Programming examples:
input: n - there are n matrices, and n+1 dimention numbers p[0..n] where matrix A_i is p[i-1] by p[i] output: M[i][j] - the minimum number of multiplication needed for expression A_i...A_j; S[i][j] - the place to put the top most two parenthesis // note that the index, except the one for p, // starts from 1 for simplicity reasons Algorithm ChainMatrixMultiplication(int p[], int n) for i = 1 to n M[i][i] = 0; S[i][i] = -1; for L = 1 to n-1 for i = 1 to n-L j = i + L; S[i][j] = i; M[i][j] = M[i][i]+M[i+1][j]+p[i-1]*p[i]*p[j] for k = i+1 to j-1 if M[i][j] > (M[i][k]+M[k+1][j]+p[i-1]*p[k]*p[j]) M[i][j] = M[i][k]+M[k+1][j]+p[i-1]*p[k]*p[j]; S[i][j] = k;
A Randomized algorithm has an element of randomness
involved in solving a problem.
Example: skip list