Dynamic Programming: When to Use Dynamic Programming

    Dynamic Programming: When to Use Dynamic Programming

      When you pay cash for something like a giant stuffed unicorn, unless you need to squirrel away quarters for laundry or you just really hate the design of nickels, there isn't an optimal combination of coins you can find for your change. You just want the right amount change. No need for dynamic programming there.

      How about when you've only got exactly 13.5 minutes to make it home before your curfew? You'd think your parents would be lenient since it's been a whole two weeks since you were last grounded for coming home too late but noooo. Now you need an optimal solution: the fastest way home, Ferris Bueller-style running through people's pools if you have to. Dynamic programming to the rescue.

      Whenever a problem talks about optimizing something, dynamic programming could be your solution. Usually, it won't jump out and scream that it's dynamic programming, though. It'll ask something like:

      • What's the minimum number of ducklings it would take to bring down a bear?
      • What's the maximum number of cookies that can fit in a human stomach? 
      • What's the shortest distance between you and the nearest Ben & Jerry's?
      • What's the fastest anyone has ever done the OK Go treadmill dance?

      The adjectives minimum, maximum, shortest, and fastest tell you that it isn't enough to find just any solution to the problem, you'll need to find the one solution that solves the problem the best.

      Any decent programming solution will find a solution, sure, but it may not find the optimal solution. But…you can't prove that something's the optimal solution unless you identify all possible solutions and compare them.

      If you're talking about chess, you'd look for questions like:

      • What's the minimum number of moves to reach the game position you want?
      • What's the maximum number of ways you can get your queen to a specific location?

      Example One: The Knapsack Problem

      There's another classic problem you'll see every time you talk about dynamic programming: the knapsack problem. In it, you have to figure out the maximum number of items you can pack into the knapsack without your stuff spilling over the sack's volume. Unlike finding the fastest treadmill dancer to ever tread a mill, this problem has some real-world applicability.

      If you want to mail something using the USPS's Flat Rate boxes, you pay one rate to fill that box with as many clothes and homemade cookies as possible. You better be ready to stuff some snickerdoodles with your tube socks.

      Or maybe you're less of a bake-it-yourself kind of person and decide to buy those cookies on Amazon. If you're a Prime member, you'll pay one fee every year to get unlimited shipping (on select items). Whenever an Amazon prime customer places an order, Amazon's going to want to use the fewest boxes to ship the customer's order so that they don't overpay.

      Example Two: The Minimum Path

      Another problem you'll see everywhere: the minimum path. If you've ever used Google Maps or Waze, you've seen a version of this problem. Sure, they won't be able to find every path from your house to Belmopan, Belize, but it will use the shortest paths from your location to intermediate places (taking out the most ridiculously long solutions to save time) and then use them to figure out which one will take you that extra distance.

      And if it finds an issue with traffic or construction, it'll start the whole process over again just to re-route you.

      Not bad for one little type of algorithm.