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.

  

原来输入数组中的graph[i],表示顶点i所有相邻的顶点,比如对于例子1来说,顶点0和顶点1,3相连,顶点1和顶点0,2相连,顶点2和结点1,3相连,顶点3和顶点0,2相连。这道题让我们验证给定的图是否是二分图,所谓二分图,就是可以将图中的所有顶点分成两个不相交的集合,使得同一个集合的顶点不相连。为了验证是否有这样的两个不相交的集合存在,我们采用一种很机智的染色法,大体上的思路是要将相连的两个顶点染成不同的颜色,一旦在染的过程中发现有两连的两个顶点已经被染成相同的颜色,说明不是二分图。这里我们使用两种颜色,分别用1和-1来表示,初始时每个顶点用0表示未染色,然后遍历每一个顶点,如果该顶点未被访问过,则调用递归函数,如果返回false,那么说明不是二分图,则直接返回false。如果循环退出后没有返回false,则返回true。在递归函数中,如果当前顶点已经染色,如果该顶点的颜色和将要染的颜色相同,则返回true,否则返回false。如果没被染色,则将当前顶点染色,然后再遍历与该顶点相连的所有的顶点,调用递归函数,如果返回false了,则当前递归函数的返回false,循环结束返回true,参见代码如下:

class Solution {
public boolean isBipartite(int[][] graph) {
if(graph == null){
return false;
}
int v = graph.length;
//if we only have 2 vertices, then it's a Bipartite
if(v <= 2){
return true;
}
int[] colors = new int[v];
for(int i = 0; i < v; i++){
if(colors[i] == 0 && !valid(graph, i, 1, colors)){
return false;
}
}
return true;
} private boolean valid (int[][] graph, int cur, int color, int[] colors){
if(colors[cur] != 0){
return colors[cur] == color;
}
colors[cur] = color;
for(int i : graph[cur]){
if(!valid(graph, i, color * -1, colors)){
return false;
}
}
return true;
}
}

LeetCode - Is Graph Bipartite?的更多相关文章

  1. [LeetCode] Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  2. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  3. [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, ...

  4. [LeetCode] 785. Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  5. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

  6. [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 ...

  7. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  8. [LeetCode] Clone Graph 无向图的复制

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  9. LeetCode OJ-- Clone Graph **@

    https://oj.leetcode.com/problems/clone-graph/ 图的拷贝,就是给一个图,再弄出一个一模一样的来. /** * Definition for undirect ...

随机推荐

  1. UVa LA 3882 - And Then There Was One 递推,动态规划 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. day20 类的约束

    今日所学 : 1 .类的约束 2 .异常处理 try except raise 3. MD5加密 4. 日记处理(不要记,留一份,侧重点再用) 1 .类的约束 1) 写一个父类,父类中的某个方法要抛出 ...

  3. 进程创建过程详解 CreateProcess

    转载请您注明出处:http://www.cnblogs.com/lsh123/p/7405796.html 0x01 CreateProcessW CreateProcess的使用有ANSI版本的Cr ...

  4. JavaScript+CSS+DIV实现下拉菜单示例

    <!DOCTYPE html> <html> <head> <title>下拉菜单示例</title> <script languag ...

  5. fk输入地壳模型容易出错的地方

    结束的那一层地壳模型后面不再有空格,否则不会有波形.

  6. softmax与多分类

    sotfmax 函数在机器学习和深度学习中有着广泛的应用, 主要用于多分类问题. softmax 函数 1. 定义 假定数组V,那么第i个元素的softmax值为 也就是该元素的指数 除以 所有元素的 ...

  7. UVALive - 6434 (贪心)

    题目链接:https://vjudge.net/problem/UVALive-6434 题意:给你n个数字,要你把这n个数字分成m组,每一组的消耗值定义为改组最大值和最小值之差,要求这m组的消耗值总 ...

  8. day 43 数据库的密码的的更改如何操作

    day 43 数据库的密码的的更改如何操作 步骤: 1 首先看环境变量有没有配好 方法 查看计算机 高级系统设置 2 以上步骤完成后 运行 services  找到 mysql的停止 在启动 3,运行 ...

  9. Java 内存监控命令简介(零)

    一.Java性能监控与调优命令.工具简介 1.jps :查看当前运行的Java程序端口号,包括运行jps的程序端口号. 2.jinfo :查看Java进程的运行时信息. 3.jmap + MAT :通 ...

  10. [工作日志] 2018-11-21 主要: 改bug 自测 :校验图片后缀名

    正则表达式 用以下方式去校验图片的后缀 String reg = ".+(.JPEG|.jpeg|.JPG|.jpg)$";String imgp= "Redocn_20 ...