Dynamic Programming
Dynamic Programming (DP) is a powerful algorithmic technique used to solve problems by breaking them down into simpler subproblems. It is used for optimization problems where the goal is to find the best solution to a problem, often by combining solutions to subproblems.
Dynamic programming is particularly useful when a problem exhibits the following two properties:
Dynamic Programming is ideal for problems where:
DP typically avoids recomputing solutions to the same subproblems by storing the results of these subproblems, either through memoization (top-down) or tabulation (bottom-up).
There are two main approaches in dynamic programming:
Top-Down (Memoization):
Bottom-Up (Tabulation):
The Fibonacci sequence is one of the most famous problems that can be solved using dynamic programming. The sequence is defined as:
Without dynamic programming, a naive recursive solution would recompute the values for the same inputs multiple times, leading to exponential time complexity. With dynamic programming, we store the results of subproblems to avoid redundant calculations.
def fibonacci_memoization(n, memo={}):
if n in memo:
return memo[n]
if n <= 1:
return n
memo[n] = fibonacci_memoization(n-1, memo) + fibonacci_memoization(n-2, memo)
return memo[n]
# Example Usage
n = 10
print(f"Fibonacci of {n} is {fibonacci_memoization(n)}")
Explanation:
fibonacci_memoization
stores the result of each Fibonacci calculation in the memo
dictionary.n
has already been computed, the function retrieves it directly from the dictionary instead of recalculating it.
def fibonacci_tabulation(n):
if n <= 1:
return n
table = [0] * (n + 1)
table[1] = 1
for i in range(2, n + 1):
table[i] = table[i - 1] + table[i - 2]
return table[n]
# Example Usage
n = 10
print(f"Fibonacci of {n} is {fibonacci_tabulation(n)}")
Explanation:
fibonacci_tabulation
builds the solution from the base cases up to the desired value.table
array iteratively.The 0/1 Knapsack Problem is another classic problem that is solved using dynamic programming. The problem is as follows:
Given a set of items, each with a weight and a value, determine the maximum value you can carry in a knapsack of a given capacity, where you cannot break an item, and each item can be used at most once.
def knapsack_memoization(weights, values, capacity, n, memo={}):
if (n, capacity) in memo:
return memo[(n, capacity)]
if n == 0 or capacity == 0:
return 0
if weights[n-1] > capacity:
return knapsack_memoization(weights, values, capacity, n-1, memo)
else:
include = values[n-1] + knapsack_memoization(weights, values, capacity - weights[n-1], n-1, memo)
exclude = knapsack_memoization(weights, values, capacity, n-1, memo)
memo[(n, capacity)] = max(include, exclude)
return memo[(n, capacity)]
# Example Usage
weights = [1, 2, 3, 8, 7, 4]
values = [20, 5, 10, 40, 15, 25]
capacity = 10
n = len(weights)
print(f"Maximum value: {knapsack_memoization(weights, values, capacity, n)}")
def knapsack_tabulation(weights, values, capacity, n):
dp = [[0] * (capacity + 1) for _ in range(n + 1)]
for i in range(n + 1):
for w in range(capacity + 1):
if i == 0 or w == 0:
dp[i][w] = 0
elif weights[i-1] <= w:
dp[i][w] = max(values[i-1] + dp[i-1][w - weights[i-1]], dp[i-1][w])
else:
dp[i][w] = dp[i-1][w]
return dp[n][capacity]
# Example Usage
weights = [1, 2, 3, 8, 7, 4]
values = [20, 5, 10, 40, 15, 25]
capacity = 10
n = len(weights)
print(f"Maximum value: {knapsack_tabulation(weights, values, capacity, n)}")
Time Complexity:
The time complexity of dynamic programming depends on the number of subproblems and how each subproblem is solved. For problems like the Fibonacci sequence or knapsack, the time complexity is generally O(n) or O(n * capacity), where n is the number of elements, and capacity is the maximum capacity (for knapsack).
Space Complexity:
The space complexity is O(n) in most cases, where n is the number of subproblems (for example, the size of the memoization table or the DP table). In the case of recursive solutions with memoization, the space complexity also includes the recursion stack.
Optimization Problems:
Data Structures:
Machine Learning:
Finance and Economics: