-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbipartiteOrNot.cpp
More file actions
94 lines (87 loc) · 2.21 KB
/
Copy pathbipartiteOrNot.cpp
File metadata and controls
94 lines (87 loc) · 2.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
bool dfsUtil(int node, vector<int> adj[], vector<int> &visited)
{
if (visited[node] == -1)
visited[node] = 1;
// cout<<node<<" "<<color<<endl;
for (auto it : adj[node])
{
if (visited[it] == -1)
{
visited[it] = 1 - visited[node];
if (!dfsUtil(it, adj, visited))
return false;
}
else if (visited[it] == visited[node])
return false;
}
return true;
}
bool isBipartiteByDFS(int V, vector<int> adj[])
{
vector<int> visited(V, -1);
for (int i = 0; i < V; i++)
{
if (visited[i] == -1 && !(dfsUtil(i, adj, visited)))
return false;
}
return true;
}
bool isBipartiteByBFS(int V, vector<int> adj[])
{
// '0' unvisited
// '1' and '2' colours
vector<char> visited(V, '0');
for (int i = 0; i < V; i++)
{
if (visited[i] == '0')
{
queue<int> q;
q.push(i);
visited[i] = '1';
while (!q.empty())
{
int node = q.front();
q.pop();
for (auto it : adj[node])
{
if (visited[it] == '0')
{
if (visited[node] == '1')
visited[it] = '2';
else
visited[it] = '1';
q.push(it);
}
else if (visited[it] == visited[node])
return false;
}
}
}
}
return true;
}
int main()
{
int V, E;
cout << "Enter number of vertices and edges: " << endl;
cin >> V >> E;
vector<int> adj[V];
vector<vector<int>> edges(E, vector<int>(3));
cout << "please enter as: vertex vertex :\n";
for (int i = 0; i < E; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
bool isBipartite = isBipartiteByDFS(V, adj);
if (isBipartite)
cout << "This is Bipartite Graph" << endl;
else
cout << "This is not a Bipartite graph" << endl;
return 0;
}
// } Driver Code Ends