color#

color(G)[source]#

Returns a two-coloring of the graph.

Raises an exception if the graph is not bipartite.

Parameters:
GNetworkX graph
Returns:
colordictionary

A dictionary keyed by node with a 1 or 0 as data for each node color.

Raises:
NetworkXError

If the graph is not two-colorable.

Examples

>>> from networkx.algorithms import bipartite
>>> G = nx.path_graph(4)
>>> c = bipartite.color(G)
>>> print(c)
{0: 1, 1: 0, 2: 1, 3: 0}

You can use this to set a node attribute indicating the bipartite set:

>>> nx.set_node_attributes(G, c, "bipartite")
>>> print(G.nodes[0]["bipartite"])
1
>>> print(G.nodes[1]["bipartite"])
0