Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I partition a bipartite graph by color?

For instance, suppose I have a graph G = (V, E) where

V = {A, B, C, D}
E = {(A, B), (A,D), (C, D)}

This graph is bipartite, and thus can be split into two disjoint sets {A, C} and {B, D}. My first guess is that I can simply walk the graph and assign alternating colors to each vertex. Is this the case, or is it more complicated/simpler than this? Are there any known algorithms for this?

like image 429
Jason Baker Avatar asked Oct 31 '09 18:10

Jason Baker


2 Answers

Your first guess is correct - traverse the graph and alternate.

The algorithm should be simple. I'd keep two queues of nodes to visit, one for each colour. Pop nodes off the queues alternately, mark its colour, and push any non-visited adjacent nodes into the queue for the opposite colour. Terminate when the number of visited nodes + the length of both queues = number of nodes in the graph.

like image 83
Pete Kirkham Avatar answered Sep 23 '22 17:09

Pete Kirkham


From Wikipedia (http://en.wikipedia.org/wiki/Bipartite_graph)

If a bipartite graph is connected, its bipartition can be defined by the parity of the distances from any arbitrarily chosen vertex v: one subset consists of the vertices at even distance to v and the other subset consists of the vertices at odd distance to v.

Thus, one may efficiently test whether a graph is bipartite by using this parity technique to assign vertices to the two subsets U and V, separately within each connected component of the graph, and then examine each edge to verify that it has endpoints assigned to different subsets.

like image 38
peter.murray.rust Avatar answered Sep 23 '22 17:09

peter.murray.rust