Java:检查无向图是否是二部图。

Problem Statement: Given an undirected graph , return true if and only if it is bipartite.

问题陈述:给定一个无向图,当且仅当它是二分图时才返回true。

A graph is bipartite if we can split its 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.

一个图是二分 ,如果我们可以将其节点集合分割成两个独立的子集A和B,使得在图中每个边缘具有在一个节点和B.另一节点

Example:

例:

In the above examples, 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.

在以上示例中, graph[i]是索引j的列表,节点ij之间的边缘存在于索引j 。 每个节点都是介于0graph.length - 1.之间的整数graph.length - 1.

Code:

码:

class BipartiteGraph {
    public boolean isBipartite(int[][] graph) {
        int n = graph.length;
        int[] color = new int[n];
        //initially all graph are uncolored, so all entries are -1.
        Arrays.fill(color,-1);


        for(int start = 0; start < n; start++){
            Stack<Integer> stack = new Stack<>();
            stack.push(start);
            color[start] = 0; // 0 represents some color
            while (!stack.isEmpty()){
                Integer node = stack.pop();
                if (color[node] == -1){
                    for (int adj : graph[node]){ // loop through all the adjacent nodes of current node
                        if (color[adj] == -1){
                            stack.push(adj);
                            color[adj] = color[node] ^ 1; // color all the adjacent nodes with different color
                        }else if (color[adj] == color[node]){ // if adj nodes color is same, return false.
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
}

Explanation:

说明:

Above approach is the graph colouring using DFS.

上面的方法是使用DFS进行图形着色。

Create an array of size equal to number of nodes and initialise all values with -1, which shows initially all the nodes are uncoloured.

创建一个大小等于节点数的数组,并使用-1初始化所有值,这最初显示所有节点都是未着色的。

To perform DFS, make use of a stack. Initially keep the start node in the stack and colour it. Use 0, 1, and so on to colour the nodes. Then iterate through all the adjacent nodes of the current node, if they are uncoloured, colour it with different colour and repeat the process for all the nodes.

要执行DFS,请使用堆栈。 最初将起始节点保留在堆栈中并为其着色。 使用0、1等为节点着色。 然后遍历当前节点的所有相邻节点(如果未着色),则用不同的颜色为其着色,并对所有节点重复该过程。

If the colour of current node is same as the node adjacent to it, it means colouring is not possible and graph is not a bipartite graph.

如果当前节点的颜色与相邻节点的颜色相同,则意味着无法着色,并且图形不是二分图。

Complexity Analysis:

复杂度分析:

Time Complexity: O(V+E), V is the number of nodes and E is the number of edges.

时间复杂度:O(V + E),V是节点数,E是边数。

Space Complexity: O(V), array of size V to store the colour.

空间复杂度:O(V),大小为V的数组,用于存储颜色。

翻译自: https://medium.com/nerd-for-tech/java-check-if-an-undirected-graph-is-bipartite-or-not-8166696bd1bf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值