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.
分析:

Our goal is trying to use two colors to color the graph and see if there are any adjacent nodes having the same color.
Initialize a color[] array for each node. Here are three states for colors[] array:
0: Haven't been colored yet.
1: Blue.
-1: Red.
For each node,

  1. If it hasn't been colored, use a color to color it. Then use the other color to color all its adjacent nodes (DFS).
  2. If it has been colored, check if the current color is the same as the color that is going to be used to color it.

DFS

 class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
// 0 unvisited 1 red -1 blue
int[] colors = new int[n];
//This graph might be a disconnected graph. So check each unvisited node.
for (int i = ; i < n; i++) {
if (colors[i] == ) {
if (!validColor(graph, colors, , i)) {
return false;
}
}
}
return true;
} public boolean validColor(int[][] graph, int[] colors, int color, int node) {
colors[node] = color;
for (int next : graph[node]) {
if (colors[next] == && !validColor(graph, colors, -color, next)) {
return false;
} else if (colors[next] == colors[node]) {
return false;
}
}
return true;
}
}

BFS

 class Solution {
public boolean isBipartite(int[][] graph) {
// 0: unvisited; 1: black; 2: white
int[] status = new int[graph.length];
// for loop is used to handle unconnected components in the graph
for (int i = ; i < graph.length; i++) {
if (status[i] == ) {
status[i] = ;
if (!bfs(i, status, graph)) {
return false;
};
}
}
return true;
} private boolean bfs(int i, int[] status, int[][] graph) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
while (!queue.isEmpty()) {
int current = queue.poll();
for (int neighbor : graph[current]) {
if (status[neighbor] == ) {
status[neighbor] = (status[current] == ) ? : ;
queue.offer(neighbor);
} else {
if (status[neighbor] == status[current]) return false;
}
}
}
return true;
}
}

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? 解题报告(Python)

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

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

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

  4. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

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

  5. LeetCode - Is Graph Bipartite?

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

  6. [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 ...

  7. 785. Is Graph Bipartite?

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

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

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

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

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

  10. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

随机推荐

  1. 爬虫(十七):scrapy分布式原理

    一:scrapy工作流程 scrapy单机架构: 单主机爬虫架构: 分布式爬虫架构: 这里重要的就是我的队列通过什么维护?这里一般我们通过Redis为维护,Redis,非关系型数据库,Key-Valu ...

  2. 传统PC机I/O位址空间范围

    空间范围是0x000--0x3FF,有1024个I/O端口位址可供使用! 使用EISA或PCI等汇流排结构的现代PC机,有64KB的I/O位址空间可供使用.在普通Linux系统下透过查看/proc/i ...

  3. Python的十种常见算法

    十种排序算法 1. 常见算法分类 十种常见排序算法一般分为以下几种: (1)非线性时间比较类排序: ​ a. 交换类排序(快速排序.冒泡排序) ​ b. 插入类排序(简单插入排序.希尔排序) ​ c. ...

  4. Scrapy不同的item指定不同的Pipeline

    scrapy不同的item指定不同的Pipeline from items import AspiderItem, BspiderItem, CspiderItem class myspiderPip ...

  5. Oracle 给表添加备注

    给表字段添加备注 comment on column TableName.ColumnName is ‘备注名’; 给表添加备注comment on table TableName is '备注名';

  6. 关于long_query_time的设置,可不可以说是mysql的一个小小bug呢

    我们知道对对于MySQL的日志功能,我们可以完全自己控制到底写还是不写.一般来说,binlog我们一般会开启,而对于慢查询我们一般会在开发的时候调试和观察SQL语句的执行速度.但今天发现一个问题.在使 ...

  7. python何时用list,dict,set

    从读取的角度来讲: 看是用来随机读取(查询)还是连续读取. list数组集中存放,连续读取效率高(具体还没测试,理论上应该如此). dict散列表,使用hash计算存放的位置,随机读取效率高. 随机读 ...

  8. php实现excel单元格合并,字体加粗居中等操作

    使用的是phpexcel,基本用的原生语法,所见即所得,直接复制下面代码,即可: // 引用phpexcel类 $this->load->library('PHPExcel'); // 创 ...

  9. http://www.cnblogs.com/sealedbook/p/6194047.html

    http://www.cnblogs.com/sealedbook/p/6194047.html

  10. C# 怎么生成DLL文件(转)

    有两种方法:     但是一般这个使用     打开VS2008,依次点击:菜单->文件->新建项目->项目类型visual C#(这里假设为该项目所取的名字是DllBuild)-& ...