-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckBipartiteBFS.cpp
More file actions
65 lines (63 loc) · 1.04 KB
/
CheckBipartiteBFS.cpp
File metadata and controls
65 lines (63 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using namespace std;
class Graph
{
int v;
vector<int>* adj;
public:
Graph(int n)
{
v=n;
adj=new vector<int>[v];
}
void addEdge(int a,int b)
{
adj[a].push_back(b);
adj[b].push_back(a);
}
bool checkBipartite();
};
bool Graph::checkBipartite()
{
queue<int> que;
que.push(0);
cout<<"pushed : 0\n";
int color[v];
for(int i=0;i<v;i++)
color[i]=-1;
color[0]=1;
while(!que.empty())
{
int x=que.front();
que.pop();
for(int i=0;i<adj[x].size();i++)
{
if(color[adj[x][i]]==color[x])
return false;
if(color[adj[x][i]]!=-1)
continue;
que.push(adj[x][i]);
cout<<"pushed : "<<adj[x][i]<<"\n";
color[adj[x][i]]=(color[x]+1)%2;
}
}
return true;
}
int main()
{
cout<<"Enter number of vertices and the number of edges: \n";
int n,m,a,b;
cin>>n>>m;
Graph grp(n);
cout<<"Enter the edge co-ordinates(in form of 'x' 'y' pair) :\n";
while(m--)
{
cin>>a>>b;
grp.addEdge(a,b);
}
if(grp.checkBipartite())
cout<<"Bipartite\n";
else
cout<<"Not Bipartite\n";
return 0;
}