Dynamic programming always reminds of a favorite quotation:

    those who cannot repeat the past are condemned to repeat it.
    Fig: Quotation on Dynamic Programming

    Of the several different ways to solve a problem, dynamic programming is a paradigm where we tend to solve the problem as we move ahead and keep on learning. This is one of the techniques which even seasoned programmers find hard to implement. In this post we will try to address some of these fears and aim to have a better understanding of this approach. If you are new to problem solving and somehow landed at this post, it is recommended to go through some easier paradigms first.

    What on earth is Dynamic Programming?

    This is a question which people usually fail to understand. While we are growing up as a kid, what do we generally do in school? We start from the first standard and then move on to second and so on, until we graduate. Suppose you learn some specific topics in your first standard, you pass your exams and move on to the second standard. What do you do in the second standard? We don’t learn everything from the beginning and then add on the new syllabus. We just memorize whatever we had learnt and add new stuff.

    This is exactly what is dynamic programming. Some problems in computer science may have several successive parts. To solve them, instead of solving them again and again, we try to memorize the results from our previous computation and use them again. This technique is called memoization.

    We don’t want to repeat the things for which we already have the answer. It is same like, we don’t want to learn how to solve 1 + 1, because we already know it. For dynamic programming, we would always remember answers to problems that we have already solved.

    A classroom example:
    example of dp in classroom.
    Fig: Dynamic Programming in a classroom.

    Teacher:
    What is "1 + 1 + 1 + 1"?

    Student:
    (After some counting)
    The answer would be 4.

    Teacher:
    Okay, what if now I add 1 more to the right of this equation?
    What is "1 + 1 + 1 + 1 + 1"?

    Student:
    (Almost immediately)
    The answer would be 5.

    Teacher:
    How could you say that so fast? Didn’t you have to count it all again?

    Student:
    No, I had the previous result stored in my brain.

    This would be the easiest example of how dynamic programming works.
    – It has an Overlapping Sub-problem, this means that the sub-problems of the given problem are not independent. They are related to each other.
    – It follows the Optimal sub-structure property, this means that the sub-problems can be constructed together to solve the overall problem.

    Dynamic Programming Patterns:

    Suppose you want to become a musician by using dynamic programming. You have 2 ways:

    Memoization (Top-down)

    I will be an amazing musician. How? I will work hard like crazy. How? I’ll practice more instruments and try to improve. How? I’ll start taking part in contests. Then? I’ll be practicing. What? I’m going to learn chords.

    Hence, this is an approach in which we look for the answer of a sub-problem in a lookup table before computing its solution.

    The top down memoization approach starts from a large problem and breaks it down to smaller parts
    Fig: The top down memoization approach starts from a large problem and breaks it down to smaller parts.

    Tabulation (Bottom-up)

    I’m going to learn chords. After that, I will start practicing. Then, I will start taking part in contests. After that, I’ll practice even more and try to improve. After working hard like crazy, I’ll be an amazing musician.

    In this approach, we solve the problem as we go or bottom-up. This is typically done by filling up the lookup table, and computing the solution to the top/original problem based on the results in the table.

    The bottom up tabulation approach starts from a small problem and builds it up to a larger one
    Fig: The bottom up tabulation approach starts from a small problem and builds it up to a larger one
    Fibonacci Numbers:

    One cannot talk about dynamic programming and forget the popular Fibonacci series. It is one of the most popular sequence and used in a lot of programming applications and cryptographic techniques. The nth Fibonacci number is given by:

    fib(n) = fib(n-1) + fib (n-2)

    where:
    fib(0) = 0
    fib(1) = 1

    Given this, we can calculate fib(2) as sum of fib(1) and fib(0).

    Calculating fib(2)
    Fig: Calculating fib(2)

    This seems pretty straightforward, but what happens when we try to calculate a bigger Fibonacci number. fib(4) would look something like:

    calculating fib(4)
    Fig: Calculating fib(4)

    In this scenario, we are calculating the value of fib(2) twice. What happens when we try to calculate even higher Fibonacci numbers? We would end up having higher branched trees and more repetitive calculations. Referring to the quotation at the start of this post, we must try to avoid un-necessary computations. Hence dynamic programming comes to the rescue.

    We can save all this effort by computing only once and re-using every other time. We can achieve that by storing the results the first time we compute them and instead of doing the same computation again, we can just look it up from the stored memory.

    Further Reading:

    Some more examples where we use dynamic programming are:

    Video Explanation:
    Part 1:
    YouTube player
    Part 2:
    YouTube player
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    How do you compare two algorithms?

    by nikoo28
    8 minutes read

    We compare things all the time to determine which one is better over the other. The best example is when you are buying stuff online, we compare prices across websites, two different mobile phones, or even two different outfits. We know that there are different techniques of solving a problem in computer science: Brute Force Divide and Conquer Greedy Dynamic Programming Naturally, when we try to solve a problem with different methods, one would want to compare them. Once they are put side to side, we can then analyze which …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Greedy algorithms is a paradigm that describes the approach on how you can go about solving a problem. You might have heard about a lot of algorithmic techniques while learning how to solve problems in computer science. The others being: Brute Force Divide and Conquer Dynamic Programming In this post we shall understand what a greedy algorithm is and how can it be used to solve problems which don’t seem very trivial at the first glance. What is a Greedy Algorithm? What is the first thing that comes to your …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Algorithmic Paradigms – Brute Force

    by nikoo28
    5 minutes read

    Brute force method would probably be the favorite algorithmic paradigm of every developer. The beauty of a brute force method is that it is pretty straightforward and if a solution to a problem exists, you are guaranteed to find it. This option is also the most exhaustive option as we might go through all the possibilities before arriving at the result. What is Brute Force Method? You might have heard the phrase “Brute force method to solve the problem”. What exactly do you mean by that? It simply means that …

    0 FacebookTwitterLinkedinWhatsappEmail
  • What comes to your mind when you think about divide and conquer? Do you think about wars? A strategy? A way you can split up your forces and then conquer the enemy? I am here to tell you that is exactly right. Luckily, in computer science you don’t have to deal with weapons and artillery. There are several ways to approach a problem. Divide and conquer is one of them. In our case a problem statement is our enemy. We need to break it into parts (divide) and then solve …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Valid anagram strings

    by nikoo28
    5 minutes read

    Given two strings str1 and str2. Write a function to determine if str1 is an anagram of str2. Example 1:str1 = “listen”, str2 = “silent”Output = True Example 2:str1 = “mississippi”, str2 = “mips”Output = False Terminology: Anagram: Two words or phrases are said to be anagrams of each other if they can be formed by re-shuffling of characters in one of them. Problem Statement: In the given problem we have 2 strings as input. They may be single words or phrases. Return true if they anagrams of each other, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    First unique character in a string

    by nikoo28
    4 minutes read

    You are given a string with some repeating characters and some unique characters. Find the first unique character and return its index. Example 1:Input: studyAlgorithmsOutput: 2 Example 2:Input: iLoveToCodeOutput: 0 Problem Statement: I want to simplify the problem statement first before we begin to solve it. An input string consists of some uppercase and lowercase characters which may or may not be repeated. Out of all the unique characters, you need to return the characters that occurs first in the string. In the first example studyAlgorithms, s, and t are …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More