ó žÃÒYc@s•dZddlZddlmZdZddddd gZd d ded „Z dd „Z dd„Z ed„Z d ded„Z d„ZdS(s2Hubs and authorities analysis of graph structure. iÿÿÿÿN(t NetworkXErrorsAric Hagberg (hagberg@lanl.gov)thitst hits_numpyt hits_scipytauthority_matrixt hub_matrixidg:Œ0âŽyE>c Csít|ƒtjks*t|ƒtjkr9tdƒ‚nt|ƒdkrUiifS|dkr€tj|d|j ƒƒ}n=|}dt |j ƒƒ}x|D]}||c|9|| D]2} | | c| | || | jddƒ7|| D]2} || c| | || | jddƒ7>> 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.igð?tweightiN(ttypetnxt MultiGrapht MultiDiGrapht ExceptiontlentNonetdicttfromkeystnumber_of_nodestsumtvaluestrangetkeystgettmaxtabstPowerIterationFailedConvergence(tGtmax_iterttoltnstartt normalizedthtstkt_thlasttatntnbrterr((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyRsL@*    4 4  1   cCs tj|d|ƒ}|j|S(s!Return the HITS authority matrix.tnodelist(Rtto_numpy_matrixtT(RR'tM((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyR‚scCs tj|d|ƒ}||jS(sReturn the HITS hub matrix.R'(RR(R)(RR'R*((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyRˆsc Cs¥yddl}Wntk r/tdƒ‚nXt|ƒdkrLiifStj|t|ƒƒ}|jj|ƒ\}}|jƒd}|j |dd…|fƒj ƒ}tj |t|ƒƒ}|jj|ƒ\}}|jƒd}|j |dd…|fƒj ƒ} |r?||j ƒ}| | j ƒ} n ||j ƒ}| | j ƒ} tt|tt|ƒƒƒ} tt|tt| ƒƒƒ} | | fS(sReturn 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) >>> 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(tnumpyt ImportErrorR RRtlisttlinalgteigtargsorttarraytflattenRRRRtziptmaptfloat( RRtnptHtetevtmRtAR#thubst authorities((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyRŽs.-   %%gíµ ÷Æ°>cCsÉyddl}ddl}Wntk r;tdƒ‚nXt|ƒdkrXiifStj|dt|ƒƒ}|j\}}|j|} |j |dfƒ|} d} x{t r+| } | | } | | j ƒ} |j | | ƒj ƒ} | |krPn| |krtj|ƒ‚n| d7} q±W|j| ƒjƒ}|j||ƒjƒ}|rƒ||j ƒ}||j ƒ}ntt|tt|ƒƒƒ}tt|tt|ƒƒƒ}||fS(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. 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. iÿÿÿÿNs.hits_scipy() requires SciPy: http://scipy.org/iR'i(t scipy.sparseR+R,R Rtto_scipy_sparse_matrixR-tshapeR)tonestTrueRtabsoluteRRtasarrayR2RR3R4R5(RRRRtscipyR6R*R$R:R;txtitxlastR&R#RR<R=((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyRÕs>B         cCs`ddlm}yddl}Wn|dƒ‚nXyddl}Wn|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestsNumPy not availablesSciPy not available(tnoseRIR+RE(tmoduleRIR+RE((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyt setup_module>s(t__doc__tnetworkxRtnetworkx.exceptionRt __author__t__all__R RBRRRRRRL(((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/link_analysis/hits_alg.pyts r   Gi