785. Is Graph Bipartite?从两个集合中取点构图

Posted 排序和map

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了785. 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.

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

需要考虑非连通图的情况,此时无法染色 语言:

!validColor(graph, colors, 0, i)

[思维问题]:

[一句话思路]:

双色问题的本质是BFS

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

 [代码风格] :

 

以上是关于785. Is Graph Bipartite?从两个集合中取点构图的主要内容,如果未能解决你的问题,请参考以下文章

LWC 72: 785. Is Graph Bipartite?

LeetCode 785. Is Graph Bipartite?

785. Is Graph Bipartite?( 判断是否为二分图)

LeetCode - 785. Is Graph Bipartite?

[leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图

LeetCode 785. 判断二分图 | Python