Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Dynamic Programming

Uploaded by

himajashree06
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Dynamic Programming

Uploaded by

himajashree06
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

DYNAMIC PROGRAMMING

 Dynamic programming (usually referred to as DP ) is a very powerful


technique to solve a particular class of problems.
 It demands very elegant formulation of the approach and simple
thinking and the coding part is very easy.
 The idea is very simple, If you have solved a problem with the given
input, then save the result for future reference, so as to avoid solving the
same problem again.. shortly 'Remember your Past' :) .
 If the given problem can be broken up in to smaller sub-problems and
these smaller subproblems are in turn divided in to still-smaller ones,
and in this process, if you observe some over-lappping subproblems,
then its a big hint for DP.
 Also, the optimal solutions to the subproblems contribute to the optimal
solution of the given problem ( referred to as the Optimal Substructure
Property ).
There are two ways of doing this.
1.) Top-Down : Start solving the given problem by breaking it down. If
you see that the problem has been solved already, then just return the saved
answer. If it has not been solved, solve it and save the answer. This is
usually easy to think of and very intuitive. This is referred to
as Memoization.
2.) Bottom-Up : Analyze the problem and see the order in which the sub-
problems are solved and start solving from the trivial subproblem, up
towards the given problem. In this process, it is guaranteed that the
subproblems are solved before solving the problem. This is referred to
as Dynamic Programming.
Note that divide and conquer is slightly a different technique. In that, we
divide the problem in to non-overlapping subproblems and solve them
independently, like inmergesort and quick sort.
DYNAMIC PROGRAMMING:

• Matrix Chain Multiplication


• Floyds
• Warshalls
• Multi stage graphs
• 0/1 Knapsack
Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by or formulated as recurrences with
overlapping subinstances

• Invented by American mathematician Richard Bellman in the


1950s to solve optimization problems and later assimilated by CS

• “Programming” here means “planning”

• Main idea:
- set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Dynamic Programming
Algorithm
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 a
bottom-up fashion
4. Construct an optimal solution from computed
information (not always necessary)

8
Examples of DP algorithms
• Multistage graph

•Matrix chain multiplication

• Warshall’s algorithm for transitive closure

• Floyd’s algorithm for all-pairs shortest paths

• Constructing an optimal binary search tree

• Some instances of difficult discrete optimization problems:


- traveling salesman
Principle of optimality

 The principal of optimality states that an


optimal sequence of decisions has the
property that whatever the initial state and
decision are, the remaining decisions must
constitute an optimal decision sequence with
regard to the state resulting from the first
decision.
Dynamic Programming cont.

Fibonacci:
function Fibonacci(n: integer) : integer;
var
i : index; sum, interm1, interm2: integer;
begin
interm1:= 0; {F0}
interm2:= 1; {F1}
for i:=3 to n do
sum :=interm1 + interm2;
interm1:=interm2;
interm2:= sum;
end {for}
Fibonacci := sum;
end {Fibonacci}
Dynamic Programming vs. Recursion
and Divide & Conquer

 In a recursive program, a problem of size n is


solved by first solving a sub-problem of size n-1.
 In a divide & conquer program, you solve a
problem of size n by first solving a sub-problem of
size k and another of size k-1, where 1 < k < n.
 In dynamic programming, you solve a problem of
size n by first solving all sub-problems of all sizes
k, where k < n.

You might also like