zoukankan      html  css  js  c++  java
  • 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.

    Note: graph may not be one component. Thus for each node that is still 0, still need BFS on it.

    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 }

    类似Possible Bipartition.

  • 相关阅读:
    博客阅读计数优化
    博客阅读简单计数
    博客后台富文本编辑
    博客分类统计
    Django关联关系查询
    上下篇博客,按月归档
    浅谈闭包以及常见面试题
    浅谈前端缓存(转至大佬)
    post请求头的常见类型
    浅谈RegExp 对象的方法
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11318773.html
Copyright © 2011-2022 走看看