user2667066
user2667066

Reputation: 2109

Reading MultiDiGraph as dict-of-dicts ignores multigraph_input

In NetworkX 2.5 I can't seem to read in a multigraph using a dict-of-dicts. Here's an example trying to round-trip some data:

G = nx.MultiDiGraph()
G.add_edge(2, 3, weight=5)
G.add_edge(2, 3, weight=4)  # Create 2 edges

dict_of_dicts = nx.to_dict_of_dicts(G)
G2 = nx.MultiDiGraph(dict_of_dicts, multigraph_input=True)

print("Original", G.edges(data=True))
print("Round tripped", G2.edges(data=True))
print(G2.number_of_edges(), "edge(s) in round tripped graph")

Giving only a single edge in the round-tripped graph G2:

Original [(2, 3, {'weight': 5}), (2, 3, {'weight': 4})]
Round tripped [(2, 3, {0: {'weight': 5}, 1: {'weight': 4}})]
1 edge(s) in round tripped graph

So what's the format for reading in a MultiDiGraph as a dict-of-dicts?

Upvotes: 0

Views: 174

Answers (1)

Sparky05
Sparky05

Reputation: 4892

For me using the nx.from_dict_of_dicts with both create_using and multigraph_input parameters used worked:

import networkx as nx

G = nx.MultiDiGraph()
G.add_edge(2, 3, weight=5)
G.add_edge(2, 3, weight=4)  # Create 2 edges

dict_of_dicts = nx.to_dict_of_dicts(G)
G2 = nx.from_dict_of_dicts(dict_of_dicts, create_using=nx.MultiDiGraph(), multigraph_input=True)

Upvotes: 1

Related Questions