Warning

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

color

color(G)[source]

Returns a two-coloring of the graph.

Raises an exception if the graph is not bipartite.

Parameters:

G : NetworkX graph

Returns:

color : dictionary

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 biparite set:

>>> nx.set_node_attributes(G, 'bipartite', c)
>>> print(G.node[0]['bipartite'])
1
>>> print(G.node[1]['bipartite'])
0