You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contrib/ds-algorithms/dynamic-programming.md
+189Lines changed: 189 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -234,3 +234,192 @@ print("Length of lis is", lis(arr))
234
234
## Complexity Analysis
235
235
-**Time Complexity**: O(n * n) for both approaches, where n is the length of the array.
236
236
-**Space Complexity**: O(n * n) for the memoization table in Top-Down Approach, O(n) in Bottom-Up Approach.
237
+
238
+
# 5. String Edit Distance
239
+
240
+
The String Edit Distance algorithm calculates the minimum number of operations (insertions, deletions, or substitutions) required to convert one string into another.
241
+
242
+
**Algorithm Overview:**
243
+
-**Base Cases:** If one string is empty, the edit distance is the length of the other string.
244
+
-**Memoization:** Store the results of previously computed edit distances to avoid redundant computations.
245
+
-**Recurrence Relation:** Compute the edit distance by considering insertion, deletion, and substitution operations.
246
+
247
+
## String Edit Distance Code in Python (Top-Down Approach with Memoization)
print(f"Minimum number of multiplications is {matrix_chain_order(p)}.")
348
+
# Output: Minimum number of multiplications is 18.
349
+
```
350
+
## **Complexity Analysis:**
351
+
-**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`
352
+
-**Space Complexity:** O(n^2) for both top-down and bottom-up approaches
353
+
354
+
355
+
# 7. Optimal Binary Search Tree
356
+
357
+
The Matrix Chain Multiplication finds the optimal way to multiply a sequence of matrices to minimize the number of scalar multiplications.
358
+
359
+
**Algorithm Overview:**
360
+
-**Base Cases:** The cost of a single key is its frequency.
361
+
-**Memoization:** Store the results of previously computed subproblems to avoid redundant computations.
362
+
-**Recurrence Relation:** Compute the optimal cost by trying each key as the root and choosing the minimum cost.
363
+
364
+
## Optimal Binary Search Tree Code in Python (Top-Down Approach with Memoization)
365
+
```python
366
+
defoptimal_bst(keys, freq, memo={}):
367
+
n =len(keys)
368
+
defcompute_cost(i, j):
369
+
if (i, j) in memo:
370
+
return memo[(i, j)]
371
+
if i > j:
372
+
return0
373
+
if i == j:
374
+
return freq[i]
375
+
memo[(i, j)] =float('inf')
376
+
total_freq =sum(freq[i:j+1])
377
+
for r inrange(i, j +1):
378
+
cost = (compute_cost(i, r -1) +
379
+
compute_cost(r +1, j) +
380
+
total_freq)
381
+
if cost < memo[(i, j)]:
382
+
memo[(i, j)] = cost
383
+
return memo[(i, j)]
384
+
return compute_cost(0, n -1)
385
+
386
+
keys = [10, 12, 20]
387
+
freq = [34, 8, 50]
388
+
print(f"Cost of Optimal BST is {optimal_bst(keys, freq)}.")
389
+
# Output: Cost of Optimal BST is 142.
390
+
```
391
+
## Optimal Binary Search Tree Code in Python (Bottom-Up Approach)
392
+
```python
393
+
defoptimal_bst(keys, freq):
394
+
n =len(keys)
395
+
cost = [[0for x inrange(n)] for y inrange(n)]
396
+
397
+
for i inrange(n):
398
+
cost[i][i] = freq[i]
399
+
400
+
for L inrange(2, n +1):
401
+
for i inrange(n - L +1):
402
+
j = i + L -1
403
+
cost[i][j] =float('inf')
404
+
total_freq =sum(freq[i:j+1])
405
+
for r inrange(i, j +1):
406
+
c = (cost[i][r -1] if r > i else0) + \
407
+
(cost[r +1][j] if r < j else0) + \
408
+
total_freq
409
+
if c < cost[i][j]:
410
+
cost[i][j] = c
411
+
412
+
return cost[0][n -1]
413
+
414
+
keys = [10, 12, 20]
415
+
freq = [34, 8, 50]
416
+
print(f"Cost of Optimal BST is {optimal_bst(keys, freq)}.")
417
+
# Output: Cost of Optimal BST is 142.
418
+
```
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
0 commit comments