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.
分析:
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,
- If it hasn't been colored, use a color to color it. Then use the other color to color all its adjacent nodes (DFS).
- 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?的更多相关文章
- [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, ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- [LeetCode] Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- LeetCode - Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [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 ...
- 785. Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- 785. Is Graph Bipartite?从两个集合中取点构图
[抄题]: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is ...
- [LeetCode] 785. Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- LeetCode 785. Is Graph Bipartite?
原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...
随机推荐
- Gym 102346A Artwork dfs
Artwork Gym - 102346A 题意:给n*m的地图,入口是(0,0),出口是(n,m),其中有k个监视器,坐标是(xi,yi),监视半径是r,问一个人能不能不被监视到,从起点到终点. 如 ...
- windows下安装redis、celery,并启动测试
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现 搭建环境: Windows-x64 10 Celery 3.1.23 Celery-with-redis 3.0 Redi ...
- linux经常用的命令
常用 安装包 centos yum / Ubuntu Debian apt-get clear :清空终端 [cmd 下是cls] vi/vim 编辑器 详情 ...
- 全国行政区域代码(免费来拿) xls格式 可直接导入
全部区域代码 地址:https://pan.baidu.com/s/1Elz-zW_nLS8YR8GZAn3WRw 提取码:ze3s
- [线性代数] 线性子空间入門 Basic Vector Subspaces
导语:其他集数可在[线性代数]标籤文章找到.线性子空间是一个大课题,这里先提供一个简单的入门,承接先前关于矩阵代数的讨论,期待与你的交流. Overview: Subspace definition ...
- Java实现多线程生产者消费者模式的两种方法
生产者消费者模式:生产者和消费者在同一时间段内共用同一存储空间,生产者向空间里生产数据,而消费者取走数据.生产者生产一个,消费者消费一个,不断循环. 第一种实现方法,用BlockingQueue阻塞队 ...
- Web API 跨域
1. NuGet下载## microsoft.aspnet.webapi.cors 2 . Web API 路由中 config.EnableCors(new EnableCorsAttribute( ...
- java四种对象引用类型
java四种对象引用类型 对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序 ...
- Cesium官方教程6--相机
相机(Camera) 相机控制了场景的观察视角.有很多相机操控方法,比如旋转.缩放.平移以及飞行定位.Cesium默认支持使用鼠标和触摸事件控制相机.Cesium也提供了一套可编程的相机控制API.这 ...
- OpenTK学习笔记(2)-工作窗口的三种方法创建方法(控制台)
参考资料: 控制台下类的形式创建:http://www.cnblogs.com/podolski/p/7406628.html 总结: 一.控制台下类的形式创建 1.新建控制台应用 2.连网执行Nug ...