
    hp                         S SK Jr  S SKrS SKJrJr  SS/r\" S5      \R                  " SSS	9SS
 j5       5       r	S r
S rS r\\
S.r\" S5      \R                  " SSS9SS j5       5       rg)    )chainN)not_implemented_forpairwisemetric_closuresteiner_treedirectedweightT)
edge_attrsreturns_graphc           	         [         R                  " 5       n[        U 5      n[         R                  " XS9n[	        U5      u  nu  pgU[        U5      -
  (       a  Sn[         R
                  " U5      eUR                  U5        U H  n	UR                  XYXi   Xy   S9  M     U H7  u  nu  pgUR                  U5        U H  n	UR                  XYXi   Xy   S9  M     M9     U$ )a'  Return the metric closure of a graph.

The metric closure of a graph *G* is the complete graph in which each edge
is weighted by the shortest path distance between the nodes in *G* .

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

Returns
-------
NetworkX graph
    Metric closure of the graph `G`.

r	   z:G is not a connected graph. metric_closure is not defined.)distancepath)nxGraphsetall_pairs_dijkstranextNetworkXErrorremoveadd_edge)
Gr	   MGnodesall_paths_iterur   r   msgvs
             _/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/approximation/steinertree.pyr   r   	   s    $ 	
AVF **1<N~.AHJs##
MM!	

1(+DG
<   .HaAJJqhkJ@   .
 H    c                   ^ ^ [         R                  " T U5      n0 n0 nT R                  5        H"  nX6   S   XV'   [        X6   5      S-
  XFXV   4'   M$     [         R                  " 5       nT R                  SS9 Hy  u  pn	XX   XV   pXHU
4   U	R                  TS5      -   XFU4   -   nUR                  X5      (       d  UR                  XUS9  MU  [        XU
   U   S   5      nUR                  XUS9  M{     [         R                  " USS9n[         R                  " 5       nU HE  u  pn[         R                  " T XT5      n[        U5       H  u  nnUR                  UU5        M     MG     [        [         R                  " USS95      nT R                  5       (       a  U U4S jU 5       nT R                  U5      R!                  5       n[#        UU5        UR                  5       $ )	Nr      T)datar   r	   Fc           
   3   b   >^^#    U  H"  u  mmTT[        TT   T   UUUU4S  jS94v   M$     g7f)c                     > TT   T   U    T   $ N kr   r   r   r	   s    r   <lambda>2_mehlhorn_steiner_tree.<locals>.<genexpr>.<lambda>Q       ad1gaj.@r    keyNmin.0r   r   r   r	   s    @@r   	<genexpr>)_mehlhorn_steiner_tree.<locals>.<genexpr>P   s/      
OVtq!Q3qtAw$@ABw   */)r   multi_source_dijkstra_pathnodeslenr   edgesgethas_edger   r0   minimum_spanning_edgesshortest_pathr   listis_multigraphedge_subgraphcopy_remove_nonterminal_leaves)r   terminal_nodesr	   pathsd_1sr   	G_1_primer   r#   susvweight_here
new_weightG_2G_3dr   n1n2G_3_mstG_4s   ` `                   r   _mehlhorn_steiner_treerS   2   s   ))!^<E
C
AWWYx{UX*I 
 
Igg4g(
dqtBb'lTXXfa%8832w<G!!"))rk:[B-*;H*EFJrj9 ) 
#
#ID
9C
((*Ca10tnFBLLR  % 
 2,,Su=>G
OV
 //'
"
'
'
)CsN399;r    c                   ^ ^ [        T TS9nUR                  U5      n[        R                  " USSS9n[        R
                  " S U 5       5      nT R                  5       (       a  U U4S jU 5       nT R                  U5      n[        R                  " USSS9nT R                  U5      R                  5       n	[        X5        U	R                  5       $ )	Nr   r   T)r	   r#   c              3   D   #    U  H  u  pn[        US    5      v   M     g7f)r   N)r   )r2   r   r   rN   s       r   r3   $_kou_steiner_tree.<locals>.<genexpr>a   s      'U9a6(;(;9s    c           
   3   b   >^^#    U  H"  u  mmTT[        TT   T   UUUU4S  jS94v   M$     g7f)c                     > TT   T   U    T   $ r&   r'   r(   s    r   r*   -_kou_steiner_tree.<locals>.<genexpr>.<lambda>d   r,   r    r-   Nr/   r1   s    @@r   r3   rV   c   s3      
%1 3qtAw$@AB%r5   r	   F)r   subgraphr   r<   r   from_iterabler?   r@   rA   rB   r9   )
r   rC   r	   r   H	mst_edgesmst_all_edgesG_ST_ST_Hs
   ` `       r   _kou_steiner_treerb   X   s    q(A	

>"A ))!JTJI '''U9'UUM
%
 //-
(C

#
#Cu
EC //#

#
#
%Cs399;r    c           	        ^  [        U5      nT  Vs1 s H&  n[        [        T U   5      U1-
  5      S:X  d  M$  UiM(     nnXB-
  nU(       av  [         R                  " U 4S jU 5       6 nXeU-  -  nT R                  U5        U Vs1 s H&  n[        [        T U   5      U1-
  5      S:X  d  M$  UiM(     nnXB-
  nU(       a  Mu  g g s  snf s  snf )Nr"   c              3   @   >#    U  H  n[        TU   5      v   M     g 7fr&   )r   )r2   nr   s     r   r3   -_remove_nonterminal_leaves.<locals>.<genexpr>|   s     &M:LQs1Q4yy:Ls   )r   r8   unionremove_nodes_from)r   	terminalsterminal_setre   leavesnonterminal_leavescandidate_leavess   `      r   rB   rB   s   s    y>L8Ac#ad)qc/2a7aF8.
 99&M:L&MN==	./-K-S1Y!_1E1J!-K#2 
 9 Ls   #CC#C+C)koumehlhorn)preserve_all_attrsr   c                    ^ ^ Uc  Sn [         U   nU" T UT5      nT R                  5       (       a  U U4S jU 5       nT R	                  U5      nU$ ! [         a  n[        U S35      UeSnAff = f)u1
  Return an approximation to the minimum Steiner tree of a graph.

The minimum Steiner tree of `G` w.r.t a set of `terminal_nodes` (also *S*)
is a tree within `G` that spans those nodes and has minimum size (sum of
edge weights) among all such trees.

The approximation algorithm is specified with the `method` keyword
argument. All three available algorithms produce a tree whose weight is
within a ``(2 - (2 / l))`` factor of the weight of the optimal Steiner tree,
where ``l`` is the minimum number of leaf nodes across all possible Steiner
trees.

* ``"kou"`` [2]_ (runtime $O(|S| |V|^2)$) computes the minimum spanning tree of
  the subgraph of the metric closure of *G* induced by the terminal nodes,
  where the metric closure of *G* is the complete graph in which each edge is
  weighted by the shortest path distance between the nodes in *G*.

* ``"mehlhorn"`` [3]_ (runtime $O(|E|+|V|\log|V|)$) modifies Kou et al.'s
  algorithm, beginning by finding the closest terminal node for each
  non-terminal. This data is used to create a complete graph containing only
  the terminal nodes, in which edge is weighted with the shortest path
  distance between them. The algorithm then proceeds in the same way as Kou
  et al..

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

terminal_nodes : list
     A list of terminal nodes for which minimum steiner tree is
     to be found.

weight : string (default = 'weight')
    Use the edge attribute specified by this string as the edge weight.
    Any edge attribute not present defaults to 1.

method : string, optional (default = 'mehlhorn')
    The algorithm to use to approximate the Steiner tree.
    Supported options: 'kou', 'mehlhorn'.
    Other inputs produce a ValueError.

Returns
-------
NetworkX graph
    Approximation to the minimum steiner tree of `G` induced by
    `terminal_nodes` .

Raises
------
NetworkXNotImplemented
    If `G` is directed.

ValueError
    If the specified `method` is not supported.

Notes
-----
For multigraphs, the edge between two nodes with minimum weight is the
edge put into the Steiner tree.


References
----------
.. [1] Steiner_tree_problem on Wikipedia.
       https://en.wikipedia.org/wiki/Steiner_tree_problem
.. [2] Kou, L., G. Markowsky, and L. Berman. 1981.
       ‘A Fast Algorithm for Steiner Trees’.
       Acta Informatica 15 (2): 141–45.
       https://doi.org/10.1007/BF00288961.
.. [3] Mehlhorn, Kurt. 1988.
       ‘A Faster Approximation Algorithm for the Steiner Problem in Graphs’.
       Information Processing Letters 27 (3): 125–28.
       https://doi.org/10.1016/0020-0190(88)90066-X.
Nro   z( is not a valid choice for an algorithm.c           
   3   b   >^^#    U  H"  u  mmTT[        TT   T   UUUU4S  jS94v   M$     g7f)c                     > TT   T   U    T   $ r&   r'   r(   s    r   r*   (steiner_tree.<locals>.<genexpr>.<lambda>   r,   r    r-   Nr/   r1   s    @@r   r3   steiner_tree.<locals>.<genexpr>   s/      
OTtq!Q3qtAw$@ABur5   )
ALGORITHMSKeyError
ValueErrorr?   r@   )r   rC   r	   methodalgoer9   Ts   ` `     r   r   r      s    Z ~U&! NF+E
OT
 	
AH  UF8#KLMSTTUs   	A 
A/A**A/r   )r	   N)	itertoolsr   networkxr   networkx.utilsr   r   __all___dispatchabler   rS   rb   rB   rv   r   r'   r    r   <module>r      s      8^
, Z XT:$ ; !$N#L63& &
 Z T>Z ? !Zr    