Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3500598
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:45:14+00:00 2026-05-18T12:45:14+00:00

I can’t figure out the principles of dynamic programming and I really do want

  • 0

I can’t figure out the principles of dynamic programming and I really do want it. DP is very powerful, it can solve problems like this:

Getting the lowest possible sum from numbers' difference

So, can you suggest me good books or articles (preferably with examples with real code) which would explain me what is dynamic programming? I really want simple examples first of all, then I’ll move on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:45:15+00:00Added an answer on May 18, 2026 at 12:45 pm

    Dynamic programming is a useful type of algorithm that can be used to optimize hard problems by breaking them up into smaller subproblems. By storing and re-using partial solutions, it manages to avoid the pitfalls of using a greedy algorithm. There are two kinds of dynamic programming, bottom-up and top-down.

    In order for a problem to be solvable using dynamic programming, the problem must possess the property of what is called an optimal substructure. This means that, if the problem was broken up into a series of subproblems and the optimal solution for each subproblem was found, then the resulting solution would be realized through the solution to these subproblems. A problem that does not have this structure cannot be solved with dynamic programming.

    Top-Down

    Top-down is better known as memoization. It is the idea of storing past calculations in order to avoid re-calculating them each time.

    Given a recursive function, say:

    fib(n) = 0 if n = 0
             1 if n = 1
             fib(n - 1) + fib(n - 2) if n >= 2
    

    We can easily write this recursively from its mathematic form as:

    function fib(n)
      if(n == 0 || n == 1)
        n
      else
        fib(n-1) + fib(n-2)
    

    Now, anyone that has been programming for awhile or knows a thing or two about algorithmic efficiency will tell you that this is a terrible idea. The reason is that, at each step, you must to re-calculate the value of fib(i), where i is 2..n-2.

    A more efficient example of this is storing these values, creating a top-down dynamic programming algorithm.

    m = map(int, int)
    m[0] = 0
    m[1] = 1
    function fib(n)
      if(m[n] does not exist)
        m[n] = fib(n-1) + fib(n-2)
    

    By doing this, we calculate fib(i) at most once.


    Bottom-Up

    Bottom-up uses the same technique of memoization that is used in top-down. The difference, however, is that bottom-up uses comparative sub-problems known as recurrences to optimize your final result.

    In most bottom-up dynamic programming problems, you are often trying to either minimize or maximize a decision. You are given two (or more) options at any given point and you have to decide which is more optimal for the problem you’re trying to solve. These decisions, however, are based on previous choices you made.

    By making the most optimal decision at each point (each subproblem), you are making sure that your overall result is the most optimal.

    The most difficult part of these problems is finding the recurrence relationships for solving your problem.

    To pay for a bunch of algorithm textbooks, you plan to rob a store that has n items. The problem is that your tiny knapsack can only hold at most W kg. Knowing the weight (w[i]) and value (v[i]) of each item, you want to maximize the value of your stolen goods that all together weight at most W. For each item, you must make a binary choice – take it or leave it.

    Now, you need to find what the subproblem is. Being a very bright thief, you realize that the maximum value of a given item, i, with a maximum weight, w, can be represented m[i, w]. In addition, m[0, w] (0 items at most weight w) and m[i, 0] (i items with 0 max weight) will always be equal to 0 value.

    so,

    m[i, w] = 0 if i = 0 or w = 0
    

    With your thinking full-face mask on, you notice that if you have filled your bag with as much weight as you can, a new item can’t be considered unless its weight is less than or equal to the difference between your max weight and the current weight of the bag. Another case where you might want to consider an item is if it has less than or equal weight of an item in the bag but more value.

     m[i, w] = 0 if i = 0 or w = 0
               m[i - 1, w] if w[i] > w
               max(m[i - 1, w], m[i - 1, w - w[i]] + v[i]) if w[i] <= w
    

    These are the recurrence relations described above. Once you have these relations, writing the algorithm is very easy (and short!).

    v = values from item1..itemn
    w = weights from item1..itemn
    n = number of items
    W = maximum weight of knapsack
       
    m[n, n] = array(int, int)
    function knapsack
      for w=0..W
        m[0, w] = 0
      for i=1 to n
        m[i, 0] = 0
        for w=1..W
          if w[i] <= w
            if v[i] + m[i-1, w - w[i]] > m[i-1, w]
               m[i, w] = v[i] + m[i-1, w - w[i]]
            else
               m[i, w] = m[i-1, w]
          else
            m[i, w] = c[i-1, w]
      
      return m[n, n]
    

    Additional Resources

    1. Introduction to Algorithms
    2. Programming Challenges
    3. Algorithm Design Manual

    Example Problems

    Luckily, dynamic programming has become really in when it comes to competitive programming. Check out Dynamic Programming on UVAJudge for some practice problems that will test your ability to implement and find recurrences for dynamic programming problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me how I can display a status message like 12 seconds
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can you tell me what is the difference between abstraction and information hiding in
Can I get a 'when to use' for these and others? <% %> <%#
Can anyone recommend a good library for generating an audio file, such as mp3,
Can you suggest some good MVC framework for perl -- one I am aware

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.