785 Is Graph Bipartite?

785 Is Graph Bipartite?

// bfs 





class Solution {
    public boolean isBipartite(int[][] graph) {
      int n = graph.length; // number of nodes 
      int[] visited = new int[n]; // initial defualt value is 0, we can color as 1 or 2 
      for(int i = 0; i < graph.length; i++){  // check every node, because graph can have more than one connected component 
        if(visited[i] == 0){ 
          if(!bfs(i, visited, graph)){
          return false;
          }
        }
      }
      return true;
    }
      
      
    private boolean bfs(int i, int[] visited, int[][] graph){
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(i);
            visited[i] = 1;
      
            while(!queue.isEmpty()){
              int cur = queue.poll();
              for(int nei : graph[cur]){
                if(visited[nei] == 0){
                    visited[nei] = visited[cur] == 1 ? 2 : 1;
                    queue.offer(nei);
                }else{
                  if(visited[nei] == visited[cur]){
                    return false;
                  }
                }
              }
            }
            return true;
    }
}
 
785. Is Graph Bipartite?

       // no need to build a hashmap, since we are given the adj list
        // the key is the index, the value is a list of its nei
        
        // no need to have a int[] visited and int[] colored, 
        // if a node is visited, its colored 
        // initlize the visited[] as 0, and black is 1, red is 2 
        
        // since this might have more than one connected component
        // so we want to check every unvisited node 



// bfs
class Solution {
    public boolean isBipartite(int[][] graph) {
      int n = graph.length; // number of nodes 
      int[] visited = new int[n]; // initial defualt value is 0, we can color as 1 or 2 
      for(int i = 0; i < graph.length; i++){  // check every node, because graph can have more than one connected component 
        if(visited[i] == 0){ 
          if(!bfs(i, visited, graph)){
          return false;
          }
        }
      }
      return true;
    }
      
      
    private boolean bfs(int i, int[] visited, int[][] graph){
            Queue<Integer> queue = new LinkedList<>();
            queue.offer(i);
            visited[i] = 1;
      
            while(!queue.isEmpty()){
              int cur = queue.poll();
              for(int nei : graph[cur]){
                if(visited[nei] == 0){
                    visited[nei] = visited[cur] == 1 ? 2 : 1;
                    queue.offer(nei);
                }else{
                  if(visited[nei] == visited[cur]){
                    return false;
                  }
                }
              }
            }
            return true;
    }
}



// dfs  not mine 不太明白
class Solution {
    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        int[] colors = new int[n];
        Arrays.fill(colors, -1);            
                
        for (int i = 0; i < n; i++) {              //This graph might be a disconnected graph. So check each unvisited node.
            if (colors[i] == -1 && !validColor(graph, colors, 0, i)) {
                return false;
            }
        }
        return true;
    }
    
    public boolean validColor(int[][] graph, int[] colors, int color, int node) {
        if (colors[node] != -1) {
            return colors[node] == color;
        }       
        colors[node] = color;       
        for (int next : graph[node]) {
            if (!validColor(graph, colors, 1 - color, next)) {
                return false;
            }
        }
        return true;
    }
}

 

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.

 

posted on 2018-08-09 17:52  猪猪&#128055;  阅读(135)  评论(0)    收藏  举报

导航