ó
ú£Õ\c           @   s¡   d  Z  d d l Z d d l m Z d Z d d d d g Z d	 d d
 d d d d „ Z d	 d d d d „ Z	 d	 d d d „ Z
 d	 d d
 d d d „ Z d „  Z d S(   s&   PageRank analysis of graph structure. iÿÿÿÿN(   t   NetworkXErrors   Aric Hagberg (hagberg@lanl.gov)t   pagerankt   pagerank_numpyt   pagerank_scipyt   google_matrixg333333ë?id   g:Œ0âŽyE>t   weightc            s  t  |  ƒ t j k s* t  |  ƒ t j k r9 t d ƒ ‚ n  t |  ƒ d k rO i  S|  j ƒ  sj |  j ƒ  } n |  } t j | d | ƒ} d | j	 ƒ  }	 | d
 k r¶ t j | |	 ƒ }
 n= | }
 d t |
 j ƒ  ƒ } x |
 D] } |
 | c | 9<qÙ W| d
 k rt j | |	 ƒ } nd | } d t | j ƒ  ƒ } x | D] } | | c | 9<q7Wt | ƒ t |  ƒ k rxt d ƒ ‚ n  | j ƒ  } g  | D] } | | d k r‹| ^ q‹} d } x_t r|
 ‰  t j ˆ  j ƒ  d ƒ }
 | |	 t ‡  f d †  | Dƒ ƒ } xk |
 D]c } x: | | D]. } |
 | c | ˆ  | | | | | 7<qW|
 | c | d | | | 7<qWd t |
 j ƒ  ƒ } x |
 D] } |
 | c | 9<q‰Wt g  |
 D] } t |
 | ˆ  | ƒ ^ q­ƒ } | | k  räPn  | | k rt d | d	 ƒ ‚ n  | d	 7} q¶W|
 S(   sv  Return the PageRank of the nodes in the graph.

    PageRank computes a ranking of the nodes in the graph G based on
    the structure of the incoming links. It was originally designed as
    an algorithm to rank web pages.

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

    alpha : float, optional
      Damping parameter for PageRank, default=0.85

    personalization: dict, optional
       The "personalization vector" consisting of a dictionary with a
       key for every graph node and nonzero personalization value for each node.

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

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

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

    weight : key, optional
      Edge data key to use as weight.  If None weights are set to 1.

    Returns
    -------
    pagerank : dictionary
       Dictionary of nodes with PageRank as value

    Examples
    --------
    >>> G=nx.DiGraph(nx.path_graph(4))
    >>> pr=nx.pagerank(G,alpha=0.9)

    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 PageRank algorithm was designed for directed graphs but this
    algorithm does not check if the input graph is directed and will
    execute on undirected graphs by converting each oriented edge in the
    directed graph to two edges.

    See Also
    --------
    pagerank_numpy, pagerank_scipy, google_matrix

    References
    ----------
    .. [1] A. Langville and C. Meyer,
       "A survey of eigenvector methods of web information retrieval."
       http://citeseer.ist.psu.edu/713792.html
    .. [2] Page, Lawrence; Brin, Sergey; Motwani, Rajeev and Winograd, Terry,
       The PageRank citation ranking: Bringing order to the Web. 1999
       http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf
    s2   pagerank() not defined for graphs with multiedges.i    R   g      ð?s7   Personalization vector must have a value for every nodeg        c         3   s   |  ] } ˆ  | Vq d  S(   N(    (   t   .0t   n(   t   xlast(    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pys	   <genexpr>~   s    s=   pagerank: power iteration failed to convergein %d iterations.i   N(   t   typet   nxt
   MultiGrapht   MultiDiGrapht	   Exceptiont   lent   is_directedt   to_directedt   stochastic_grapht   number_of_nodest   Nonet   dictt   fromkeyst   sumt   valuest   setR    t
   out_degreet   Truet   keyst   abs(   t   Gt   alphat   personalizationt   max_itert   tolt   nstartR   t   Dt   Wt   scalet   xt   st   kt   pR   R   t   danglet   it	   danglesumt   nbrt   err(    (   R   sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pyR      sZ    C* )	$,$1c         C   s  y d d l  } Wn t k
 r/ t d ƒ ‚ n X| d k rK |  j ƒ  } n3 | j ƒ  } t | ƒ t |  ƒ k r~ t d ƒ ‚ n  t j |  d | d | ƒ} | j	 \ } } | d k r¸ | S| j
 | j d d	 ƒ d k ƒ }	 x  |	 d D] }
 d
 | | |
 <qä W| | j d d	 ƒ } | j | ƒ } | d k	 rQ| j t | j ƒ  ƒ d t ƒ} n | } | | j ƒ  } | | d	 | | j | | ƒ } | S(   s  Return the Google matrix of the graph.

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

    alpha : float
      The damping factor

    personalization: dict, optional
       The "personalization vector" consisting of a dictionary with a
       key for every graph node and nonzero personalization value for each node.

    nodelist : list, optional
      The rows and columns are ordered according to the nodes in nodelist.
      If nodelist is None, then the ordering is produced by G.nodes().

    weight : key, optional
      Edge data key to use as weight.  If None weights are set to 1.

    Returns
    -------
    A : NumPy matrix
       Google matrix of the graph

    See Also
    --------
    pagerank, pagerank_numpy, pagerank_scipy
    iÿÿÿÿNs1   google_matrix() requires NumPy: http://scipy.org/sA   Personalization vector dictionarymust have a value for every nodet   nodelistR   i    t   axisi   g      ð?t   dtype(   t   numpyt   ImportErrorR   t   nodesR   R   R    R
   t   to_numpy_matrixt   shapet   whereR   t   onest   arrayt   listR   t   floatt   outer(   R   R   R   R/   R   t   npt   MR   t   mt   danglingt   dt   et   vt   P(    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pyR   ”   s2     !$"c      	   C   s  y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ d k rF i  S| d k ra |  j ƒ  } n | j ƒ  } t |  | d | d | d | ƒ} | j j | j	 ƒ \ } } | j
 ƒ  }	 | j | d d … |	 d f ƒ j ƒ  j }
 t |
 j ƒ  ƒ } t t | t t |
 | ƒ ƒ ƒ } | S(   sA  Return the PageRank of the nodes in the graph.

    PageRank computes a ranking of the nodes in the graph G based on
    the structure of the incoming links. It was originally designed as
    an algorithm to rank web pages.

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

    alpha : float, optional
      Damping parameter for PageRank, default=0.85

    personalization: dict, optional
       The "personalization vector" consisting of a dictionary with a
       key for every graph node and nonzero personalization value for each node.

    weight : key, optional
      Edge data key to use as weight.  If None weights are set to 1.

    Returns
    -------
    pagerank : dictionary
       Dictionary of nodes with PageRank as value

    Examples
    --------
    >>> G=nx.DiGraph(nx.path_graph(4))
    >>> pr=nx.pagerank_numpy(G,alpha=0.9)

    Notes
    -----
    The eigenvector calculation uses NumPy's interface to the LAPACK
    eigenvalue solvers.  This will be the fastest and most accurate
    for small graphs.

    This implementation works with Multi(Di)Graphs.

    See Also
    --------
    pagerank, pagerank_scipy, google_matrix

    References
    ----------
    .. [1] A. Langville and C. Meyer,
       "A survey of eigenvector methods of web information retrieval."
       http://citeseer.ist.psu.edu/713792.html
    .. [2] Page, Lawrence; Brin, Sergey; Motwani, Rajeev and Winograd, Terry,
       The PageRank citation ranking: Bringing order to the Web. 1999
       http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf
    iÿÿÿÿNs2   pagerank_numpy() requires NumPy: http://scipy.org/i    R   R/   R   (   R2   R3   R   R   R4   R   R   t   linalgt   eigt   Tt   argsortR9   t   flattent   realR;   R   R   t   zipt   map(   R   R   R   R   R=   R/   R>   t   eigenvaluest   eigenvectorst   indt   largestt   normt
   centrality(    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pyR   Ö   s"    5,"gíµ ÷Æ°>c         C   s]  y d d l  } Wn t k
 r/ t d ƒ ‚ n Xt |  ƒ d k rF i  S| d k ra |  j ƒ  } n | j ƒ  } t j |  d | d | d d ƒ} | j \ }	 }
 | j	 | j
 d	 d
 ƒ ƒ j ƒ  } d | | d k | | d k <| j j | j d d d | j Œ} | | } | j |	 ƒ |	 } | j	 | j | j
 d	 d
 ƒ d k d |	 d ƒ ƒ j ƒ  } | d k	 r—| j	 t | j ƒ  ƒ d t ƒ} | | j
 ƒ  } n | } d } xŸ | | k rD| } | | | | j | | ƒ d
 | | } | | j
 ƒ  } | j | | ƒ j
 ƒ  } | |	 | k  r7t t | t t | ƒ ƒ ƒ S| d
 7} q¦Wt d | d
 ƒ ‚ d S(   sœ  Return the PageRank of the nodes in the graph.

    PageRank computes a ranking of the nodes in the graph G based on
    the structure of the incoming links. It was originally designed as
    an algorithm to rank web pages.

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

    alpha : float, optional
      Damping parameter for PageRank, default=0.85

    personalization: dict, optional
       The "personalization vector" consisting of a dictionary with a
       key for every graph node and nonzero personalization value for each node.

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

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

    weight : key, optional
      Edge data key to use as weight.  If None weights are set to 1.

    Returns
    -------
    pagerank : dictionary
       Dictionary of nodes with PageRank as value

    Examples
    --------
    >>> G=nx.DiGraph(nx.path_graph(4))
    >>> pr=nx.pagerank_scipy(G,alpha=0.9)

    Notes
    -----
    The eigenvector calculation uses power iteration with a SciPy
    sparse matrix representation.

    See Also
    --------
    pagerank, pagerank_numpy, google_matrix

    References
    ----------
    .. [1] A. Langville and C. Meyer,
       "A survey of eigenvector methods of web information retrieval."
       http://citeseer.ist.psu.edu/713792.html
    .. [2] Page, Lawrence; Brin, Sergey; Motwani, Rajeev and Winograd, Terry,
       The PageRank citation ranking: Bringing order to the Web. 1999
       http://dbpubs.stanford.edu:8090/pub/showDoc.Fulltext?lang=en&doc=1999-66&format=pdf
    iÿÿÿÿNs2   pagerank_scipy() requires SciPy: http://scipy.org/i    R/   R   R1   t   fR0   i   g      ð?t   formatt   csrsC   pagerank_scipy: power iteration failed to convergein %d iterations.(   t   scipy.sparseR3   R   R   R4   R   R
   t   to_scipy_sparse_matrixR6   R9   R   RI   t   sparset   spdiagsRG   R8   R7   R:   R   R;   t   dott   absoluteR   RK   RL   R    (   R   R   R   R    R!   R   t   scipyR/   R>   R   R?   t   St   QR&   R*   RC   R+   R   R.   (    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pyR   "  s@    9!!$
:!*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   noseR_   R2   R\   (   t   moduleR_   R2   R\   (    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pyt   setup_module†  s    (   t   __doc__t   networkxR
   t   networkx.exceptionR    t
   __author__t   __all__R   R   R   R   R   Rb   (    (    (    sf   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/algorithms/link_analysis/pagerank_alg.pyt   <module>   s   …ALc