Networkx : Convert multigraph into simple graph with weighted edges

前端 未结 3 768
名媛妹妹
名媛妹妹 2020-12-08 08:10

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 fin

3条回答
  •  失恋的感觉
    2020-12-08 08:50

    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.

提交回复
热议问题