Skip to content

Instantly share code, notes, and snippets.

@shawon100
Created January 26, 2017 09:17
Show Gist options
  • Select an option

  • Save shawon100/fe5a3bf7297b6047d11d1f56db2c5372 to your computer and use it in GitHub Desktop.

Select an option

Save shawon100/fe5a3bf7297b6047d11d1f56db2c5372 to your computer and use it in GitHub Desktop.
Bipartite Checking
#include <bits/stdc++.h>
using namespace std;
vector<int>edges[100];
queue<int>q;
vector<int>item;
int level[100],color[100],visited[100],tn;
int bipartite(int s)
{
int j,k,fr;
q.push(s);
color[s]=1;
memset(color,-1,sizeof color);
for(j=0;j<tn;j++)
{
visited[j]=0;
}
while(!q.empty())
{
fr=q.front();
for(k=0;k<edges[fr].size();k++)
{
if(visited[edges[fr][k]]==0 && color[edges[fr][k]]==-1)
{
q.push(edges[fr][k]);
visited[edges[fr][k]]=1;
color[edges[fr][k]]=1-color[fr];
}
}
for(k=0;k<edges[fr].size();k++)
{
if(color[edges[fr][k]]==color[fr])
{
return false;
}
}
q.pop();
}
return true;
}
int main()
{
int i,e,p,n,u,v,f,m;
cout<<"Enter Total Nodes And Edges="<<endl;
cin>>tn>>e;
for(i=1;i<=e;i++)
{
cin>>u>>v;
edges[u].push_back(v);
}
cout<<endl;
if(bipartite(0))
{
cout<<"Input Graph Is Bipartite"<<endl;
}
else
{
cout<<"Input Graph Is Not Bipartite"<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment