#NEWLINE# def hits( G , max_iter = 100 , tol = 1e-08 , nstart = None , normalized = True ) : #NEWLINE# #INDENT# 'Return HITS hubs and authorities values for nodes.\n\n    The HITS algorithm computes two numbers for a node.\n    Authorities estimates the node value based on the incoming links.\n    Hubs estimates the node value based on outgoing links.\n\n    Parameters\n    ----------\n    G : graph\n      A NetworkX graph\n\n    max_iter : interger, optional\n      Maximum number of iterations in power method.\n\n    tol : float, optional\n      Error tolerance used to check convergence in power method iteration.\n\n    nstart : dictionary, optional\n      Starting value of each node for power method iteration.\n\n    normalized : bool (default=True)\n       Normalize results by the sum of all of the values.\n\n    Returns\n    -------\n    (hubs,authorities) : two-tuple of dictionaries\n       Two dictionaries keyed by node containing the hub and authority\n       values.\n\n    Examples\n    --------\n    >>> G=nx.path_graph(4)\n    >>> h,a=nx.hits(G)\n\n    Notes\n    -----\n    The eigenvector calculation is done by the power iteration method\n    and has no guarantee of convergence.  The iteration will stop\n    after max_iter iterations or an error tolerance of\n    number_of_nodes(G)*tol has been reached.\n\n    The HITS algorithm was designed for directed graphs but this\n    algorithm does not check if the input graph is directed and will\n    execute on undirected graphs.\n\n    References\n    ----------\n    .. [1] A. Langville and C. Meyer,\n       "A survey of eigenvector methods of web information retrieval."\n       http://citeseer.ist.psu.edu/713792.html\n    .. [2] Jon Kleinberg,\n       Authoritative sources in a hyperlinked environment\n       Journal of the ACM 46 (5): 604-32, 1999.\n       doi:10.1145/324133.324140.\n       http://www.cs.cornell.edu/home/kleinber/auth.pdf.\n    ' #NEWLINE# if ( ( type ( G ) == nx . MultiGraph ) or ( type ( G ) == nx . MultiDiGraph ) ) : #NEWLINE# #INDENT# raise Exception ( 'hits() not defined for graphs with multiedges.' ) #NEWLINE# #UNINDENT# if ( len ( G ) == 0 ) : #NEWLINE# #INDENT# return ( { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# if ( nstart is None ) : #NEWLINE# #INDENT# h = dict . fromkeys ( G , ( 1.0 / G . number_of_nodes ( ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# h = nstart #NEWLINE# s = ( 1.0 / sum ( h . values ( ) ) ) #NEWLINE# for k in h : #NEWLINE# #INDENT# h [ k ] *= s #NEWLINE# #UNINDENT# #UNINDENT# i = 0 #NEWLINE# while True : #NEWLINE# #INDENT# hlast = h #NEWLINE# h = dict . fromkeys ( hlast . keys ( ) , 0 ) #NEWLINE# a = dict . fromkeys ( hlast . keys ( ) , 0 ) #NEWLINE# for n in h : #NEWLINE# #INDENT# for nbr in G [ n ] : #NEWLINE# #INDENT# a [ nbr ] += ( hlast [ n ] * G [ n ] [ nbr ] . get ( 'weight' , 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# for n in h : #NEWLINE# #INDENT# for nbr in G [ n ] : #NEWLINE# #INDENT# h [ n ] += ( a [ nbr ] * G [ n ] [ nbr ] . get ( 'weight' , 1 ) ) #NEWLINE# #UNINDENT# #UNINDENT# s = ( 1.0 / max ( h . values ( ) ) ) #NEWLINE# for n in h : #NEWLINE# #INDENT# h [ n ] *= s #NEWLINE# #UNINDENT# s = ( 1.0 / max ( a . values ( ) ) ) #NEWLINE# for n in a : #NEWLINE# #INDENT# a [ n ] *= s #NEWLINE# #UNINDENT# err = sum ( [ abs ( ( h [ n ] - hlast [ n ] ) ) for n in h ] ) #NEWLINE# if ( err < tol ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# if ( i > max_iter ) : #NEWLINE# #INDENT# raise NetworkXError ( ( 'HITS: power iteration failed to converge in %d iterations.' % ( i + 1 ) ) ) #NEWLINE# #UNINDENT# i += 1 #NEWLINE# #UNINDENT# if normalized : #NEWLINE# #INDENT# s = ( 1.0 / sum ( a . values ( ) ) ) #NEWLINE# for n in a : #NEWLINE# #INDENT# a [ n ] *= s #NEWLINE# #UNINDENT# s = ( 1.0 / sum ( h . values ( ) ) ) #NEWLINE# for n in h : #NEWLINE# #INDENT# h [ n ] *= s #NEWLINE# #UNINDENT# #UNINDENT# return ( h , a )