-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite_Graph_BFS.cpp
More file actions
70 lines (60 loc) · 1.67 KB
/
Copy pathBipartite_Graph_BFS.cpp
File metadata and controls
70 lines (60 loc) · 1.67 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
//
// Created by Aditya on 14-12-2024.
//
#include "Bipartite_Graph_BFS.h"
#include <iostream>
#include <vector>
#include <queue> // Required for std::queue
using namespace std;
class Solution {
public:
bool checkBipartiteBFS(vector<vector<int>>& adj, int curr, vector<int>& color, int currColor) {
queue<int> que;
que.push(curr);
color[curr] = currColor;
while (!que.empty()) {
int u = que.front();
que.pop();
for (int& v : adj[u]) {
if (color[v] == color[u]) {
return false;
} else if (color[v] == -1) { // not visited yet
color[v] = 1 - color[u];
que.push(v);
}
}
}
return true;
}
bool isBipartite(vector<vector<int>>& adj) {
int V = adj.size();
vector<int> color(V, -1); // all vertices initially uncolored
for (int i = 0; i < V; i++) {
if (color[i] == -1) {
if (!checkBipartiteBFS(adj, i, color, 1)) {
return false;
}
}
}
return true;
}
};
int main() {
Solution sol;
// Example graph: adjacency list representation
// Graph: 0 -- 1
// | |
// 3 -- 2
vector<vector<int>> graph = {
{1, 3}, // edges for vertex 0
{0, 2}, // edges for vertex 1
{1, 3}, // edges for vertex 2
{0, 2} // edges for vertex 3
};
if (sol.isBipartite(graph)) {
cout << "The graph is bipartite." << endl;
} else {
cout << "The graph is not bipartite." << endl;
}
return 0;
}