Reputation: 175
I'm sure this is very basic, but is it possible to specify an arbitrary number of edges between nodes when constructing a graph? I've been searching under terms: 'directed graph', 'multiple directed graph', 'parallel edges', 'add_edges', etc.
Say, I have four nodes: A, B, C, D and I want to show: A to B, has 600 edges A to C, has 100 edges A to D, has 400 edges
I'm thinking something like:
import networkx as nx
G = nx.MultiDiGraph()
G.add_node('a', {'b': 600, 'c':100, 'd':400})
G.add_node('b')
G.add_node('c')
G.add_node('d')
(Except, obviously this isn't correct.)
Upvotes: 1
Views: 2202
Reputation: 36635
According to documentation: https://networkx.github.io/documentation/development/tutorial/tutorial.html you have to add edges from a list of tuples:
import networkx as nx
a = {'b': 600, 'c':100, 'd':400}
MG=nx.MultiGraph()
MG.add_weighted_edges_from([('a', k, v) for k, v in a.iteritems()], weight='weight')
print MG.edges(data='weight')
Output:
[('a', 'c', 100), ('a', 'b', 600), ('a', 'd', 400)]
Upvotes: 2