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

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

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

问题

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

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_nodes_from(["A", "B", "C", "D", "E"])
G.add_edges_from([("A","C"), ("B","D"), ("B","E"), ("C", "E"), ("A", "E"), ("E", "D")])

fig, ax = plt.subplots()
nx.draw_networkx(G, pos=pos, ax=ax)
fig.tight_layout()
plt.show()

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

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

node = "A"
connected_nodes = list(nx.node_connected_component(G, "A"))
connected_edges = G.edges([node])
print(connected_nodes)
print(connected_edges)

H = nx.Graph()
H.add_nodes_from(connected_nodes)
H.add_edges_from(connected_edges)

fig, ax = plt.subplots()
nx.draw_networkx(H, pos=pos, ax=ax)
fig.tight_layout()
plt.show()

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

英文:

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

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_nodes_from(["A", "B", "C", "D", "E"])
G.add_edges_from([("A","C"), ("B","D"), ("B","E"), ("C", "E"), ("A", "E"), ("E", "D")])
#pos = nx.random_layout(G)

fig, ax = plt.subplots()
nx.draw_networkx(G, pos=pos, ax=ax)
fig.tight_layout()
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:

node = "A"
# connected_nodes = nx.shortest_path(G,node).keys()
connected_nodes = list(nx.node_connected_component(G, "A"))
connected_edges = G.edges([node])
print(connected_nodes)
print(connected_edges)

H = nx.Graph()
H.add_nodes_from(connected_nodes)
H.add_edges_from(connected_edges)

# https://stackoverflow.com/questions/33088008/fetch-connected-nodes-in-a-networkx-graph
# https://stackoverflow.com/questions/63169294/how-to-plot-a-subset-of-nodes-of-a-networkx-graph
fig, ax = plt.subplots()
nx.draw_networkx(H, pos=pos, ax=ax)
fig.tight_layout()
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 指出了这一点,它只需简单地删除以下代码:

H.add_nodes_from(connected_nodes)

所以解决方案是:

node = "A"
connected_edges = G.edges([node])
print(connected_edges)

H = nx.Graph()
H.add_edges_from(connected_edges)

fig, ax = plt.subplots()
nx.draw_networkx(H, pos=pos, ax=ax)
fig.tight_layout()
plt.show()
英文:

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

H.add_nodes_from(connected_nodes)

So the solution is:

node = "A"
connected_edges = G.edges([node])
print(connected_edges)

H = nx.Graph()
H.add_edges_from(connected_edges)

fig, ax = plt.subplots()
nx.draw_networkx(H, pos=pos, ax=ax)
fig.tight_layout()
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:

确定