"""Hubs and authorities analysis of graph structure.
"""
import networkx as nx

__all__ = ["hits", "hits_numpy", "hits_scipy", "authority_matrix", "hub_matrix"]

[docs]def hits(G, max_iter=100, tol=1.0e-8, nstart=None, normalized=True): """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 ---------- ..  A. Langville and C. Meyer, "A survey of eigenvector methods of web information retrieval." http://citeseer.ist.psu.edu/713792.html ..  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. """ import numpy as np import scipy as sp import scipy.sparse.linalg # call as sp.sparse.linalg if len(G) == 0: return {}, {} M = nx.adjacency_matrix(G, nodelist=list(G), dtype=float) if nstart is None: u, s, vt = sp.sparse.linalg.svds(M, k=1, maxiter=max_iter, tol=tol) else: nstart = np.array(list(nstart.values())) u, s, vt = sp.sparse.linalg.svds(M, k=1, v0=nstart, maxiter=max_iter, tol=tol) a = vt.flatten().real h = np.asarray(M * a).flatten() if normalized: h = h / h.sum() a = a / a.sum() hubs = dict(zip(G, map(float, h))) authorities = dict(zip(G, map(float, a))) return hubs, authorities
def _hits_python(G, max_iter=100, tol=1.0e-8, nstart=None, normalized=True): if type(G) == nx.MultiGraph or type(G) == nx.MultiDiGraph: raise Exception("hits() not defined for graphs with multiedges.") if len(G) == 0: return {}, {} # choose fixed starting vector if not given if nstart is None: h = dict.fromkeys(G, 1.0 / G.number_of_nodes()) else: h = nstart # normalize starting vector s = 1.0 / sum(h.values()) for k in h: h[k] *= s for _ in range(max_iter): # power iteration: make up to max_iter iterations hlast = h h = dict.fromkeys(hlast.keys(), 0) a = dict.fromkeys(hlast.keys(), 0) # this "matrix multiply" looks odd because it is # doing a left multiply a^T=hlast^T*G for n in h: for nbr in G[n]: a[nbr] += hlast[n] * G[n][nbr].get("weight", 1) # now multiply h=Ga for n in h: for nbr in G[n]: h[n] += a[nbr] * G[n][nbr].get("weight", 1) # normalize vector s = 1.0 / max(h.values()) for n in h: h[n] *= s # normalize vector s = 1.0 / max(a.values()) for n in a: a[n] *= s # check convergence, l1 norm err = sum([abs(h[n] - hlast[n]) for n in h]) if err < tol: break else: raise nx.PowerIterationFailedConvergence(max_iter) if normalized: s = 1.0 / sum(a.values()) for n in a: a[n] *= s s = 1.0 / sum(h.values()) for n in h: h[n] *= s return h, a
[docs]def authority_matrix(G, nodelist=None): """Returns the HITS authority matrix. .. deprecated:: 2.6 """ import warnings msg = ( "\nauthority_matrix is deprecated as of version 2.6 and will be removed " "in version 3.0.\n" "The authority matrix can be computed by::\n" " >>> M = nx.to_numpy_array(G, nodelist=nodelist)\n" " >>> M.T @ M" ) warnings.warn(msg, DeprecationWarning) M = nx.to_numpy_array(G, nodelist=nodelist) return M.T @ M
[docs]def hub_matrix(G, nodelist=None): """Returns the HITS hub matrix. .. deprecated:: 2.6 """ import warnings msg = ( "\nhub_matrix is deprecated as of version 2.6 and will be removed " "in version 3.0.\n" "The hub matrix can be computed by::\n" " >>> M = nx.to_numpy_array(G, nodelist=nodelist)\n" " >>> M @ M.T" ) warnings.warn(msg, DeprecationWarning) M = nx.to_numpy_array(G, nodelist=nodelist) return M @ M.T