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: README.md
-4
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,5 @@
1
1
[](https://bit.ly/heyfoss)
Contributors should go through the [Contributing Guide](https://github.com/animator/learn-python/blob/main/CONTRIBUTING.md) to learn how you can contribute to the project.
Copy file name to clipboardExpand all lines: contrib/ds-algorithms/dynamic-programming.md
+217
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
-**Time Complexity:** O(n+k) for all cases.No matter how the elements are placed in the array, the algorithm goes through n+k times
563
563
-**Space Complexity:** O(max). Larger the range of elements, larger is the space complexity.
564
+
565
+
566
+
## 9. Cyclic Sort
567
+
568
+
### Theory
569
+
Cyclic Sort is an in-place sorting algorithm that is useful for sorting arrays where the elements are in a known range (e.g., 1 to N). The key idea behind the algorithm is that each number should be placed at its correct index. If we find a number that is not at its correct index, we swap it with the number at its correct index. This process is repeated until every number is at its correct index.
570
+
571
+
### Algorithm
572
+
- Iterate over the array from the start to the end.
573
+
- For each element, check if it is at its correct index.
574
+
- If it is not at its correct index, swap it with the element at its correct index.
575
+
- Continue this process until the element at the current index is in its correct position. Move to the next index and repeat the process until the end of the array is reached.
576
+
577
+
### Steps
578
+
- Start with the first element.
579
+
- Check if it is at the correct index (i.e., if arr[i] == i + 1).
580
+
- If it is not, swap it with the element at the index arr[i] - 1.
581
+
- Repeat step 2 for the current element until it is at the correct index.
582
+
- Move to the next element and repeat the process.
This is because in each cycle, each element is either placed in its correct position or a swap is made. Since each element is swapped at most once, the total number of swaps (and hence the total number of operations) is linear in the number of elements.
614
+
615
+
**Space Complexity:**
616
+
617
+
The space complexity of Cyclic Sort is **O(1)**.
618
+
This is because the algorithm only requires a constant amount of additional space beyond the input array.
0 commit comments