1. Gyanastra
  2. Algorithms
  3. Dynamic Programming Basics
  4. Introduction to Dynamic Programming and Overlapping Subproblems
Free lesson, no account needed 4 min read 13 practice questions after it
Asked in past papers9 times
  • GATE CSE202620212014201120101994
  • UGC NET201920182013

Algorithms · Dynamic Programming Basics

Introduction to Dynamic Programming and Overlapping Subproblems

Overview

Dynamic programming (DP) is a programming paradigm that is very similar to divide and conquer and different from greedy.
It matters because it avoids the repeated recomputation that plain divide and conquer suffers when subproblems overlap, by storing subproblem solutions in a table and reusing them.

Key concepts

  • Dynamic programming (DP): A programming paradigm that divides a problem, solves subproblems, and combines their solutions, like divide and conquer, but stores subproblem solutions in a table and reuses them instead of recomputing.
  • "Programming" in the name: Does not mean writing programs and is not related to any programming language. It means using tables, in which the solutions are stored.
  • "Dynamic" in the name: Means deciding dynamically whether to call a function (recompute) or to use the table (reuse a stored value).
  • Overlapping subproblems: The same small subproblem is needed by multiple larger subproblems, so it is required in all the solutions.
  • Divide and conquer: Splits a problem into two halves, solves both subproblems separately, and then merges the solutions.
  • Main advantage of DP: Avoiding repeated computation of overlapping subproblems by reusing already-computed solutions.

How it works

  1. Divide and conquer takes a problem, divides it into two halves, solves both subproblems separately, and merges the solutions.
  2. The problem with divide and conquer: the subproblems may be overlapping. A big problem is divided into two halves; to solve one half it is again divided into two parts, and one of those parts is a small subproblem.
    That same small subproblem is also needed when solving the other half of the big problem.
  3. So the same small subproblem is repeated when solving the big problem as well as the other subproblem — it is required in all the solutions.
  4. If divide and conquer is applied in this situation, it solves this small part recursively over and over again, without knowing that this part was already computed a long time back.
  5. The DP idea: whenever the main problem requires solving many subproblems that have already been solved, instead of solving them over and again, maintain the solutions in a table and reuse them.

Revision summary

  • DP is similar to divide and conquer, not to greedy.
  • "Programming" in DP means using tables, not writing programs; it is unrelated to any programming language.
  • "Dynamic" means deciding dynamically whether to recompute (call a function) or reuse (use the table).
  • Plain divide and conquer recomputes an overlapping subproblem again and again, unaware that it was already computed.
  • DP's main advantage is avoiding repeated computation of overlapping subproblems by storing and reusing their solutions.

Previous-year exam insights

  • DP applies when a problem has both optimal substructure (an optimal solution is built from optimal solutions of subproblems) and overlapping subproblems; if subproblems are independent/disjoint, divide and conquer is appropriate instead.
    Asked inUGC NET 2019GATE 1994
  • Two standard ways to implement DP: top-down memoization (recursion plus a table, computing only needed states) and bottom-up tabulation (filling the table in dependency order); both give the same values, and memoization can be asymptotically slower only by constant factors, not in order.
    Asked inUGC NET 2019
  • A DP recurrence must be evaluated in an order consistent with its dependencies; if the recurrence has a cyclic dependency, it cannot be used directly and must be reformulated (e.g., by changing the state definition or solving by a different order/iteration).
    Asked inUGC NET 2019
  • The running time of a DP is (number of distinct states) x (time per state), i.e., proportional to the number of table entries times the work per entry, not simply the number of subproblems alone.
    Asked inUGC NET 2019GATE 2026
  • Classic problems solved by DP include: rod cutting, matrix-chain multiplication, longest common subsequence, longest increasing subsequence (O(n2)O(n^{2}) DP, O(nlog⁡n)O(n \log n) with patience sorting), 0/1 knapsack, optimal binary search tree, Floyd-Warshall all-pairs shortest paths, and Bellman-Ford single-source shortest paths; greedy (not DP) solves fractional knapsack, Dijkstra, Huffman coding and activity selection.
    Asked inGATE 2021GATE 2011UGC NET 2019UGC NET 2018UGC NET 2013
  • Both DP and greedy rely on optimal substructure (characterizing the structure of an optimal solution), but only DP also requires overlapping subproblems and tries all choices; greedy makes an irrevocable locally optimal choice without a table.
    Asked inUGC NET 2019
Pages 1–2 of 2