
    Vh(                     j    d Z ddlZdgZ ej                  dddii      dd       Zdd	Zdd
ZddZy)z1Hubs and authorities analysis of graph structure.    NhitsGweight   )preserve_edge_attrsc           	      
   ddl }ddl}t        |       dk(  ri i fS t        j                  | t        |       t              }|(|j                  t        |j                                     }|dk  rt        j                  |      	 |j                  j                  j                  |d|||      \  }}}	|	j                         j                  }||z  }|r&||j!                         z  }||j!                         z  }t#        t%        | t'        t        |                  }t#        t%        | t'        t        |                  }||fS # |j                  j                  j                  $ r}
t        j                  |      |
d}
~
ww xY w)a  Returns HITS hubs and authorities values for nodes.

    The HITS algorithm computes two numbers for a node.
    Authorities estimates the node value based on the incoming links.
    Hubs estimates the node value based on outgoing links.

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

    max_iter : integer, optional
      Maximum number of iterations in power method.

    tol : float, optional
      Error tolerance used to check convergence in power method iteration.

    nstart : dictionary, optional
      Starting value of each node for power method iteration.

    normalized : bool (default=True)
       Normalize results by the sum of all of the values.

    Returns
    -------
    (hubs,authorities) : two-tuple of dictionaries
       Two dictionaries keyed by node containing the hub and authority
       values.

    Raises
    ------
    PowerIterationFailedConvergence
        If the algorithm fails to converge to the specified tolerance
        within the specified number of iterations of the power iteration
        method.

    Examples
    --------
    >>> G = nx.path_graph(4)
    >>> h, a = nx.hits(G)

    Notes
    -----
    The eigenvector calculation is done by the power iteration method
    and has no guarantee of convergence.  The iteration will stop
    after max_iter iterations or an error tolerance of
    number_of_nodes(G)*tol has been reached.

    The HITS algorithm was designed for directed graphs but this
    algorithm does not check if the input graph is directed and will
    execute on undirected graphs.

    References
    ----------
    .. [1] A. Langville and C. Meyer,
       "A survey of eigenvector methods of web information retrieval."
       http://citeseer.ist.psu.edu/713792.html
    .. [2] Jon Kleinberg,
       Authoritative sources in a hyperlinked environment
       Journal of the ACM 46 (5): 604-32, 1999.
       doi:10.1145/324133.324140.
       http://www.cs.cornell.edu/home/kleinber/auth.pdf.
    r   N)nodelistdtyper   )kv0maxitertol)numpyscipylennxadjacency_matrixlistfloatarrayvaluesPowerIterationFailedConvergencesparselinalgsvdsArpackNoConvergenceflattenrealsumdictzipmap)r   max_iterr   nstart
normalizednpspA_vtexcahhubsauthoritiess                  Z/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/link_analysis/hits_alg.pyr   r      sM   B 
1v{2v
AQu=A$v}}/01}00::D99##((aFHRU(V1b 	

A	AA	QUUW	QUUWAs5!}%&Ds1c%m,-K 99// D00:CDs   .E #F'E==Fc                    t        | t        j                  t        j                  z        rt	        d      t        |       dk(  ri i fS |(t        j                  | d| j                         z        n2|dt        j                               z  }D ]  }|xx   |z  cc<    t        |      D ]=  }t        j                  j                         d      t        j                  j                         d      }D ]5  }	| |	   D ]+  }
||
xx   |	   | |	   |
   j                  dd      z  z  cc<   - 7 D ]5  }	| |	   D ]+  }
