networkx 2.8.2 Pypi GitHub Homepage
Other Docs
BackRef

The bipartite algorithms are not imported into the networkx namespace at the top level so the easiest way to use them is with:

>>> from networkx.algorithms import bipartite

NetworkX does not have a custom bipartite graph class but the Graph() or DiGraph() classes can be used to represent bipartite graphs. However, you have to keep track of which set each node belongs to, and make sure that there is no edge between nodes of the same set. The convention used in NetworkX is to use a node attribute named bipartite with values 0 or 1 to identify the sets each node belongs to. This convention is not enforced in the source code of bipartite functions, it's only a recommendation.

For example:

>>> B = nx.Graph()
>>> # Add nodes with the node attribute "bipartite"
>>> B.add_nodes_from([1, 2, 3, 4], bipartite=0)
>>> B.add_nodes_from(["a", "b", "c"], bipartite=1)
>>> # Add edges only between nodes of opposite node sets
>>> B.add_edges_from([(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c"), (4, "a")])

Many algorithms of the bipartite module of NetworkX require, as an argument, a container with all the nodes that belong to one set, in addition to the bipartite graph :None:None:`B`. The functions in the bipartite package do not check that the node set is actually correct nor that the input graph is actually bipartite. If :None:None:`B` is connected, you can find the two node sets using a two-coloring algorithm:

>>> nx.is_connected(B)
True
>>> bottom_nodes, top_nodes = bipartite.sets(B)

However, if the input graph is not connected, there are more than one possible colorations. This is the reason why we require the user to pass a container with all nodes of one bipartite node set as an argument to most bipartite functions. In the face of ambiguity, we refuse the temptation to guess and raise an AmbiguousSolution <networkx.AmbiguousSolution> Exception if the input graph for bipartite.sets <networkx.algorithms.bipartite.basic.sets> is disconnected.

Using the bipartite node attribute, you can easily get the two node sets:

>>> top_nodes = {n for n, d in B.nodes(data=True) if d["bipartite"] == 0}
>>> bottom_nodes = set(B) - top_nodes

So you can easily use the bipartite algorithms that require, as an argument, a container with all nodes that belong to one node set:

>>> print(round(bipartite.density(B, bottom_nodes), 2))
0.5
>>> G = bipartite.projected_graph(B, top_nodes)

All bipartite graph generators in NetworkX build bipartite graphs with the bipartite node attribute. Thus, you can use the same approach:

>>> RB = bipartite.random_graph(5, 7, 0.2)
>>> RB_top = {n for n, d in RB.nodes(data=True) if d["bipartite"] == 0}
>>> RB_bottom = set(RB) - RB_top
>>> list(RB_top)
[0, 1, 2, 3, 4]
>>> list(RB_bottom)
[5, 6, 7, 8, 9, 10, 11]

For other bipartite graph generators see Generators <networkx.algorithms.bipartite.generators> .

This module provides functions and operations for bipartite graphs. Bipartite graphs :None:None:`B = (U, V, E)` have two node sets :None:None:`U,V` and edges in :None:None:`E` that only connect nodes from opposite sets. It is common in the literature to use an spatial analogy referring to the two node sets as top and bottom nodes.

This module provides functions and operations for bipartite graphs. Bipartite graphs :None:None:`B = (U, V, E)` have two node sets :None:None:`U,V` and edges in :None:None:`E` that only connect nodes from opposite sets. It is common in the literature to use an spatial analogy referring to the two node sets as top and bottom nodes.

The bipartite algorithms are not imported into the networkx namespace at the top level so the easiest way to use them is with:

>>> from networkx.algorithms import bipartite

NetworkX does not have a custom bipartite graph class but the Graph() or DiGraph() classes can be used to represent bipartite graphs. However, you have to keep track of which set each node belongs to, and make sure that there is no edge between nodes of the same set. The convention used in NetworkX is to use a node attribute named bipartite with values 0 or 1 to identify the sets each node belongs to. This convention is not enforced in the source code of bipartite functions, it's only a recommendation.

For example:

>>> B = nx.Graph()
>>> # Add nodes with the node attribute "bipartite"
>>> B.add_nodes_from([1, 2, 3, 4], bipartite=0)
>>> B.add_nodes_from(["a", "b", "c"], bipartite=1)
>>> # Add edges only between nodes of opposite node sets
>>> B.add_edges_from([(1, "a"), (1, "b"), (2, "b"), (2, "c"), (3, "c"), (4, "a")])

Many algorithms of the bipartite module of NetworkX require, as an argument, a container with all the nodes that belong to one set, in addition to the bipartite graph :None:None:`B`. The functions in the bipartite package do not check that the node set is actually correct nor that the input graph is actually bipartite. If :None:None:`B` is connected, you can find the two node sets using a two-coloring algorithm:

>>> nx.is_connected(B)
True
>>> bottom_nodes, top_nodes = bipartite.sets(B)

However, if the input graph is not connected, there are more than one possible colorations. This is the reason why we require the user to pass a container with all nodes of one bipartite node set as an argument to most bipartite functions. In the face of ambiguity, we refuse the temptation to guess and raise an AmbiguousSolution <networkx.AmbiguousSolution> Exception if the input graph for bipartite.sets <networkx.algorithms.bipartite.basic.sets> is disconnected.

Using the bipartite node attribute, you can easily get the two node sets:

>>> top_nodes = {n for n, d in B.nodes(data=True) if d["bipartite"] == 0}
>>> bottom_nodes = set(B) - top_nodes

So you can easily use the bipartite algorithms that require, as an argument, a container with all nodes that belong to one node set:

>>> print(round(bipartite.density(B, bottom_nodes), 2))
0.5
>>> G = bipartite.projected_graph(B, top_nodes)

All bipartite graph generators in NetworkX build bipartite graphs with the bipartite node attribute. Thus, you can use the same approach:

>>> RB = bipartite.random_graph(5, 7, 0.2)
>>> RB_top = {n for n, d in RB.nodes(data=True) if d["bipartite"] == 0}
>>> RB_bottom = set(RB) - RB_top
>>> list(RB_top)
[0, 1, 2, 3, 4]
>>> list(RB_bottom)
[5, 6, 7, 8, 9, 10, 11]

For other bipartite graph generators see Generators <networkx.algorithms.bipartite.generators> .

Examples

See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.algorithms.bipartite.edgelist.generate_edgelist networkx.algorithms.bipartite.basic.is_bipartite_node_set networkx.drawing.layout.bipartite_layout networkx.algorithms.bipartite.cluster.average_clustering networkx.algorithms.bipartite.basic.sets networkx.algorithms.bipartite networkx.algorithms.bipartite.basic.density networkx.algorithms.bipartite.basic.is_bipartite networkx.algorithms.bipartite.projection.weighted_projected_graph networkx.algorithms.bipartite.cluster.robins_alexander_clustering networkx.algorithms.bipartite.matrix.from_biadjacency_matrix networkx.algorithms.bipartite.edgelist.read_edgelist networkx.algorithms.bipartite.projection.projected_graph networkx.algorithms.bipartite.edgelist.parse_edgelist networkx.algorithms.bipartite.basic.degrees networkx.algorithms.bipartite.cluster.latapy_clustering networkx.algorithms.bipartite.projection.collaboration_weighted_projected_graph networkx.algorithms.bipartite.spectral.spectral_bipartivity networkx.algorithms.bipartite.projection.overlap_weighted_projected_graph networkx.algorithms.bipartite.projection.generic_weighted_projected_graph networkx.algorithms.bipartite.basic.color

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/algorithms/bipartite/__init__.py#0
type: <class 'module'>
Commit: