
    h                        S r SSKrSSKrSSKJr  / SQrSS jr\R                  " SS9SS j5       r	\R                  " SS9SS	 j5       r
\R                  SS
 j5       r\R                  " SS9SS j5       r\R                  " SS9SS j5       r\R                  " SS9SS j5       r\R                  " SSS0S9SS j5       r\" S5      \R                  " SS9SS j5       5       r\" S5      \R                  " SS9SS j5       5       r\R$                  R                  S5      \R                  " SS9SS.S j5       5       rg)z:Graph diameter, radius, eccentricity and other properties.    N)not_implemented_for)
eccentricitydiameterharmonic_diameterradius	peripherycenter
barycenterresistance_distancekemeny_constanteffective_graph_resistancec           	        ^^ [        U R                  5       5      n[        X3R                  S9n[	        U5      nSn[         R                  U S5      m[         R                  X5      m[        U 5      nUnSn	Un
SnU(       Gag  U(       a  WnOUnU(       + n[        R                  " XUS9n[	        U5      U:w  a  Sn[        R                  " U5      e[        UR                  5       5      nSnSnU H|  nUU   n[        TU   [        UUU-
  5      5      =TU'   n[        TU   UU-   5      =TU'   n[        TU   U5      n[        TU   U	5      n	[        TU   U
5      n
[        TU   U5      nM~     US:X  a,  U Vs1 s H  nTU   U	::  d  M  STU   -  U:  d  M  UiM      nnOUS	:X  a/  U Vs1 s H!  nTU   U
:  d  M  TU   S
-   SU-  ::  d  M  UiM#     nnOUS:X  a.  U Vs1 s H   nTU   U	:  d  M  X:X  d  TU   U:  d  M  UiM"     nnOXUS:X  a4  U Vs1 s H&  nTU   U
:  d  M  X:X  d  TU   S
-   SU-  :  d  M$  UiM(     nnOUS:X  a  [        5       nOSn[        U5      eUR                  UU4S jU 5       5        UU-  nU HU  nUb#  TU   TU   :X  a  UU   X4   :  d  TU   TU   :  a  UnUb%  TU   TU   :X  a  UU   X<   :  d  TU   TU   :  d  MS  UnMW     U(       a  GMg  US:X  a  U	$ US	:X  a  U
$ US:X  a  U  Vs/ s H  nTU   U	:X  d  M  UPM     nnU$ US:X  a  U  Vs/ s H  nTU   U
:X  d  M  UPM     nnU$ US:X  a  T$ gs  snf s  snf s  snf s  snf s  snf s  snf )a  Compute requested extreme distance metric of undirected graph G

Computation is based on smart lower and upper bounds, and in practice
linear in the number of nodes, rather than quadratic (except for some
border cases such as complete graphs or circle shaped graphs).

Parameters
----------
G : NetworkX graph
   An undirected graph

compute : string denoting the requesting metric
   "diameter" for the maximal eccentricity value,
   "radius" for the minimal eccentricity value,
   "periphery" for the set of nodes with eccentricity equal to the diameter,
   "center" for the set of nodes with eccentricity equal to the radius,
   "eccentricities" for the maximum distance from each node to all other nodes in G

weight : string, function, or None
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number.

    If this is None, every edge has weight/distance/cost 1.

    Weights stored as floating point values can lead to small round-off
    errors in distances. Use integer weights to avoid this.

    Weights should be positive, since they are distances.

Returns
-------
value : value of the requested metric
   int for "diameter" and "radius" or
   list of nodes for "center" and "periphery" or
   dictionary of eccentricity values keyed by node for "eccentricities"

Raises
------
NetworkXError
    If the graph consists of multiple components
ValueError
    If `compute` is not one of "diameter", "radius", "periphery", "center", or "eccentricities".

Notes
-----
This algorithm was proposed in [1]_ and discussed further in [2]_ and [3]_.

References
----------
.. [1] F. W. Takes, W. A. Kosters,
   "Determining the diameter of small world networks."
   Proceedings of the 20th ACM international conference on Information and knowledge management, 2011
   https://dl.acm.org/doi/abs/10.1145/2063576.2063748
.. [2] F. W. Takes, W. A. Kosters,
   "Computing the Eccentricity Distribution of Large Graphs."
   Algorithms, 2013
   https://www.mdpi.com/1999-4893/6/1/100
.. [3] M. Borassi, P. Crescenzi, M. Habib, W. A. Kosters, A. Marino, F. W. Takes,
   "Fast diameter and radius BFS-based computation in (weakly connected) real-world graphs: With an application to the six degrees of separation games. "
   Theoretical Computer Science, 2015
   https://www.sciencedirect.com/science/article/pii/S0304397515001644
)keyFr   sourceweightz5Cannot compute metric because graph is not connected.Nr      r      r   r	   eccentricitieszTcompute must be one of 'diameter', 'radius', 'periphery', 'center', 'eccentricities'c              3   D   >#    U  H  nTU   TU   :X  d  M  Uv   M     g 7fN ).0i	ecc_lower	ecc_uppers     W/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/distance_measures.py	<genexpr>$_extrema_bounding.<locals>.<genexpr>   s"     OJq)A,)A,2NJs    	 )dictdegreemaxgetlenfromkeyssetnxshortest_path_lengthNetworkXErrorvaluesmin
ValueErrorupdate)Gcomputer   degreesminlowernodeNhigh
candidatesminlowermaxlowerminuppermaxuppermaxuppernodecurrentdistmsgcurrent_eccr   dlowupp	ruled_outvpcr   r   s                            @@r   _extrema_boundingrE      s6   R 188:GwKK0LGADa#Ia#IQJ HHHH "G"Gx &&qHt9>IC""3''$++-(  AQA!$Yq\3q;?3L!MMIaL3!$Yq\;?!CCIaL3 9Q<2H9Q<2H9Q<2H9Q<2H  j  $#AQ<8+ 01IaL0@H0L #  I
   $#AQ<8+ 09!q0@AL0P #  I
 # $#AQ<(*  )Yq\H-D #  I   $#AQ<(*  )Yq\A-=H-L #  I ((IhCS/!OJOOi
 A$aLIl$;;
W%::aL9\#::  $aLIl$;;
W%::aL9\#:: ' k *h *(+61Yq\X5Q6(61Yq\X5Q6""q| 7 7s`   :M
MM,M=MM!M2MMM&M<MM M (M%9M%r   )
edge_attrsc                    U R                  5       n0 nU R                  U5       H  nUc!  [        R                  " XUS9n[	        U5      nO X&   n[	        U5      nX:w  a0  U R                  5       (       a  Sn
OSn
[        R                  " U
5      e[        UR                  5       5      XV'   M     X;   a  XQ   $ U$ ! [
         a  n	[        R                  " S5      U	eSn	A	ff = f)a
  Returns the eccentricity of nodes in G.

The eccentricity of a node v is the maximum distance from v to
all other nodes in G.

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

v : node, optional
   Return value of specified node

sp : dict of dicts, optional
   All pairs shortest path lengths as a dictionary of dictionaries

weight : string, function, or None (default=None)
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number.

    If this is None, every edge has weight/distance/cost 1.

    Weights stored as floating point values can lead to small round-off
    errors in distances. Use integer weights to avoid this.

    Weights should be positive, since they are distances.

Returns
-------
ecc : dictionary
   A dictionary of eccentricity values keyed by node.

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> dict(nx.eccentricity(G))
{1: 2, 2: 3, 3: 2, 4: 2, 5: 3}

>>> dict(
...     nx.eccentricity(G, v=[1, 5])
... )  # This returns the eccentricity of node 1 & 5
{1: 2, 5: 3}

Nr   Format of "sp" is invalid.zHFound infinite path length because the digraph is not strongly connectedz=Found infinite path length because the graph is not connected)
ordernbunch_iterr'   r(   r$   	TypeErrorr)   is_directedr"   r*   )r.   rB   spr   rI   enlengthLerrr<   s              r   r   r      s    z GGIE
A]]1:,,QHFFANK :}}* 
 Y""3''6==?#+ . 	vtH!  N&&'CD#MNs   B::
C CC c                     USL a#  Uc   U R                  5       (       d  [        U SUS9$ Uc	  [        XS9n[        UR	                  5       5      $ )a  Returns the diameter of the graph G.

The diameter is the maximum eccentricity.

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

e : eccentricity dictionary, optional
  A precomputed dictionary of eccentricities.

weight : string, function, or None
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number.

    If this is None, every edge has weight/distance/cost 1.

    Weights stored as floating point values can lead to small round-off
    errors in distances. Use integer weights to avoid this.

    Weights should be positive, since they are distances.

Returns
-------
d : integer
   Diameter of graph

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> nx.diameter(G)
3

See Also
--------
eccentricity
Tr   r/   r   r   rL   rE   r   r"   r*   r.   rN   	useboundsr   s       r   r   r   N  sI    b DQYq}} JvFFy*qxxz?    c                    U R                  5       nSnU  HT  nUc  [        R                  " X5      nO X   n[        U5      nUR                  5        H  nUS:w  d  M  USU-  -  nM     MV     US:w  a
  X"S-
  -  U-  $ US:  a  [        R                  $ [        R                  $ ! [         a  n[        R
                  " S5      UeSnAff = f)aj  Returns the harmonic diameter of the graph G.

The harmonic diameter of a graph is the harmonic mean of the distances
between all pairs of distinct vertices. Graphs that are not strongly
connected have infinite diameter and mean distance, making such
measures not useful. Restricting the diameter or mean distance to
finite distances yields paradoxical values (e.g., a perfect match
would have diameter one). The harmonic mean handles gracefully
infinite distances (e.g., a perfect match has harmonic diameter equal
to the number of vertices minus one), making it possible to assign a
meaningful value to all graphs.

Note that in [1] the harmonic diameter is called "connectivity length":
however, "harmonic diameter" is a more standard name from the
theory of metric spaces. The name "harmonic mean distance" is perhaps
a more descriptive name, but is not used in the literature, so we use the
name "harmonic diameter" here.

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

sp : dict of dicts, optional
   All-pairs shortest path lengths as a dictionary of dictionaries

Returns
-------
hd : float
   Harmonic diameter of graph

References
----------
.. [1] Massimo Marchiori and Vito Latora, "Harmony in the small-world".
       *Physica A: Statistical Mechanics and Its Applications*
       285(3-4), pages 539-546, 2000.
       <https://doi.org/10.1016/S0378-4371(00)00311-3>
r   NrH   r   )
rI   r'   "single_source_shortest_path_lengthr$   rK   r)   r*   mathinfnan)	r.   rM   rI   sum_invdrO   rP   rQ   rR   r>   s	            r   r   r     s    P GGIEH:::1@FNK A AvAE!	 !   1}	"X--qyxx88O  N&&'CD#MNs   B##
C	-CC	c                     USL a#  Uc   U R                  5       (       d  [        U SUS9$ Uc	  [        XS9n[        UR	                  5       5      nU Vs/ s H  oQU   U:X  d  M  UPM     nnU$ s  snf )a  Returns the periphery of the graph G.

The periphery is the set of nodes with eccentricity equal to the diameter.

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

e : eccentricity dictionary, optional
  A precomputed dictionary of eccentricities.

weight : string, function, or None
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number.

    If this is None, every edge has weight/distance/cost 1.

    Weights stored as floating point values can lead to small round-off
    errors in distances. Use integer weights to avoid this.

    Weights should be positive, since they are distances.

Returns
-------
p : list
   List of nodes in periphery

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> nx.periphery(G)
[2, 5]

See Also
--------
barycenter
center
Tr   rT   rU   rV   )r.   rN   rX   r   r   rB   rC   s          r   r   r     so    d DQYq}} KGGy*188:H*Aq1)AA*H 	+   A,"A,c                     USL a#  Uc   U R                  5       (       d  [        U SUS9$ Uc	  [        XS9n[        UR	                  5       5      $ )a  Returns the radius of the graph G.

The radius is the minimum eccentricity.

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

e : eccentricity dictionary, optional
  A precomputed dictionary of eccentricities.

weight : string, function, or None
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number.

    If this is None, every edge has weight/distance/cost 1.

    Weights stored as floating point values can lead to small round-off
    errors in distances. Use integer weights to avoid this.

    Weights should be positive, since they are distances.

Returns
-------
r : integer
   Radius of graph

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> nx.radius(G)
2

Tr   rT   rU   rL   rE   r   r+   r*   rW   s       r   r   r     sI    \ DQYq}} HVDDy*qxxz?rY   c                     USL a#  Uc   U R                  5       (       d  [        U SUS9$ Uc	  [        XS9n[        UR	                  5       5      nU Vs/ s H  oQU   U:X  d  M  UPM     nnU$ s  snf )a  Returns the center of the graph G.

The center is the set of nodes with eccentricity equal to radius.

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

e : eccentricity dictionary, optional
  A precomputed dictionary of eccentricities.

weight : string, function, or None
    If this is a string, then edge weights will be accessed via the
    edge attribute with this key (that is, the weight of the edge
    joining `u` to `v` will be ``G.edges[u, v][weight]``). If no
    such edge attribute exists, the weight of the edge is assumed to
    be one.

    If this is a function, the weight of an edge is the value
    returned by the function. The function must accept exactly three
    positional arguments: the two endpoints of an edge and the
    dictionary of edge attributes for that edge. The function must
    return a number.

    If this is None, every edge has weight/distance/cost 1.

    Weights stored as floating point values can lead to small round-off
    errors in distances. Use integer weights to avoid this.

    Weights should be positive, since they are distances.

Returns
-------
c : list
   List of nodes in center

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> list(nx.center(G))
[1, 3, 4]

See Also
--------
barycenter
periphery
Tr	   rT   rU   rc   )r.   rN   rX   r   r   rB   rC   s          r   r	   r	   8  sn    d DQYq}} HVDDy*_F(Aq1AA(H 	)ra   attrr   )rF   mutates_inputc                    Uc  [         R                  " XS9nOUR                  5       nUb  [        S5      e[	        S5      / [        U 5      penU H  u  px[        U5      U:  a  [         R                  " SU  S35      e[        UR                  5       5      n	Ub  XR                  U   U'   X:  a  U	nU/nMg  X:X  d  Mn  UR                  U5        M     Ub  [         R                  " U 5        U$ )a  Calculate barycenter of a connected graph, optionally with edge weights.

The :dfn:`barycenter` a
:func:`connected <networkx.algorithms.components.is_connected>` graph
:math:`G` is the subgraph induced by the set of its nodes :math:`v`
minimizing the objective function

.. math::

    \sum_{u \in V(G)} d_G(u, v),

where :math:`d_G` is the (possibly weighted) :func:`path length
<networkx.algorithms.shortest_paths.generic.shortest_path_length>`.
The barycenter is also called the :dfn:`median`. See [West01]_, p. 78.

Parameters
----------
G : :class:`networkx.Graph`
    The connected graph :math:`G`.
weight : :class:`str`, optional
    Passed through to
    :func:`~networkx.algorithms.shortest_paths.generic.shortest_path_length`.
attr : :class:`str`, optional
    If given, write the value of the objective function to each node's
    `attr` attribute. Otherwise do not store the value.
sp : dict of dicts, optional
   All pairs shortest path lengths as a dictionary of dictionaries

Returns
-------
list
    Nodes of `G` that induce the barycenter of `G`.

Raises
------
NetworkXNoPath
    If `G` is disconnected. `G` may appear disconnected to
    :func:`barycenter` if `sp` is given but is missing shortest path
    lengths for any pairs.
ValueError
    If `sp` and `weight` are both given.

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> nx.barycenter(G)
[1, 3, 4]

See Also
--------
center
periphery
rU   z-Cannot use both sp, weight arguments togetherr]   zInput graph zH is disconnected, so every induced subgraph has infinite barycentricity.)r'   r(   itemsr,   floatr$   NetworkXNoPathsumr*   nodesappend_clear_cache)
r.   r   re   rM   smallestbarycenter_verticesrO   rB   distsbarycentricitys
             r   r
   r
   s  s    n 
z$$Q6XXZLMM',U|RQ1Hu:>##qc "/ /  U\\^,-GGAJt$%H#$#'&&q)  
rY   directedc                    SSK n[        U 5      S:X  a  [        R                  " S5      e[        R                  " U 5      (       d  [        R                  " S5      eUb  X;  a  [        R                  " S5      eUb  X ;  a  [        R                  " S5      eU R                  5       n [        U 5      nU(       a^  Ub[  U R                  5       (       a$  U R                  SSS9 H  u  pxpS	X   -  X'   M     O"U R                  SS
9 H  u  pxn
S	X   -  X'   M     [        R                  " XS9R                  5       nUR                  R                  USS9nUbl  Ubi  UR                  U5      nUR                  U5      nUR                  X5      UR                  X5      -   UR                  X5      -
  UR                  X5      -
  $ Ubx  UR                  U5      n0 n
U  H]  nUR                  U5      nUR                  X5      UR                  X5      -   UR                  X5      -
  UR                  X5      -
  X'   M_     U
$ Ubx  UR                  U5      n0 n
U  H]  nUR                  U5      nUR                  X5      UR                  X5      -   UR                  X5      -
  UR                  X5      -
  X'   M_     U
$ 0 n
U  H~  nUR                  U5      n0 X'   U  H`  nUR                  U5      nUR                  X5      UR                  X5      -   UR                  X5      -
  UR                  X5      -
  X   U'   Mb     M     U
$ )a  Returns the resistance distance between pairs of nodes in graph G.

The resistance distance between two nodes of a graph is akin to treating
the graph as a grid of resistors with a resistance equal to the provided
weight [1]_, [2]_.

If weight is not provided, then a weight of 1 is used for all edges.

If two nodes are the same, the resistance distance is zero.

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

nodeA : node or None, optional (default=None)
  A node within graph G.
  If None, compute resistance distance using all nodes as source nodes.

nodeB : node or None, optional (default=None)
  A node within graph G.
  If None, compute resistance distance using all nodes as target nodes.

weight : string or None, optional (default=None)
   The edge data key used to compute the resistance distance.
   If None, then each edge has weight 1.

invert_weight : boolean (default=True)
    Proper calculation of resistance distance requires building the
    Laplacian matrix with the reciprocal of the weight. Not required
    if the weight is already inverted. Weight cannot be zero.

Returns
-------
rd : dict or float
   If `nodeA` and `nodeB` are given, resistance distance between `nodeA`
   and `nodeB`. If `nodeA` or `nodeB` is unspecified (the default), a
   dictionary of nodes with resistance distances as the value.

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

NetworkXError
    If `G` is not connected, or contains no nodes,
    or `nodeA` is not in `G` or `nodeB` is not in `G`.

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> round(nx.resistance_distance(G, 1, 3), 10)
0.625

Notes
-----
The implementation is based on Theorem A in [2]_. Self-loops are ignored.
Multi-edges are contracted in one edge with weight equal to the harmonic sum of the weights.

References
----------
.. [1] Wikipedia
   "Resistance distance."
   https://en.wikipedia.org/wiki/Resistance_distance
.. [2] D. J. Klein and M. Randic.
    Resistance distance.
    J. of Math. Chem. 12:81-95, 1993.
r   N'Graph G must contain at least one node.z#Graph G must be strongly connected.zNode A is not in graph G.zNode B is not in graph G.Tkeysdatar   rx   rU   )	hermitian)numpyr$   r'   r)   is_connectedcopylistis_multigraphedgeslaplacian_matrixtodenselinalgpinvindexitem)r.   nodeAnodeBr   invert_weightnp	node_listurB   kr>   rQ   Linvr   jrO   n2s                    r   r   r     s   N 
1v{HII??1DEEU^:;;U^:;;	AQI +??gg4dg;
a	M	 < 777-a	M	 .
 	A-557A99>>!t>,D U.OOE"OOE"yy10499Q?BTYYq_TT		OOE"A"A99Q?TYYq_4tyyFSTXAD  		OOE"A"A99Q?TYYq_4tyyFSTXAD   A"AADOOB'IIaOiio&iio& iio& R   rY   c                 2   SSK n[        U 5      S:X  a  [        R                  " S5      e[        R                  " U 5      (       d  [        S5      $ U R                  5       n U(       a^  Ub[  U R                  5       (       a$  U R                  SSS9 H  u  pEpgSXq   -  Xq'   M     O"U R                  SS9 H  u  pEnSXq   -  Xq'   M     UR                  [        R                  " XS	95      n[        UR                  SUSS -  5      U R                  5       -  5      $ )
a  Returns the Effective graph resistance of G.

Also known as the Kirchhoff index.

The effective graph resistance is defined as the sum
of the resistance distance of every node pair in G [1]_.

If weight is not provided, then a weight of 1 is used for all edges.

The effective graph resistance of a disconnected graph is infinite.

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

weight : string or None, optional (default=None)
   The edge data key used to compute the effective graph resistance.
   If None, then each edge has weight 1.

invert_weight : boolean (default=True)
    Proper calculation of resistance distance requires building the
    Laplacian matrix with the reciprocal of the weight. Not required
    if the weight is already inverted. Weight cannot be zero.

Returns
-------
RG : float
    The effective graph resistance of `G`.

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

NetworkXError
    If `G` does not contain any nodes.

Examples
--------
>>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
>>> round(nx.effective_graph_resistance(G), 10)
10.25

Notes
-----
The implementation is based on Theorem 2.2 in [2]_. Self-loops are ignored.
Multi-edges are contracted in one edge with weight equal to the harmonic sum of the weights.

References
----------
.. [1] Wolfram
   "Kirchhoff Index."
   https://mathworld.wolfram.com/KirchhoffIndex.html
.. [2] W. Ellens, F. M. Spieksma, P. Van Mieghem, A. Jamakovic, R. E. Kooij.
    Effective graph resistance.
    Lin. Alg. Appl. 435:2491-2506, 2011.
r   Nru   r]   Trv   r   ry   rU   )r{   r$   r'   r)   r|   ri   r}   r   r   sortlaplacian_spectrumrk   number_of_nodes)	r.   r   r   r   r   rB   r   r>   mus	            r   r   r   M  s    z 
1v{HII ??1U| 	
A+??gg4dg;
a	M	 < 777-a	M	 . 
&&q8	9B BqrF
#a&7&7&99::rY   rU   c          
      N   SSK nSSKn[        U 5      S:X  a  [        R                  " S5      e[        R
                  " U 5      (       d  [        R                  " S5      e[        R                  " XS9(       a  [        R                  " S5      e[        R                  " XS9nUR                  u  pVUR                  SS9nUR                  S	S
9   SUR                  U5      -  nSSS5        SWUR                  U5      '   UR                  R                  UR                  R                  USXeSS95      n	XU	-  -  n
UR!                  UR"                  R%                  U
R'                  5       5      5      n[)        UR                  SSUSS -
  -  5      5      $ ! , (       d  f       N= f)u  Returns the Kemeny constant of the given graph.

The *Kemeny constant* (or Kemeny's constant) of a graph `G`
can be computed by regarding the graph as a Markov chain.
The Kemeny constant is then the expected number of time steps
to transition from a starting state i to a random destination state
sampled from the Markov chain's stationary distribution.
The Kemeny constant is independent of the chosen initial state [1]_.

The Kemeny constant measures the time needed for spreading
across a graph. Low values indicate a closely connected graph
whereas high values indicate a spread-out graph.

If weight is not provided, then a weight of 1 is used for all edges.

Since `G` represents a Markov chain, the weights must be positive.

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

weight : string or None, optional (default=None)
   The edge data key used to compute the Kemeny constant.
   If None, then each edge has weight 1.

Returns
-------
float
    The Kemeny constant of the graph `G`.

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

NetworkXError
    If the graph `G` is not connected, or contains no nodes,
    or has edges with negative weights.

Examples
--------
>>> G = nx.complete_graph(5)
>>> round(nx.kemeny_constant(G), 10)
3.2

Notes
-----
The implementation is based on equation (3.3) in [2]_.
Self-loops are allowed and indicate a Markov chain where
the state can remain the same. Multi-edges are contracted
in one edge with weight equal to the sum of the weights.

References
----------
.. [1] Wikipedia
   "Kemeny's constant."
   https://en.wikipedia.org/wiki/Kemeny%27s_constant
.. [2] Lovász L.
    Random walks on graphs: A survey.
    Paul Erdös is Eighty, vol. 2, Bolyai Society,
    Mathematical Studies, Keszthely, Hungary (1993), pp. 1-46
r   Nru   zGraph G must be connected.rU   z+The weights of graph G must be nonnegative.r   )axisignore)divideg      ?csr)format)r{   scipyr$   r'   r)   r|   is_negatively_weightedadjacency_matrixshaperk   errstatesqrtisinfsparse	csr_arrayspdiagsr   r   eigvalshr   ri   )r.   r   r   rM   ArO   mdiags
diags_sqrtDHHeigs               r   r   r     sW   B 
1v{HII??1;<<	  2LMM 	A-A77DAEEqEME	H	%2775>)
 
&'(Jrxx
#$			RYY..z1a5.Q	RB
"fA ''"))$$QYY[1
2C QSb\*+,, 
&	%s   F
F$)r   N)NNN)NFNr   )NNNT)NT)__doc__r\   networkxr'   networkx.utilsr   __all__rE   _dispatchabler   r   r   r   r   r	   r
   r   r   utilsr   r   rY   r   <module>r      s   @   .Zz X&W 'Wt X&4 '4n > >B X&7 '7t X&1 '1h X&7 '7t Xfa[AM BM` Z X&D ' !DN Z X&S; ' !S;l j)X&!% W- ' *W-rY   