原题链接在这里:https://leetcode.com/problems/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].

题解:

Try to color the nodes with 2 different colors. Two adjcent nodes can't have same colors.

Use BFS to traverse nodes. At the beginning, put node 0 in the queue, color it as 1.

While queue is not empty, poll the cur node. For neighbors, if nei is not traversed before, color nei with -colors[cur].

If nei is traversed and its color is the same as cur, then 2 adjcent nodes have same color, return false.

For questions like this, traversed nodes need to be put into multiple categories, use array of integers to mark their categories.

Note: graph may not be one component. Thus for each node that is still 0, still need BFS on it.

Time Complexity: O(V+E). V = graph.length. E is count of all the edges.

Space: O(V).

AC Java:

 class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int [] colors = new int[n];
for(int i = 0; i<n; i++){
if(colors[i] == 0){
LinkedList<Integer> que = new LinkedList<>();
colors[i] = 1;
que.add(i);
while(!que.isEmpty()){
int cur = que.poll();
for(int nei : graph[cur]){
if(colors[nei] == 0){
colors[nei] = -colors[cur];
que.add(nei);
}else if(colors[nei] == colors[cur]){
return false;
}
}
}
}
} return true;
}
}

Use DFS to iterate nodes.

Start with first node with color 0, dfs try to color it with desiredColor.

For its neighbors, if they are not colored before, try to color them with -desiredColor.

Otherwise, check if they have the color they should have.

If any of them not, then return false.

Time Complexity: O(V+E).

Space: O(V). stack space.

AC Java:

 class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
int [] colors = new int[n];
for(int i = 0; i<n; i++){
if(colors[i] == 0 && !dfs(i, 1, colors, graph)){
return false;
}
} return true;
} private boolean dfs(int cur, int desiredColor, int [] colors, int[][] graph){
if(colors[cur] == 0){
colors[cur] = desiredColor;
for(int nei : graph[cur]){
if(!dfs(nei, -desiredColor, colors, graph)){
return false;
}
} return true;
}else if(colors[cur] != desiredColor){
return false;
}else{
return true;
}
}
}

类似Possible Bipartition.

LeetCode 785. Is Graph Bipartite?的更多相关文章

  1. [leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图

    Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0, ...

  2. [LeetCode] 785. Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  3. [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  4. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  5. 785. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  6. 785. Is Graph Bipartite?从两个集合中取点构图

    [抄题]: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is  ...

  7. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  8. [LeetCode] Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  9. LeetCode - Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

随机推荐

  1. CSP(noip)中的简单对拍写法

    以a+b为例 这是随机数据 #include<iostream> #include<cstdio> #include<ctime> using namespace ...

  2. yzoj 2372 小B的数字 题解

    题意 判断是否存在一个序列 $ b_i $ 使得 $ \prod_{i = 1}^{n} b_i  | b_i^{a_i}$ 恒成立,其中 $ b_i $ 中的每个数都是2的正整数次幂. 样例输入 3 ...

  3. C语言环境搭建

    UNIX/Linux 上的安装 如果您使用的是 Linux 或 UNIX,请在命令行使用下面的命令来检查您的系统上是否安装了 GCC: $ gcc -v 如果您的计算机上已经安装了 GNU 编译器,则 ...

  4. 解决使用RabbitTemplate操作RabbitMQ,发生The channelMax limit is reached. Try later.问题

    使用RabbitTemplate操纵RabbitMQ,每个RabbitTemplate等于一个connection,每个connection最多支持2048个channel,当hannel达到2048 ...

  5. win7安装镜像注入USB3.0,NVMe驱动

    现在的新款主板和笔记本因为原生自带了USB3.0和NVMe,在安装WIN7的时候会出现进入安装界面后不识别USB设备且在硬盘列表中无法读取M.2类型的固态硬盘信息.导致这个现象的原因就是在WIN7安装 ...

  6. Python进阶----UDP协议使用socket通信,socketserver模块实现并发

    Python进阶----UDP协议使用socket通信,socketserver模块实现并发 一丶基于UDP协议的socket 实现UDP协议传输数据 代码如下:

  7. permission 权限清单

    <uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permiss ...

  8. 图解HTTP(二)

    第四章 返回结果的HTTP状体码 1.状态码告知从服务器返回的结果   类别 原因短语 1XX Infomational信息性状态码 接收的请求正在处理中 2XX Success成功状态码 请求正常处 ...

  9. github上传本地项目代码

    进入github首页,点击新项目new repository,如下图所示: 然后进入如下页面,填写信息: 最后点击Create repository,生成如下页面: 红色圈圈画的步骤,先点击Clone ...

  10. Oracle SQL函数-NLSSORT

    Syntax 用途: NLSSORT返回字符值char的排序规则键和显式或隐式指定的排序规则.排序规则键是一个用于根据指定的排序规则对char进行排序的字节字符串.排序规则键的属性是:按二进制比较由给 ...