Release 1.8.1 Aric Hagberg, Dan Schult, Pieter
Total Page:16
File Type:pdf, Size:1020Kb
NetworkX Reference Release 1.8.1 Aric Hagberg, Dan Schult, Pieter Swart August 04, 2013 CONTENTS 1 Overview 1 1.1 Who uses NetworkX?..........................................1 1.2 Goals...................................................1 1.3 The Python programming language...................................1 1.4 Free software...............................................2 1.5 History..................................................2 2 Introduction 3 2.1 NetworkX Basics.............................................3 2.2 Nodes and Edges.............................................4 3 Graph types 9 3.1 Which graph class should I use?.....................................9 3.2 Basic graph types.............................................9 4 Algorithms 127 4.1 Approximation.............................................. 127 4.2 Assortativity............................................... 132 4.3 Bipartite................................................. 141 4.4 Blockmodeling.............................................. 160 4.5 Boundary................................................. 161 4.6 Centrality................................................. 163 4.7 Chordal.................................................. 181 4.8 Clique.................................................. 185 4.9 Clustering................................................ 187 4.10 Communities............................................... 191 4.11 Components............................................... 191 4.12 Connectivity............................................... 205 4.13 Cores................................................... 214 4.14 Cycles.................................................. 218 4.15 Directed Acyclic Graphs......................................... 219 4.16 Distance Measures............................................ 222 4.17 Distance-Regular Graphs......................................... 224 4.18 Eulerian.................................................. 226 4.19 Flows................................................... 227 4.20 Graphical degree sequence........................................ 239 4.21 Hierarchy................................................. 243 4.22 Isolates.................................................. 244 4.23 Isomorphism............................................... 245 4.24 Link Analysis............................................... 258 4.25 Matching................................................. 264 i 4.26 Maximal independent set......................................... 265 4.27 Minimum Spanning Tree......................................... 266 4.28 Operators................................................. 268 4.29 Rich Club................................................. 275 4.30 Shortest Paths.............................................. 276 4.31 Simple Paths............................................... 294 4.32 Swap................................................... 295 4.33 Traversal................................................. 297 4.34 Vitality.................................................. 298 5 Functions 301 5.1 Graph................................................... 301 5.2 Nodes................................................... 303 5.3 Edges................................................... 305 5.4 Attributes................................................. 305 5.5 Freezing graph structure......................................... 307 6 Graph generators 309 6.1 Atlas................................................... 309 6.2 Classic.................................................. 309 6.3 Small................................................... 314 6.4 Random Graphs............................................. 318 6.5 Degree Sequence............................................. 327 6.6 Random Clustered............................................ 333 6.7 Directed................................................. 334 6.8 Geometric................................................ 337 6.9 Hybrid.................................................. 341 6.10 Bipartite................................................. 341 6.11 Line Graph................................................ 345 6.12 Ego Graph................................................ 346 6.13 Stochastic................................................. 347 6.14 Intersection................................................ 347 6.15 Social Networks............................................. 349 7 Linear algebra 351 7.1 Graph Matrix............................................... 351 7.2 Laplacian Matrix............................................. 352 7.3 Spectrum................................................. 355 7.4 Attribute Matrices............................................ 356 8 Converting to and from other data formats 361 8.1 To NetworkX Graph........................................... 361 8.2 Dictionaries................................................ 362 8.3 Lists................................................... 363 8.4 Numpy.................................................. 364 8.5 Scipy................................................... 367 9 Reading and writing graphs 369 9.1 Adjacency List.............................................. 369 9.2 Multiline Adjacency List......................................... 373 9.3 Edge List................................................. 376 9.4 GEXF................................................... 383 9.5 GML................................................... 385 9.6 Pickle................................................... 388 9.7 GraphML................................................. 389 ii 9.8 JSON................................................... 391 9.9 LEDA................................................... 397 9.10 YAML.................................................. 398 9.11 SparseGraph6.............................................. 399 9.12 Pajek................................................... 400 9.13 GIS Shapefile............................................... 402 10 Drawing 405 10.1 Matplotlib................................................ 405 10.2 Graphviz AGraph (dot).......................................... 413 10.3 Graphviz with pydot........................................... 416 10.4 Graph Layout............................................... 418 11 Exceptions 423 12 Utilities 425 12.1 Helper functions............................................. 425 12.2 Data structures and Algorithms..................................... 426 12.3 Random sequence generators...................................... 426 12.4 Decorators................................................ 429 13 License 431 14 Citing 433 15 Credits 435 16 Glossary 437 Bibliography 439 Python Module Index 449 Index 451 iii iv CHAPTER ONE 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 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.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. 1 NetworkX Reference, Release 1.8.1 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