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
+217Lines changed: 217 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -234,3 +234,220 @@ 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)}.")
364
+
```
365
+
366
+
#### Output
367
+
```
368
+
Minimum number of multiplications is 18.
369
+
```
370
+
371
+
## **Complexity Analysis:**
372
+
-**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`
373
+
-**Space Complexity:** O(n^2) for both top-down and bottom-up approaches
374
+
375
+
# 7. Optimal Binary Search Tree
376
+
377
+
The Matrix Chain Multiplication finds the optimal way to multiply a sequence of matrices to minimize the number of scalar multiplications.
378
+
379
+
**Algorithm Overview:**
380
+
-**Base Cases:** The cost of a single key is its frequency.
381
+
-**Memoization:** Store the results of previously computed subproblems to avoid redundant computations.
382
+
-**Recurrence Relation:** Compute the optimal cost by trying each key as the root and choosing the minimum cost.
383
+
384
+
## Optimal Binary Search Tree Code in Python (Top-Down Approach with Memoization)
385
+
386
+
```python
387
+
defoptimal_bst(keys, freq, memo={}):
388
+
n =len(keys)
389
+
defcompute_cost(i, j):
390
+
if (i, j) in memo:
391
+
return memo[(i, j)]
392
+
if i > j:
393
+
return0
394
+
if i == j:
395
+
return freq[i]
396
+
memo[(i, j)] =float('inf')
397
+
total_freq =sum(freq[i:j+1])
398
+
for r inrange(i, j +1):
399
+
cost = (compute_cost(i, r -1) +
400
+
compute_cost(r +1, j) +
401
+
total_freq)
402
+
if cost < memo[(i, j)]:
403
+
memo[(i, j)] = cost
404
+
return memo[(i, j)]
405
+
return compute_cost(0, n -1)
406
+
407
+
keys = [10, 12, 20]
408
+
freq = [34, 8, 50]
409
+
print(f"Cost of Optimal BST is {optimal_bst(keys, freq)}.")
410
+
```
411
+
412
+
#### Output
413
+
```
414
+
Cost of Optimal BST is 142.
415
+
```
416
+
417
+
## Optimal Binary Search Tree Code in Python (Bottom-Up Approach)
418
+
419
+
```python
420
+
defoptimal_bst(keys, freq):
421
+
n =len(keys)
422
+
cost = [[0for x inrange(n)] for y inrange(n)]
423
+
424
+
for i inrange(n):
425
+
cost[i][i] = freq[i]
426
+
427
+
for L inrange(2, n +1):
428
+
for i inrange(n - L +1):
429
+
j = i + L -1
430
+
cost[i][j] =float('inf')
431
+
total_freq =sum(freq[i:j+1])
432
+
for r inrange(i, j +1):
433
+
c = (cost[i][r -1] if r > i else0) + \
434
+
(cost[r +1][j] if r < j else0) + \
435
+
total_freq
436
+
if c < cost[i][j]:
437
+
cost[i][j] = c
438
+
439
+
return cost[0][n -1]
440
+
441
+
keys = [10, 12, 20]
442
+
freq = [34, 8, 50]
443
+
print(f"Cost of Optimal BST is {optimal_bst(keys, freq)}.")
444
+
```
445
+
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