#NEWLINE# @ not_implemented_for ( 'directed' ) #NEWLINE# @ not_implemented_for ( 'multigraph' ) #NEWLINE# def cn_soundarajan_hopcroft( G , ebunch = None , community = 'community' ) : #NEWLINE# #INDENT# "Count the number of common neighbors of all node pairs in ebunch\n        using community information.\n\n    For two nodes `u` and `v`, this function computes the number of\n    common neighbors and bonus one for each common neighbor belonging to\n    the same community as `u` and `v`. Mathematically,\n\n    .. math::\n\n        |\\Gamma(u) \\cap \\Gamma(v)| + \\sum_{w \\in \\Gamma(u) \\cap \\Gamma(v)} f(w)\n\n    where `f(w)` equals 1 if `w` belongs to the same community as `u`\n    and `v` or 0 otherwise and :math:`\\Gamma(u)` denotes the set of\n    neighbors of `u`.\n\n    Parameters\n    ----------\n    G : graph\n        A NetworkX undirected graph.\n\n    ebunch : iterable of node pairs, optional (default = None)\n        The score will be computed for each pair of nodes given in the\n        iterable. The pairs must be given as 2-tuples (u, v) where u\n        and v are nodes in the graph. If ebunch is None then all\n        non-existent edges in the graph will be used.\n        Default value: None.\n\n    community : string, optional (default = 'community')\n        Nodes attribute name containing the community information.\n        G[u][community] identifies which community u belongs to. Each\n        node belongs to at most one community. Default value: 'community'.\n\n    Returns\n    -------\n    piter : iterator\n        An iterator of 3-tuples in the form (u, v, p) where (u, v) is a\n        pair of nodes and p is their score.\n\n    Examples\n    --------\n    >>> import networkx as nx\n    >>> G = nx.path_graph(3)\n    >>> G.node[0]['community'] = 0\n    >>> G.node[1]['community'] = 0\n    >>> G.node[2]['community'] = 0\n    >>> preds = nx.cn_soundarajan_hopcroft(G, [(0, 2)])\n    >>> for u, v, p in preds:\n    ...     '(%d, %d) -> %d' % (u, v, p)\n    ...\n    '(0, 2) -> 2'\n\n    References\n    ----------\n    .. [1] Sucheta Soundarajan and John Hopcroft.\n       Using community information to improve the precision of link\n       prediction methods.\n       In Proceedings of the 21st international conference companion on\n       World Wide Web (WWW '12 Companion). ACM, New York, NY, USA, 607-608.\n       http://doi.acm.org/10.1145/2187980.2188150\n    " #NEWLINE# def predict( u , v ) : #NEWLINE# #INDENT# Cu = _community ( G , u , community ) #NEWLINE# Cv = _community ( G , v , community ) #NEWLINE# cnbors = list ( nx . common_neighbors ( G , u , v ) ) #NEWLINE# neighbors = ( sum ( ( ( _community ( G , w , community ) == Cu ) for w in cnbors ) ) if ( Cu == Cv ) else 0 ) #NEWLINE# return ( len ( cnbors ) + neighbors ) #NEWLINE# #UNINDENT# return _apply_prediction ( community , predict , ebunch )