def generic_weighted_projected_graph(B, nodes, weight_function=None):
 if B.is_multigraph():
  raise nx.NetworkXError('not defined for multigraphs')
 if B.is_directed():
  pred = B.pred
  G = nx.DiGraph()
 else:
  pred = B.adj
  G = nx.Graph()
 if (weight_function is None):
  def weight_function(G, u, v):
   return len((set(G[u]) & set(pred[v])))
 G.graph.update(B.graph)
 G.add_nodes_from(((n, B.node[n]) for n in nodes))
 for u in nodes:
  nbrs2 = (set((n for nbr in set(B[u]) for n in B[nbr])) - set([u]))
  for v in nbrs2:
   weight = weight_function(B, u, v)
   G.add_edge(u, v, weight=weight)
 return G

def generic_weighted_projected_graph ( B , nodes , weight_function = None ) : if B . is_multigraph ( ) : raise nx . NetworkXError ( 'not defined for multigraphs' ) if B . is_directed ( ) : pred = B . pred G = nx . DiGraph ( ) else : pred = B . adj G = nx . Graph ( ) if ( weight_function is None ) : def weight_function ( G , u , v ) : return len ( ( set ( G [ u ] ) & set ( pred [ v ] ) ) ) G . graph . update ( B . graph ) G . add_nodes_from ( ( ( n , B . node [ n ] ) for n in nodes ) ) for u in nodes : nbrs2 = ( set ( ( n for nbr in set ( B [ u ] ) for n in B [ nbr ] ) ) - set ( [ u ] ) ) for v in nbrs2 : weight = weight_function ( B , u , v ) G . add_edge ( u , v , weight = weight ) return G
weighted projection of b with a user-specified weight function .