Previous topic


Next topic



generic_weighted_projected_graph(B, nodes, weight_function=None)[source]

Weighted projection of B with a user-specified weight function.

The bipartite network B is projected on to the specified nodes with weights computed by a user-specified function. This function must accept as a parameter the neighborhood sets of two nodes and return an integer or a float.

The nodes retain their attributes and are connected in the resulting graph if they have an edge to a common node in the original graph.

Parameters :

B : NetworkX graph

The input graph should be bipartite.

nodes : list or iterable

Nodes to project onto (the “bottom” nodes).

weight_function: function :

This function must accept as a parameters two sets, the neighborhoods of two nodes, and return an integer or a float. The default function computes the number of shared neighbors.

Returns :

Graph : NetworkX graph

A graph that is the projection onto the given nodes.


No attempt is made to verify that the input graph B is bipartite. The graph and node properties are (shallow) copied to the projected graph.


>>> from networkx.algorithms import bipartite
>>> def jaccard(unbrs, vnbrs):
...     return float(len(unbrs & vnbrs)) / len(unbrs | vnbrs)
>>> def shared(unbrs, vnbrs):
...     return len(unbrs & vnbrs)
>>> B = nx.path_graph(5)
>>> G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4], weight_function=jaccard)
>>> print(G.nodes())
[0, 2, 4]
>>> print(G.edges(data=True))
[(0, 2, {'weight': 0.5}), (2, 4, {'weight': 0.5})]
>>> G = bipartite.generic_weighted_projected_graph(B, [0, 2, 4], weight_function=shared)
>>> print(G.nodes())
[0, 2, 4]
>>> print(G.edges(data=True))
[(0, 2, {'weight': 1}), (2, 4, {'weight': 1})]