Skip to content

Commit 5552e51

Browse files
committed
Matrix Chain
1 parent fc378ef commit 5552e51

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

DP/Matrix_chain_multiplication.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import sys
2+
n = int(input())
3+
arr = list(map(int,input().split()))
4+
dp = [[0 for i in range(n)]for j in range(n)]
5+
for l in range(2,n):
6+
for i in range(1,n-l+1):
7+
j,dp[i][j] = i+l-1,sys.maxsize
8+
for k in range(i,j):dp[i][j] = min(dp[i][j],dp[i][k]+dp[k+1][j]+arr[i-1]*arr[k]*arr[j])
9+
print("Min no. of scalar mult = ",dp[1][n-1])

0 commit comments

Comments
 (0)