Learn NetworkX

Create, analyse and explore graphs and networks in Python

15 lessons · 3 hours · 4 levels · covers NetworkX 3.x (3.6+)

NetworkX is the standard pure-Python library for graphs: nodes connected by edges, with attributes on both. It ships hundreds of algorithms for paths, traversal, centrality, communities, flows and DAGs, plus readers and writers for common graph formats. Learn it to model relationships such as social networks, dependencies, road maps and knowledge graphs, and to prototype graph analytics quickly.

Before you start

NetworkX builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics5 lessons · 54 min in total
2. PracticalUse it productively at work, with the usual idioms9 lessons · 2 hours in total
3. AdvancedThe less common features, performance and trade-offs12 lessons · 2 hours in total
4. ExpertInternals and the edge cases experts know15 lessons · 3 hours in total

The NetworkX tree

NetworkXGraphs, nodes and edgesGraphs, nodes andedgesNode and edge attributes, weightsNode and edgeattributes, weightsDirected graphs and multigraphsDirected graphs andmultigraphsBuilding graphs from data and filesBuilding graphs fromdata and filesShortest pathsShortest pathsTraversal and connected componentsTraversal andconnected compon…DAGs: topological order and dependenciesDAGs: topologicalorder and depende…Centrality: degree, betweenness, PageRankCentrality: degree,betweenness, Page…Views, subgraphs and copiesViews, subgraphsand copiesDictionariesDictionariesModules and importsModules and importsIterators and generatorsIterators andgenerators
Essentials and Practical shown (9 of 15 lessons); the full list is below. Click a lesson to read it.

All 15 lessons, in order

Graphs, nodes and edges

  1. Graphs, nodes and edges · Essentials · 10 min

    Create a Graph, add nodes and edges, and query neighbours, degrees and sizes.

  2. Node and edge attributes, weights · Essentials · 10 min

    Attach data such as weights, labels and timestamps to nodes and edges, and read it back with data=True views.

  3. Directed graphs and multigraphs · Essentials · 10 min

    Choose between Graph, DiGraph, MultiGraph and MultiDiGraph, and use successors, predecessors and in/out degree.

  4. Building graphs from data and files · Essentials · 12 min

    Create graphs from edge lists, pandas DataFrames and generators, and read and write formats like edge lists, GraphML and JSON.

  5. Views, subgraphs and copies · Practical · 12 min

    Use live views like G.nodes and G.adj, create subgraph and reversed views cheaply, and know when to copy.

Paths, traversal and DAGs

  1. Shortest paths · Essentials · 12 min

    Find shortest paths and distances, weighted and unweighted, handle unreachable nodes, and compute all-pairs results.

  2. Traversal and connected components · Practical · 12 min

    Walk graphs with BFS and DFS, and split them into connected, weakly connected and strongly connected components.

  3. DAGs: topological order and dependencies · Practical · 12 min

    Model dependencies as directed acyclic graphs, order them topologically, find ancestors and cycles, and compute longest paths.

Network analysis

  1. Centrality: degree, betweenness, PageRank · Practical · 15 min

    Rank important nodes with degree, closeness, betweenness, eigenvector centrality and PageRank, and know what each one measures.

  2. Communities and clustering · Advanced · 15 min

    Detect communities with Louvain and label propagation, score partitions with modularity, and measure clustering.

  3. Flows, cuts and matchings · Advanced · 15 min

    Solve maximum flow and minimum cut problems, and find matchings in bipartite and general graphs.

  4. Graphs as matrices · Advanced · 12 min

    Convert graphs to NumPy and SciPy sparse adjacency matrices, use Laplacians and spectra, and go back from matrices to graphs.

Performance and internals

  1. Inside the graph data structure · Expert · 12 min

    Understand NetworkX's dict-of-dicts adjacency, its memory and speed costs, and how to subclass graphs with custom dict factories.

  2. Algorithm cost and scaling strategies · Expert · 12 min

    Estimate algorithm complexity, use approximations and sampling, and decide when to move to igraph, graph-tool, a graph database or GPU.

  3. Backends and dispatching · Expert · 12 min

    Run NetworkX algorithms on accelerated backends like nx-cugraph and nx-parallel through the dispatch system, without rewriting code.