LeetCode785. Judgment bipartite graph

topic

Given an undirected graph graph, return true when the graph is a bipartite graph.

If we can divide the set of nodes in a graph into two independent subsets A and B, and make the two nodes of each edge in the graph one from the set A and one from the set B, we call this graph binary picture.

The graph will be given as an adjacency list, and graph[i] represents all the nodes connected to node i in the graph. Each node is an integer between 0 and graph.length-1. There are no self-loops and parallel edges in this graph: i does not exist in graph[i], and there are no duplicate values ​​in graph[i]. link

Ideas

Did not understand the question at first. . His adjacency list is a bit interesting, there are a total graph.lengthof nodes, and each subscript represents a node, that is [0,graph.length-1], graph[i]an array, which stores 节点ielements with edges between and. There is no idea. After reading the solution, the dyeing method can be solved. You can use dfs or bfs.

class Solution {
    
    
    final static int RED = 1;
    final static int GREEN = 2; 
    int [] colors;
	/*bfs*/
    public boolean isBipartite(int[][] graph) {
    
    
        int num = graph.length;
        colors = new int[num];
        //从第一个节点开始
        for(int i = 0; i < num; i++){
    
    
            //遇到未染色的节点才开始bfs搜索
            if(colors[i] == 0){
    
    
                //未染色的都染为红色
                colors[i] = RED;
                Queue<Integer> queue = new LinkedList<>();
                //从i开始bfs
                queue.offer(i);
                while(!queue.isEmpty()){
    
    
                    int curNode = queue.poll();
                    int color = colors[curNode];
                    //对其邻居进行染色
                    for(int neighbor: graph[curNode]){
    
    
                        //邻居未染色color为0,需要染上与curNode不同的颜色
                        if(colors[neighbor] == 0){
    
    
                            colors[neighbor] = color == RED ? GREEN : RED;
                            queue.offer(neighbor);
                        }//邻居染色了,若与curNode颜色相同直接返回
                        else if(colors[neighbor] == color){
    
    
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}
class Solution {
    
    
    final static int RED = 1;
    final static int GREEN = 2; 
    int [] colors;
    boolean valid = true; 
    /*dfs*/
    public boolean isBipartite(int[][] graph) {
    
    
        int num = graph.length;
        colors = new int[num];
        for(int i = 0; i < num; i++){
    
    
            if(colors[i] == 0){
    
    
                dfs(graph, i, RED);
            }
        }
        return valid;
    }

    void dfs(int[][] graph, int curNode, int color){
    
    
        if(!valid){
    
    
            return;
        }
        colors[curNode] = color;
        int neighborColor = color == RED ? GREEN : RED;
        for(int neighbor: graph[curNode]){
    
    
            if(colors[neighbor] == 0){
    
    
                dfs(graph, neighbor, neighborColor);
            }else if(colors[neighbor] != neighborColor){
    
    
                valid = false;
                return;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_42007742/article/details/107378819