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

    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].
    class Solution {
        public boolean isBipartite(int[][] graph) {
            //BFS
            // 0(not meet), 1(black), 2(white)
            int[] visited = new int[graph.length];
            
            for (int i = 0; i < graph.length; i++) {
                if (graph[i].length != 0 && visited[i] == 0) {
                    visited[i] = 1;
                    Queue<Integer> q = new LinkedList<>();
                    q.offer(i);
                    while(! q.isEmpty()) {
                        int current = q.poll();
                        for (int c: graph[current]) {
    
                                if (visited[c] == 0) {
                                    visited[c] = (visited[current] == 1) ? 2 : 1;
                                    q.offer(c);
                                } else {
                                    if (visited[c] == visited[current]) return false;
                                }
                        }
                    }                                       
                }
            }        
            return true;
        }
    }

    BFS,涂色法,对每个点涂色,然后对current它所有的adjacent node涂成相反颜色,只要有一步的检查里和current颜色相同就返回false

     public boolean isBipartite(int[][] g) {
            int[] colors = new int[g.length];
            for (int i = 0; i < g.length; i++)
                if (colors[i] == 0 && !validColor(g, colors, 1, i))
                    return false;
            return true;
        }
    
        boolean validColor(int[][] g, int[] colors, int color, int node) {
            if (colors[node] != 0)
                return colors[node] == color;
            colors[node] = color;
            for (int adjacent : g[node])
                if (!validColor(g, colors, -color, adjacent))
                    return false;
            return true;
        }

    DFS

    https://www.youtube.com/watch?v=670Gn4e89B8

  • 相关阅读:
    Java的值传递和引用传递的说法
    将对象写道硬盘上and从硬盘上读入对象
    分割一个文件and合并一个文件(并且带有配置信息记录)
    文件的切割和合并
    SequenceInputStream的用法(用来合并流然后一起操作)
    PrintStream和PrintWrite用法
    将一个文件夹中的所有含有某个后缀的文件写进一个文件里面
    关于Properties的制作配置文件(当一个app已经5次被打开我们就收费)
    Properties的用法
    深层删除一个目录(java)
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13439202.html
Copyright © 2020-2023  润新知