CSC 208 Dynamic PProgramming
CSC 208 Dynamic PProgramming
Dynamic Programming
Dynamic programming is a method for solving complex problems by breaking them
down into simpler subproblems. It is particularly useful in data structures and
algorithms for optimising problems that can be divided into overlapping subproblems
with optimal substructure properties.
1. Optimal Substructure:
○ A problem exhibits optimal substructure if an optimal solution to the
problem can be constructed from the solutions to its subproblems. This
is a fundamental property required for a problem to be solvable by
dynamic programming.
○ In other words, the optimal solution to the problem can be derived by
combining optimal solutions to its subproblems.
2. Overlapping Subproblems:
○ This means that the problem can be broken down into smaller, simpler
subproblems that are reused multiple times. Instead of solving these
subproblems repeatedly, dynamic programming solves each
subproblem once and stores the results.
○ Unlike divide-and-conquer algorithms (which solve subproblems
independently), dynamic programming is used when subproblems
overlap. This means that the same subproblems are solved multiple
times. Dynamic programming saves these solutions to avoid redundant
computations.
3. Memoization vs. Tabulation:
○ Memoization: This is a top-down approach where solutions to
subproblems are stored (usually in a hash table or an array) to avoid
recomputation.
○ Tabulation: This is a bottom-up approach where solutions to all
possible subproblems are computed and stored in a table, often using
iterative methods.
Approach to Dynamic Programming
Recursion
1. Fibonacci Sequence: Finding the nth Fibonacci number, where each number
is the sum of the two preceding ones.
2. Knapsack Problem: Given a set of items, each with a weight and a value,
determine the number of items to include in a collection so that the total
weight is less than or equal to a given limit and the total value is as large as
possible.
3. Longest Common Subsequence (LCS): Finding the longest subsequence
common to two sequences.
4. Shortest Path Problems: Finding the shortest path in a graph, such as the
Bellman-Ford algorithm or Floyd-Warshall algorithm.
It is widely used in arrays, strings, trees, and graphs to develop efficient algorithms
for complex problems.