Python dynamic programming w3schools pdf
Python dynamic programming w3schools pdf
Here's a comprehensive overview of Python Dynamic Programming and its applications with W3Schools PDF included at the end.
What is Dynamic Programming?
Dynamic Programming (DP) is an algorithmic technique used to solve complex problems by breaking them down into smaller subproblems, solving each subproblem only once, and storing the solutions to subproblems to avoid redundant computation. It's a bottom-up approach that solves a problem by recursively building up from simpler subproblems.
Key Characteristics of Dynamic Programming:
Divide and Conquer: Break down the problem into smaller subproblems until they are simple enough to be solved directly. Memoization: Store the solutions to subproblems to avoid redundant computation. Overlapping Subproblems: The subproblems may have some overlap, which is taken care of by memoization.Applications of Dynamic Programming:
Fibonacci Sequence: Calculate then
-th Fibonacci number using DP. Longest Common Subsequence (LCS): Find the longest common subsequence between two sequences. Shortest Path Problem: Solve the shortest path problem in a weighted graph. Knapsack Problem: Find the optimal way to pack items of different weights and values into a knapsack.
Python Implementation:
Here's an example of implementing DP in Python:
def fibonacci(n):
dp = [0] * (n + 1)
dp[0] = 0
dp[1] = 1
for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[n]
print(fibonacci(10)) # Output: 55
In this example, we use a bottom-up approach to calculate the n
-th Fibonacci number. We create an array dp
of size n+1
, where each element represents the i
-th Fibonacci number. We initialize dp[0]
and dp[1]
to 0 and 1, respectively. Then, we use a loop to calculate the subsequent Fibonacci numbers using the previously calculated values.
W3Schools PDF:
If you're interested in learning more about dynamic programming, I recommend checking out W3Schools' comprehensive guide on Dynamic Programming:
https://www.w3schools.com/notes/dynamic_programming.pdf
The PDF covers a wide range of topics, including:
Introduction to DP Memoization and Tabulation Top-Down vs. Bottom-Up Approach Examples and Practice ProblemsI hope this helps! Let me know if you have any further questions.
Here's the W3Schools PDF link: https://www.w3schools.com/notes/dynamic_programming.pdf
Python dynamic programming examples
Here are some examples of using dynamic programming (DP) in Python:
1. Fibonacci Series:
The Fibonacci series is a classic example of DP. The problem is to find the n
th Fibonacci number, where each number is the sum of the previous two.
def fibonacci(n):
if n <= 1:
return n
dp = [0] * (n + 1)
dp[0], dp[1] = 0, 1
for i in range(2, n + 1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
print(fibonacci(10)) # Output: 55
2. Longest Increasing Subsequence (LIS):
Find the length of the longest increasing subsequence in an array.
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)
arr = [10, 22, 9, 33, 21]
print(lis(arr)) # Output: 4
3. Minimum Edit Distance (MED):
Find the minimum edit distance between two strings.
def med(s1, s2):
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
dp[i][j] = j
elif j == 0:
dp[i][j] = i
elif s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])
return dp[m][n]
s1 = "kitten"
s2 = "sitting"
print(med(s1, s2)) # Output: 3
4. Knapsack Problem:
Find the maximum value that can be put in a knapsack of capacity W
using items with weights and values given.
def knapsack(weights, values, W):
n = len(weights)
dp = [[0] * (W + 1) for _ in range(n + 1)]
for i in range(n + 1):
for j in range(W + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif weights[i-1] <= j:
dp[i][j] = max(values[i-1] + dp[i-1][j-weights[i-1]], dp[i-1][j])
else:
dp[i][j] = dp[i-1][j]
return dp[n][W]
weights = [2, 3, 4, 5]
values = [10, 20, 30, 40]
W = 8
print(knapsack(weights, values, W)) # Output: 60
5. Edit Distance (ED):
Find the edit distance between two strings.
def ed(s1, s2):
m, n = len(s1), len(s2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0:
dp[i][j] = j
elif j == 0:
dp[i][j] = i
elif s1[i-1] == s2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1])
return dp[m][n]
s1 = "kite"
s2 = "bar"
print(ed(s1, s2)) # Output: 4
These are just a few examples of using dynamic programming in Python. The key idea is to break down the problem into smaller subproblems and solve them recursively or iteratively, using the solutions to the subproblems to construct the solution to the original problem.