Networkx:将多图转换成具有加权边的简单图

时间:2023-02-03 19:03:27

I have a multigraph object and would like to convert it to a simple graph object with weighted edges. I have looked through the networkx documentation and can't seem to find a built in function to achieve this. I was just wondering if anyone knew of a built-in function in networkx that could achieve this goal. I looked at the to_directed() , to_undirected() functions but they don't serve my goal.

我有一个多图对象,我想把它转换成一个具有加权边的简单图形对象。我已经浏览了networkx文档,并且似乎找不到一个内置函数来实现这一点。我只是想知道是否有人知道networkx中的一个内置函数可以实现这个目标。我查看了to_direct()、to_undirect()函数,但它们不能满足我的目标。

3 个解决方案

#1


15  

Here is one way to create a weighted graph from a weighted multigraph by summing the weights:

这里有一种方法可以通过将权重相加,从加权多图中创建一个加权图:

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(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})]

#2


12  

One very simple way of doing it is just to pass your multigraph as input to Graph.

一种很简单的方法就是把多图作为输入传递给图。

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)

This will create an undirected graph of your multigraph where multiple edges are merged into single edges. However, if you have different attributes for the edges that get merged, I don't know if there's any way of determining which attribute is kept.

这将创建一个多图的无向图,其中多个边合并为单个边。但是,如果合并的边有不同的属性,我不知道是否有任何方法可以确定保留哪个属性。

#3


-3  

You can use igraph library. Download python extension module from here: http://igraph.sourceforge.net/download.html

你可以使用igraph库。从这里下载python扩展模块:http://igraph.sourceforge.net/download.html。

#1


15  

Here is one way to create a weighted graph from a weighted multigraph by summing the weights:

这里有一种方法可以通过将权重相加,从加权多图中创建一个加权图:

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(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})]

#2


12  

One very simple way of doing it is just to pass your multigraph as input to Graph.

一种很简单的方法就是把多图作为输入传递给图。

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)

This will create an undirected graph of your multigraph where multiple edges are merged into single edges. However, if you have different attributes for the edges that get merged, I don't know if there's any way of determining which attribute is kept.

这将创建一个多图的无向图,其中多个边合并为单个边。但是,如果合并的边有不同的属性,我不知道是否有任何方法可以确定保留哪个属性。

#3


-3  

You can use igraph library. Download python extension module from here: http://igraph.sourceforge.net/download.html

你可以使用igraph库。从这里下载python扩展模块:http://igraph.sourceforge.net/download.html。