
- 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 Check whether Graph is a Bipartite using 2 Color Algorithm
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. This is a C++ program to Check whether a graph bipartite or not using 2 color algorithm.
Functions and pseudocodes
Begin 1. Develop function isSafe() to check if the current color assignment is safe for vertex v, i.e. checks whether the edge exists or not. If it exists, then next check whether the color to be filled in the new vertex is already used by its adjacent vertices. 2. function graphColoringtil(bool g[V][V], int k, int col[], int v) : g[V][V] = It is a 2D array where V is the number of vertices in graph k = maximum number of colors that can be used. col[] = an color array that should have numbers from 1 to m. if v == V return true For c = 1 to k if (isSafe(v, g, col, c)) col[v] = c if (graphColoringtil (g, k, col, v+1) == true) return true col[v] = 0 return false 3.function graphColoring(bool g[V][V], int k): for i = 0 to V-1 color[i] = 0 if (graphColoringtil(g, k, color, 0) == false) return false return true
Example
#include <iostream> #include <cstdio> #define V 4 using namespace std; bool isSafe (int v, bool g[V][V], int col[], int C) //to solve m coloring //problem { for (int i = 0; i < V; i++) if (g[v][i] && C == col[i]) return false; return true; } bool graphColoringtil(bool g[V][V], int k, int col[], int v) { if (v == V) //If all vertices are assigned a color then return true; for (int c = 1; c <= k; c++) //Consider this vertex v and try different colors { if (isSafe(v, g, col, c)) //Check if assignment of color c to v is fine { col[v] = c; if (graphColoringtil (g, k, col, v+1) == true) //recur to assign colors to rest of the vertices return true; col[v] = 0; //If assigning color c doesn't lead to a solution then remove it } } return false; }
Output
The graph is Bipartite
- Related Articles
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Check whether Graph is a Bipartite using DFS
- Golang program to check a graph is bipartite using DFS
- Check if a given graph is Bipartite using DFS in C++ program
- Program to check whether given graph is bipartite or not in Python
- Check if a given graph is Bipartite using DFS using C++
- C++ Program to Check Whether Graph is DAG
- Java Program to Check Whether Undirected Graph is Connected Using DFS
- C++ Program to Check Whether a Graph is Strongly Connected or Not
- C++ Program to Perform Graph Coloring on Bipartite Graphs
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Cycle
- C++ Program to Check Whether a Directed Graph Contains a Eulerian Path
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
- C++ Program to Check Whether an Undirected Graph Contains a Eulerian Path
- How to find if a graph is Bipartite?

Advertisements