Warning

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

Version 1.9 notes and API changes

This page reflects API changes from NetworkX 1.8 to NetworkX 1.9.

Please send comments and questions to the networkx-discuss mailing list: <http://groups.google.com/group/networkx-discuss>.

Flow package

The flow package (networkx.algorithms.flow) is completely rewritten with backward incompatible changes. It introduces a new interface to flow algorithms. Existing code that uses the flow package will not work unmodified with NetworkX 1.9.

Main changes

  1. We added two new maximum flow algorithms (preflow_push and shortest_augmenting_path) and rewrote the Edmonds–Karp algorithm in flow_fulkerson which is now in edmonds_karp. @ysitu contributed implementations of all new maximum flow algorithms. The legacy Edmonds–Karp algorithm implementation in ford_fulkerson is still available but will be removed in the next release.
  2. All maximum flow algorithm implementations (including the legacy ford_fulkerson) output now a residual network (i.e., a DiGraph) after computing the maximum flow. See maximum_flow documentation for the details on the conventions that NetworkX uses for defining a residual network.
  3. We removed the old max_flow and min_cut functions. The main entry points to flow algorithms are now the functions maximum_flow, maximum_flow_value, minimum_cut and minimum_cut_value, which have new parameters that control maximum flow computation: flow_func for specifying the algorithm that will do the actual computation (it accepts a function as argument that implements a maximum flow algorithm), cutoff for suggesting a maximum flow value at which the algorithm stops, value_only for stopping the computation as soon as we have the value of the flow, and residual that accepts as argument a residual network to be reused in repeated maximum flow computation.
  4. All flow algorithms are required to accept arguments for these parameters but may selectively ignored the inapplicable ones. For instance, preflow_push algorithm can stop after the preflow phase without computing a maximum flow if we only need the flow value, but both edmonds_karp and shortest_augmenting_path always compute a maximum flow to obtain the flow value.
  5. The new function minimum_cut returns the cut value and a node partition that defines the minimum cut. The function minimum_cut_value returns only the value of the cut, which is what the removed min_cut function used to return before 1.9.
  6. The functions that implement flow algorithms (i.e., preflow_push, edmonds_karp, shortest_augmenting_path and ford_fulkerson) are not imported to the base NetworkX namespace. You have to explicitly import them from the flow package:
>>> from networkx.algorithms.flow import (ford_fulkerson, preflow_push,
...        edmonds_karp, shortest_augmenting_path)  # doctest: +SKIP
  1. We also added a capacity-scaling minimum cost flow algorithm: capacity_scaling. It supports MultiDiGraph and disconnected networks.

Examples

Below are some small examples illustrating how to obtain the same output than in NetworkX 1.8.1 using the new interface to flow algorithms introduced in 1.9:

>>> import networkx as nx
>>> G = nx.icosahedral_graph()
>>> nx.set_edge_attributes(G, 'capacity', 1)

With NetworkX 1.8:

>>> flow_value = nx.max_flow(G, 0, 6)  # doctest: +SKIP
>>> cut_value = nx.min_cut(G, 0, 6)  # doctest: +SKIP
>>> flow_value == cut_value  # doctest: +SKIP
True
>>> flow_value, flow_dict = nx.ford_fulkerson(G, 0, 6)  # doctest: +SKIP

With NetworkX 1.9:

>>> from networkx.algorithms.flow import (ford_fulkerson, preflow_push,
...        edmonds_karp, shortest_augmenting_path)  # doctest: +SKIP
>>> flow_value = nx.maximum_flow_value(G, 0, 6)  # doctest: +SKIP
>>> cut_value = nx.minimum_cut_value(G, 0, 6)  # doctest: +SKIP
>>> flow_value == cut_value  # doctest: +SKIP
True
>>> # Legacy: this returns the exact same output than ford_fulkerson in 1.8.1
>>> flow_value, flow_dict = nx.maximum_flow(G, 0, 6, flow_func=ford_fulkerson)  # doctest: +SKIP
>>> # We strongly recommend to use the new algorithms:
>>> flow_value, flow_dict = nx.maximum_flow(G, 0, 6)  # doctest: +SKIP
>>> # If no flow_func is passed as argument, the default flow_func
>>> # (preflow-push) is used. Therefore this is the same than:
>>> flow_value, flow_dict = nx.maximum_flow(G, 0, 6, flow_func=preflow_push)  # doctest: +SKIP
>>> # You can also use alternative maximum flow algorithms:
>>> flow_value, flow_dict = nx.maximum_flow(G, 0, 6, flow_func=shortest_augmenting_path)  # doctest: +SKIP
>>> flow_value, flow_dict = nx.maximum_flow(G, 0, 6, flow_func=edmonds_karp)  # doctest: +SKIP

