Browse Source

Used dynamic programming to solve matrix chain multiplication problem.

pull/16/head^2
jrtechs 6 years ago
parent
commit
0c088b46fe
1 changed files with 55 additions and 0 deletions
  1. +55
    -0
      other/minMul.py

+ 55
- 0
other/minMul.py View File

@ -0,0 +1,55 @@
import math
"""
Jeffery Russell
11-12-18
"""
def minMul(S):
"""
Simple function to print the matrixes used in
the minimum matrix chain multiplication problem
using dynamic programming.
"""
n = len(S)
m = [[math.inf for i in range(n)] for j in range(n)]
c = [[0 for i in range(n)] for j in range(n)]
for i in range(0, n):
m[i][i] = 0
for l in range(1, n + 1):
for i in range(0, n-l + 1):
j = l + i -1
for k in range(i, j):
temp = m[i][k] + m[k + 1][j] + S[i][0] * S[k][1] * S[j][1]
print(temp)
if temp < m[i][j]:
m[i][j] = temp
c[i][j] = k + 1
for i in range(0, n):
for y in range(0, n):
print(str(m[i][y]) + " ", end =" ")
print()
print()
print()
for i in range(0, n):
for y in range(0, n):
print(str(c[i][y]) + " ", end =" ")
print()
"""
Makes sure that other programs don't execute the main
"""
if __name__ == '__main__':
try:
minMul([(10,100),(100, 5),(5, 50)])
#minMul([(5,10),(10,3),(3,12), (12,5), (5,50), (50,6)])
except KeyboardInterrupt:
exit()

Loading…
Cancel
Save