
    h99                         S r SSKrSSKJrJrJr  SSKJr  \r	SSK
Jr  SSKJrJr  SS	/r\R"                  " SS
S.SS\" S5      00S9 SS j5       r\R"                  " SS
S.SSS00S1S9 SS j5       rS rg)z(Flow based node and edge disjoint paths.    N)edmonds_karppreflow_pushshortest_augmenting_path)NetworkXNoPath)filterfalse   )!build_auxiliary_edge_connectivity!build_auxiliary_node_connectivityedge_disjoint_pathsnode_disjoint_paths   )Gz
auxiliary?	auxiliarycapacityinf)graphspreserve_edge_attrsc              #     #    X;  a  [         R                  " SU S35      eX ;  a  [         R                  " SU S35      eUc  [        nUc  [        U 5      nOUn[	        UR                  U5      UR                  U5      5      nU(       d  [        eUc  UnO[	        XH5      nSUUSS.n	U[        L a  U	S	 U[        L a  SU	S'   U" XqU40 U	D6n
U
R                  S	   S
:X  a  [        eU
R                  SS9 VVVs/ s H"  u  pnUS   US   :X  d  M  US   S
:  d  M  X4PM$     nnnnU VVs0 s H  o  H  nU0 _M     M     nnnU H  u  pSUU   U'   M     S
n[        UU   5       Hz  nUU:  a    gU/nX:X  a  UR                  U5        Uv   M*  UnX:w  a/  UR                  U5         UU   R                  5       u  nnX:w  a  M/  UR                  U5        Uv   US-  nM|     gs  snnnf s  snnf ! [         a     M  f = f7f)a  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
----------
G : NetworkX graph

s : node
    Source node for the flow.

t : node
    Sink node for the flow.

flow_func : function
    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 :meth:`maximum_flow` for
    details). If flow_func is None, the default maximum flow function
    (:meth:`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.

cutoff : integer 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.

auxiliary : NetworkX 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.

residual : NetworkX DiGraph
    Residual network to compute maximum flow. If provided it will be
    reused instead of recreated. Default value: None.

Returns
-------
paths : generator
    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
--------
:meth:`node_disjoint_paths`
:meth:`edge_connectivity`
:meth:`maximum_flow`
:meth:`edmonds_karp`
:meth:`preflow_push`
:meth:`shortest_augmenting_path`

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
:meth:`shortest_augmenting_path` will usually perform better than
the default :meth:`edmonds_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

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.

node  not in graphNr   T)r   residualcutoff
value_onlyr   	two_phase
flow_valuer   )dataflowr   )nxNetworkXErrordefault_flow_funcr	   min
out_degree	in_degreer   r   r   graphedgeslistappendpopitemKeyError)r   st	flow_funcr   r   r   HpossiblekwargsRuvdcutsetedgen	flow_dictpaths_foundpath_s                        a/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/connectivity/disjoint_paths.pyr   r      sI    J 	zqc788zqc788%	-a0 1<<?AKKN3H~V&
 	F L 8,,"{!$V$Aww|! wwDw))GA!Z=AfI% 	*+F)a- 	)   $*8644aB46I8	!Q  K)A,&  s6KKNJfKKN |++-1 f KKNJ1K)   9*  sU   C8H:G'G'G'#	H,G.A(H+G4H,H4
H>HHHid)r   preserve_node_attrspreserve_graph_attrsc              #   J  ^#    X;  a  [         R                  " SU S35      eX ;  a  [         R                  " SU S35      eUc  [        U 5      mOUmTR                  R	                  SS5      nUc  [         R                  " S5      e[        TR                  Xq    S35      TR                  Xr    S35      5      nU(       d  [        eUc  UnO[        XH5      nUUTUS.n	[        TXq    S3Xr    S340 U	D6n
U
 H#  n[        [        U4S	 jU 5       5      5      v   M%     g7f)
