[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], [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.
设G=(V,E)是一个无向图。如顶点集V可分割为两个互不相交的子集V1,V2之并,并且图中每条边依附的两个顶点都分别属于这两个不同的子集
思路
1. based on Graph Bipartite attribute, we can fill two different color for each subset.
2. if not Graph Bipartite, at lease one node such that its color happens to be the same as its neighbor
3. coz we need to traversal each node, we can both use dfs and bfs
代码
class Solution {
public boolean isBipartite(int[][] graph) {
int[] visited = new int[graph.length];
//default 0: not visited;
//lable 1: green
//lable 2: red
for(int i = 0; i < graph.length; i++) {
// such node has been visited
if(visited[i] != 0) {continue;}
//such node has not been visited
Queue<Integer> queue = new LinkedList();
queue.add(i);
// mark as green
visited[i] = 1;
while(!queue.isEmpty()) {
int cur = queue.poll();
int curLable = visited[cur];
// if curLable is green, fill neighborLable to red
int neighborLable = curLable == 1? 2:1;
for(int neighbor:graph[cur]) {
//such node has not been visited
if(visited[neighbor] == 0) {
visited[neighbor] = neighborLable;
queue.add(neighbor);
}
// node visited, and visited[neighbor] != neighborLable, conflict happens
else if(visited[neighbor] != neighborLable) {
return false;
}
}
}
}
return true;
}
}
[leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图的更多相关文章
- [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 ...
- [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 ...
- [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】图论 graph(共20题)
[133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...
- Java实现 LeetCode 785 判断二分图(分析题)
785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...
随机推荐
- 如何缓解DDOS攻击
1.减少攻击面 (a) reduce the number of necessary Internet entry points,(b) eliminate non-critical Internet ...
- 推荐一个lamp的一键安装包
本来我是一直用的nginx的,现在安全者的服务器是用的tengine,稳定性就不用多说了! 前段时间用thinkphp写了两个两个项目,刚开始放到了国外的服务器上,环境也是lnmp的,最后发现ngin ...
- 解决 pycharm can not save setting
这个问题出现的原因是因为PyCharm中存在相同名字的虚拟环境变量. 解决方法:Configure Python Interpreter 点击右上角齿轮状按钮, 选择 show all,然后删除相同名 ...
- PHP mysqli_fetch_object() 函数实例讲解
定义和用法 mysqli_fetch_object() 函数从结果集中取得当前行,并作为对象返回. 注释:该函数返回的字段名是区分大小写的. 语法 mysqli_fetch_object(result ...
- 基于Linux的Samba开源共享解决方案测试(五)
对于客户端的网络监控如图: 双NAS网关50Mb码率视音频文件的稳定写测试结果如下: 100Mb/s负载性能记录 NAS网关资源占用 稳定写 稳定写 CPU空闲 内存空闲 网卡占用 NAS1 16个稳 ...
- gz文件最后四位检测
[root@node-0 ~]# ll -rw-r--r-- 1 root root 24048 Nov 29 11:29 install.log 文件大小为24048 [root@node-0 ~ ...
- 图解http pdf
扫加公众号,回复“图解HTTP”,免费获取此书.
- linux随机数
linux系统随机数生成;1,利用uuid(universally unique identifier),由open software foundation在distributed computing ...
- python3基础:字符串、文本文件
字符串: 练习1: str = "大胖三百磅不是二百磅陪着一百磅的小胖" print(str.replace("磅", "斤")) # 替换 ...
- js小效果:返回顶部 scrollTop 。 滚屏:animate
返回顶部: (scroll 滚屏事件,如果超出第一屏,显示返回顶部的按钮) <div id="gotop" onclick="javascript:scroll(0 ...