Skip to content

Commit 59e2353

Browse files
authored
Update dynamic-programming.md
1 parent bd2635b commit 59e2353

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

contrib/ds-algorithms/dynamic-programming.md

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,13 @@ def edit_distance(str1, str2, memo={}):
265265
str1 = "sunday"
266266
str2 = "saturday"
267267
print(f"Edit Distance between '{str1}' and '{str2}' is {edit_distance(str1, str2)}.")
268-
# Output: Edit Distance between 'sunday' and 'saturday' is 3.
269268
```
269+
270+
#### Output
271+
```
272+
Edit Distance between 'sunday' and 'saturday' is 3.
273+
```
274+
270275
## String Edit Distance Code in Python (Bottom-Up Approach)
271276
```python
272277
def edit_distance(str1, str2):
@@ -289,8 +294,13 @@ def edit_distance(str1, str2):
289294
str1 = "sunday"
290295
str2 = "saturday"
291296
print(f"Edit Distance between '{str1}' and '{str2}' is {edit_distance(str1, str2)}.")
292-
# Output: Edit Distance between 'sunday' and 'saturday' is 3.
293297
```
298+
299+
#### Output
300+
```
301+
Edit Distance between 'sunday' and 'saturday' is 3.
302+
```
303+
294304
## **Complexity Analysis:**
295305
- **Time Complexity:** O(m * n) where m and n are the lengths of string 1 and string 2 respectively
296306
- **Space Complexity:** O(m * n) for both top-down and bottom-up approaches
@@ -324,8 +334,14 @@ def matrix_chain_order(p, memo={}):
324334

325335
p = [1, 2, 3, 4]
326336
print(f"Minimum number of multiplications is {matrix_chain_order(p)}.")
327-
# Output: Minimum number of multiplications is 18.
328337
```
338+
339+
#### Output
340+
```
341+
Minimum number of multiplications is 18.
342+
```
343+
344+
329345
## Matrix Chain Multiplication Code in Python (Bottom-Up Approach)
330346
```python
331347
def matrix_chain_order(p):
@@ -345,13 +361,17 @@ def matrix_chain_order(p):
345361

346362
p = [1, 2, 3, 4]
347363
print(f"Minimum number of multiplications is {matrix_chain_order(p)}.")
348-
# Output: Minimum number of multiplications is 18.
349364
```
365+
366+
#### Output
367+
```
368+
Minimum number of multiplications is 18.
369+
```
370+
350371
## **Complexity Analysis:**
351372
- **Time Complexity:** O(n^3) where n is the number of matrices in the chain. For an `array p` of dimensions representing the matrices such that the `i-th matrix` has dimensions `p[i-1] x p[i]`, n is `len(p) - 1`
352373
- **Space Complexity:** O(n^2) for both top-down and bottom-up approaches
353374

354-
355375
# 7. Optimal Binary Search Tree
356376

357377
The Matrix Chain Multiplication finds the optimal way to multiply a sequence of matrices to minimize the number of scalar multiplications.
@@ -362,6 +382,7 @@ The Matrix Chain Multiplication finds the optimal way to multiply a sequence of
362382
- **Recurrence Relation:** Compute the optimal cost by trying each key as the root and choosing the minimum cost.
363383

364384
## Optimal Binary Search Tree Code in Python (Top-Down Approach with Memoization)
385+
365386
```python
366387
def optimal_bst(keys, freq, memo={}):
367388
n = len(keys)
@@ -386,9 +407,15 @@ def optimal_bst(keys, freq, memo={}):
386407
keys = [10, 12, 20]
387408
freq = [34, 8, 50]
388409
print(f"Cost of Optimal BST is {optimal_bst(keys, freq)}.")
389-
# Output: Cost of Optimal BST is 142.
390410
```
411+
412+
#### Output
413+
```
414+
Cost of Optimal BST is 142.
415+
```
416+
391417
## Optimal Binary Search Tree Code in Python (Bottom-Up Approach)
418+
392419
```python
393420
def optimal_bst(keys, freq):
394421
n = len(keys)
@@ -414,12 +441,13 @@ def optimal_bst(keys, freq):
414441
keys = [10, 12, 20]
415442
freq = [34, 8, 50]
416443
print(f"Cost of Optimal BST is {optimal_bst(keys, freq)}.")
417-
# Output: Cost of Optimal BST is 142.
418444
```
419-
## **Complexity Analysis:**
420-
- **Time Complexity:** O(n^3) where n is the number of keys in the binary search tree.
421-
- **Space Complexity:** O(n^2) for both top-down and bottom-up approaches
422445

423-
</br>
424-
<hr>
425-
</br>
446+
#### Output
447+
```
448+
Cost of Optimal BST is 142.
449+
```
450+
451+
### Complexity Analysis
452+
- **Time Complexity**: O(n^3) where n is the number of keys in the binary search tree.
453+
- **Space Complexity**: O(n^2) for both top-down and bottom-up approaches

0 commit comments

Comments
 (0)