[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

代码

 1 class Solution {
 2     public boolean isBipartite(int[][] graph) {
 3         int[] visited = new int[graph.length];
 4         //default 0: not visited;
 5         //lable 1: green
 6         //lable 2: red
 7         for(int i = 0; i < graph.length; i++) {
 8             // such node has been visited
 9             if(visited[i] != 0) {continue;}
10             //such node has not been visited
11             Queue<Integer> queue = new LinkedList();
12             queue.add(i);
13             // mark as green
14             visited[i] = 1;
15             while(!queue.isEmpty()) {
16                 int cur = queue.poll();
17                 int curLable = visited[cur];
18                 // if curLable is green, fill neighborLable to red
19                 int neighborLable = curLable == 1? 2:1;
20                 for(int neighbor:graph[cur]) {
21                     //such node has not been visited
22                     if(visited[neighbor] == 0) {
23                         visited[neighbor] = neighborLable;
24                         queue.add(neighbor);
25                     }
26                     // node visited, and visited[neighbor] != neighborLable, conflict happens
27                     else if(visited[neighbor] != neighborLable) {
28                         return false;
29                     }
30                 }
31             }
32         }
33         return true;
34     }
35 }

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

原文地址:https://www.cnblogs.com/liuliu5151/p/9814350.html

时间: 2024-11-09 05:43:01

[leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图的相关文章

LeetCode - 785. Is Graph Bipartite?

判断一个给定图是不是二分图. 题目提供一个用二维数组存储的邻接表. 常规的二分图判断,点着色. 注意要将图存入类中,因为dfs需要访问图中的点. 1 class Solution { 2 private int[][] graph; 3 private boolean[] visited; 4 private int[] colors; 5 6 public boolean isBipartite(int[][] graph) { 7 this.graph = graph; 8 int V =

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.

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.

【leetcode】Clone Graph(python)

类似于二叉树的三种遍历,我们可以基于遍历的模板做很多额外的事情,图的两种遍历,深度和广度模板同样也可以做很多额外的事情,这里举例利用深度优先遍历的模板来进行复制,深度优先中,我们先访问第一个结点,接着访问第一个邻接点,再访问邻节点的邻节点.... class Solution: # @param node, a undirected graph node # @return a undirected graph node def cloneGraph(self, node): if None =

【leetcode】Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh

leetcode -day11 Clone Graph &amp; Palindrome Partitioning I II

 1.Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

leetcode.图.785判断二分图-Java

1. 具体题目 给定一个无向图graph,当这个图为二分图时返回true.如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图.graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点.每个节点都是一个在0到graph.length-1之间的整数.这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值. 示例 1: 输入: [[1,3], [0,2],

785. 判断二分图——本质上就是图的遍历 dfs或者bfs

785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图. graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点.每个节点都是一个在0到graph.length-1之间的整数.这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值. 示例 1: 输入: [[1,3], [

[LeetCode] 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.