NetworkX Reference
Release 1.11
Aric Hagberg, Dan Schult, Pieter Swart
Jul 05, 2017
Contents
1 Overview
.
.
1.1 Who uses NetworkX? .
1.2 Goals .
.
1.3
1.4
1.5 History .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The Python programming language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Free software .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2 Introduction
2.1 NetworkX Basics .
2.2 Nodes and Edges .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Graph types
3.1 Which graph class should I use? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2
Basic graph types .
.
.
.
.
.
.
.
1
1
1
2
2
2
3
3
4
9
9
9
4 Algorithms
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4.1 Approximation .
.
4.2 Assortativity .
.
.
Bipartite
4.3
.
.
.
Blockmodeling .
4.4
.
.
.
Boundary .
4.5
.
.
.
4.6
Centrality .
.
.
.
.
.
.
Chordal .
4.7
.
.
.
Clique
4.8
.
.
.
.
.
.
Clustering .
4.9
.
.
4.10 Coloring .
.
.
.
.
.
4.11 Communities .
.
.
4.12 Components
.
.
.
4.13 Connectivity .
.
4.14 Cores .
.
.
4.15 Cycles
.
.
.
4.16 Directed Acyclic Graphs .
4.17 Distance Measures .
.
4.18 Distance-Regular Graphs .
.
4.19 Dominance .
.
.
4.20 Dominating Sets .
4.21 Eulerian .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
133
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
i
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
4.22 Flows .
4.23 Graphical degree sequence .
.
.
.
4.24 Hierarchy .
.
.
.
.
.
4.25 Hybrid .
.
.
.
4.26 Isolates .
.
.
.
.
.
4.27 Isomorphism .
.
.
4.28 Link Analysis .
.
.
.
4.29 Link Prediction .
.
.
.
4.30 Matching .
.
4.31 Minors .
.
.
.
.
.
4.32 Maximal independent set .
.
4.33 Minimum Spanning Tree .
.
.
4.34 Operators .
.
4.35 Rich Club .
.
.
.
.
.
4.36 Shortest Paths
.
.
4.37 Simple Paths .
.
.
.
4.38 Swap .
.
4.39 Traversal
.
.
.
.
.
.
.
4.40 Tree .
.
.
.
.
4.41 Triads .
.
4.42 Vitality .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5 Functions
.
.
.
.
5.1 Graph .
.
5.2 Nodes .
.
5.3
Edges .
5.4 Attributes .
5.5
.
.
.
.
Freezing graph structure .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6 Graph generators
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Classic .
.
Expanders
Small
.
.
Random Graphs
.
.
.
.
.
.
Random Clustered .
.
.
.
.
.
.
.
.
.
6.1 Atlas
.
.
6.2
.
.
6.3
.
.
6.4
.
.
6.5
.
.
6.6 Degree Sequence .
.
6.7
.
.
.
6.8 Directed .
.
.
.
6.9 Geometric
.
.
6.10 Line Graph .
.
.
6.11 Ego Graph .
.
.
6.12 Stochastic .
.
.
.
6.13 Intersection .
.
6.14 Social Networks .
6.15 Community .
.
.
6.16 Non Isomorphic Trees .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7 Linear algebra
.
.
Laplacian Matrix .
Spectrum .
.
.
7.1 Graph Matrix .
.
7.2
7.3
.
7.4 Algebraic Connectivity .
.
7.5 Attribute Matrices
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
375
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
383
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
427
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
.
8 Converting to and from other data formats
439
ii
Lists
8.1
8.2 Dictionaries .
.
8.3
.
.
8.4 Numpy .
8.5
.
.
.
.
8.6
To NetworkX Graph .
.
.
.
.
.
Scipy .
Pandas
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
9 Reading and writing graphs
.
.
.
.
.
.
.
.
9.1 Adjacency List
.
9.2 Multiline Adjacency List .
.
.
9.3
Edge List .
.
.
9.4 GEXF .
.
.
.
9.5 GML .
.
.
9.6
Pickle .
.
.
.
.
9.7 GraphML .
.
.
.
.
9.8
.
.
.
9.9
.
.
9.10 YAML .
.
.
.
9.11 SparseGraph6 .
.
.
9.12 Pajek .
.
9.13 GIS Shapefile .
.
.
JSON .
LEDA .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
10 Drawing
.
.
.
.
10.1 Matplotlib .
.
10.2 Graphviz AGraph (dot) .
.
10.3 Graphviz with pydot
10.4 Graph Layout .
.
.
.
.
.
.
.
11 Exceptions
11.1 Exceptions .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
453
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
493
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
513
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513
.
12 Utilities
.
.
.
.
.
.
.
515
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
12.1 Helper Functions .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
.
12.2 Data Structures and Algorithms .
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516
12.3 Random Sequence Generators .
.
.
.
12.4 Decorators .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520
.
12.5 Cuthill-Mckee Ordering .
12.6 Context Managers
.
.
.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
13 License
14 Citing
15 Credits
15.1 Contributions .
15.2 Support .
.
.
.
16 Glossary
Python Module Index
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
527
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
523
525
531
533
iii
iv
CHAPTER 1
Overview
NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics,
and function of complex networks.
With NetworkX you can load and store networks in standard and nonstandard data formats, generate many types of
random and classic networks, analyze network structure, build network models, design new network algorithms, draw
networks, and much more.
1.1 Who uses NetworkX?
The potential audience for NetworkX includes mathematicians, physicists, biologists, computer scientists, and social
scientists. Good reviews of the state-of-the-art in the science of complex networks are presented in Albert and Barabási
[BA02], Newman [Newman03], and Dorogovtsev and Mendes [DM03]. See also the classic texts [Bollobas01],
[Diestel97] and [West01] for graph theoretic results and terminology. For basic graph algorithms, we recommend the
texts of Sedgewick, e.g. [Sedgewick01] and [Sedgewick02] and the survey of Brandes and Erlebach [BE05].
1.2 Goals
NetworkX is intended to provide
• tools for the study of the structure and dynamics of social, biological, and infrastructure networks,
• a standard programming interface and graph implementation that is suitable for many applications,
• a rapid development environment for collaborative, multidisciplinary projects,
• an interface to existing numerical algorithms and code written in C, C++, and FORTRAN,
• the ability to painlessly slurp in large nonstandard data sets.
1
NetworkX Reference, Release 1.11
1.3 The Python programming language
Python is a powerful programming language that allows simple and flexible representations of networks, and clear and
concise expressions of network algorithms (and other algorithms too). Python has a vibrant and growing ecosystem
of packages that NetworkX uses to provide more features such as numerical linear algebra and drawing. In addition
Python is also an excellent “glue” language for putting together pieces of software from other languages which allows
reuse of legacy code and engineering of high-performance algorithms [Langtangen04].
Equally important, Python is free, well-supported, and a joy to use.
In order to make the most out of NetworkX you will want to know how to write basic programs in Python. Among
the many guides to Python, we recommend the documentation at http://www.python.org and the text by Alex Martelli
[Martelli03].
1.4 Free software
NetworkX is free software; you can redistribute it and/or modify it under the terms of the BSD License. We welcome
contributions from the community. Information on NetworkX development is found at the NetworkX Developer Zone
at Github https://github.com/networkx/networkx
1.5 History
NetworkX was born in May 2002. The original version was designed and written by Aric Hagberg, Dan Schult, and
Pieter Swart in 2002 and 2003. The first public release was in April 2005.
Many people have contributed to the success of NetworkX. Some of the contributors are listed in the credits.
1.5.1 What Next
• A Brief Tour
• Installing
• Reference
• Examples
2
Chapter 1. Overview