Professional Documents
Culture Documents
DYNAMIC PROGRAMMING
Algorithm design technique
Used when solution to problems can be viewed as result of a sequence of
decisions.
Collection of sub problems are identified and solved one by one, smallest
the first.
Using answers to small problems, solutions to large ones are figured out,
until whole set of sub problems are solved
Used for optimization problems.
Such algorithms examine previously solved sub-problems and will
combine their solutions to give the best solution for a given problem.
Eg: Travelling from A to B during rush hour. Finds shortest path to point
near A and from there to next shortest point and eventually reach B using
shortest path.
Steps in DP:
1. Characterize the structure of an optimal solution.
2. Recursively define the value of an optimal solution
3. Compute the value of an optimal solution in bottom up fashion
4. Construct an optimal solution from computed information
EXAMPLE PROBLEMS:
ALL PAIRS SHORTEST PATH ALGORITHM
SINGLE SOURCE SHORTEST PATH
MATRIX CHAIN MULTIPLICATION
OPTIMAL BINARY SEARCH TREE
0/1 KNAPSACK PROBLEM
TRAVELLING SALESMAN PROBLEM
STRING EDITING
OPTIMALITY PRINCIPLE
The principle of optimality is the basic principle of dynamic programming
An optimal path has the property that whatever the initial conditions and
control variables (choices) over some initial period, the control (or decision
variables) chosen over the remaining period must be optimal for the
remaining problem, with the state resulting from the early decisions taken
to be the initial condition.
EXAMPLE:
Shortest path Problem: Find the shortest path between vertices i & j
in directed graph G
To find this one must decide the sequence of vertices to be visited so
as to find the shortest route.
Optimal sequence of decisions results in least length path
Iterative Algorithm
Example problems:
N QUEENS PROBLEM
0/1 KNAPSACK PROBLEM
SUM OF SUBSET PROBLEM
N QUEENS PROBLEM
› Position n queens on an nn chessboard so that no two queens threaten
each other
4 Queens problem
› 4 queens have to be placed on an 4x4 chessboard, so that no two attack