
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Perform Graph Coloring on Bipartite Graphs
A bipartite graph is a graph in which if the graph coloring is possible using two colors i.e.; vertices in a set are colored with the same color. In this program we take a bipartite graph as input and outputs colors of each vertex after coloring the vertices.
Algorithm
Begin BFS algorithm is used to traverse all the vertices. Take a vertex and colour it yellow. Colour all its neighbour vertices as blue. Colour the next level vertices as yellow and so, until all vertices are coloured. End.
Example Code
#include<bits/stdc++.h> using namespace std; int n, e, i, j; vector<vector<int> > g; vector<int> color; bool v[11101]; void c(int node,int n) { queue<int> q; if(v[node]) return; color[node]=n; v[node]=1; for(i=0;i<n;i++) { if(!v[g[node][i]]) { q.push(g[node][i]); } } while(!q.empty()) { c(q.front(),(n+1)%2); q.pop(); } return; } int main() { int a,b; cout<<"Enter number of vertices and edges respectively:"; cin>>n>>e; cout<<"'Y' is for Yellow Colour and 'B' is for Blue Colour."; cout<<"\n"; g.resize(n); color.resize(n); memset(v,0,sizeof(v)); for(i=0;i<e;i++) { cout<<"\nEnter edge vertices of edge "<<i+1<<" :"; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); } c(0,1); for(i=0;i<n;i++) { if(color[i]) cout<<i+1<<" "<<'Y'<<"\n"; else cout<<i+1<<" "<<'B'<<"\n"; } }
Output
Enter number of vertices and edges respectively:4 3 'Y' is for Yellow Colour and 'B' is for Blue Colour. Enter edge vertices of edge 1 :1 2 Enter edge vertices of edge 2 :3 2 Enter edge vertices of edge 3 :4 2 1 Y 2 B 3 B 4 B
- Related Articles
- C++ Program to Perform Edge Coloring on Complete Graph
- C++ Program to Perform Edge Coloring of a Graph
- C++ Program to Perform Edge Coloring to the Line Graph of an Input Graph
- C++ Program to Perform Greedy Coloring
- Bipartite Graphs
- Graph Coloring
- Coloring Graph
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Check whether Graph is a Bipartite using DFS
- The Graph Coloring
- C++ Program to Check whether Graph is a Bipartite using 2 Color Algorithm
- Golang program to check a graph is bipartite using DFS
- What are the applications of Bipartite graphs?
- Check if a given graph is Bipartite using DFS in C++ program
- Program to check whether given graph is bipartite or not in Python

Advertisements