ó žÃÒYc @sØdZddlZddlmZddlmZdjddgƒZdd d d gZ ed ƒd ddddddd„ƒZ d ddddd„Z d dddd„Z d dddddd„Zd„ZdS(s&PageRank analysis of graph structure. iÿÿÿÿN(t NetworkXError(tnot_implemented_fors s%Aric Hagberg s$Brandon Liu tweightc sÌt|ƒdkriS|jƒs1|jƒ}n|}tj|d|ƒ} | jƒ} |d kr}tj| d| ƒ} n:t t |j ƒƒƒ‰t‡fd†|j ƒDƒƒ} |d krÜtj| d| ƒ} n:t t |j ƒƒƒ‰t‡fd†|j ƒDƒƒ} |d kr+| } n:t t |j ƒƒƒ‰t‡fd†|j ƒDƒƒ} g| D]'}| j |d|ƒdkrl|^ql}xt|ƒD]}| ‰tjˆjƒdƒ} |t ‡fd†|Dƒƒ}xƒ| D]{}x:| |D].}| |c|ˆ|| |||7>> 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 an error tolerance of ``len(G) * tol`` has been reached. If the number of iterations exceed `max_iter`, a :exc:`networkx.exception.PowerIterationFailedConvergence` exception is raised. 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 edge in the directed graph to two edges. See Also -------- pagerank_numpy, pagerank_scipy, google_matrix 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] 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 iRgð?c3s%|]\}}||ˆfVqdS(N((t.0tktv(ts(s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pys sc3s%|]\}}||ˆfVqdS(N((RR R (R (s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pys †sc3s%|]\}}||ˆfVqdS(N((RR R (R (s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pys sgc3s|]}ˆ|VqdS(N((Rtn(txlast(s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pys ”sN(tlent is_directedt to_directedtnxtstochastic_graphtnumber_of_nodestNonetdicttfromkeystfloattsumtvaluestitemst out_degreetrangetkeystgettabstPowerIterationFailedConvergence(tGtalphatpersonalizationtmax_iterttoltnstartRtdanglingtDtWtNtxtptdangling_weightsR tdangling_nodest_t danglesumtnbrterr((R R s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pyRs@\   " "  "4  ,<1cCsŠddl}|dkr't|ƒ}ntj|d|d|ƒ}t|ƒ}|dkr^|S|dkrƒ|jd||ƒ} nD|jg|D]} |j| dƒ^qdt ƒ} | | j ƒ:} |dkrÜ| } nD|jg|D]} |j| dƒ^qédt ƒ} | | j ƒ:} |j |j dd ƒdkƒd} x| D]} | || >> 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. For multigraphs the weight between two nodes is set to be the sum of all edge weights between those nodes. 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ÿÿÿÿNiR#RR'(R6RRtlinalgteigtTtargmaxR:tflattentrealRRRtziptmap( R!R"R#RR'R<R=t eigenvaluest eigenvectorstindtlargesttnorm((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pyRsE (cCsTddl}t|ƒ}|dkr(iSt|ƒ} tj|d| d|dtƒ} |j| jddƒƒjƒ} d | | dk| | dk<|j j | j dd d | j Œ} | | } |j d ||ƒ} |dkrý|j d ||ƒ}nD|jg| D]}|j|dƒ^q dtƒ}||jƒ}|dkrV|}nD|jg| D]}|j|dƒ^qcdtƒ}||jƒ:}|j| dkƒd}x‹t|ƒD]}}| }|| | t| |ƒ|d||} |j| |ƒjƒ}|||krÀtt| tt| ƒƒƒSqÀWtj|ƒ‚dS( so 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. Undirected graphs will be converted to a directed graph with two directed edges for each undirected edge. alpha : float, optional Damping parameter for PageRank, default=0.85. personalization: dict, optional The "personalization vector" consisting of a dictionary with a key some subset of graph nodes and personalization value each of those. At least one personalization value must be non-zero. If not specfiied, a nodes personalization value will be zero. By default, a uniform distribution is used. 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. dangling: dict, optional The outedges to be assigned to any "dangling" nodes, i.e., nodes without any outedges. The dict key is the node the outedge points to and the dict value is the weight of that outedge. By default, dangling nodes are given outedges according to the personalization vector (uniform if not specified) This must be selected to result in an irreducible transition matrix (see notes under google_matrix). It may be common to have the dangling dict to be the same as the personalization dict. 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. This implementation works with Multi(Di)Graphs. For multigraphs the weight between two nodes is set to be the sum of all edge weights between those nodes. See Also -------- pagerank, pagerank_numpy, google_matrix 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] 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ÿÿÿÿNiR3RR4R5igð?tformattcsr(t scipy.sparseRR7Rtto_scipy_sparse_matrixRR:RRCtsparsetspdiagsRAtshapeR9RRR;RtabsoluteRRERFR (R!R"R#R$R%RR'tscipyR*R3R=tStQR+R,R R-t is_danglingR/R R2((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pyRSs<R     !$  4  +  cCs`ddlm}yddl}Wn|dƒ‚nXyddl}Wn|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestsNumPy not availablesSciPy not available(tnoseRXR6RT(tmoduleRXR6RT((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pyt setup_moduleÔs(t__doc__tnetworkxRtnetworkx.exceptionRtnetworkx.utilsRtjoint __author__t__all__RRRRRR[(((s„/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/pagerank_alg.pyts$     Ž]  R