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.make_list_of_ints({1, 2, 3})
[1, 2, 3]
>>> networkx.utils.arbitrary_element({5, 1, 7})  
1

arbitrary_element(iterable)

Returns an arbitrary element of iterable without removing it.

flatten(obj[, result])

Return flattened version of (possibly nested) iterable object.

make_list_of_ints(sequence)

Return list of ints from sequence of integral numbers.

dict_to_numpy_array(d[, mapping])

Convert a dictionary of dictionaries to a numpy array with optional mapping.

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 or numpy.random.Generator instance depending on input.

create_py_random_state([random_state])

Returns a random.Random instance depending on input.

nodes_equal(nodes1, nodes2)

Check if nodes are equal.

edges_equal(edges1, edges2)

Check if edges are equal.

graphs_equal(graph1, graph2)

Check if graphs are equal.

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, seed])

Return sample sequence of length n from a power law distribution.

cumulative_distribution(distribution)

Returns 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])

Returns a random value chosen from the Zipf distribution.

random_weighted_sample(mapping, k[, seed])

Returns k items without replacement from a weighted sample.

weighted_choice(mapping[, seed])

Returns 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.

np_random_state(random_state_argument)

Decorator to generate a numpy RandomState or Generator instance.

py_random_state(random_state_argument)

Decorator to generate a random.Random instance (or equiv).

argmap(func, *args[, try_finally])

A decorator to apply a map to arguments before calling the function

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.

Mapped Queue#

Priority queue class with updatable priorities.

MappedQueue([data])

The MappedQueue class implements a min-heap with removal and update-priority.