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() ...

Get Data Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.