+
    )i                     
   R t ^ RIt^ RIHt . R	Ot]! R4      ]P                  ! RR7      R
R l4       4       t]! R4      ]P                  ! RR7      R
R l4       4       t]! R4      ]P                  ! RR7      RR l4       4       t	R# )zTrophic levelsN)not_implemented_for
undirectedweight)
edge_attrsc                r   V P                    UUu. uF  w  r#V^ 8X  g   K  VNK  	  pppV'       g   \        P                  ! R4      h\        P                  ! WR7       UUu0 uF  qU F  qfkK  	  K  	  ppp\	        V4      \	        V P
                  4      8w  d   \        P                  ! R4      h^ RIp\        P                  ! WR7      P                  P                  4       p	VP                  V	^R7      p
W^ 8g  ,          RV
^ 8g  3,          pWV
^ 8g  ,          RVP                  3,          ,          pVP                  ^ ,          pVP                  V4      p VP                  P                  W,
          4      pTP                  ^R7      ^,           p/ pR T P                    4       pT F  p^TT&   K
  	  R	 T P                    4       p\#        T4       F  w  ppTP%                  T4      TT&   K  	  T# u uppi u uppi   TP                  P                    d   pRp\        P                  ! T4      ThRp?ii ; i)
aB  Compute the trophic levels of nodes.

The trophic level of a node $i$ is

.. math::

    s_i = 1 + \frac{1}{k^{in}_i} \sum_{j} a_{ij} s_j

where $k^{in}_i$ is the in-degree of i

.. math::

    k^{in}_i = \sum_{j} a_{ij}

and nodes with $k^{in}_i = 0$ have $s_i = 1$ by convention.

These are calculated using the method outlined in Levine [1]_.

Parameters
----------
G : DiGraph
    A directed networkx graph

Returns
-------
nodes : dict
    Dictionary of nodes with trophic level as the value.

References
----------
.. [1] Stephen Levine (1980) J. theor. Biol. 83, 195-207
z|This graph has no basal nodes (nodes with no incoming edges).Trophic levels are not defined without at least one basal node.)sourceszTrophic levels are only defined for graphs where every node has a path from a basal node (basal nodes are nodes with no incoming edges).Nr   )axis:NNNc              3   <   "   T F  w  rV^ 8X  g   K  Vx  K  	  R# 5i    N .0node_iddegrees   &  d/var/www/html/photoedit/myenv/lib/python3.14/site-packages/networkx/algorithms/centrality/trophic.py	<genexpr>!trophic_levels.<locals>.<genexpr>[   s     OK6Q;WWK   
c              3   <   "   T F  w  rV^ 8w  g   K  Vx  K  	  R# 5ir   r   r   s   &  r   r   r   `   s     R{OGfPQk{r   )	in_degreenxNetworkXError
bfs_layerslennodesnumpyadjacency_matrixTtoarraysumnewaxisshapeeyelinalginvLinAlgError	enumerateitem)Gr   ndegbasal_nodeslayernodereachable_nodesnparowsumpnnierrmsgylevelszero_node_idsr   nonzero_node_idss   &&                   r   trophic_levelsr=   	   s   H $%;;;;#(11;K;N
 	
 --??EDE?   ?s177|+P
 	

  	A-//779A VVAAVF	A+q&A+~&A	6Q;2::..A 
B
r
A	-IIMM!%  	
1AF PAKKOM w ! Sq{{R 01
7&&)w 2 Mo <4 99   -) 	
 s#,-s(   G1G1!G7!G= =H6H11H6c                ~    \        WR7      p/ pV P                   F  w  rEW%,          W$,          ,
          W4V3&   K   	  V# )a+  Compute the trophic differences of the edges of a directed graph.

The trophic difference $x_ij$ for each edge is defined in Johnson et al.
[1]_ as:

.. math::
    x_ij = s_j - s_i

Where $s_i$ is the trophic level of node $i$.

Parameters
----------
G : DiGraph
    A directed networkx graph

Returns
-------
diffs : dict
    Dictionary of edges with trophic differences as the value.

References
----------
.. [1] Samuel Johnson, Virginia Dominguez-Garcia, Luca Donetti, Miguel A.
    Munoz (2014) PNAS "Trophic coherence determines food-web stability"
r   )r=   edges)r*   r   r:   diffsuvs   &&    r   trophic_differencesrC   g   s<    8 A-FE	FI-!f L    c                D   ^ RI pV'       d   \        WR7      pMW\        \        P                  ! V 4      4      pV'       d#   V P                  4       pVP                  V4       MT p\        WaR7      p\        VP                  \        VP                  4       4      4      4      # )a  Compute the trophic incoherence parameter of a graph.

Trophic coherence is defined as the homogeneity of the distribution of
trophic distances: the more similar, the more coherent. This is measured by
the standard deviation of the trophic differences and referred to as the
trophic incoherence parameter $q$ by [1].

Parameters
----------
G : DiGraph
    A directed networkx graph

cannibalism: Boolean
    If set to False, self edges are not considered in the calculation

Returns
-------
trophic_incoherence_parameter : float
    The trophic coherence of a graph

References
----------
.. [1] Samuel Johnson, Virginia Dominguez-Garcia, Luca Donetti, Miguel A.
    Munoz (2014) PNAS "Trophic coherence determines food-web stability"
Nr   )
r   rC   listr   selfloop_edgescopyremove_edges_fromfloatstdvalues)r*   r   cannibalismr1   r@   
self_loopsG_2s   &&&    r   trophic_incoherence_parameterrP      sw    8 #A5 "++A./
&&(C!!*- C#C7U\\^,-..rD   )r=   rC   rP   r   )r   F)
__doc__networkxr   networkx.utilsr   __all___dispatchabler=   rC   rP   r   rD   r   <module>rV      s      .
T \"X&Y ' #Yx \"X& ' #B \"X&)/ ' #)/rD   