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.

思路
Start with some vertex v, add it to some group 1.
dfs traverse the graph starting with vertex v, look at the edges along the traversal.
Say there is an edge (v,u), then we should put u into group 2 because it has to be in a different group than v. If we ever see the case that we should put a vertex into group X, but it actually already exists in group Y, then we reach a conflict. Therefore we know the graph is not biparite. Kind of like proof of contradiction isn't it ?

答案

class Solution {
    public boolean isBipartite(int[][] graph) {
        Set<Integer> group1 = new HashSet<>();
        Set<Integer> group2 = new HashSet<>();
        boolean[] visited = new boolean[graph.length];
        boolean b = false, ret = false;
        for(int i = 0; i < graph.length; i++) {
            // Do not consider vertices with no neighbors
            if(graph[i].length == 0) continue;
            if(visited[i]) continue;

            group1.add(i);
            ret = dfs(graph, i, visited, group1, group2);
            if(!ret) return false;            
        }
        return true;
    }
    
    public boolean dfs(int[][] graph, int curr, boolean[] visited, Set<Integer> group1, Set<Integer> group2) {
        boolean ret = true;
        visited[curr] = true;
        
        int[] curr_neighbors = graph[curr];
        for(int i = 0; i < curr_neighbors.length; i++) {
            int neighbor = curr_neighbors[i];
            // edge: curr->neighbor
            if(group1.contains(curr)) {
                if(group1.contains(neighbor)) return false;
                group2.add(neighbor);
            }
            else {
                if(group2.contains(neighbor)) return false;
                group1.add(neighbor);                
            }
            
            if(!visited[neighbor]) {
                ret = dfs(graph, neighbor, visited, group1, group2);
                if(!ret) return false;
            }
        }
        return true;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 黃白双色乒乓球, 飞来飞去好自由。 时而落在地面游, 时而被劫在案头。 你来我往拼球技, 小球时而耍脾气。 不走正...
    旖旎i阅读 202评论 0 0
  • 今天在小学同学群,有个小学同学谈到他相过亲。 女方家里比较有钱,有几套房子,说送一套也是可以的。 他说:“尽管她家...
    黄志博阅读 320评论 4 1
  • 又是一年菊花黄, 细雨携秋凉。 心湿透, 总是思念 举目雁成行。 秋色本来如此, 何必乱想! 年年花开, 岁岁人老...
    读懂生活宁静守心阅读 520评论 2 11
  • 今年过年,你回家了吗?如果回家了,你在家待了几天?完全陪父母的时间有多少?如果你没回家,那你想家吗?想父母吗? 问...
    舒小陌阅读 479评论 0 0

友情链接更多精彩内容