-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIs_Graph_Bipartite.cpp
More file actions
72 lines (60 loc) · 1.81 KB
/
Is_Graph_Bipartite.cpp
File metadata and controls
72 lines (60 loc) · 1.81 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
//
// Created by Aditya on 13-12-2024.
//
#include "Is_Graph_Bipartite.h"
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
//using DFS
bool checkBipartiteDFS(vector<vector<int>>& graph, int curr, vector<int>& color, int currColor) {
color[curr] = currColor; // Color the current node
// Visit adjacent nodes
for (int &v : graph[curr]) {
if (color[v] == color[curr]) {
return false; // Adjacent nodes cannot have the same color
}
if (color[v] == -1) { // Not visited
int colorOfV = 1 - currColor; // Alternate color
if (!checkBipartiteDFS(graph, v, color, colorOfV)) {
return false;
}
}
}
return true;
}
bool isBipartite(vector<vector<int>>& graph) {
int V = graph.size(); // Get the number of nodes
vector<int> color(V, -1); // Initialize all nodes as uncolored
// Check all components of the graph
for (int i = 0; i < V; i++) {
if (color[i] == -1) { // If not visited
if (!checkBipartiteDFS(graph, i, color, 1)) {
return false;
}
}
}
return true;
}
};
int main() {
Solution solution;
// Example 1: Bipartite graph
vector<vector<int>> graph1 = {
{1, 3},
{0, 2},
{1, 3},
{0, 2}
};
// Example 2: Non-bipartite graph
vector<vector<int>> graph2 = {
{1, 2, 3},
{0, 2},
{0, 1, 3},
{0, 2}
};
cout << "Graph 1: " << (solution.isBipartite(graph1) ? "Bipartite" : "Not Bipartite") << endl;
cout << "Graph 2: " << (solution.isBipartite(graph2) ? "Bipartite" : "Not Bipartite") << endl;
return 0;
}