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
print(f"Minimum number of multiplications is {matrix_chain_order(p)}.")
327
-
# Output: Minimum number of multiplications is 18.
328
337
```
338
+
339
+
#### Output
340
+
```
341
+
Minimum number of multiplications is 18.
342
+
```
343
+
344
+
329
345
## Matrix Chain Multiplication Code in Python (Bottom-Up Approach)
330
346
```python
331
347
defmatrix_chain_order(p):
@@ -345,13 +361,17 @@ def matrix_chain_order(p):
345
361
346
362
p = [1, 2, 3, 4]
347
363
print(f"Minimum number of multiplications is {matrix_chain_order(p)}.")
348
-
# Output: Minimum number of multiplications is 18.
349
364
```
365
+
366
+
#### Output
367
+
```
368
+
Minimum number of multiplications is 18.
369
+
```
370
+
350
371
## **Complexity Analysis:**
351
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`
352
373
-**Space Complexity:** O(n^2) for both top-down and bottom-up approaches
353
374
354
-
355
375
# 7. Optimal Binary Search Tree
356
376
357
377
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
362
382
-**Recurrence Relation:** Compute the optimal cost by trying each key as the root and choosing the minimum cost.
363
383
364
384
## Optimal Binary Search Tree Code in Python (Top-Down Approach with Memoization)
0 commit comments