o
    3ήcq.                     @   sT   d Z ddlZg dZdddZdd	d
ZdddZdddZdddZdddZ	dS )z2Hubs and authorities analysis of graph structure.
    N)hits
hits_numpy
hits_scipyauthority_matrix
hub_matrixd   :0yE>Tc                 C   s   ddl }ddl}ddl}t| dkri i fS tj| t| td}|du r3|jj	j
|d||d\}	}	}
n|t| }|jj	j
|d|||d\}	}	}
|
 j}|| }|rb||  }||  }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

    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dtype   )kmaxitertol)r   v0r   r   )numpyscipyscipy.sparse.linalglennxadjacency_matrixlistfloatsparselinalgsvdsarrayvaluesflattenrealsumdictzipmap)Gmax_iterr   nstart
normalizednpspr   A_vtahhubsauthorities r0   Q/tmp/pip-target-vg8gfxp4/lib/python/networkx/algorithms/link_analysis/hits_alg.pyr      s$   @
r   c                    s  t | tjtjfrtdt| dkri i fS |d u r&t| d|    n| dt	 
  } D ]
} |  |9  < q2t|D ]} t d t d} D ]}	| |	 D ]}
||
  |	 | |	 |
 dd 7  < q]qW D ]}	| |	 D ]}
 |	  ||
 | |	 |
 dd 7  < q}qwdt 
  } D ]
}	 |	  |9  < qdt|
  }|D ]
}	||	  |9  < qt	 fdd D }||k r nqAt||rdt	|
  }|D ]
}	||	  |9  < qdt	 
  } D ]
}	 |	  |9  < q |fS )Nz.hits() not defined for graphs with multiedges.r   g      ?weightr   c                 3   s$    | ]}t  | |  V  qd S N)abs.0nr-   hlastr0   r1   	<genexpr>   s   " z_hits_python.<locals>.<genexpr>)
isinstancer   
MultiGraphMultiDiGraph	Exceptionr   r    fromkeysnumber_of_nodesr   r   rangekeysgetmaxPowerIterationFailedConvergence)r#   r$   r   r%   r&   sr   r*   r,   r7   nbrerrr0   r8   r1   _hits_python`   sR   **
rI   c                 C   s0   ddl }d}||t tj| |d}|j| S )z@Returns the HITS authority matrix.

    .. deprecated:: 2.6
    r   Nz
authority_matrix is deprecated as of version 2.6 and will be removed in version 3.0.
The authority matrix can be computed by::
    >>> M = nx.to_numpy_array(G, nodelist=nodelist)
    >>> M.T @ Mr	   warningswarnDeprecationWarningr   to_numpy_arrayTr#   r	   rL   msgMr0   r0   r1   r         
r   c                 C   s0   ddl }d}||t tj| |d}||j S )z:Returns the HITS hub matrix.

    .. deprecated:: 2.6
    r   Nz
hub_matrix is deprecated as of version 2.6 and will be removed in version 3.0.
The hub matrix can be computed by::
    >>> M = nx.to_numpy_array(G, nodelist=nodelist)
    >>> M @ M.TrJ   rK   rQ   r0   r0   r1   r      rT   r   c                 C   s  ddl }ddl}|jdtdd t| dkri i fS t| }||j }|j	|\}}|dd|
|f }|j| }	|j	|	\}}|dd|
|f }
|r^||  }|
|
  }
n||  }|
|
  }
tt| tt|}tt| tt|
}||fS )a  Returns HITS hubs and authorities values for nodes.

    .. deprecated:: 2.6

       hits_numpy is deprecated and will be removed in networkx 3.0.

    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 adjancency
    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`:

    >>> hubs, authority = nx.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   Nz`networkx.hits_numpy is deprecated and will be removedin NetworkX 3.0, use networkx.hits instead.   
stacklevel)rL   r   rM   rN   r   r   rO   rP   r   eigargmaxr   rD   r    r!   r"   r   )r#   r&   rL   r'   adj_aryHeevr-   r)   r,   r.   r/   r0   r0   r1   r      s0   ?	


r   ư>c                    sT  ddl }ddl}|jdtdd t| dkri i fS tj| t| d}|j\}}	|j	| }
 du r;|
|df| }n|j fdd	t| D td
}||  }d}	 |}|
| }||  }|||  }||k rnn||krwt||d7 }qT| }|| }|r||  }||  }tt| tt|}tt| tt|}||fS )aY  Returns HITS hubs and authorities values for nodes.

    .. deprecated:: 2.6

       hits_scipy is deprecated and will be removed in networkx 3.0

    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
    --------
    >>> G = nx.path_graph(4)
    >>> h, a = nx.hits(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   Nz`networkx.hits_scipy is deprecated and will be removedin NetworkX 3.0, use networkx.hits instead.rU   rV   rJ   r   c                    s   g | ]}  |d qS )r   )rC   r5   r%   r0   r1   
<listcomp>w  s    zhits_scipy.<locals>.<listcomp>)r
   )rL   r   rM   rN   r   r   to_scipy_sparse_arrayr   shaperP   onesr   r   r   rD   absoluterE   r   r    r!   r"   )r#   r$   r   r%   r&   rL   r'   r)   r7   r*   ATAxixlastrH   r,   r-   r.   r/   r0   r_   r1   r     sH   F	

 
r   )r   r   NTr3   )T)r   r^   NT)
__doc__networkxr   __all__r   rI   r   r   r   r   r0   r0   r0   r1   <module>   s    

X
3

b