LeetCode 785. Is Graph Bipartite?
原题链接在这里: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:
graphwill have length in range[1, 100].graph[i]will contain integers in range[0, graph.length - 1].graph[i]will not containior duplicate values.- The graph is undirected: if any element
jis ingraph[i], theniwill be ingraph[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;
}
}
}
LeetCode 785. 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? 是二分图么?
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 ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 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】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
- [LeetCode] 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 ...
随机推荐
- 函数this指向哪个对象?
函数的this指向是根据函数调用时所处的执行环境来确定的. this指向对象的情况有四种: 1.使用new关键字时:this会绑定构造函数所创建的对象. function Foo(){ this.a ...
- windows桌面远程连接突然不能双向复制文件
远程桌面连接windows 2008,突然无法在本地和服务器之间互相复制文件.根据微软的说明,由rdpclip.exe进程来控制,打开远程服务器的任务管理器,看到rdpclip.exe进程存在,即可进 ...
- selenium中的元素操作之下拉列表操作(四)
下拉列表操作中分为两种:select.非select 1.非select的下拉框操作 非select下拉列表操作与网页元素操作一致,找到元素,定位元素,设置等待,点击元素等等 接下来操作百度的设置按钮 ...
- 设计模式--Bulider模式
起因:最近在做统计计算,创建的实体中属性比较多,都是一些数值,一开始是通过get.set方法进行赋值,占用了很多业务代码方法的长度,可读性不太好,后来改用了添加构造器的方式,稍显精简了一点,但是每次赋 ...
- pandas-01 Series()的几种创建方法
pandas-01 Series()的几种创建方法 pandas.Series()的几种创建方法. import numpy as np import pandas as pd # 使用一个列表生成一 ...
- react学习记录(一)
一.React是什么 声明式写法(强调结果,命令式编程强调过程) 组件化 一次学习,随处编写(多种应用场景,web程序,原生手机应用,系统应用,命令行工具) 二.为什么学习react 大公司加持-fa ...
- Jmeter学习笔记(十九)——后置处理器之正则表达式的使用
一.正则表达式提取器的作用 允许用户从服务器的响应中通过使用perl的正则表达式提取值.作为一个后置处理器,该元素会作用在指定范围的取样器,应用正则表达式,提取所需要的值,生成模板字符串,并将结果存储 ...
- mysql修改字符集问题
mysql字符集问题: 本文主要解决mysql7以下问题:mysql7在默认安装后,关于数据库,表默认保存字符格式为latin1: 可以通过命令:查询当前mysql的编码设置: show variab ...
- 如何去除有道云笔记广告(windows)
一.适用于6.0之前版本 你只需要:找到有道云笔记的安装路径,*\Youdao\YoudaoNote\theme\build.xml 用笔记本打开这个文件,找到'左下角广告'这几个字,把下面的代码删掉 ...
- 【兼容调试】cffi library '_openssl' has no function, constant or global variable named 'Cryptography_HAS
重装cryptography就好了. conda uninstall cryptography conda install cryptography https://github.com/pyca/c ...