3.4 Testing Bipartiteness: An Application of Breadth-First Search

A bipartite graph is a graph where one node V can be partitioned into sets X and Y in such a way that every edge has an end in X and another end in Y. If we suppose the nodes of X are colored red and the nodes in Y are colored blue, we can say that a graph is bipartite if it is possible to color its nodes red and blue such that every edge has one red end and one blue end. If a graph is bipartite, then it can not contain an odd cycle. Thus an odd cycle is an obstacle to bipartiteness. The obvious problem that arises is then to ask if there are other complex obstacles to bipartiteness other than and odd cycle.

Designing the algorithm

This algorithm thus uses BFS: we are coloring each layer at a times,with consecutive layers having different colors, where odd-numbered layers are colored blue while the even-numbered layers are red.

Thus to implement it, we just use BFS and add an extra array color which will indicate the color of the nodes.
In BFS,

Analyzing the algorithm

Thus BFS algorithm can be used to efficiently test whether or not a graph is bipartite. This answers the problem asking whether or not a given graph is bipartite, and is a very good application of the BFS.

This section was interesting, as it gave us a more direct application of the BFS. I give it a 9/10.