Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | None | 0 0
  1. # e_lin (c)
  2.  
  3. # Инициализация либы и объекта графа
  4.  
  5. import networkx as nx
  6. G = nx.Graph()
  7. #Чтение файла
  8.  
  9. with open('graphedges88.txt') as file:
  10.     for i in file.readlines():
  11.         inp = i.split()
  12.         a = int(inp[0])
  13.         b = int(inp[1])
  14.         G.add_edge(a,b)
  15.  
  16. # Вопрос 1
  17.  
  18. print("Количество ребер: ", G.number_of_edges())
  19.  
  20. # Вопрос 2
  21.  
  22. iso = list(nx.isolates(G))
  23. print("Количество изолятов: ", len(iso))
  24. iso.sort()
  25. for i in iso:
  26.     print(i,end = ' ')
  27. if len(iso)>0:
  28.     print()
  29.  
  30. # Вопрос 3
  31.  
  32. the_largest_degree_nodes = []
  33. maxdegree = -1
  34. for node, degree in G.degree():
  35.     if maxdegree < degree:
  36.         maxdegree = degree
  37.         the_largest_degree_nodes = [node]
  38.     elif maxdegree == degree:
  39.         the_largest_degree_nodes.append(node)
  40. print("Вершины с самой большой степенью(",maxdegree,"): ",end="")
  41. for i in the_largest_degree_nodes:
  42.     print(i,end=" ")
  43. print()
  44.  
  45. # Вопрос 4
  46.  
  47. r = G.subgraph(list(max(nx.connected_components(G), key=len)))
  48. print("Диаметр компоненты связности графа: ", nx.diameter(r))
  49.  
  50. A = 469
  51. B = 821
  52. C = 356
  53. D = 877
  54. E = 950
  55. F = 584
  56.  
  57. # Вопрос 5
  58.  
  59. def printPath(path):
  60.     for i in path:
  61.         print(i, end="")
  62.         if i != path[-1]:
  63.             print(" -> ",end="")
  64.     print()
  65. print("Кратчайший путь от A(469) до B(821): ",nx.shortest_path_length(G,A,B))
  66. printPath(nx.shortest_path(G,A,B))
  67.  
  68. # Вопрос 6
  69.  
  70. print("Кратчайший путь от C(356) до D(877): ",nx.shortest_path_length(G,C,D))
  71. printPath(nx.shortest_path(G,C,D))
  72.  
  73. # Вопрос 7
  74.  
  75. print("Кратчайший путь от E(950) до F(584): ",nx.shortest_path_length(G,E,F))
  76. printPath(nx.shortest_path(G,E,F))
  77.  
  78. #Удаление ребер
  79.  
  80.  
  81. nodes_to_removing = [17*i for i in range(59)]
  82. nodes_to_removing.extend([450, 323, 389, 491, 756, 534, 446])
  83. G.remove_nodes_from(nodes_to_removing)
  84. nodes_to_removing.sort()
  85. print("Удалены вершины: ",nodes_to_removing)
  86.  
  87.  
  88. # Вопрос 8
  89.  
  90. print("Количество ребер: ", G.number_of_edges())
  91.  
  92. # Вопрос 9
  93.  
  94. iso = list(nx.isolates(G))
  95. print("Количество изолятов: ", len(iso))
  96. iso.sort()
  97. for i in iso:
  98.     print(i,end = ' ')
  99. print()
  100.  
  101. # Вопрос 10
  102.  
  103. the_largest_degree_nodes = []
  104. maxdegree = -1
  105. for node, degree in G.degree():
  106.     if maxdegree < degree:
  107.         maxdegree = degree
  108.         the_largest_degree_nodes = [node]
  109.     elif maxdegree == degree:
  110.         the_largest_degree_nodes.append(node)
  111.  
  112. print("Вершины с самой большой степенью(",maxdegree,"): ",end="")
  113. for i in the_largest_degree_nodes:
  114.     print(i,end=" ")
  115. print()
  116.  
  117. # Вопрос 11
  118.  
  119. r = G.subgraph(list(max(nx.connected_components(G), key=len)))
  120. print("Диаметр компоненты связности графа: ", nx.diameter(r))
  121.  
  122. # Вопрос 12
  123.  
  124. print("Кратчайший путь от A(469) до B(821): ",nx.shortest_path_length(G,A,B))
  125. printPath(nx.shortest_path(G,A,B))
  126.  
  127. # Вопрос 13
  128.  
  129. print("Кратчайший путь от C(356) до D(877): ",nx.shortest_path_length(G,C,D))
  130. printPath(nx.shortest_path(G,C,D))
  131.  
  132. # Вопрос 14
  133.  
  134. print("Кратчайший путь от E(950) до F(584): ",nx.shortest_path_length(G,E,F))
  135. printPath(nx.shortest_path(G,E,F))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement