Algorithms - Dynamic Programming

date
Dec 9, 2024
type
Post
AI summary
slug
algorithm-dp
status
Published
tags
Algorithm
summary

Introduction

Dynamic programming (DP) is a powerful technique in computer science and mathematics used to solve problems by breaking them into smaller, simpler subproblems. These subproblems are solved individually, and their solutions are stored to avoid redundant calculations. This method is particularly effective for problems exhibiting two key properties:
  • Overlapping Subproblems: The problem can be divided into subproblems that are solved multiple times.
  • Optimal Substructure: The solution to a larger problem can be constructed using solutions to its subproblems.
Video preview

Steps in Dynamic Programming

  1. Define the Problem: Clearly define the state and how it represents a subproblem.
  1. Identify the Recursive Relation: Determine how the solution to the current state depends on previous states.
  1. Base Cases: Specify the simplest cases with known solutions.
  1. Memoization or Tabulation: Decide whether to use a top-down or bottom-up approach to compute and store results.
  1. Solve Iteratively or Recursively: Compute the solution to the original problem using the stored subproblem results.

Two Main Approaches

Top-Down (Memoization)

  • Description: Solve the problem recursively, caching the results of subproblems in a data structure (e.g., a hash table or array) to avoid recalculating them.
  • Steps:
      1. Start from the original problem.
      1. Break it into smaller subproblems recursively.
      1. Store the result of each subproblem as it is computed.
      1. Reuse stored results for overlapping subproblems.
  • Example: Calculating Fibonacci numbers using recursion with memoization.

Bottom-Up (Tabulation)

  • Description: Solve the problem iteratively by starting with the smallest subproblems and building up to the original problem. Store results in a table and use them to solve larger subproblems.
  • Steps:
      1. Identify the order of subproblem computation (e.g., smallest to largest).
      1. Initialize a table with base case solutions.
      1. Iterate through the table, solving subproblems in order.
      1. Use previously computed values to fill in the table.
  • Example: Computing Fibonacci numbers using an iterative loop.

Applications

Dynamic programming is widely used in areas such as:
  • Graph Algorithms: E.g., shortest path algorithms like Floyd-Warshall.
  • Sequence Analysis: E.g., Fibonacci numbers, Catalan numbers.
By leveraging overlapping subproblems and optimal substructure, dynamic programming reduces computational complexity and provides efficient solutions to problems that would otherwise be computationally expensive.
 

© Qiwei Mao 2024 - 2025