Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CSC 208

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.

Key Concepts of Dynamic Programming:

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

Dynamic programming problems can be approached in three main ways:

Recursion

Top-Down Approach (Memoization): This involves recursively breaking down the


problem into subproblems and storing the results of these subproblems to avoid
redundant computations. This is typically implemented using recursion with a data
structure (such as an array or a dictionary) to cache results.
python
Bottom-Up Approach (Tabulation): This involves iteratively solving all subproblems
starting from the smallest ones and building up the solution to the original problem.
This approach usually uses a table to store the results of subproblems.
python

Examples of Dynamic Programming Problems

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.

Advantages of Dynamic Programming

● Efficiency: By storing the results of subproblems, dynamic programming


avoids redundant computations, significantly reducing the time complexity of
algorithms.
● Applicability: It can be applied to a wide range of problems in computer
science, from optimization problems to sequence alignment in bioinformatics.
Summary:

Dynamic programming is a versatile and powerful technique used in various data


structures to solve optimization problems efficiently by leveraging the principles of
optimal substructure and overlapping subproblems.

It is widely used in arrays, strings, trees, and graphs to develop efficient algorithms
for complex problems.

By understanding and implementing both top-down and bottom-up approaches, one


can solve many complex problems efficiently.

You might also like