1. Introduction
A Bipartite Graph is a graph in which all vertices can be divided into two separate groups (or sets) such that no two adjacent vertices belong to the same group.
In simple terms, we should be able to color all vertices using only two colors, ensuring that connected vertices always have different colors.
For example:
- Blue represents Group 1.
- Green represents Group 2.
- Every edge must connect a blue vertex to a green vertex.
- Two adjacent vertices must never have the same color.
This problem can be solved efficiently using Breadth-First Search (BFS) or Depth-First Search (DFS).
2. Understanding the Bipartite Graph
Consider the following graph:
0 -------- 1 -------- 2The vertices can be divided into two groups:
- Group 1: {0, 2}
- Group 2: {1}
Every edge connects vertices from different groups.
Therefore, this graph is a Bipartite Graph.
Example 1
Input:
adj = [[1], [0, 2], [1]]Output:
trueExplanation:
The graph contains three vertices: 0, 1 and 2.
We can assign colors as follows:
| Vertex | Connected Vertices | Color |
|---|---|---|
| 0 | 1 | Blue (0) |
| 1 | 0, 2 | Green (1) |
| 2 | 1 | Blue (0) |
All adjacent vertices have different colors.
Therefore, the graph is bipartite.
Example 2 – Non-Bipartite Graph
Consider a triangle:
0
/ \
1---2Input:
adj = [[1, 2], [0, 2], [0, 1]]Output:
falseExplanation:
- Vertex 0 is assigned color 0.
- Vertices 1 and 2 must both receive color 1.
- However, vertices 1 and 2 are connected to each other.
Both adjacent vertices have the same color.
Therefore, the graph is not bipartite.
3. What Is BFS (Breadth-First Search)?
BFS is a graph traversal algorithm that visits vertices level by level.
It uses a Queue (FIFO – First In, First Out) data structure.
For example:
0
/ \
1 2
|
3BFS traversal order:
0 → 1 → 2 → 3In the Bipartite Graph problem, BFS helps us visit every vertex and assign alternating colors to adjacent vertices.
4. Algorithm – Step-by-Step
We use a color array to maintain the color assigned to each vertex.
Initially, every vertex is assigned -1.
Meaning:
-1= Not visited / Not colored.0= First color.1= Second color.
Algorithm
- Find the total number of vertices using
adj.Count. - Create a color array of size V.
- Initialize all elements to
-1. - Iterate through all vertices to handle disconnected components.
- If a vertex is not colored, start BFS from that vertex.
- Assign color
0to the starting vertex. - Visit all its adjacent vertices.
- If an adjacent vertex is not colored, assign the opposite color.
- If an adjacent vertex already has the same color, return
false. - If all vertices are processed without any conflict, return
true.
5. C# Implementation – BFS Approach
class Solution
{
public bool isBipartite(List<List<int>> adj)
{
int V = adj.Count;
// Initialize color array
int[] color = new int[V];
for (int i = 0; i < V; i++)
{
color[i] = -1;
}
// Check all graph components
for (int i = 0; i < V; i++)
{
if (color[i] == -1)
{
Queue<int> queue = new Queue<int>();
queue.Enqueue(i);
color[i] = 0;
while (queue.Count > 0)
{
int node = queue.Dequeue();
foreach (int neighbor in adj[node])
{
// If the neighbor is not colored
if (color[neighbor] == -1)
{
color[neighbor] = 1 - color[node];
queue.Enqueue(neighbor);
}
// If adjacent vertices have the same color
else if (color[neighbor] == color[node])
{
return false;
}
}
}
}
}
return true;
}
}
Join the conversation! Your thoughts help the community grow.