Write a program to check Bipartite Graph
Example 1:
Input : n=6, edges={{1,2},{1,3},{2,4},{3,5},{5,6}}
Output: Graph is Bipartite
Example 2:
Input : n=6, edges={{1,2},{1,3},{2,4},{3,5},{5,6},{1,5}
Output: Graph is not Bipartite
Approach:
Java
import java.util.ArrayList;import java.util.LinkedList;import java.util.Queue;public class BipartiteGraphCheck {// Adjacency Listsstatic ArrayList<ArrayList<Integer>> adj =new ArrayList<>();public static void main(String[] args) {int n = 6;for (int i = 0; i <=n; i++)adj.add(new ArrayList<>());addEdge(adj, 1, 2);addEdge(adj, 1, 3);addEdge(adj, 2, 4);addEdge(adj, 3, 5);addEdge(adj, 5, 6);if (isBipartite(n))System.out.println("Graph is Bipartite");elseSystem.out.println("Graph is not Bipartite");}// Method to check graph is bipartiteprivate static boolean isBipartite(int n) {// vector to store the distance// of all nodes initialize with INT_MAX(infinite)int part[] = new int[n+1];for (int i = 0; i <part.length; i++) {part[i] = -1;}// variable hold the bipartiteness// of the graphboolean isBipartite = true;// queue to hold the incomimg// nodesQueue<Integer> q = new LinkedList<Integer>();// iterate for all nodesfor (int i = 1; i <= n; i++) {// if node is not partitionedif (part[i] == -1) {// push into the queueq.add(i);// assine side as 0part[i] = 0;// iterate till the queue is not emptywhile (!q.isEmpty()) {int curr = q.poll();// iterate for adjacency list// of the current nodefor (int child : adj.get(curr)) {// if not partioned then// partitioned opposide side// of the current elementif (part[child] == -1) {part[child] = part[curr] ^ 1;q.add(child);}// if already partitione then// if both are are same side then bipartite// will we false else bipartite as true// so update this as bipartite&=part[child]!=part[curr]elseisBipartite &= part[child] != part[curr];}}}}return isBipartite;}private static void addEdge(ArrayList<ArrayList<Integer>> g,int u, int v) {// undirected graphg.get(u).add(v);g.get(v).add(u);}}
C++
#include <bits/stdc++.h>using namespace std;// adjacency listvector<int> arr[1000001];//function to add the edges into//the graphvoid addEdge(int u,int v){//undirected grapharr[u].push_back(v);arr[v].push_back(u);}//function to check the graph//is bipartite or notbool isBipartite(int n){//to hold the which side the//node comesvector<int> part(n+1,-1);//variable hold the bipartiteness//of the graphbool is_bipartite=true;//queue to hold the incomimg//nodesqueue<int> q;//iterate for all nodesfor(int i=1;i<=n;i++){//if node is not partitionedif(part[i]==-1){//push into the queueq.push(i);//assine side as 0part[i]=0;//iterate till the queue is not emptywhile(!q.empty()){int curr=q.front();q.pop();//iterate for adjacency list//of the current nodefor(int child:arr[curr]){//if not partioned then//partitioned opposide side//of the current elementif(part[child]==-1){part[child]=part[curr]^1;q.push(child);}//if already partitione then//if both are are same side then bipartite//will we false else bipartite as true//so update this as bipartite&=part[child]!=part[curr]elseis_bipartite&=part[child]!=part[curr];}}}}return is_bipartite;}int main(){int n=6;addEdge(1,2);addEdge(1,3);addEdge(2,4);addEdge(3,5);addEdge(5,6);if(isBipartite(n))cout<<"Graph is Bipartite\n";elsecout<<"Graph is not Bipartite\n";return 0;}
No comments:
Post a Comment