edge_disjoint_paths#
- edge_disjoint_paths(G, s, t, flow_func=None, cutoff=None, auxiliary=None, residual=None)[source]#
Returns the edges disjoint paths between source and target.
Edge disjoint paths are paths that do not share any edge. The number of edge disjoint paths between source and target is equal to their edge connectivity.
- Parameters:
- GNetworkX graph
- snode
Source node for the flow.
- tnode
Sink node for the flow.
- flow_funcfunction
A function for computing the maximum flow among a pair of nodes. The function has to accept at least three parameters: a Digraph, a source node, and a target node. And return a residual network that follows NetworkX conventions (see
maximum_flow()
for details). If flow_func is None, the default maximum flow function (edmonds_karp()
) is used. The choice of the default function may change from version to version and should not be relied on. Default value: None.- cutoffinteger or None (default: None)
Maximum number of paths to yield. If specified, the maximum flow algorithm will terminate when the flow value reaches or exceeds the cutoff. This only works for flows that support the cutoff parameter (most do) and is ignored otherwise.
- auxiliaryNetworkX DiGraph
Auxiliary digraph to compute flow based edge connectivity. It has to have a graph attribute called mapping with a dictionary mapping node names in G and in the auxiliary digraph. If provided it will be reused instead of recreated. Default value: None.
- residualNetworkX DiGraph
Residual network to compute maximum flow. If provided it will be reused instead of recreated. Default value: None.
- Returns:
- pathsgenerator
A generator of edge independent paths.
- Raises:
- NetworkXNoPath
If there is no path between source and target.
- NetworkXError
If source or target are not in the graph G.
See also
node_disjoint_paths()
edge_connectivity()
maximum_flow()
edmonds_karp()
preflow_push()
shortest_augmenting_path()
Notes
This is a flow based implementation of edge disjoint paths. We compute the maximum flow between source and target on an auxiliary directed network. The saturated edges in the residual network after running the maximum flow algorithm correspond to edge disjoint paths between source and target in the original network. This function handles both directed and undirected graphs, and can use all flow algorithms from NetworkX flow package.
Examples
We use in this example the platonic icosahedral graph, which has node edge connectivity 5, thus there are 5 edge disjoint paths between any pair of nodes.
>>> G = nx.icosahedral_graph() >>> len(list(nx.edge_disjoint_paths(G, 0, 6))) 5
If you need to compute edge disjoint paths on several pairs of nodes in the same graph, it is recommended that you reuse the data structures that NetworkX uses in the computation: the auxiliary digraph for edge connectivity, and the residual network for the underlying maximum flow computation.
Example of how to compute edge disjoint paths among all pairs of nodes of the platonic icosahedral graph reusing the data structures.
>>> import itertools >>> # You also have to explicitly import the function for >>> # building the auxiliary digraph from the connectivity package >>> from networkx.algorithms.connectivity import build_auxiliary_edge_connectivity >>> H = build_auxiliary_edge_connectivity(G) >>> # And the function for building the residual network from the >>> # flow package >>> from networkx.algorithms.flow import build_residual_network >>> # Note that the auxiliary digraph has an edge attribute named capacity >>> R = build_residual_network(H, "capacity") >>> result = {n: {} for n in G} >>> # Reuse the auxiliary digraph and the residual network by passing them >>> # as arguments >>> for u, v in itertools.combinations(G, 2): ... k = len(list(nx.edge_disjoint_paths(G, u, v, auxiliary=H, residual=R))) ... result[u][v] = k >>> all(result[u][v] == 5 for u, v in itertools.combinations(G, 2)) True
You can also use alternative flow algorithms for computing edge disjoint paths. For instance, in dense networks the algorithm
shortest_augmenting_path()
will usually perform better than the defaultedmonds_karp()
which is faster for sparse networks with highly skewed degree distributions. Alternative flow functions have to be explicitly imported from the flow package.>>> from networkx.algorithms.flow import shortest_augmenting_path >>> len(list(nx.edge_disjoint_paths(G, 0, 6, flow_func=shortest_augmenting_path))) 5