
- 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
Check if a given graph is Bipartite using DFS using C++
A bipartite graph is a graph in which if the graph coloring is possible using two colors only 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 DFS.
Algorithm
Begin An array color[] is used to stores 0 or 1 for every node which denotes opposite colors. Call function DFS from any node. If the node w has not been visited previously, then assign ! color[v] to color[w] and call DFS again to visit nodes connected to w. If at any instance, color[u] is equal to !color[v], then the node is bipartite. Modify the DFS function End
Example
#include<iostream> #include <bits/stdc++.h> using namespace std; void addEd(vector<int> adj[], int w, int v) //adding edge to the graph { adj[w].push_back(v); //add v to w’s list adj[v].push_back(w); //add w to v’s list } bool Bipartite(vector<int> adj[], int v, vector<bool>& visited, vector<int>& color) { for (int w : adj[v]) { // if vertex w is not explored before if (visited[w] == false) { // mark present vertex as visited visited[w] = true; color[w] = !color[v]; //mark color opposite to its parents if (!Bipartite(adj, w, visited, color)) return false; } // if two adjacent are colored with same color then the graph is not bipartite else if (color[w] == color[v]) return false; } return true; } int main() { int M = 6; vector<int> adj[M + 1]; // to keep a check on whether a node is discovered or not vector<bool> visited(M + 1); vector<int> color(M + 1); //to color the vertices of the graph with 2 color addEd(adj, 3, 2); addEd(adj, 1, 4 ); addEd(adj, 2, 1); addEd(adj, 5, 3); addEd(adj, 6, 2); addEd(adj, 3, 1); visited[1] = true; color[1] = 0; if (Bipartite(adj, 1, visited, color)) { cout << "Graph is Bipartite"; } else { cout << "Graph is not Bipartite"; } return 0; }
Output
Graph is not Bipartite
- Related Articles
- Check if a given graph is Bipartite using DFS in C++ program
- C++ Program to Check whether Graph is a Bipartite using DFS
- Golang program to check a graph is bipartite using DFS
- C++ Program to Check whether Graph is a Bipartite using BFS
- Check if a graph is strongly connected - Set 1 (Kosaraju using DFS) in C++
- C++ Program to Check if a Directed Graph is a Tree or Not Using DFS
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- C++ Program to Check whether Graph is a Bipartite using 2 Color Algorithm
- C++ Program to Check the Connectivity of Directed Graph Using DFS
- C++ Program to Check the Connectivity of Undirected Graph Using DFS
- Java Program to Check Whether Undirected Graph is Connected Using DFS
- Program to check whether given graph is bipartite or not in Python
- How to find if a graph is Bipartite?
- Check if a given directed graph is strongly connected in C++
- Check if a given tree graph is linear or not in C++

Advertisements