原题链接在这里: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. python 之 面向对象(元类、__call__、单例模式)

    7.13 元类 元类:类的类就是元类,我们用class定义的类来产生我们自己的对象的,内置元类type是用来专门产生class定义的类 code=""" global x ...

  2. 第1课,python输出,输入,变量,运算

    课程内容: 为什么要学习python 如何学python 实践体验 图片来源网络分享 为什么要学python: 简单 (设计如此) 强大(因为开源,有库) 如何学习python: 变量 --> ...

  3. Jenkins+harbor+gitlab+k8s 部署maven项目

    一.概述 maven项目部署流程图如下: 环境介绍 操作系统 ip 角色 版本 ubuntu-16.04.4-server-amd64 192.168.10.122 Jenkins+harbor Je ...

  4. Java之数据类型讲解

    Java数据类型关系图 基本数据类型 从小到大的关系图: 图中从左向右的转换都是隐式转换,无需再代码中进行强制转换 : byte i = 12; System.out.println("by ...

  5. 2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐

    原文:2019 年在 Raspberry Pi 「树莓派」上运行的 10 个操作系统推荐 image Raspberry Pi** 是一款基于 ARM 的单板计算机,默认运行一款称为 Raspbian ...

  6. python学习之利用socketserver的文件传输

    使用socketserver进行多用户的文件传输 服务端 class FtpServer(socketserver.BaseRequestHandler): # 继承socketserver.Base ...

  7. selenium自学笔记---下拉框定位元素select

    下拉框1.先定位select 然后在定位option city = driver.find_element_by_id("selCities_0") city.find_eleme ...

  8. Softmax学习笔记

    softmax用于多分类过程中,它将多个神经元的输出,映射到(0,1)区间内,可以看成概率来理解,从而来进行多分类! 他把一些输入映射为0-1之间的实数,并且归一化保证和为1,因此多分类的概率之和也刚 ...

  9. php xml解析

    XML处理是开发过程中经常遇到的,PHP对其也有很丰富的支持,本文只是对其中某几种解析技术做简要说明,包括:Xml parser, SimpleXML, XMLReader, DOMDocument. ...

  10. EasyPOI导入导出Excel

    EasyPOI工具可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 导入maven依赖 <dependency> <group ...