-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBipertile_BFS.cpp
62 lines (56 loc) · 1.44 KB
/
Bipertile_BFS.cpp
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
//
// Created by rajat joshi on 18-06-2022.
//
#include<bits/stdc++.h>
using namespace std;
int n,e,i,j;
vector<vector<int> > graph;
vector<int> color;
bool vis[100011];
bool isBipartite()
{
color[0] = 1; // Mark colour as 1 for first vertex.
queue <int> q;
q.push(0);
while (!q.empty())
{
int temp = q.front();
q.pop();
for (i=0;i<n;i++)
{
if (graph[temp][i] && color[i] == -1) //if there is an edge, and colour is not assigned
{
color[i] = 1 - color[temp];
q.push(i);
}
else if (graph[temp][i] && color[i] == color[temp]) // if there is an edge and both vertices have same colours
return 0; // graph is not bipartite
}
}
return 1;
}
int main()
{
int x,y;
cout<<"Enter number of vertices and edges respectively:";
cin>>n>>e;
cout<<"\n";
graph.resize(n);
color.resize(n,-1);
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
graph[i].resize(n);
for(i=0;i<e;i++)
{
cout<<"\nEnter edge vertices of edge "<<i+1<<" :";
cin>>x>>y;
x--; y--;
graph[x][y]=1;
graph[y][x]=1;
}
if(isBipartite())
cout<<"Yes, The given graph is Bipartite.\n";
else
cout<<"No, The given graoh is not Bipartite.\n";
return 0;
}