A bipartite graph also called a bi-graph, is a set of graph vertices, i.e, points where multiple lines meet, decomposed into two disjoint sets, meaning they have no element in common, such that no two graph vertices within the same set are adjacent.
Adjacent nodes are any two nodes that are connected by an edge.
A bipartite graph is a special case of a k-partite graph with k = 2. The illustration below shows some bipartite graphs, with vertices in each graph colored based on the disjoint set they belong to
Bipartite graphs are equivalent to two-colorable graphs i.e., coloring of the vertices using two colors in such a way that vertices of the same color are never adjacent along an edge. All Acyclic1 graphs are bipartite. A cyclic2 graph is bipartite iff all its cycles are of even length. Some common examples of a bipartite graph include star graphs3, grid graphs4 and gear graphs5.
A simple algorithm using BFS to check if a graph is bipartite or not is given as follows:
#include <iostream>#include <queue>#define V 4using namespace std;// This function returns true if graph// G[V][V] is Bipartite, else falsebool isBipartite(int G[][V], int src){// Create a color array to store colors// assigned to all veritces. Vertex// number is used as index in this array.// The value '-1' of colorArr[i]// is used to indicate that no color// is assigned to vertex 'i'. The value 1// is used to indicate first color// is assigned and value 0 indicates// second color is assigned.int colorArr[V];for (int i = 0; i < V; ++i)colorArr[i] = -1;// Assign first color to sourcecolorArr[src] = 1;// Create a queue (FIFO) of vertex// numbers and enqueue source vertex// for BFS traversalqueue <int> q;q.push(src);// Run while there are vertices// in queue (Similar to BFS)while (!q.empty()){// Dequeue a vertex from queue ( Refer http://goo.gl/35oz8 )int u = q.front();q.pop();// Return false if there is a self-loopif (G[u][u] == 1)return false;// Find all non-colored adjacent verticesfor (int v = 0; v < V; ++v){// An edge from u to v exists and// destination v is not coloredif (G[u][v] && colorArr[v] == -1){// Assign alternate color to this adjacent v of ucolorArr[v] = 1 - colorArr[u];q.push(v);}// An edge from u to v exists and destination// v is colored with same color as uelse if (G[u][v] && colorArr[v] == colorArr[u])return false;}}// If we reach here, then all adjacent// vertices can be colored with alternate colorreturn true;}//Program to test above functionint main(){int G[][V] = {{0, 1, 0, 1},{1, 0, 1, 0},{0, 1, 0, 1},{1, 0, 1, 0}};isBipartite(G, 0) ? cout << "Yes, the graph is Bipartite" : cout << "No, the graph is not Bipartite";return 0;}
If V is the number of vertices and E is the number of edges, then the time complexity of a graph represented using an adjacency list will be:
Bipartite graph can be used in the medical field in the detection of lung cancer, throat cancer etc.
Used in search advertising and e-commerce for similarity ranking.
Predict movie preferences of a person.
Stable marriage6 and other matching problems.
Free Resources