From 0c088b46fe5520e19120c640bee13743f5dc38cc Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 12 Nov 2018 15:26:25 -0500 Subject: [PATCH] Used dynamic programming to solve matrix chain multiplication problem. --- other/minMul.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 other/minMul.py diff --git a/other/minMul.py b/other/minMul.py new file mode 100644 index 0000000..a2434c1 --- /dev/null +++ b/other/minMul.py @@ -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() \ No newline at end of file