-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartiteCheckUsingDFS.cpp
More file actions
37 lines (31 loc) · 1 KB
/
Copy pathBipartiteCheckUsingDFS.cpp
File metadata and controls
37 lines (31 loc) · 1 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
// Q163 https://leetcode.com/problems/is-graph-bipartite/
// other way of question: can the graph be coloured only using 2 colors with no adjacent nodes having same colors
// Time: O(N+E)
// Space: Graph + rec stack + color array
// O(N+E) + O(N) + O(N)
class Solution {
bool bipartiteDFS(int node,vector<int> &col, vector<vector<int>>& g){
if(col[node]==-1) col[node]=1;// making the 1st call the node is uncolored
for(auto it: g[node]){
if(col[it]==-1){
col[it]=1-col[node];
if(!bipartiteDFS(it,col,g)) return false;
}
else if(col[node]==col[it]) return false;
}
return true;
}
public:
bool isBipartite(vector<vector<int>>& g) {
int n=g.size();
vector<int> col(n,-1);
for(int i=0;i<n;i++){
if(col[i]==-1){
if(!bipartiteDFS(i,col,g)){
return false;
}
}
}
return true;
}
};