This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite_Graph.cpp
More file actions
58 lines (58 loc) · 1.66 KB
/
Bipartite_Graph.cpp
File metadata and controls
58 lines (58 loc) · 1.66 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
class BipartiteGraph {
private:
int vertices;
vector<vector<int>> adjacencyList;
public:
BipartiteGraph(int v) : vertices(v), adjacencyList(v) {}
void addEdge(int u, int v) {
adjacencyList[u].push_back(v);
adjacencyList[v].push_back(u);
}
bool isBipartite() {
vector<int> color(vertices, -1);
queue<int> q;
for (int i = 0; i < vertices; ++i) {
if (color[i] == -1) {
color[i] = 0;
q.push(i);
while (!q.empty()) {
int current = q.front();
q.pop();
for (int neighbor : adjacencyList[current]) {
if (color[neighbor] == -1) {
color[neighbor] = 1 - color[current];
q.push(neighbor);
} else if (color[neighbor] == color[current]) {
return false;
}
}
}
}
}
return true;
}
};
int main() {
int vertices, edges;
cout << "Enter the number of vertices: ";
cin >> vertices;
cout << "Enter the number of edges: ";
cin >> edges;
BipartiteGraph graph(vertices);
cout << "Enter the edges (format: u v):" << endl;
for (int i = 0; i < edges; ++i) {
int u, v;
cin >> u >> v;
graph.addEdge(u, v);
}
if (graph.isBipartite()) {
cout << "The graph is Bipartite." << endl;
} else {
cout << "The graph is not Bipartite." << endl;
}
return 0;
}