Advertisement
Uncleeee

Untitled

May 5th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. from random import randint
  2. from copy import copy, deepcopy
  3. from pprint import pprint
  4.  
  5. matrix = [
  6. [0, 1, 0, 0, 1],
  7. [1, 0, 1, 0, 0],
  8. [0, 1, 0, 1, 0],
  9. [0, 0, 1, 0, 1],
  10. [1, 0, 0, 1, 0]
  11. ]
  12.  
  13. cycle = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 1]]
  14.  
  15.  
  16. def random_graph():
  17. """
  18. Составлем новую зашифрованную матрицу на основе изначальной
  19. :return:
  20. """
  21. _list = []
  22. _dict_no = dict()
  23. _dict_on = dict()
  24. # случайным образом выбираем обозначения вершин
  25. for i in range(1, len(matrix)+1):
  26. value = randint(1, len(matrix))
  27. while value in _list:
  28. value = randint(1, len(matrix))
  29. _list.append(value)
  30. _dict_no[value] = i
  31. _dict_on[i] = value
  32. rand_matrix = []
  33. # создаем новую матрицу в соответствии с новыми номерами вершин
  34. for i in range(len(matrix)):
  35. _list_m = []
  36. for j in range(len(matrix)):
  37. value = 0 if i+1 == j+1 else matrix[_dict_no[i+1]-1][_dict_no[j+1]-1]
  38. # добавляем случайные числа
  39. _list_m.append(str(value) + str(randint(1, 5)))
  40. rand_matrix.append(_list_m)
  41. # вовзращаем матрицу H и словари соответствия с изначальной матрицей
  42. return rand_matrix, _dict_no, _dict_on
  43.  
  44.  
  45. def chiper(rand_matrix, N):
  46. """
  47. Шифруем матрицу
  48. :param rand_matrix:
  49. :param N:
  50. :return:
  51. """
  52. F = deepcopy(rand_matrix)
  53. for i in range(len(F)):
  54. for j in range(len(F)):
  55. F[i][j] = (int(F[i][j]) ** 3) % N
  56. return F
  57.  
  58.  
  59. def check_morf(H1, F, N, d):
  60. """
  61. Доказательство изоморфизма
  62. :param H1:
  63. :param F:
  64. :param N:
  65. :param d:
  66. :return:
  67. """
  68. test = deepcopy(H1)
  69. for i in range(len(test)):
  70. for j in range(len(test)):
  71. test[i][j] = (int(test[i][j]) ** d) % N
  72. return True if test == F else False
  73.  
  74.  
  75. def get_H(H1):
  76. test = deepcopy(H1)
  77. for i in range(len(test)):
  78. for j in range(len(test)):
  79. test[i][j] = int(test[i][j]) // 10
  80. return test
  81.  
  82.  
  83. def check2(H1, dict_no):
  84. """
  85. Убеждаемся что H и G это один и тот же граф
  86. :param H1:
  87. :param dict_no:
  88. :return:
  89. """
  90. test = get_H(H1)
  91. test_matrix = []
  92. for i in range(len(test)):
  93. _list_m = []
  94. for j in range(len(test)):
  95. value = 0 if i + 1 == j + 1 else test[dict_no[i + 1] - 1][dict_no[j + 1] - 1]
  96. _list_m.append(value)
  97. test_matrix.append(_list_m)
  98. return True if test_matrix == matrix else False
  99.  
  100.  
  101. def new_cycle(dict_on):
  102. """
  103. В соответствии со словарем соответстия изначальной матрицы
  104. и зашифрованной составляем новый гамльтонов цикл
  105. :param dict_on:
  106. :return:
  107. """
  108. new_cycle = []
  109. for c in cycle:
  110. new_cycle.append([dict_on[c[0]], dict_on[c[1]]])
  111. return new_cycle
  112.  
  113.  
  114. def check_cycle(new_cycle, F, H1, N, d):
  115. """
  116. Проверка гамильтонова цикла
  117. :param new_cycle:
  118. :param F:
  119. :param H1:
  120. :param N:
  121. :param d:
  122. :return:
  123. """
  124. for i in range(len(new_cycle)):
  125. new_cycle[i].append(int(H1[new_cycle[i][0]-1][new_cycle[i][1]-1]))
  126.  
  127. for i in range(len(new_cycle)):
  128. t = (new_cycle[i][2] ** d) % N
  129. if F[new_cycle[i][0]-1][new_cycle[i][1]-1] != t:
  130. return False
  131. return True
  132.  
  133.  
  134. def check_cycle_2(new_cycle):
  135. """
  136. Проверка что гамильтонов цикл проходит через каждую вершину только один раз
  137. :param new_cycle:
  138. :return:
  139. """
  140. test = []
  141. for c in new_cycle:
  142. if c[1] not in test:
  143. test.append(c[1])
  144. else:
  145. return False
  146. return True
  147.  
  148.  
  149. if __name__ == '__main__':
  150. H1, dict_no, dict_on = random_graph()
  151. new_cycle = new_cycle(dict_on)
  152. N = 55
  153. d = 3
  154. F = chiper(H1, N)
  155. check = check_morf(H1, F, N, d)
  156. check_2 = check2(H1, dict_on)
  157. check_3 = check_cycle(new_cycle, F, H1, N, d)
  158. check_4 = check_cycle_2(new_cycle)
  159. print('Исходная матрица')
  160. pprint(matrix)
  161. print()
  162. print('Гамильтонов цикл')
  163. print(cycle)
  164. print()
  165. print('Матрица H')
  166. pprint(H1)
  167. print()
  168. print('Гамильтонов цикл')
  169. print(new_cycle)
  170. print()
  171. print('Матрица F')
  172. pprint(F)
  173. print('Проверка на изоморфность:', check)
  174. print('H и G один и тот же граф:', check_2)
  175. print('Проверка наличия цикла:', check_3)
  176. print('Проверка, что цикл гамильтонов:', check_4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement