Getting started with the networkx graph library
Before we start, if not already done, we need to install the networkx
library using the pip
tool. Execute the following code in its own cell:
!pip install networkx
Note
Note: As always, don't forget to restart the kernel after the installation is complete.
Most of the algorithms provided by networkx
are directly callable from the main module. Therefore a user will only need the following import
statement:
import networkx as nx
Creating a graph
As a starting point, let's review the different types of graphs supported by networkx
and the constructors that create empty graphs:
Graph
: An undirected graph with only one edge between vertices allowed. Self-loop edges are permitted. Constructor example:G = nx.Graph()
Digraph
: Subclass ofGraph
that implements a directed graph. Constructor example:G = nx.DiGraph()
MultiGraph
: Undirected graph that allows multiple edges between vertices. Constructor example:G = nx.MultiGraph()
MultiDiGraph
: Directed graph...