Advertisement
makispaiktis

Dynamic Programming - Τρόποι πολ/σμού πινάκων

May 13th, 2020 (edited)
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. n = 20
  2. # m will be a 20x20 matrix with zeros
  3. m = [[0 for _ in range(n)] for __ in range(n)]
  4.  
  5. # Initialization
  6. # Diagwnia stoixeia = 1
  7. for i in range(n):
  8.     m[i][i] = 1
  9.  
  10.  
  11. '''
  12. for i in range(n):
  13.    for j in range(i+1, n):
  14.        m[i][j] = 0
  15.        for k in range(i, j):
  16.            m[i][j] += m[i][k] * m[k+1][j]
  17.            if m[i][k] == 0 or m[k+1][j] == 0:
  18.                raise Exception('There is a zero element')
  19. '''
  20.  
  21. # To k "deixnei" tin diagwnio pou eimai
  22. # 1η διαγώνιος το i πάει από 0 ως και το 5
  23. # 2η διαγώνιος το ι πάει από 0 ως και το 4
  24. for diag in range(1, n):
  25.     for i in range(0, n-diag):
  26.         j = i+diag
  27.         m[i][j] = 0
  28.         for k in range(i, j):
  29.             m[i][j] += m[i][k] * m[k + 1][j]
  30.             if m[i][k] == 0 or m[k + 1][j] == 0:
  31.                 raise Exception('There is a zero element')
  32.  
  33.  
  34. print(m[0][n-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement