#NEWLINE# def closeness_centrality( G , nodes , normalized = True ) : #NEWLINE# #INDENT# 'Compute the closeness centrality for nodes in a bipartite network.\n\n    The closeness of a node is the distance to all other nodes in the \n    graph or in the case that the graph is not connected to all other nodes\n    in the connected component containing that node.\n\n    Parameters\n    ----------\n    G : graph\n        A bipartite network\n\n    nodes : list or container\n        Container with all nodes in one bipartite node set.\n\n    normalized : bool, optional      \n      If True (default) normalize by connected component size.\n\n    Returns\n    -------\n    closeness : dictionary\n        Dictionary keyed by node with bipartite closeness centrality \n        as the value.\n\n    See Also\n    --------\n    betweenness_centrality,\n    degree_centrality\n    sets,\n    is_bipartite\n\n    Notes\n    -----\n    The nodes input parameter must conatin all nodes in one bipartite node set,\n    but the dictionary returned contains all nodes from both node sets.\n\n    Closeness centrality is normalized by the minimum distance possible. \n    In the bipartite case the minimum distance for a node in one bipartite \n    node set is 1 from all nodes in the other node set and 2 from all \n    other nodes in its own set [1]_. Thus the closeness centrality\n    for node `v`  in the two bipartite sets `U` with \n    `n` nodes and `V` with `m` nodes is \n \n    .. math::\n\n        c_{v} = \\frac{m + 2(n - 1)}{d}, \\mbox{for} v \\in U,\n\n        c_{v} = \\frac{n + 2(m - 1)}{d}, \\mbox{for} v \\in V,\n\n    where `d` is the sum of the distances from `v` to all\n    other nodes.\n\n    Higher values of closeness  indicate higher centrality.\n\n    As in the unipartite case, setting normalized=True causes the\n    values to normalized further to n-1 / size(G)-1 where n is the\n    number of nodes in the connected part of graph containing the\n    node.  If the graph is not completely connected, this algorithm\n    computes the closeness centrality for each connected part\n    separately.\n\n    References\n    ----------\n    .. [1] Borgatti, S.P. and Halgin, D. In press. "Analyzing Affiliation \n        Networks". In Carrington, P. and Scott, J. (eds) The Sage Handbook \n        of Social Network Analysis. Sage Publications.\n        http://www.steveborgatti.com/papers/bhaffiliations.pdf\n    ' #NEWLINE# closeness = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# path_length = nx . single_source_shortest_path_length #NEWLINE# top = set ( nodes ) #NEWLINE# bottom = ( set ( G ) - top ) #NEWLINE# n = float ( len ( top ) ) #NEWLINE# m = float ( len ( bottom ) ) #NEWLINE# for node in top : #NEWLINE# #INDENT# sp = dict ( path_length ( G , node ) ) #NEWLINE# totsp = sum ( sp . values ( ) ) #NEWLINE# if ( ( totsp > 0.0 ) and ( len ( G ) > 1 ) ) : #NEWLINE# #INDENT# closeness [ node ] = ( ( m + ( 2 * ( n - 1 ) ) ) / totsp ) #NEWLINE# if normalized : #NEWLINE# #INDENT# s = ( ( len ( sp ) - 1.0 ) / ( len ( G ) - 1 ) ) #NEWLINE# closeness [ node ] *= s #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# closeness [ n ] = 0.0 #NEWLINE# #UNINDENT# #UNINDENT# for node in bottom : #NEWLINE# #INDENT# sp = dict ( path_length ( G , node ) ) #NEWLINE# totsp = sum ( sp . values ( ) ) #NEWLINE# if ( ( totsp > 0.0 ) and ( len ( G ) > 1 ) ) : #NEWLINE# #INDENT# closeness [ node ] = ( ( n + ( 2 * ( m - 1 ) ) ) / totsp ) #NEWLINE# if normalized : #NEWLINE# #INDENT# s = ( ( len ( sp ) - 1.0 ) / ( len ( G ) - 1 ) ) #NEWLINE# closeness [ node ] *= s #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# closeness [ n ] = 0.0 #NEWLINE# #UNINDENT# #UNINDENT# return closeness