ó
ú£Õ\c           @   sŒ   d  Z  d Z d d d d d g Z d d l Z d d	 l m Z d
 d d d „ Z d d „ Z	 d d „ Z
 d „  Z d
 d d „ Z d „  Z d S(   s3   
Hubs and authorities analysis of graph structure.
s   Aric Hagberg (hagberg@lanl.gov)t   hitst
   hits_numpyt
   hits_scipyt   authority_matrixt
   hub_matrixiÿÿÿÿN(   t   NetworkXErrorid   g:Œ0âŽyE>c      	   C   s  t  |  ƒ t j k s* t  |  ƒ t j k r9 t d ƒ ‚ n  t |  ƒ d k rU i  i  f S| d k r€ t j |  d |  j	 ƒ  ƒ } n= | } d t
 | j ƒ  ƒ } x | D] } | | c | 9<q£ Wd } xÀt r…| } t j | j ƒ  d ƒ } t j | j ƒ  d ƒ }	 xO | D]G }
 x> |  |
 D]2 } |	 | c | |
 |  |
 | j d d ƒ 7<qWq	WxO | D]G }
 x> |  |
 D]2 } | |
 c |	 | |  |
 | j d d ƒ 7<qlWq[Wd t
 | j ƒ  ƒ } x | D] }
 | |
 c | 9<qÃWd t
 |	 j ƒ  ƒ } x |	 D] }
 |	 |
 c | 9<qúWt
 g  | D] }
 t | |
 | |
 ƒ ^ qƒ } | | k  rUPn  | | k rxt d | d ƒ ‚ n  | d 7} qÆ W| |	 f S(   s•  Return 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 : interger, 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.

    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
    -----
    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.
    s.   hits() not defined for graphs with multiedges.i    g      ð?t   weighti   s:   HITS: power iteration failed to converge in %d iterations.N(   t   typet   nxt
   MultiGrapht   MultiDiGrapht	   Exceptiont   lent   Nonet   dictt   fromkeyst   number_of_nodest   sumt   valuest   Truet   keyst   gett   absR   (   t   Gt   max_itert   tolt   nstartt   ht   st   kt   it   hlastt   at   nt   nbrt   err(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyR       sF    6*
	44  1c         C   s    t  j |  d | ƒ} | j | S(   s!   Return the HITS authority matrix.t   nodelist(   R   t   to_numpy_matrixt   T(   R   R$   t   M(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyR   u   s    c         C   s    t  j |  d | ƒ} | | j S(   s   Return the HITS hub matrix.R$   (   R   R%   R&   (   R   R$   R'   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyR   z   s    c         C   s|  y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ d k rL i  i  f St j |  |  j ƒ  ƒ } | j j | ƒ \ } } | j ƒ  d } | j	 | d d … | f ƒ j
 ƒ  } t j |  |  j ƒ  ƒ } | j j | ƒ \ } } | j ƒ  d } | j	 | d d … | f ƒ j
 ƒ  } t t |  j ƒ  t t | | j ƒ  ƒ ƒ ƒ }	 t t |  j ƒ  t t | | j ƒ  ƒ ƒ ƒ }
 |	 |
 f S(   sÇ  Return 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 

    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
    -----
    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.
    iÿÿÿÿNs.   hits_numpy() requires NumPy: http://scipy.org/i    (   t   numpyt   ImportErrorR   R   R   t   nodest   linalgt   eigt   argsortt   arrayt   flattenR   R   t   zipt   mapt   floatR   (   R   t   npt   Ht   et   evt   mR   t   AR    t   hubst   authorities(    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyR   €   s$    *
%%..gíµ ÷Æ°>c         C   sÅ  y d d l  } d d l } Wn t k
 r; t d ƒ ‚ n Xt |  ƒ d k rX i  i  f St j |  d |  j ƒ  ƒ} | j \ } } | j | } | j	 | d f ƒ | }	 d }
 x€ t
 r0|	 } | |	 }	 |	 |	 j ƒ  }	 | j |	 | ƒ j ƒ  } | | k  r Pn  |
 | k r#t d |
 d ƒ ‚ n  |
 d 7}
 q± W| j |	 ƒ j ƒ  } | j | | ƒ j ƒ  } t t |  j ƒ  t t | | j ƒ  ƒ ƒ ƒ } t t |  j ƒ  t t | | j ƒ  ƒ ƒ ƒ } | | f S(   sÌ  Return 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 : interger, 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.

    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.

    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.
    iÿÿÿÿNs.   hits_scipy() requires SciPy: http://scipy.org/i    R$   i   s:   HITS: power iteration failed to converge in %d iterations.(   t   scipy.sparseR(   R)   R   R   t   to_scipy_sparse_matrixR*   t   shapeR&   t   onesR   R   t   absoluteR   t   asarrayR/   R   R0   R1   R2   (   R   R   R   t   scipyR3   R'   R!   R7   R8   t   xR   t   xlastR#   R    R   R9   R:   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyR   ¾   s:    8
	
..c         C   s`   d d l  m } y d d  l } Wn | d ƒ ‚ n Xy d d  l } Wn | d ƒ ‚ n Xd  S(   Niÿÿÿÿ(   t   SkipTests   NumPy not availables   SciPy not available(   t   noseRD   R(   RA   (   t   moduleRD   R(   RA   (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyt   setup_module  s    (   t   __doc__t
   __author__t   __all__t   networkxR   t   networkx.exceptionR   R   R    R   R   R   R   RG   (    (    (    sb   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/hits_alg.pyt   <module>   s   	b	>\