【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/is-graph-bipartite/description/
题目描述:
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].
题目大意
判断一个无向图是不是二分图。二分图的定义是,可以把一个图划分成两部分,使得图中的每个边的两个定点分别来自这两部分。
解题方法
这个题很容易理解了,做法也很简单,使用众所周知的染色法。可以通过BFS或者DFS来解决。我使用的是BFS.
使用一个visited数组来保存每个节点被染的颜色。0代表没染色,1代表染成蓝色,2代表染成红色。对图的每个顶点进行一个遍历,把和这个顶点相邻的顶点全部染成相反的颜色。如果相邻顶点已经染色,而且染色和当前顶点染色相同,则返回False。全部成功染色后返回True。
这个题没有说明是连通图,这个就很坑爹了,不能通过一次的BFS就把所有的顶点染色成功。所以需要的是一个外层的对顶点进行遍历,一个内层的对每个顶点相邻的顶点遍历,这样两重遍历才能保证每个顶点、这个顶点相邻的顶点都被强行的染色。
时间复杂度是O(E+V),空间复杂度是O(E).
代码如下:
class Solution(object):
def isBipartite(self, graph):
"""
:type graph: List[List[int]]
:rtype: bool
"""
visited = [0] * len(graph)# 0-not visited; 1-blue; 2-red;
for i in range(len(graph)):
if graph[i] and visited[i] == 0:
visited[i] = 1
q = collections.deque()
q.append(i)
while q:
v = q.popleft()#every point
for e in graph[v]:#every edge
if visited[e] != 0:
if visited[e] == visited[v]:
return False
else:
visited[e] = 3 - visited[v]
q.append(e)
return True
参考资料:
https://leetcode.com/problems/is-graph-bipartite/discuss/115503/java-BFS
日期
2018 年 9 月 20 日 —— 趁年轻多读书
【LeetCode】785. Is Graph Bipartite? 解题报告(Python)的更多相关文章
- [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?
原题链接在这里: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】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
随机推荐
- 7本Python必读的入门书籍,你看过吗?(附福利)
Python入门书籍不用看太多,看一本就够.重要的是你要学习Python的哪个方向,或者说你对什么方向感兴趣,因为Python这门语言的应用领域比较广泛,比如说可以用来做数据分析.机器学习,也可以用来 ...
- Python中的随机采样和概率分布(一)
Python(包括其包Numpy)中包含了了许多概率算法,包括基础的随机采样以及许多经典的概率分布生成.我们这个系列介绍几个在机器学习中常用的概率函数.先来看最基础的功能--随机采样. 1. rand ...
- JVM1 JVM与Java体系结构
目录 JVM与Java体系结构 虚拟机与Java虚拟机 虚拟机 Java虚拟机 JVM的位置 JVM的整体结构 Java代码执行流程 JVM的架构模型 基于栈的指令级架构 基于寄存器的指令级架构 两种 ...
- 17. yum
https://www.linuxidc.com/Linux/2015-04/116331.htm
- windows Visual Studio 上安装 CUDA【转载】
原文 : http://blog.csdn.net/augusdi/article/details/12527497 前提安装: Visual Studio 2012 Visual Assist X ...
- eclipse上安装 windowBuilder方法
最近因为需要用java Swing做一些组件设计,但想想以前在大学时候为了布局组件和位置设计花了很多时间.所以再网上查了一些带有可视化的设计插件用来提高工作效率. 其中一个是 windowBuilde ...
- C++ 写出这个数
题目如下: 读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1. 输出格式: 在一行内 ...
- 分布式全局ID生成器原理剖析及非常齐全开源方案应用示例
为何需要分布式ID生成器 **本人博客网站 **IT小神 www.itxiaoshen.com **拿我们系统常用Mysql数据库来说,在之前的单体架构基本是单库结构,每个业务表的ID一般从1增,通过 ...
- 使用matplotlib中的bar函数绘制柱状图
使用柱状图显示三日电影的票房信息 要显示的数据为2018年12月7日-9日四场电影的票房信息 四场电影分别为:无名之辈,狗十三,毒液:知名守卫者,憨豆特工3 2018年12月7日四场电影票房分别为:[ ...
- Wireshark(四):网络性能排查之TCP重传与重复ACK
原文出处: EMC中文支持论坛 作为网络管理员,很多时间必然会耗费在修复慢速服务器和其他终端.但用户感到网络运行缓慢并不意味着就是网络问题. 解决网络性能问题,首先从TCP错误恢复功能(TCP重传与重 ...