Is Graph Bipartite? (Medium)

update Mar 4, 2018 13:33

LeetCodearrow-up-right

Given a 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:

  1. graph will have length in range [1, 100].

  2. graph[i] will contain integers in range [0, graph.length - 1].

  3. graph[i] will not contain i or duplicate values.

Basic Idea:

  • 思路 1:(BFS + 2 sets)

    用两个set U and V 表示两个 subset,确保一个set中的node的neighbor都在另一个set中,当发现有两个相连的node在同一个set中时,返回false;

    • Java Code:

  • 思路 2:(DFS + 2 sets)

    和之前的BFS思路类似,也是用两个set表示不同部分,然后再dfs的过程中将相连node放入不同的set,同时检查当前node和其neighbor是否在同一个set中;时间复杂度都是 O(|E|),因为都遍历了所有的边;

    • Java Code:

    • Java Code:

      不用 set,而是使用一个vector来记录两种颜色,0 表示未见,1 2 分别表示两个set。

update 2018-06-27 22:50:08

Update C++ BFS 染色解法

update Feb 25 2019, 21:53

Update

在vmw的onsite中遇到了这道题的变种,但因为和上次做题的时间相隔了近一年,几乎想不起最优解的做法,于是在面试中给出了一个现场想出来的解,应该可以work,只是会比较别扭。所以说还是需要注意对从前做过的题目进行复习,尤其是Graph和Tree以及DP这种套路比较强的类型题目。

双色涂色法的思考: 虽然我们是想办法将图分成两部分,使得任意一部分内部node之间没有边相连,但事实上如果一个connected component自身和整个图但其他部分没有联系,我们是可以自由决定将这个component的两部分的摆放顺序的。所以在一开始的时候,对于没有被染色过的node,我们可以直接将其染为1。 接下来我们只需要对与该node相连的component之间交替染色,同时检查是否valid。所以,我们可以BFS或者DFS,都不重要,因为我们只需要每次从一个node出发,将其所在的connected component nodes交替染色。

Last updated