dynamic programming n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Dynamic Programming PowerPoint Presentation
Download Presentation
Dynamic Programming

Loading in 2 Seconds...

play fullscreen
1 / 20

Dynamic Programming - PowerPoint PPT Presentation


  • 81 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Dynamic Programming' - macha


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
what is 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 subproblemswhich are only slightly smaller
  • 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
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
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 programming1
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

0 1 knapsack
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
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
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
Dynamic Programming Matrix with the initialization

The matrix labels are colored orange and the initialized cells

dynamic programming matrix with the initialization1
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 initialization2
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 initialization3
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 initialization4
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].

a dynamic programming solution coin change
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
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.
slide18

Coin Change – Source Code

Time Complexity:O(mn)

sample source code
Sample Source Code

Dynamic programming example--typesetting a paragraph.

Overall running time: O(n3)