22

私は加重グラフを持っています:

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 などのノードを 1 つ削除する(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はほんの一例です。グラフの種類に合わせたプログラムが必要です。さらに、プログラムは反復可能である必要があります。つまり、元のグラフから毎回 1 つのノードを削除できます。

4

1 に答える 1

35

エッジの重みには、G [u] [v] ['weight']としてアクセスするか、エッジデータを反復処理することでアクセスできます。だからあなたは例えば

In [1]: import networkx as nx

In [2]: G=nx.DiGraph()

In [3]: G.add_edge(1,2,weight=10)

In [4]: G.add_edge(2,3,weight=20)

In [5]: G[2][3]['weight']
Out[5]: 20

In [6]: G[2][3]['weight']=200

In [7]: G[2][3]['weight']
Out[7]: 200

In [8]: G.edges(data=True)
Out[8]: [(1, 2, {'weight': 10}), (2, 3, {'weight': 200})]

In [9]: for u,v,d in G.edges(data=True):
   ...:     d['weight']+=7
   ...:     
   ...:     

In [10]: G.edges(data=True)
Out[10]: [(1, 2, {'weight': 17}), (2, 3, {'weight': 207})]
于 2010-11-04T15:05:29.787 に答える