如何获取与networkx中特定节点连接的所有节点和边?

huangapple go评论117阅读模式
英文:

How to get all the nodes and edges connected to a certain node in networkx?

问题

我已在Python的networkx库中创建了一个简单的无向图:

  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. G = nx.Graph()
  4. G.add_nodes_from(["A", "B", "C", "D", "E"])
  5. G.add_edges_from([("A","C"), ("B","D"), ("B","E"), ("C", "E"), ("A", "E"), ("E", "D")])
  6. fig, ax = plt.subplots()
  7. nx.draw_networkx(G, pos=pos, ax=ax)
  8. fig.tight_layout()
  9. plt.show()

我想要返回与某个节点相连的所有节点和边的子图。例如,如果节点是"A",则连接的节点是"C"和"E"。因此,它应该返回这3个节点和它们之间的边。

这是我尝试的内容,遵循了一些其他StackOverflow的答案:

  1. node = "A"
  2. connected_nodes = list(nx.node_connected_component(G, "A"))
  3. connected_edges = G.edges([node])
  4. print(connected_nodes)
  5. print(connected_edges)
  6. H = nx.Graph()
  7. H.add_nodes_from(connected_nodes)
  8. H.add_edges_from(connected_edges)
  9. fig, ax = plt.subplots()
  10. nx.draw_networkx(H, pos=pos, ax=ax)
  11. fig.tight_layout()
  12. plt.show()

当前输出看起来像这样(几乎完成了!),但我得到了比我应该得到的更多的节点。如何修复它?

英文:

I have created a simple undirected graph in Python networkx library:

  1. import networkx as nx
  2. import matplotlib.pyplot as plt
  3. G = nx.Graph()
  4. G.add_nodes_from(["A", "B", "C", "D", "E"])
  5. G.add_edges_from([("A","C"), ("B","D"), ("B","E"), ("C", "E"), ("A", "E"), ("E", "D")])
  6. #pos = nx.random_layout(G)
  7. fig, ax = plt.subplots()
  8. nx.draw_networkx(G, pos=pos, ax=ax)
  9. fig.tight_layout()
  10. plt.show()

I would like to return a subgraph of all the nodes and edges connected to a certain node. As an example, if the node is "A", the connected nodes are "C" and "E". So it should return these 3 nodes and the edges between them.

This is what I tried following a couple other StackOverflow answers:

  1. node = "A"
  2. # connected_nodes = nx.shortest_path(G,node).keys()
  3. connected_nodes = list(nx.node_connected_component(G, "A"))
  4. connected_edges = G.edges([node])
  5. print(connected_nodes)
  6. print(connected_edges)
  7. H = nx.Graph()
  8. H.add_nodes_from(connected_nodes)
  9. H.add_edges_from(connected_edges)
  10. # https://stackoverflow.com/questions/33088008/fetch-connected-nodes-in-a-networkx-graph
  11. # https://stackoverflow.com/questions/63169294/how-to-plot-a-subset-of-nodes-of-a-networkx-graph
  12. fig, ax = plt.subplots()
  13. nx.draw_networkx(H, pos=pos, ax=ax)
  14. fig.tight_layout()
  15. plt.show()

The current output looks like this (almost there!) but I am getting more nodes returned than I should:

如何获取与networkx中特定节点连接的所有节点和边?

How do I fix it?

答案1

得分: 1

@RomanPerekhrest 指出了这一点,它只需简单地删除以下代码:

  1. H.add_nodes_from(connected_nodes)

所以解决方案是:

  1. node = "A"
  2. connected_edges = G.edges([node])
  3. print(connected_edges)
  4. H = nx.Graph()
  5. H.add_edges_from(connected_edges)
  6. fig, ax = plt.subplots()
  7. nx.draw_networkx(H, pos=pos, ax=ax)
  8. fig.tight_layout()
  9. plt.show()
英文:

@RomanPerekhrest pointed it out as comment, it was a simply as taking out

  1. H.add_nodes_from(connected_nodes)

So the solution is:

  1. node = "A"
  2. connected_edges = G.edges([node])
  3. print(connected_edges)
  4. H = nx.Graph()
  5. H.add_edges_from(connected_edges)
  6. fig, ax = plt.subplots()
  7. nx.draw_networkx(H, pos=pos, ax=ax)
  8. fig.tight_layout()
  9. plt.show()

huangapple
  • 本文由 发表于 2023年3月9日 19:33:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定