我有一个加权图:
F=nx.path_graph(10)
G=nx.Graph()
for (u, v) in F.edges():
G.add_edge(u,v,weight=1)
获取节点列表:
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]
我想通过这条规则改变每条边的权重:
删除一个节点,比如节点5,很明显,边(4, 5)
,和(5, 6)
将被删除,每条边的权重变为:
{# these edges are nearby the deleted edge (4, 5) and (5, 6)
(3,4):'weight'=1.1,
(6,7):'weight'=1.1,
#these edges are nearby the edges above mentioned
(2,3):'weight'=1.2,
(7,8):'weight'=1.2,
#these edges are nearby the edges above mentioned
(1,2):'weight'=1.3,
(8,9):'weight'=1.3,
# this edge is nearby (1,2)
(0,1):'weight'=1.4}
这个算法怎么写?
path_graph
只是一个例子。我需要一个适合任何图形类型的程序。此外,程序需要是可迭代的,这意味着我每次都可以从原始图中删除一个节点。