|	xx   ||
   | |	   |
   j                  dd      z  z  cc<   - 7 dt        j                               z  }D ]  }	|	xx   |z  cc<    dt        |j                               z  }|D ]  }	||	xx   |z  cc<    t        fdD              }||k  s> n t        j                  |      |r`dt        |j                               z  }|D ]  }	||	xx   |z  cc<    dt        j                               z  }D ]  }	|	xx   |z  cc<    |fS )Nz.hits() not defined for graphs with multiedges.r   g      ?r   r   c              3   F   K   | ]  }t        |   |   z
          y w)N)abs).0nr-   hlasts     r0   	<genexpr>z_hits_python.<locals>.<genexpr>   s"     21#adU1Xo&2s   !)
isinstancer   
MultiGraphMultiDiGraph	Exceptionr   r    fromkeysnumber_of_nodesr   r   rangekeysgetmaxr   )r   r#   r   r$   r%   sr   r)   r,   r5   nbrerrr-   r6   s               @@r0   _hits_pythonrE   c   su   !R]]R__45HII
1v{2v~MM!S1#4#4#667#ahhj/! 	AaDAID	8_ ;MM%**,*MM%**,*  	@At @#%(QqT#Y]]8Q%???@	@  	<At <!#1cx!;;;<	< #ahhj/! 	AaDAID	 #ahhj/! 	AaDAID	 22291;4 00::#ahhj/! 	AaDAID	#ahhj/! 	AaDAID	a4K    c           	         ddl }t        |       dk(  ri i fS t        j                  |       }||j                  z  }|j
                  j                  |      \  }}|dd|j                  |      f   }|j                  |z  }|j
                  j                  |      \  }}|dd|j                  |      f   }	|r'||j                         z  }|	|	j                         z  }	n&||j                         z  }|	|	j                         z  }	t        t        | t        t        |                  }
t        t        | t        t        |	                  }|
|fS )a  Returns HITS hubs and authorities values for nodes.

    The HITS algorithm computes two numbers for a node.
    Authorities estimates the node value based on the incoming links.
    Hubs estimates the node value based on outgoing links.

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

    normalized : bool (default=True)
       Normalize results by the sum of all of the values.

    Returns
    -------
    (hubs,authorities) : two-tuple of dictionaries
       Two dictionaries keyed by node containing the hub and authority
       values.

    Examples
    --------
    >>> G = nx.path_graph(4)

    The `hubs` and `authorities` are given by the eigenvectors corresponding to the
    maximum eigenvalues of the hubs_matrix and the authority_matrix, respectively.

    The ``hubs`` and ``authority`` matrices are computed from the adjacency
    matrix:

    >>> adj_ary = nx.to_numpy_array(G)
    >>> hubs_matrix = adj_ary @ adj_ary.T
    >>> authority_matrix = adj_ary.T @ adj_ary

    `_hits_numpy` maps the eigenvector corresponding to the maximum eigenvalue
    of the respective matrices to the nodes in `G`:

    >>> from networkx.algorithms.link_analysis.hits_alg import _hits_numpy
    >>> hubs, authority = _hits_numpy(G)

    Notes
    -----
    The eigenvector calculation uses NumPy's interface to LAPACK.

    The HITS algorithm was designed for directed graphs but this
    algorithm does not check if the input graph is directed and will
    execute on undirected graphs.

    References
    ----------
    .. [1] A. Langville and C. Meyer,
       "A survey of eigenvector methods of web information retrieval."
       http://citeseer.ist.psu.edu/713792.html
    .. [2] Jon Kleinberg,
       Authoritative sources in a hyperlinked environment
       Journal of the ACM 46 (5): 604-32, 1999.
       doi:10.1145/324133.324140.
       http://www.cs.cornell.edu/home/kleinber/auth.pdf.
    r   N)r   r   r   to_numpy_arrayTr   eigargmaxr   rA   r    r!   r"   r   )r   r%   r&   adj_aryHeevr-   r(   r,   r.   r/   s               r0   _hits_numpyrP      s   x 
1v{2v"G'))AIIMM!EAr
1biil?A		GAIIMM!EAr
1biil?A	QUUW	QUUW	QUUW	QUUWAs5!}%&Ds1c%m,-KrF   c           	      `   ddl }t        |       dk(  ri i fS t        j                  | t	        |             }|j
                  \  }}|j                  |z  }	||j                  |df      |z  }
nQ|j                  t	        |       D cg c]  }|j                  |d       c}t              }
|
|
j                         z  }
d}	 |
}|	|
z  }
|
|
j                         z  }
|j                  |
|z
        j                         }||k  rn ||kD  rt        j                  |      |dz  }b|
j                         }||z  }|r&||j                         z  }||j                         z  }t!        t#        | t%        t        |                  }t!        t#        | t%        t        |                  }||fS c c}w )aK  Returns HITS hubs and authorities values for nodes.


    The HITS algorithm computes two numbers for a node.
    Authorities estimates the node value based on the incoming links.
    Hubs estimates the node value based on outgoing links.

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

    max_iter : integer, optional
      Maximum number of iterations in power method.

    tol : float, optional
      Error tolerance used to check convergence in power method iteration.

    nstart : dictionary, optional
      Starting value of each node for power method iteration.

    normalized : bool (default=True)
       Normalize results by the sum of all of the values.

    Returns
    -------
    (hubs,authorities) : two-tuple of dictionaries
       Two dictionaries keyed by node containing the hub and authority
       values.

    Examples
    --------
    >>> from networkx.algorithms.link_analysis.hits_alg import _hits_scipy
    >>> G = nx.path_graph(4)
    >>> h, a = _hits_scipy(G)

    Notes
    -----
    This implementation uses SciPy sparse matrices.

    The eigenvector calculation is done by the power iteration method
    and has no guarantee of convergence.  The iteration will stop
    after max_iter iterations or an error tolerance of
    number_of_nodes(G)*tol has been reached.

    The HITS algorithm was designed for directed graphs but this
    algorithm does not check if the input graph is directed and will
    execute on undirected graphs.

    Raises
    ------
    PowerIterationFailedConvergence
        If the algorithm fails to converge to the specified tolerance
        within the specified number of iterations of the power iteration
        method.

    References
    ----------
    .. [1] A. Langville and C. Meyer,
       "A survey of eigenvector methods of web information retrieval."
       http://citeseer.ist.psu.edu/713792.html
    .. [2] Jon Kleinberg,
       Authoritative sources in a hyperlinked environment
       Journal of the ACM 46 (5): 604-632, 1999.
       doi:10.1145/324133.324140.
       http://www.cs.cornell.edu/home/kleinber/auth.pdf.
    r   N)r	   r   )r
   )r   r   r   to_scipy_sparse_arrayr   shaperI   onesr   r@   r   r   rA   absoluter   r   r    r!   r"   )r   r#   r   r$   r%   r&   r(   r5   r)   ATAxixlastrD   r,   r-   r.   r/   s                     r0   _hits_scipyrZ      s   H 
1v{2v
  T!W5AWWFQ
##'C~GGQFOaHHQ81fjjA&8HF	QUUW 	
A
!G	QUUWkk!e)$((*9x<44X>>	Q  	
		A	AA	QUUW	QUUWAs5!}%&Ds1c%m,-K1 9s   F+)d   g:0yE>NT)T)r[   gư>NT)	__doc__networkxr   __all___dispatchabler   rE   rP   rZ    rF   r0   <module>ra      sR    7 ( sXqM&:;W <Wt0fQhgrF   