Connectivity package

The flow-based connecitivity and cut algorithms from the connectivity package (networkx.algorithms.connectivity) are adapted to take advantage of the new interface to flow algorithms. As a result, flow-based connectivity algorithms are up to 10x faster than in NetworkX 1.8 for some problems, such as sparse networks with highly skewed degree distributions. A few backwards incompatible changes were introduced.

  • The functions for local connectivity and cuts accept now arguments for the new parameters defined for the flow interface: flow_func for defining the algorithm that will perform the underlying maximum flow computations, residual that accepts as argument a residual network to be reused in repeated maximum flow computations, and cutoff for defining a maximum flow value at which the underlying maximum flow algorithm stops. The big speed improvement with respect to 1.8 comes mainly from the reuse of the residual network and the use of cutoff.
  • We removed the flow-based local connectivity and cut functions from the base namespace. Now they have to be explicitly imported from the connectivity package. The main entry point to flow-based connectivity and cut functions are the functions edge_connectivity, node_connectivity, minimum_edge_cut, and minimum_node_cut. All these functions accept a couple of nodes as optional arguments for computing local connectivity and cuts.
  • We improved the auxiliary network for connectivity functions: The node mapping dict needed for node connectivity and minimum node cuts is now a graph attribute of the auxiliary network. Thus we removed the mapping parameter from the local versions of connectivity and cut functions. We also changed the parameter name for the auxuliary digraph from aux_digraph to auxiliary.
  • We changed the name of the function all_pairs_node_connectiviy_matrix to all_pairs_node_connectivity. This function now returns a dictionary instead of a NumPy 2D array. We added a new parameter nbunch for computing node connectivity only among pairs of nodes in nbunch.
  • A stoer_wagner function is added to the connectivity package for computing the weighted minimum cuts of undirected graphs using the Stoer–Wagner algorithm. This algorithm is not based on maximum flows. Several heap implementations are also added in the utility package (networkx.utils) for use in this function. BinaryHeap is recommended over PairingHeap for Python implementations without optimized attribute accesses (e.g., CPython) despite a slower asymptotic running time. For Python implementations with optimized attribute accesses (e.g., PyPy), PairingHeap provides better performance.

Other new functionalities

  • A disperson function is added in the centrality package (networkx.algorithms.centrality) for computing the dispersion of graphs.
  • A community package (networkx.generators.community) is added for generating community graphs.
  • An is_semiconnected function is added in the connectivity package (networkx.algorithms.connectivity) for recognizing semiconnected graphs.
  • The eulerian_circuit function in the Euler package (networkx.algorithm.euler) is changed to use a linear-time algorithm.
  • A non_edges function in added in the function package (networkx.functions) for enumerating nonexistent edges between existing nodes of graphs.
  • The linear algebra package (networkx.linalg) is changed to use SciPy sparse matrices.
  • Functions algebraic_connectivity, fiedler_vector and spectral_ordering are added in the linear algebra package (networkx.linalg) for computing the algebraic connectivity, Fiedler vectors and spectral orderings of undirected graphs.
  • A link prediction package (networkx.algorithms.link_prediction) is added to provide link prediction-related functionalities.
  • Write Support for the graph6 and sparse6 formats is added in the read/write package (networx.readwrite).
  • A goldberg_radzik function is added in the shortest path package (networkx.algorithms.shortest_paths) for computing shortest paths using the Goldberg–Radzik algorithm.
  • A tree package (networkx.tree) is added to provide tree recognition functionalities.
  • A context manager reversed is added in the utility package (networkx.utils) for temporary in-place reversal of graphs.

Miscellaneous changes

  • The functions in the components package (networkx.algorithms.components) such as connected_components, connected_components_subgraph now return generators instead of lists. To recover the earlier behavior, use list(connected_components(G)).
  • JSON helpers in the JSON graph package (networkx.readwrite.json_graph) are removed. Use functions from the standard library (e.g., json.dumps) instead.
  • Support for Python 3.1 is dropped. Basic support is added for Jython 2.7 and IronPython 2.7, although they remain not officially supported.
  • Numerous reported issues are fixed.