a  Computes node disjoint paths between source and target.

Node disjoint paths are paths that only share their first and last
nodes. The number of node independent paths between two nodes is
equal to their local node connectivity.

Parameters
----------
G : NetworkX graph

s : node
    Source node.

t : node
    Target node.

flow_func : function
    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 :meth:`maximum_flow` for
    details). If flow_func is None, the default maximum flow function
    (:meth:`edmonds_karp`) is used. See below for details. The choice
    of the default function may change from version to version and
    should not be relied on. Default value: None.

cutoff : integer 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.

auxiliary : NetworkX DiGraph
    Auxiliary digraph to compute flow based node 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.

residual : NetworkX DiGraph
    Residual network to compute maximum flow. If provided it will be
    reused instead of recreated. Default value: None.

Returns
-------
paths : generator
    Generator of node disjoint paths.

Raises
------
NetworkXNoPath
    If there is no path between source and target.

NetworkXError
    If source or target are not in the graph G.

Examples
--------
We use in this example the platonic icosahedral graph, which has node
connectivity 5, thus there are 5 node disjoint paths between any pair
of non neighbor nodes.

>>> G = nx.icosahedral_graph()
>>> len(list(nx.node_disjoint_paths(G, 0, 6)))
5

If you need to compute node disjoint paths between 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 node connectivity and node cuts, and the
residual network for the underlying maximum flow computation.

Example of how to compute node disjoint paths reusing the data
structures:

>>> # You also have to explicitly import the function for
>>> # building the auxiliary digraph from the connectivity package
>>> from networkx.algorithms.connectivity import build_auxiliary_node_connectivity
>>> H = build_auxiliary_node_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")
>>> # Reuse the auxiliary digraph and the residual network by passing them
>>> # as arguments
>>> len(list(nx.node_disjoint_paths(G, 0, 6, auxiliary=H, residual=R)))
5

You can also use alternative flow algorithms for computing node disjoint
paths. For instance, in dense networks the algorithm
:meth:`shortest_augmenting_path` will usually perform better than
the default :meth:`edmonds_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.node_disjoint_paths(G, 0, 6, flow_func=shortest_augmenting_path)))
5

Notes
-----
This is a flow based implementation of node 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 node 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.

See also
--------
:meth:`edge_disjoint_paths`
:meth:`node_connectivity`
:meth:`maximum_flow`
:meth:`edmonds_karp`
:meth:`preflow_push`
:meth:`shortest_augmenting_path`

r   r   NmappingzInvalid auxiliary digraph.BA)r,   r   r   r   c              3   H   >#    U  H  nTR                   U   S    v   M     g7f)r<   N)nodes).0noder-   s     r;   	<genexpr>&node_disjoint_paths.<locals>.<genexpr>  s     #IDDAGGDM$$7Ds   ")r   r   r
   r$   getr!   r"   r#   r   r   r&   _unique_everseen)r   r*   r+   r,   r   r   r   r@   r.   r/   paths_edgesr9   r-   s               @r;   r   r      s3    ~ 	zqc788zqc788-a0ggkk)T*G;<< 1<<7:,a 011;;'*Q?O3PQH~V& 	F &aGJ<q)9gj\;KVvVK##ID#IIJJ s   D D#c              #      #    [        5       nUR                  n[        UR                  U 5       H  nU" U5        Uv   M     g7f)zHList unique elements, preserving order. Remember all elements ever seen.N)setadd_filterfalse__contains__)iterableseenseen_addelements       r;   rJ   rJ     s;      5DxxH 1 18< =s   AA)NNNN)__doc__networkxr   networkx.algorithms.flowr   r   r   networkx.exceptionr   r    	itertoolsr   rO   utilsr	   r
   __all___dispatchablefloatr   r   rJ        r;   <module>r`      s    .  
 .   1 X "7
8 !$$z5<&@A
 DHL	L^ !$$tTl3% DH^K
^KBr_   