[LeetCode 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 0and 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].

分析

这道题需要利用广搜的办法来解决。为了保证将所有的Node分成两个集合,并且每条边的两个node都在两个集合中,可以首先确定一个node例如0在part1中,那么0的所有边的另外一端必然在part2中。我们可以使用队列进行BFS。当遇到没有边的node时,可以随便放进一个集合,也可以不放。也要考虑一种情况就是一个graph可能会有两个独立的图。如下所示

0----1 4----5
|    | |    |
|    | |    |
3----2 6----7

所以我们只把0塞进去做广搜没法遍历所有的node,那么需要维护一个没有访问过的node的set,每次队列为空后,就从set中拿出一个node塞进queue进去搜索。

Code

class Solution {
public:
    bool isBipartite(vector>& graph) {
        set p1;
        set p2;
        int len = graph.size();
        if (len == 0)
            return false;
        
        queue> q;
        set v;
        for (int i = 0; i < len; i ++)
        {
            v.insert(i);
        }
        
        while (v.size() != 0)
        {
            q.push(make_pair(*v.begin(), 0));
            v.erase(v.begin());
            while (!q.empty())
            {
                int index = q.front().first;
                int part = q.front().second;
                q.pop();
                v.erase(index);
                if (part == 0)
                {
                    if (p2.find(index) != p2.end())
                        return false;
                    if (p1.find(index) != p1.end())
                        continue;
                    p1.insert(index);
                }
                else
                {
                    if (p1.find(index) != p1.end())
                        return false;
                    if (p2.find(index) != p2.end())
                        continue;
                    p2.insert(index);
                }
                for (int i = 0; i < graph[index].size(); i ++)
                {
                    q.push(make_pair(graph[index][i], 1-part));
                }
            }
        }
        return true;
    }
};

运行效率

Runtime: 32 ms, faster than 31.39% of C++ online submissions for Is Graph Bipartite?.

Memory Usage: 13.1 MB, less than 7.03% of C++ online submissions for Is Graph Bipartite?.

你可能感兴趣的:(LeetCode)