【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/ ...
随机推荐
- day14 linux三剑客之sed命令
day14 linux三剑客之sed命令 sed命令 Sed 主要用来自动编辑一个或多个文件.简化对文件的反复操作.编写转换程序等. sed(流式编辑器) : sed主要用来修改文件. 1.sed命令 ...
- Angular中@Output()的使用方法
子component中的html文件 <button (click)="Send()">送出</button><br> 子component中的 ...
- 【leetcode】653. Two Sum IV - Input is a BST
Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...
- recyclerView DiffUtil使用
DiffUtil是和RecyclerView一块用的,DiffUtil用来比较两个数据集,他的最大用处是在RecyclerView刷新时,不在无脑. 以前adapter.notifyDataSetCh ...
- Spring Cloud 和dubbo
一.SpringCloud微服务技术简介 Spring Cloud 作为Java 语言的微服务框架,它依赖于Spring Boot,有快速开发.持续交付和容易部署等特点.Spring Cloud 的组 ...
- 1.Java语言基础
一:java语言介绍 (1). 1991年出现,1995年5月正式发布 出生地:SUN 创始人:James Gosling 2009年4月被Oracle收购 目前最新的版本2018年3月v10.0 ...
- js--生成器总结
前言 生成器gengrator是es6 新增的函数功能,它允许你定义一个包含自有迭代算法的函数, 同时它可以自动维护自己的状态. 本文来总结一下JavaScript 中生成器的相关知识点. 正文 1. ...
- CURD系统怎么做出技术含量惊艳面试官
在<CURD系统怎么做出技术含量--怎样引导面试>有朋友开玩笑说都用上了领域驱动了,就不叫CURD系统了吧.这里我解释一下,怕大家对DDD领域驱动设计有什么误解. DDD是为解决软件复杂性 ...
- 基于Kubernetes的hpa实现pod实例数量的自动伸缩
Pod 是在 Kubernetes 体系中,承载用户业务负载的一种资源.Pod 们运行的好坏,是用户们最为关心的事情.在业务流量高峰时,手动快速扩展 Pod 的实例数量,算是玩转 Kubernetes ...
- css预处理器和css Modules是干嘛的?
CSS预处理器 1.css和js的区别 js是编程语言,它可以声明变量,编写逻辑.而css实际上只是个"表",表头是选择器,内容是里面的样式.它并不能写逻辑啥的.也就是说,对于cs ...