LeetCode 785. Is Graph Bipartite?

原题链接在这里:https://leetcode.com/problems/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.

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

题解:

Try to color the nodes with 2 different colors. Two adjcent nodes can't have same colors.

Use BFS to traverse nodes. At the beginning, put node 0 in the queue, color it as 1.

While queue is not empty, poll the cur node. For neighbors, if nei is not traversed before, color nei with -colors[cur].

If nei is traversed and its color is the same as cur, then 2 adjcent nodes have same color, return false.

For questions like this, traversed nodes need to be put into multiple categories, use array of integers to mark their categories.

Time Complexity: O(V+E). V = graph.length. E is count of all the edges.

Space: O(V).

AC Java: 

 1 class Solution {
 2     public boolean isBipartite(int[][] graph) {
 3         int n = graph.length;
 4         int [] colors = new int[n];
 5         for(int i = 0; i<n; i++){
 6             if(colors[i] == 0){
 7                 LinkedList<Integer> que = new LinkedList<>();
 8                 colors[i] = 1;
 9                 que.add(i);
10                 while(!que.isEmpty()){
11                     int cur = que.poll();
12                     for(int nei : graph[cur]){
13                         if(colors[nei] == 0){
14                             colors[nei] = -colors[cur];
15                             que.add(nei);
16                         }else if(colors[nei] == colors[cur]){
17                             return false;
18                         }
19                     }
20                 }
21             }
22         }
23         
24         return true;
25     }
26 }

Use DFS to iterate nodes.

Start with first node with color 0, dfs try to color it with desiredColor.

For its neighbors, if they are not colored before, try to color them with -desiredColor.

Otherwise, check if they have the color they should have. 

If any of them not, then return false.

Time Complexity: O(V+E).

Space: O(V). stack space.

AC Java:

 1 class Solution {
 2     public boolean isBipartite(int[][] graph) {
 3         int n = graph.length;
 4         int [] colors = new int[n];
 5         for(int i = 0; i<n; i++){
 6             if(colors[i] == 0 && !dfs(i, 1, colors, graph)){
 7                 return false;
 8             }
 9         }
10         
11         return true;
12     }
13     
14     private boolean dfs(int cur, int desiredColor, int [] colors, int[][] graph){
15         if(colors[cur] == 0){
16             colors[cur] = desiredColor;
17             for(int nei : graph[cur]){
18                 if(!dfs(nei, -desiredColor, colors, graph)){
19                     return false;
20                 }
21             }
22             
23             return true;
24         }else if(colors[cur] != desiredColor){
25             return false;
26         }else{
27             return true;
28         }
29     }
30 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11318773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值