Dynamic programming learning as a beginner

coding interview

Dynamic programming is regarded as one of the strongest and most significant algorithm design techniques. A rising number of people are trying to learn dynamic programming (DP). Before you decide to learn DP, you should know some basic knowledge at algo.monster about dynamic programming.

those who can't remmeber the past

Define dynamic programming

Dynamic programming is kind of like how a web server uses caching. In other words, dynamic programming is an optimization technique. People use DP to solve problems where you never have to do repeated work. Since DP remembers the past for future use, it saves time.

The most distinct features of dynamic programming are overlapping subproblems and optimal substructures. Overlapping subproblems means that there’re repeated problems. And an optimal substructure is to say you can get the optimal solution to the whole problem by finding the optimal solution to each of its subproblems.

Is dynamic programming hard to learn?

That depends on many factors. Generally, it’s easy to begin, but hard to master because it’s counterintuitive. People learn through patterns among problems. But in DP, it’s hard to spot similar aspects. Each of them is different even though the same technique is used.

The keyword of mastering at DP is practice. Make up your mind, and find the information you need. It takes time and effort; you should get yourself well prepared. There are several detailed steps for your practice in this article.

Advantages of DP you should know

Most of the problems in the computer world can be solved using DP. The main point of the DP solution is to avoid repeated work. Because DP remembers the results of subproblems, previous results can be reused. This saves a lot of time and boosts performance.

DP is the only technique that’s used to solve problems in some areas such as inventory, chemical engineering design, etc.

Do you use DP in real life?

Yes, definitely.

  1. We use dynamic programming when we solve problems by dividing them into similar subproblems. Then we solve and save the results for future use so that we don’t have to do it again.
  2. DP is used when optimization is needed.

Generally, almost wherever an optimal solution is required, you use DP. It is widely used in many fields, including routing, AI, graph problems, etc.

Well, at least you know DP is a practical tool. It’s definitely worth your time and efforts, right?

Reading books to learn about the concepts of DP

Books offer reliable and helpful information. If you know nothing about dynamic programming, learn about the main concepts of DP first.

  1. What is the definition of DP? You can check Wikipedia for its meaning as well.
  2. How does DP work? There’re two types of dynamic programming approaches: top-down and bottom-up.

Top-down DP uses recursion with memoization. Bottom-up uses an iterative approach.

  1. When does DP work? DP can’t be applied to deal with all problems. Only problems that admit optimal substructures and overlapping subproblems are amenable to dynamic programming.
  2. Do you understand the complexity of DP? Sometimes, you can find various DP solutions for the same problem. The runtime of DP is determined by the number of subproblems and choices for each subproblem.
  3. Read CLRS to get all the detailed information you need to know. Be patient and persistent.

Practice any DP problem from your books

You know the books have already solved many classic DP problems. These include Scheduling algorithm, Matrix algorithms, String algorithms, Graph algorithms: single-source-shortest-path, and Better exponential time algorithm using DP: traveling salesman problem.

Read your books and think on your own to solve the problems. Then compare the similarities and differences of your strategy with the algorithm in the book.

You know, practice makes a difference. The more problems you practice, the better your skills will be.

How to learn dynamic programming as a beginner?

Here are specific steps to practice DP problems.

  1. The first step is to kick off with working out problems with recursion.
  2. Keep practicing with step one till you’re comfortable with the recursive approach. Then consider how you can save the states of recursion that will reduce the number of recursive calls. That is top-down DP.
  3. Step three is to pick up the base case of recursion and deal with that problem with the bottom-up approach. Try to figure out how to get the value of the current state from the calculated state.
  4. Try to solve the classic DP problems on your own with various approaches: recursive, bottom-up, and top-down.
  5. Find tougher problems online with Online Judges, and try to solve them on your own. Other people’s solutions are good sources for you to improve.

Other examples of DP problems

0/1 Knapsack

LCS

Matrix Chain Multiplication

Coin Change

LIS

Edit Distance

Balanced Partition

Optimal Strategy for a Game

See if you can solve this quiz

Before you decide if you want to go deeper, try solving this easy one first.

The problem of climbing stairs: Someone is climbing a staircase. It will take n steps for him to get to the top point. This person can either climb 1 or 2 steps each time. In how many distinct ways can he get to the top?

Example A:

Input: n=3

Output: 3

Explanation: This person has three ways to get to the top

  1. 1 step + 1 step+ 1 step
  2. 1 step + 2 steps
  3. 2 steps+ 1 step

 

Example B:

Input: n=4

Output: n=5

Explanation: There are two ways to climb to the top

  1. 1 step then 1 step+ 1 step+ 1 step
  2. 1 step + 1 step + 2 steps
  3. first one 1 step then 2 steps + 1 step
  4. 2 steps + 1 step + 1 step
  5. 2 steps and 2 steps

Constraints: 1<= n <= 30

Conclusion

As a powerful problem-solving technique, dynamic programming has been gaining popularity these years. The DP concept is pretty simple. After solving a problem, we save the results for future reference. In this way, we don’t have to compute again.

If you’re new to it and interested in learning DP, you should find enough information before taking action. Either for job seeking or just for self-improvement, you should get an overall picture of it first. You’ll have to work hard to be good at it. Keep practicing till you manage it.

Leave a Reply

Your email address will not be published. Required fields are marked *