Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import numpy as np
  2.  
  3. N = np.array([[0,1,1,1,1,1,0,0],
  4.      [0,0,1,0,1,0,0,0],
  5.      [0,0,0,1,0,0,0,0],
  6.      [0,0,0,0,1,0,0,0],
  7.      [0,0,0,0,0,1,0,0],
  8.      [0,0,1,0,0,0,1,1],
  9.      [0,0,0,0,0,1,0,1],
  10.      [0,0,0,0,0,1,1,0]])
  11.  
  12.  
  13. def algo (W):
  14.     n=len(W)
  15.     A = [[W[i][j] for j in range(n)] for i in range(n)]
  16.     for k in range(n):
  17.         for i in range(n):
  18.             for j in range(n):
  19.                 A[i][j] = min(A[i][j], A[i][k] + A[k][j])
  20.     return k
  21. def main():
  22.     tree = None
  23.  
  24.     print('matrix1\n')
  25.     print('matrix2\n')
  26.     print('matrix3\n')
  27.     print('matrix4\n')
  28.     print('matrix5\n')
  29.     print('To end the process enter:- quit \n ')
  30.  
  31.     while True:
  32.         do = input('What would you like to do? ').split()
  33.    
  34.         operation = do[0].strip().lower()
  35.         if operation == 'matrix1':
  36.             print('Диаметр графа равен:', algo([
  37.                 [0,1,1,1,1,1,0,0],
  38.              [0,0,1,0,1,0,0,0],
  39.              [0,0,0,1,0,0,0,0],
  40.              [0,0,0,0,1,0,0,0],
  41.              [0,0,0,0,0,1,0,0],
  42.              [0,0,1,0,0,0,1,1],
  43.              [0,0,0,0,0,1,0,1],
  44.              [0,0,0,0,0,1,1,0]]))
  45.            
  46.            
  47.        
  48.  
  49.         elif operation == 'matrix2':
  50.             print('Диаметр графа равен:', algo([
  51.                 [0,1,1,1,1,1],
  52.              [0,0,1,0,1,0],
  53.              [0,0,0,1,0,0],
  54.              [0,0,0,0,1,0],
  55.              [0,0,0,0,0,1],
  56.              [0,0,1,0,0,0]]))
  57.  
  58.         elif operation == 'matrix3':
  59.             print('Диаметр графа равен:', algo([
  60.                 [0,1,0,0,0],
  61.              [0,0,1,1,0],
  62.              [0,0,0,0,1],
  63.              [0,0,0,0,1],  
  64.              [0,0,0,0,1]]))
  65.         elif operation == 'matrix4':
  66.             print('Диаметр графа равен:', algo([
  67.              [0,1,1,1,1,1,0,0,1],
  68.              [0,0,1,0,1,0,0,1,0],
  69.              [0,0,0,1,0,0,1,0,0],
  70.              [0,0,0,0,1,1,0,0,0],
  71.              [0,0,0,0,1,1,0,0,0],
  72.              [0,0,1,1,0,0,1,1,0],
  73.              [0,0,1,0,0,1,0,1,0],
  74.              [0,1,0,0,0,1,0,1,0],
  75.              [1,0,0,0,0,1,1,0,0]]))
  76.         elif operation == 'matrix5':
  77.             print('Диаметр графа равен:', algo([
  78.                 [0,1,0,0],
  79.              [0,0,1,1],  
  80.              [0,1,0,0],  
  81.              [0,0,1,0]]))
  82.        
  83.  
  84.            
  85.         elif operation == 'quit':
  86.             break
  87.  
  88. if __name__ == '__main__':
  89.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement