Graph Coloring on Bipartite Graphs in C++



Graph coloring is a technique in which, we assign colors to the vertices of a graph such that no two adjacent vertices have same color. Bipartite graph a special kind of graph which is possible to color by just two colors. In this article, we will discuss how to perform graph coloring on a bipartite graph using BFS algorithm.

What is Bipartite Graph?

Bipartite graph is special graph where you can divide the vertices into two sets, such that no two vertices of the same set are connected. This is why, it's possible to color a bipartite graph by using just two colors. Technically a graph is called a bipartite, if:

  • Its vertices can be divided into two sets U and V.
  • Every edge connects a vertex in U to a vertex in V.
  • There is no edge between vertices in the same set.

The image below shows an example of a bipartite graph.

Bipartite graph

Graph Coloring on Bipartite Graph

In the graph coloring of a bipartite graph, we will use two colors to color the graph such that no two connected vertices have the same color. If this is possible, the graph is bipartite. We can either use Breadth-First Search (BFS) or Depth-First Search (DFS) to try coloring the graph. In this section, we will use Breadth-First Search (BFS) algorithm to color the graph.

Steps to Color a Bipartite Graph

The following steps explain the logic used for coloring a bipartite graph:

  • Step 1: Initialize an array colors with size equal to the number of vertices, and set all elements to -1 to indicate the graph is uncolored.
  • Step 2: Run a loop through all vertices to handle disconnected components.
  • Step 3: For each uncolored vertex, assign an initial color (0) and push it into a queue to perform BFS.
  • Step 4: Use BFS to visit each neighbour of the current vertex. If the neighbour is uncolored, assign it the opposite color. If the neighbour already has the same color as the current vertex, then the graph is not bipartite.
  • Step 5: Finally, if all vertices can be colored without conflicts, the graph is bipartite.

C++ Program to Color a Bipartite Graph

The code below implements above algorithm in C++ language. It takes graph as input and tell if is a bipartite graph or not by coloring the graph.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

bool isBipartite(vector<vector<int>>& graph, vector<int>& color, int start) {
    queue<int> q;
    q.push(start);
    color[start] = 0; // Start with color 0

    while (!q.empty()) {
        int node = q.front();
        q.pop();

        for (int neighbor : graph[node]) {
            if (color[neighbor] == -1) {
                // Assign alternate color
                color[neighbor] = 1 - color[node];
                q.push(neighbor);
            } else if (color[neighbor] == color[node]) {
                // Same color found on adjacent nodes
                return false;
            }
        }
    }
    return true;
}

int main() {
    int n = 4; // Number of vertices
    vector<vector<int>> graph(n);

    // Example bipartite graph
    graph[0] = {1, 3};
    graph[1] = {0, 2};
    graph[2] = {1, 3};
    graph[3] = {0, 2};

    vector<int> color(n, -1); // -1 means uncolored
    bool bipartite = true;

    for (int i = 0; i < n; i++) {
        if (color[i] == -1) {
            if (!isBipartite(graph, color, i)) {
                bipartite = false;
                break;
            }
        }
    }

    if (bipartite) {
        cout << "The graph is bipartite.\n" << endl;
        for (int i = 0; i < n; i++) {
            cout << "Vertex " << i << " is colored with color " << color[i] << endl;
        }
    } else {
        cout << "The graph is NOT bipartite.\n";
    }

    return 0;
}

The output of the above code will be:

The graph is bipartite.
Vertex 0 is colored with color 0
Vertex 1 is colored with color 1
Vertex 2 is colored with color 0
Vertex 3 is colored with color 1
Updated on: 2025-05-02T18:27:53+05:30

436 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements