Natasha
Natasha

Reputation: 1521

Creating a graph with multi-edge

I am using NetworkX to create a multi-edge graph in Python,

import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
Nodes = [0, 1]
G.add_nodes_from(Nodes)
Edges = [(0,1)]
#Edges  =[(0, 1, 0), (0, 1, 1), (0, 1, 2)] 
G.add_edges_from(Edges)

nx.draw(G)
plt.savefig("path.png")

For a simple graph , defining Edges = [(0,1)] allows me to use G.add_edges_from. But, when multiple edges are defined between the nodes 0 and 1 Edges =[(0, 1, 0), (0, 1, 1), (0, 1, 2)] I couldn't use G.add_edges_from to add edges.

I would like to ask for suggestions on how to create a graph with multi-edges.

Upvotes: 1

Views: 562

Answers (1)

Natasha
Natasha

Reputation: 1521

Changing G = nx.Graph() to G = nx.MultiGraph() helps in resolving the error.

Upvotes: 2

Related Questions