785. Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite.
Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.
The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn’t contain any element twice.
Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
| |
| |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \ |
| \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.
Note:
- graph will have length in range [1, 100].
- graph[i] will contain integers in range [0, graph.length - 1].
- graph[i] will not contain i or duplicate values.
- The graph is undirected: if any element j is in graph[i], then i will be in graph[j].
方法1: bfs
grandyang: http://www.cnblogs.com/grandyang/p/8519566.html
思路:
Time complexity: O(E + V)
Space complexity: O(E)
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
if (graph.empty()) true;
vector<int> colors(graph.size());
for (int i = 0; i < graph.size(); i++) {
if (colors[i]) continue;
colors[i] = 1;
int color = 1;
queue<int> q;
q.push(i);
while (!q.empty()) {
int top = q.front();
q.pop();
for (int j: graph[top]) {
if (!colors[j]) {
colors[j] = -colors[top];
q.push(j);
}
else {
if (colors[j] != -colors[top]) return false;
}
}
}
}
return true;
}
};
方法2: dfs
Time complexity: O(E + V)
Space complexity: O(E)
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
unordered_map<int, int> hash;
for (int i = 0; i < n; i++) {
if (hash[i] == 0 && !isValid(i, graph, hash, 1)) return false;
}
return true;
}
bool isValid(int i, vector<vector<int>> & graph, unordered_map<int, int> & hash, int color) {
if (hash[i] != 0) {
return hash[i] == color;
}
hash[i] = color;
for (int j: graph[i]) {
if (hash[j] == hash[i]) return false;
else if (hash[j] == 0) {
if (!isValid(j, graph, hash, -color)) return false;
}
}
return true;
}
};
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
unordered_map<int, int> hash;
for (int i = 0; i < n; i++) {
if (hash[i] == 0) {
hash[i] = 1;
if (!isValid(i, graph, hash, 1)) return false;
}
}
return true;
}
bool isValid(int i, vector<vector<int>> & graph, unordered_map<int, int> & hash, int color) {
for (int j: graph[i]) {
if (hash[j] == hash[i]) return false;
else if (hash[j] == 0) {
hash[j] = -color;
if (!isValid(j, graph, hash, -color)) return false;
}
}
return true;
}
};
方法3:union find
思路:
首先查每一个节点是否和自己的临界节点一样,一样就返回false。如果没有一样的,就把所有临界点union起来。
易错点:
- 查空:有的节点是没有临界的
Complexity
Time complexity:O(E+V)
Space complexity: O(E).
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n = graph.size();
vector<int> root(n, 0);
for (int i = 0; i < n; i++) root[i] = i;
for (int i = 0; i < n; i++) {
if (graph[i].empty()) continue;
int par1 = findHelper(root, i);
int par0 = findHelper(root, graph[i][0]);
for (int k = 1; k < graph[i].size(); k++) {
int par2 = findHelper(root, graph[i][k]);
if (par1 == par2) return false;
root[par2] = par0;
}
}
return true;
}
int findHelper2(vector<int>& root, int i) {
return root[i] == i ? i : findHelper(root, root[i]);
}
int findHelper(vector<int>& root, int i) {
while (i != root[i]) {
i = root[i];
}
return i;
}
};