Skip to content
Network Science workspace
import networkx as nx
%matplotlib inline
# a "plain" graph is undirected
G = nx.Graph()
# give each a node a 'name', which is a letter in this case.
G.add_node('a')
# the add_nodes_from method allows adding nodes from a sequence, in this case a list
nodes_to_add = ['b', 'c', 'd']
G.add_nodes_from(nodes_to_add)
# add edge from 'a' to 'b'
# since this graph is undirected, the order doesn't matter here
G.add_edge('a', 'b')
# just like add_nodes_from, we can add edges from a sequence
# edges should be specified as 2-tuples
edges_to_add = [('a', 'c'), ('b', 'c'), ('c', 'd')]
G.add_edges_from(edges_to_add)
# draw the graph
nx.draw(G, with_labels=True)
# List all of the nodes
G.nodes()