如果可能,我想获得给定节点的直接依赖节点。
例如,在下面的示例中,nx.ancestors(G, 5)
返回{0, 1, 2, 3, 4}
,这些节点迭代地依赖于节点5
。但是我想获得{3, 4}
,这些节点直接连接到节点5
。
此外,nx.descendants(G, 0)
返回{1, 2, 3, 4, 5}
,在这里我想获得直接连接到节点{1, 2}
的{{1}}。
0
输出:
import networkx as nx
import matplotlib.pyplot as plt
g = nx.Graph()
G = nx.DiGraph()
# add 5 nodes, labeled 0-4:
map(G.add_node, range(5))
# 1,2 depend on 0:
G.add_edge(0,1)
G.add_edge(0,2)
# 3 depends on 1,2
G.add_edge(1,3)
G.add_edge(2,3)
# 4 depends on 1
G.add_edge(1,4)
# 5 depends on 3 and 4
G.add_edge(3,5)
G.add_edge(4,5)
print(nx.ancestors(G, 5))
print(nx.descendants(G, 0))
答案 0 :(得分:1)
您可以使用predecessors
和successors
:
UICollectionView
输出:
set(G.predecessors(5))
然后
{3, 4}
输出:
set(G.successors(0))