Download presentation
Presentation is loading. Please wait.
Published byCamilla Lindsey Modified over 8 years ago
1
Dynamic Programming
2
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.
3
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.)Memoization a technique that is associated with Dynamic ProgrammingDynamic 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
4
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); }
5
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
6
Using Dynamic Programming The 0-1 Knapsack Problem
7
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
8
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.
9
Given: Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6. The c i represents the value of selecting item i for inclusion in the knapsack; The a i represents the weight of item i - the weights The constant b represents the maximum weight that the knapsack is permitted to hold.
10
Dynamic Programming Matrix with the initialization The matrix labels are colored orange and the initialized cells
11
Dynamic Programming Matrix with the initialization has a weight of 4 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
12
Dynamic Programming Matrix with the initialization has a weight of 3 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
13
Dynamic Programming Matrix with the initialization has a weight of 2 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
14
Dynamic Programming Matrix with the initialization has a weight of 1 The maximum value for this knapsack problem is in the bottom leftmost entry in the matrix, knap[4][5]. Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
15
Using Dynamic Programming Coin Change
16
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¢
17
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.
18
Time Complexity: O(mn) Coin Change – Source Code
19
Sample Source Code Dynamic programming example--typesetting a paragraph. Overall running time: O(n 3 )
20
THE END.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.