Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # e_lin (c)
- # Инициализация либы и объекта графа
- import networkx as nx
- G = nx.Graph()
- #Чтение файла
- with open('graphedges88.txt') as file:
- for i in file.readlines():
- inp = i.split()
- a = int(inp[0])
- b = int(inp[1])
- G.add_edge(a,b)
- # Вопрос 1
- print("Количество ребер: ", G.number_of_edges())
- # Вопрос 2
- iso = list(nx.isolates(G))
- print("Количество изолятов: ", len(iso))
- iso.sort()
- for i in iso:
- print(i,end = ' ')
- if len(iso)>0:
- print()
- # Вопрос 3
- the_largest_degree_nodes = []
- maxdegree = -1
- for node, degree in G.degree():
- if maxdegree < degree:
- maxdegree = degree
- the_largest_degree_nodes = [node]
- elif maxdegree == degree:
- the_largest_degree_nodes.append(node)
- print("Вершины с самой большой степенью(",maxdegree,"): ",end="")
- for i in the_largest_degree_nodes:
- print(i,end=" ")
- print()
- # Вопрос 4
- r = G.subgraph(list(max(nx.connected_components(G), key=len)))
- print("Диаметр компоненты связности графа: ", nx.diameter(r))
- A = 469
- B = 821
- C = 356
- D = 877
- E = 950
- F = 584
- # Вопрос 5
- def printPath(path):
- for i in path:
- print(i, end="")
- if i != path[-1]:
- print(" -> ",end="")
- print()
- print("Кратчайший путь от A(469) до B(821): ",nx.shortest_path_length(G,A,B))
- printPath(nx.shortest_path(G,A,B))
- # Вопрос 6
- print("Кратчайший путь от C(356) до D(877): ",nx.shortest_path_length(G,C,D))
- printPath(nx.shortest_path(G,C,D))
- # Вопрос 7
- print("Кратчайший путь от E(950) до F(584): ",nx.shortest_path_length(G,E,F))
- printPath(nx.shortest_path(G,E,F))
- #Удаление ребер
- nodes_to_removing = [17*i for i in range(59)]
- nodes_to_removing.extend([450, 323, 389, 491, 756, 534, 446])
- G.remove_nodes_from(nodes_to_removing)
- nodes_to_removing.sort()
- print("Удалены вершины: ",nodes_to_removing)
- # Вопрос 8
- print("Количество ребер: ", G.number_of_edges())
- # Вопрос 9
- iso = list(nx.isolates(G))
- print("Количество изолятов: ", len(iso))
- iso.sort()
- for i in iso:
- print(i,end = ' ')
- print()
- # Вопрос 10
- the_largest_degree_nodes = []
- maxdegree = -1
- for node, degree in G.degree():
- if maxdegree < degree:
- maxdegree = degree
- the_largest_degree_nodes = [node]
- elif maxdegree == degree:
- the_largest_degree_nodes.append(node)
- print("Вершины с самой большой степенью(",maxdegree,"): ",end="")
- for i in the_largest_degree_nodes:
- print(i,end=" ")
- print()
- # Вопрос 11
- r = G.subgraph(list(max(nx.connected_components(G), key=len)))
- print("Диаметр компоненты связности графа: ", nx.diameter(r))
- # Вопрос 12
- print("Кратчайший путь от A(469) до B(821): ",nx.shortest_path_length(G,A,B))
- printPath(nx.shortest_path(G,A,B))
- # Вопрос 13
- print("Кратчайший путь от C(356) до D(877): ",nx.shortest_path_length(G,C,D))
- printPath(nx.shortest_path(G,C,D))
- # Вопрос 14
- print("Кратчайший путь от E(950) до F(584): ",nx.shortest_path_length(G,E,F))
- printPath(nx.shortest_path(G,E,F))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement