Dynamic Programming PowerPoint (PPT

advertisement
Dynamic Programming
What is Dynamic Programming
 A method for solving complex problems by breaking them down into
simpler sub problems. It is applicable to problems exhibiting the properties
of overlapping subproblems which are only slightly smaller
o The key idea behind dynamic programming is quite simple. In general, to
solve a given problem, we need to solve different parts of the problem
(subproblems), then combine the solutions of the subproblems to reach an
overall solution.
Two types of Dynamic Programming
 Bottom-up algorithm
 In order to solve a given problem, a series of subproblems is solved.
 Top-Down algorithm (often called Memoization.)
 a technique that is associated with Dynamic Programming
 The concept is to cache the result of a function given its parameter so that the
calculation will not be repeated; it is simply retrieved
Fibonacci Sequence with Dynamic Programming
Pseudo-code for a simple recursive function will be :
fib(int n)
{
if (n==0) return 0;
if (n==1) return 1;
return fib(n-1)+fib(n-2);
}
Fibonacci Sequence with Dynamic Programming
Example:
 Consider the Fibonacci Series : 0,1,1,2,3,5,8,13,21...
 F(0)=0 ; F(1) = 1; F(N)=F(N-1)+F(N-2)
Calculating 14th fibonacci no., i.e., f14
The 0-1 Knapsack
Problem
Using Dynamic Programming
0-1 Knapsack
 the 0-1 Knapsack problem and its algorithm as well as its derivation from its
recursive formulation
 to enhance the development of understanding the use of dynamic
programming to solve discrete optimization problems
The complete recursive formulation of the solution

Knap(k, y) = Knap(k-1, y)
if y < a[k]

Knap(k, y) = max { Knap(k-1, y), Knap(k-1, y-a[k])+ c[k] }
if y > a[k]

Knap(k, y) = max { Knap(k-1, y), c[k] }
if y = a[k]

Knap(0, y) = 0
 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6.
Given: Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6.
 The ci represents the value of selecting item i for inclusion in the knapsack;
 The ai represents the weight of item i - the weights
 The constant b represents the maximum weight that the knapsack is
permitted to hold.
Dynamic Programming Matrix with the initialization
The matrix labels are colored orange and the initialized cells
Dynamic Programming Matrix with the initialization
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1]
has a weight of 4
Dynamic Programming Matrix with the initialization
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1]
has a weight of 3
Dynamic Programming Matrix with the initialization
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1]
has a weight of 2
Dynamic Programming Matrix with the initialization
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
has a weight of 1
The maximum value for this knapsack problem is in the bottom leftmost entry in the matrix, knap[4][5].
Coin Change
Using Dynamic Programming
A dynamic programming solution (Coin Change )
 Idea: Solve first for one cent, then two cents, then three cents,
etc., up to the desired amount
 Save each answer in an array !
 For each new amount N, compute all the possible pairs of
previous answers which sum to N
 For example, to find the solution for 13¢,
 First, solve for all of 1¢, 2¢, 3¢, ..., 12¢
 Next, choose the best solution among:
 Solution for 1¢ + solution for 12¢
 Solution for 2¢ + solution for 11¢
 Solution for 3¢ + solution for 10¢
 Solution for 4¢ + solution for 9¢
 Solution for 5¢ + solution for 8¢
 Solution for 6¢ + solution for 7¢
Example
To count total number solutions, we can divide all set solutions in two sets.
 Suppose coins are 1¢, 3¢, and 4¢
 There’s only one way to make 1¢ (one coin)
 To make 2¢, try 1¢+1¢ (one coin + one coin = 2 coins)
 To make 3¢, just use the 3¢ coin (one coin)
 To make 4¢, just use the 4¢ coin (one coin)
 To make 5¢, try
 1¢ + 4¢ (1 coin + 1 coin = 2 coins)
 2¢ + 3¢ (2 coins + 1 coin = 3 coins)
 The first solution is better, so best solution is 2 coins
 To make 6¢, try
 1¢ + 5¢ (1 coin + 2 coins = 3 coins)
 2¢ + 4¢ (2 coins + 1 coin = 3 coins)
 3¢ + 3¢ (1 coin + 1 coin = 2 coins) – best solution
 Etc.
Coin Change – Source Code
Time Complexity: O(mn)
Sample Source Code
Dynamic programming example--typesetting a
paragraph.
Overall running time: O(n3)
THE END.
Download
Study collections