Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.algorithms.bipartite.basic.sets

sets(G, top_nodes=None)[source]

Returns bipartite node sets of graph G.

Raises an exception if the graph is not bipartite or if the input graph is disconnected and thus more than one valid solution exists. See bipartite documentation for further details on how bipartite graphs are handled in NetworkX.

Parameters
  • G (NetworkX graph)

  • top_nodes (container, optional) – Container with all nodes in one bipartite node set. If not supplied it will be computed. But if more than one solution exists an exception will be raised.

Returns

  • X (set) – Nodes from one side of the bipartite graph.

  • Y (set) – Nodes from the other side.

Raises
  • AmbiguousSolution – Raised if the input bipartite graph is disconnected and no container with all nodes in one bipartite set is provided. When determining the nodes in each bipartite set more than one valid solution is possible if the input graph is disconnected.

  • NetworkXError – Raised if the input graph is not bipartite.

Examples

>>> from networkx.algorithms import bipartite
>>> G = nx.path_graph(4)
>>> X, Y = bipartite.sets(G)
>>> list(X)
[0, 2]
>>> list(Y)
[1, 3]

See also

color()