Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Dynamic Programming

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Dynamic Programming

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Dynamic Programming (DP) Algorithms

Dynamic programming (DP) is a method used in computer science and mathematics to solve
problems by breaking them down into smaller subproblems and solving each subproblem just once,
storing the results for reuse. It is particularly useful for optimization problems where the solution to
a problem depends on solutions to smaller overlapping subproblems. DP helps reduce the time
complexity of algorithms that might otherwise have exponential time complexity.

1. Introduction to Dynamic Programming


Dynamic programming can be viewed as a technique to solve problems by solving each subproblem
once and storing the results to avoid recalculating them. The two main approaches in DP are:
• Top-Down Approach (Memoization): Solve the problem by recursively breaking it into
subproblems, while saving the results of subproblems (memoization) to avoid redundant
calculations.
• Bottom-Up Approach (Tabulation): Solve the problem iteratively by solving all possible
subproblems in a specific order and storing their solutions in a table.

2. Key Properties of Dynamic Programming Problems


For a problem to be solvable by DP, it should have two key properties:
• Overlapping Subproblems: The problem can be divided into smaller subproblems, and
these subproblems are solved multiple times.
• Optimal Substructure: The solution to the problem can be constructed efficiently from
solutions to its subproblems.

3. Types of Problems Solved Using DP


Dynamic programming is widely used for problems related to:
• Optimization: Maximizing or minimizing a particular quantity.
• Counting: Counting the number of ways something can occur.
• Sequence problems: Problems involving sequences, such as the longest common
subsequence or the longest increasing subsequence.

4. Basic Examples of Dynamic Programming Algorithms


4.1 Fibonacci Sequence
The Fibonacci sequence is one of the most basic examples of a problem solvable with DP. The
sequence is defined as: F(0)=0,F(1)=1,F(n)=F(n−1)+F(n−2)
• Naive Recursion: A simple recursive approach to calculating Fibonacci numbers can lead to
exponential time complexity.
• Memoization: By storing the results of each Fibonacci number, we avoid recalculating the
same value multiple times.
• Tabulation: An iterative approach that fills up a table of Fibonacci numbers from 0 to n.
Memoization Example:
python
Copy code
def fibonacci(n, memo={}):
if n <= 1:
return n
if n not in memo:
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)
return memo[n]

Tabulation Example:
python
Copy code
def fibonacci(n):
dp = [0] * (n+1)
dp[1] = 1
for i in range(2, n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]

4.2 0/1 Knapsack Problem


The 0/1 Knapsack problem is a classic problem where we are given a set of items, each with a
weight and value, and we need to determine the maximum value we can achieve while staying
within a weight limit.
The problem can be solved using DP by constructing a table where each entry represents the
maximum value achievable for a given weight limit and subset of items.
Tabulation Example:
python
Copy code
def knapsack(weights, values, W):
n = len(weights)
dp = [[0] * (W+1) for _ in range(n+1)]

for i in range(1, n+1):


for w in range(1, W+1):
if weights[i-1] <= w:
dp[i][w] = max(dp[i-1][w], dp[i-1][w-weights[i-1]] + values[i-
1])
else:
dp[i][w] = dp[i-1][w]
return dp[n][W]

4.3 Longest Common Subsequence (LCS)


The LCS problem is a well-known problem where we are given two sequences, and we need to find
the longest subsequence that is common to both. This problem has a clear optimal substructure and
overlapping subproblems.
Tabulation Example:
python
Copy code
def lcs(X, Y):
m = len(X)
n = len(Y)
dp = [[0] * (n+1) for _ in range(m+1)]

for i in range(1, m+1):


for j in range(1, n+1):
if X[i-1] == Y[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[m][n]

4.4 Longest Increasing Subsequence (LIS)


In the LIS problem, we are given an array of integers, and we need to find the length of the longest
subsequence such that all elements in the subsequence are sorted in strictly increasing order.
Tabulation Example:
python
Copy code
def lis(arr):
n = len(arr)
dp = [1] * n

for i in range(1, n):


for j in range(i):
if arr[i] > arr[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)

5. Advanced Dynamic Programming Algorithms


5.1 Matrix Chain Multiplication
This problem involves finding the most efficient way to multiply a sequence of matrices. The
objective is to minimize the number of scalar multiplications.
Tabulation Example:
python
Copy code
def matrixChainMultiplication(p):
n = len(p) - 1
dp = [[0] * n for _ in range(n)]

for length in range(2, n+1):


for i in range(n - length + 1):
j = i + length - 1
dp[i][j] = float('inf')
for k in range(i, j):
q = dp[i][k] + dp[k+1][j] + p[i] * p[k+1] * p[j+1]
dp[i][j] = min(dp[i][j], q)
return dp[0][n-1]
5.2 Floyd-Warshall Algorithm
The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of vertices in a
graph. It can handle graphs with negative edge weights (as long as there are no negative weight
cycles).
Tabulation Example:
python
Copy code
def floydWarshall(graph):
n = len(graph)
dist = [[float('inf')] * n for _ in range(n)]

for i in range(n):
dist[i][i] = 0

for u in range(n):
for v, weight in graph[u]:
dist[u][v] = weight

for k in range(n):
for i in range(n):
for j in range(n):
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

return dist

6. Applications of Dynamic Programming


Dynamic programming is used in a variety of fields:
• Computational Biology: LCS and sequence alignment algorithms are used in DNA
sequence comparison.
• Economics: DP is used in solving problems like optimal pricing and resource allocation.
• Operations Research: Used in problems like optimal inventory management, shortest path,
and job scheduling.
• Artificial Intelligence: DP plays a key role in decision-making processes, such as in game
theory and reinforcement learning.

7. Conclusion
Dynamic programming is a powerful technique that allows solving complex problems by breaking
them into smaller, manageable subproblems. By storing the results of subproblems, DP significantly
reduces computation time, making it an essential tool in optimization, algorithm design, and
problem-solving.
For problems that involve optimization or counting and have overlapping subproblems and optimal
substructure, DP is often the key to an efficient solution.

You might also like