
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
How to find if a graph is Bipartite?
A graph is said to be a bipartite graph, when vertices of that graph can be divided into two independent sets such that every edge in the graph is either start from the first set and ended in the second set, or starts from the second set, connected to the first set, in other words, we can say that no edge can found in the same set.
Checking of a bipartite graph is possible by using the vertex coloring. When a vertex is in the same set, it has the same color, for another set, the color will change.
Input and Output
Input: The adjacency matrix. 0 1 1 1 0 0 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 0 1 1 0 1 0 1 0 1 0 0 1 1 1 0 Output: The graph is bipartite.
Algorithm
isBipartite(source)
Input − The source vertex.
Output: True when the graph is bipartite.
Begin define an empty queue qu, and a color list coloArray initially any node is not colored with any color color the source vertex as color red add source in the qu when qu is not empty, do remove item from the qu and take in u if there is any self-loop, then return false for all vertices v, which is connected with u, do if v has no color, then if colorArray[u] = red, then colorArray[v] := blue else if colorArray[u] = blue, then colorArray[v] := red add v into the queue if colorArray[v] = colorArray[u], then return false done done return true End
Example
#include<iostream> #include<string> #include<queue> #define NODE 6 using namespace std; /*int graph[NODE][NODE] = { {0, 1, 1, 1, 0, 0}, {1, 0, 0, 1, 1, 0}, {1, 0, 0, 1, 0, 1}, {1, 1, 1, 0, 1, 1}, {0, 1, 0, 1, 0, 1}, {0, 0, 1, 1, 1, 0} }; */ int graph[NODE][NODE] = { {0, 1, 0, 0, 0, 1}, {1, 0, 1, 0, 0, 0}, {0, 1, 0, 1, 0, 0}, {0, 0, 1, 0, 1, 0}, {0, 0, 0, 1, 0, 1}, {1, 0, 0, 0, 1, 0} }; bool isBipartite(int source) { queue<int> qu; string colorArray[NODE]; for(int i = 0; i< NODE; i++) colorArray[i] = "No Color"; //initially no color is set for all vertices colorArray[source] = "Red"; //assign red with the source vertex qu.push(source); //add source into the queue. while(!qu.empty()) { int u = qu.front(); qu.pop(); if(graph[u][u] == 1) //there is a self loop return false; for(int v = 0; v < NODE; v++) { if(graph[u][v] != 0 && colorArray[v] == "No Color") { if(colorArray[u] == "Red") //assign adjacent list with alternate color colorArray[v] = "Blue"; else if(colorArray[u] == "Blue") colorArray[v] = "Red"; qu.push(v); //new adjacent node added to queue } else if(graph[u][v] != 0 && colorArray[v] == colorArray[u]) { return false; //when u and adjacent are of same color. } } } return true; } int main() { bool check; check = isBipartite(0); if(check) cout << "The graph is bipartite." << endl; else cout << "The graph is not bipartite." << endl; }
Output
The graph is bipartite.
- Related Articles
- Check if a given graph is Bipartite using DFS using C++
- Check if a given graph is Bipartite using DFS in C++ program
- Golang program to check a graph is bipartite using DFS
- C++ Program to Check whether Graph is a Bipartite using BFS
- C++ Program to Check whether Graph is a Bipartite using DFS
- C++ Program to Check whether Graph is a Bipartite using 2 Color Algorithm
- C++ Program to Perform Graph Coloring on Bipartite Graphs
- Program to check whether given graph is bipartite or not in Python
- Maximum number of edges in Bipartite graph in C++
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph in C++
- Bipartite Graphs
- Program to find out if the graph is traversable by everybody in Python
- Maximum Bipartite Matching
- Check if a given graph is tree or not
- In the concept of distance ~ time graph,How we will draw a graph if the speed is not constant ?

Advertisements