2017-08-13 90 views
0

有關於通過使用Maehler的代碼NetworkX轉換多重圖以簡單圖表通過平均平行長度

import networkx as nx 

G = nx.MultiGraph() 
G.add_nodes_from([1,2,3]) 
G.add_edges_from([(1, 2), (1, 2), (1, 3), (2, 3), (2, 3)]) 

G2 = nx.Graph(G) 

和另一個使用Aslak和ARIC的代碼求和重量

import networkx as nx 
# weighted MultiGraph 
M = nx.MultiGraph() 
M.add_edge(1,2,weight=7) 
M.add_edge(1,2,weight=19) 
M.add_edge(2,3,weight=42) 
# create weighted graph from M 
G = nx.Graph() 
for u,v,data in M.edges_iter(data=True): 
    w = data['weight'] if 'weight' in data else 1.0 
    if G.has_edge(u,v): 
     G[u][v]['weight'] += w 
    else: 
     G.add_edge(u, v, weight=w) 

print G.edges(data=True) 
# [(1, 2, {'weight': 26}), (2, 3, {'weight': 42})] 
多重圖轉換爲簡單圖的一個解決方案

想知道如何平均平行邊緣的重量?

回答

0

以下是一種類似於使用統計軟件包計算邊緣權重的方法的方法。

import networkx as nx 
from statistics import mean 
# weighted MultiGraph 
M = nx.MultiGraph() 
M.add_edge(1,2,weight=7) 
M.add_edge(1,2,weight=20) 
M.add_edge(2,3,weight=42) 
M.add_edge(2,3) 
# create weighted graph G from M 
G = nx.Graph() 
for u,v,data in M.edges(data=True): 
    if not G.has_edge(u,v): 
     # set weight to 1 if no weight is given for edge in M 
     weight = mean(d.get('weight',1) for d in M.get_edge_data(u,v).values()) 
     G.add_edge(u, v, weight=weight) 
print(G.edges(data=True)) 

OUTPUT(networkx-2.0-DEV)

EdgeView([(1,2,{ '體重':13.5}),(2,3,{ '體重':21.5})] )