def weighted_projected_graph(B, nodes, ratio=False):
 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()
 G.graph.update(B.graph)
 G.add_nodes_from(((n, B.node[n]) for n in nodes))
 n_top = float((len(B) - len(nodes)))
 for u in nodes:
  unbrs = set(B[u])
  nbrs2 = (set((n for nbr in unbrs for n in B[nbr])) - set([u]))
  for v in nbrs2:
   vnbrs = set(pred[v])
   common = (unbrs & vnbrs)
   if (not ratio):
    weight = len(common)
   else:
    weight = (len(common) / n_top)
   G.add_edge(u, v, weight=weight)
 return G

def weighted_projected_graph ( B , nodes , ratio = False ) : 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 ( ) G . graph . update ( B . graph ) G . add_nodes_from ( ( ( n , B . node [ n ] ) for n in nodes ) ) n_top = float ( ( len ( B ) - len ( nodes ) ) ) for u in nodes : unbrs = set ( B [ u ] ) nbrs2 = ( set ( ( n for nbr in unbrs for n in B [ nbr ] ) ) - set ( [ u ] ) ) for v in nbrs2 : vnbrs = set ( pred [ v ] ) common = ( unbrs & vnbrs ) if ( not ratio ) : weight = len ( common ) else : weight = ( len ( common ) / n_top ) G . add_edge ( u , v , weight = weight ) return G
returns a weighted projection of b onto one of its node sets .