Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

Utilities

Helper Functions

Miscellaneous Helpers for NetworkX.

These are not imported into the base networkx namespace but can be accessed, for example, as

>>> import networkx
>>> networkx.utils.is_string_like('spam')
True
is_string_like(obj) Check if obj is string.
flatten(obj[, result]) Return flattened version of (possibly nested) iterable object.
iterable(obj) Return True if obj is iterable with a well-defined len().
is_list_of_ints(intlist) Return True if list is a list of ints.
make_str(x) Return the string representation of t.
generate_unique_node() Generate a unique node label.
default_opener(filename) Opens filename using system’s default program.
pairwise(iterable[, cyclic]) s -> (s0, s1), (s1, s2), (s2, s3), …
groups(many_to_one) Converts a many-to-one mapping into a one-to-many mapping.
create_random_state([random_state]) Returns a numpy.random.RandomState instance depending on input.

Data Structures and Algorithms

Union-find data structure.

UnionFind.union(*objects) Find the sets containing the objects and merge them all.

Random Sequence Generators

Utilities for generating random numbers, random sequences, and random selections.

powerlaw_sequence(n[, exponent]) Return sample sequence of length n from a power law distribution.
cumulative_distribution(distribution) Return normalized cumulative distribution from discrete distribution.
discrete_sequence(n[, distribution, …]) Return sample sequence of length n from a given discrete distribution or discrete cumulative distribution.
zipf_rv(alpha[, xmin, seed]) Return a random value chosen from the Zipf distribution.
random_weighted_sample(mapping, k) Return k items without replacement from a weighted sample.
weighted_choice(mapping) Return a single element from a weighted sample.

Decorators

open_file(path_arg[, mode]) Decorator to ensure clean opening and closing of files.
not_implemented_for(*graph_types) Decorator to mark algorithms as not implemented
nodes_or_number(which_args) Decorator to allow number of nodes or container of nodes.
preserve_random_state(func) Decorator to preserve the numpy.random state during a function.
random_state(random_state_index) Decorator to generate a numpy.random.RandomState instance.

Cuthill-Mckee Ordering

Cuthill-McKee ordering of graph nodes to produce sparse matrices

cuthill_mckee_ordering(G[, heuristic]) Generate an ordering (permutation) of the graph nodes to make a sparse matrix.
reverse_cuthill_mckee_ordering(G[, heuristic]) Generate an ordering (permutation) of the graph nodes to make a sparse matrix.

Context Managers

reversed(*args, **kwds) A context manager for temporarily reversing a directed graph in place.