-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBipartite Graph check.cpp
227 lines (197 loc) · 6.19 KB
/
Bipartite Graph check.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// C++ program to check if a connected
// graph is bipartite or not using BFS and dfs
class Solution {
public:
// DFS code
vector<int>vis,col;
bool dfs(int v, int c, vector<vector<int>>& graph){
vis[v]=1;
col[v]=c;
for(int child:graph[v]){
if(vis[child]==0){
// here c^1 is for flipping 1 by 0 or 0 by 1, that is flip the current color
if(dfs(child,c^1,graph)==false)
return false;
}
else{
if(col[v]==col[child])
return false;
}
}
return true;
}
bool isBipartite(vector<vector<int>>& graph) {
int n=graph.size();
vis.resize(n);
col.resize(n);
for(int i=0;i<n;++i){
if(vis[i]==0 && dfs(i,0,graph)==false){
return false;
}
}
return true;
}
//dfs code end
// bool colorNode(int src,int parent,vector<vector<int>>& graph,vector<bool> &vis,vector<int> &colors){
// if(!vis[src]){
// vis[src]=true;
// if(colors[parent]== 1){
// colors[src]= 2;
// }else if(colors[parent]== 2){
// colors[src]= 1;
// }
// }
// if(colors[parent]==colors[src]){
// return false;
// }
// return true;
// }
// bool bipartite(vector<vector<int> >& graph,vector<int>& colors,vector<bool>& vis,int v){
// for(int i=0;i<graph[v].size();i++){
// if(!vis[graph[v][i]]){
// vis[graph[v][i]]=true;
// colors[graph[v][i]]= !colors[v];
// if(!bipartite(graph,colors,vis,graph[v][i])){
// return false;
// }
// }else if(colors[graph[v][i]]==colors[v]){
// return false;
// }
// }
// return true;
// }
// bool isBipartite(vector<vector<int>>& graph) {
// vector<bool> vis(graph.size(),0);
//Correct code with bfs
// int n = graph.size();
// vector<int> colors(n, 0);
// queue<int> q;
// for (int i = 0; i < n; i++) {
// if (colors[i]) continue;
// colors[i] = 1;
// q.push(i);
// while (!q.empty()) {
// int temp = q.front();
// for (auto neighbor : graph[temp]) {
// // Color neighbor with opposite color
// if (!colors[neighbor]){
// colors[neighbor] = -colors[temp];
// q.push(neighbor);
// }
// // If the neighbor has the same color - can't bipartite.
// else if (colors[neighbor] == colors[temp])
// return false;
// }
// q.pop();
// }
// }
// return true;
//bfs code end
// vector<int> colors(graph.size(),0);
// queue<int> q;
// for(int i=0;i<graph.size();i++){
// if(colors[i])continue;
// colors[i]=1;
// q.push(i);
// while(!q.empty()){
// int temp = q.front();
// for(int it : graph[temp]){
// if(!colors[it]){
// q.push(it);
// colors[it]=-colors[temp];
// }else if(colors[it]==colors[temp]){
// return false;
// }
// }
// q.pop();
// }
// }
// return true;
//Wrong code 79/81 test cases passed
// vis[0]=1;
// colors.push_back(0);
// if (bipartite(graph,colors,vis,0)) {
// return true;
// }
// else {
// return false;
// }
// for(int i=0;i<graph.size();i++){
// if(!vis[i]){
// for(int j=0;j<graph[i].size();j++){
// if(vis[graph[i][j]]){
// if(colors[graph[i][j]]== 1){
// colors[i]= 2;
// }else if(colors[graph[i][j]]== 2){
// colors[i]= 1;
// }
// }
// }
// if(colors[i]==0){
// colors[i]=1;
// }
// vis[i]=true;
// for(int j=0;j<graph[i].size();j++){
// if(colorNode(graph[i][j],i,graph,vis,colors)==false){
// return false;
// }
// }
// }else{
// for(int j=0;j<graph[i].size();j++){
// if(colorNode(graph[i][j],i,graph,vis,colors)==false){
// return false;
// }
// }
// }
// }
// return true;
// }
};
//#include<bits/stdc++.h>
//using namespace std;
//// 1 for red
//// 2 for blue
//bool color(int src,vector<int> &adj,vector<bool> &vis,vector<int> &colors,int parent){
// vis[src]=true;
// if(parent==1){
// colors[src]=2;
// }else{
// colors[src]=1;
// }
// vector<int> :: iterator it;
// for(it=adj[src].begin();it!=adj[src].end();it++){
// if(colors[*it]!=0){
// if(colors[*it]!=parent){
// return false;
// }
// }else{
// color[*it]=parent;
// if(!color(it,adj,vis,colors,colors[src])){
// return false;
// }
// }
// }
// return true;
//}
//int main(){
// int n,m;
// cin>>n>>m;
// vector<bool> vis(n,0);
// vector<int> adj[n];
// vector<int> colors(n,0);
// for(int i=0;i<m;i++){
// int u,v;
// cin>>u>>v;
// adj[u].push_back(v);
// adj[v].push_back(u);
// }
// bool ans =true;
// for(int i=0;i<n;i++){
// if(!vis[i]){
// if(!color(i,adj,vis,colors,2)){
// ans= false;
// }
// }
// }
// cout<<ans;
//}