判断一个图是否是二分图
二分图:其顶点可分为两组独立的U和V,使得每条边 [ u , v ] [u,v] [u,v]可以连接顶点,从 U U U到V,或者从V到 U U U,换言之,对于每一个边 [ u , v ] [u,v] [u,v],要么u属于U,v属于V,要么u属于V,v属于U。我们也可以说没有一条边连接同一个集合的顶点
如果可以使用两种颜色对图进行着色,使得集合中的顶点用相同的颜色着色,则二分图是可能的。请注意,可以使用两种颜色为具有偶数个顶点的环着色。
Alogrithm:
检查图是否为二分图的算法:
一种方法是使用回溯算法处理m着色问题检查图是否为2色的
以下是使用广度优先搜索BFS 找出给定图是否为二分图的简单算法:
- 将红色分配给源顶点(放入集合 U)
- 用蓝色给所有的邻接节点着色(放入集合V)
- 用红色给所有邻接节点的邻接节点着色(放入集合U)
- 这样,为所有顶点分配颜色,使其满足 m = 2 的 m 路着色问题的所有约束
- 在分配颜色时,如果我们找到一个与当前顶点颜色相同的邻接节点,则该图不能用2个顶点着色或者说图不是二分图
PART1:
/**
* 基础版的 图是连通的
*/
static class _1st {
final static int V = 4;
public boolean isBipartite(int[][] G, int src) {
int[] colors = new int[V];
Arrays.fill(colors, -1);
colors[src] = 1;
Queue<Integer> q = new LinkedList<>();
q.offer(src);
while (!q.isEmpty()) {
int u = q.poll();
if (G[u][u] == 1) return false;
for (int v = 0; v < V; v++) {
if (G[u][v] == 1 && colors[v] == -1) {
colors[v] = 1 - colors[u];
q.offer(v);
} else if (G[u][v] == 1 && colors[v] == colors[u]) return false;
}
}
return true;
}
public static void main(String[] args) {
int G[][] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}
};
_1st b = new _1st();
if (b.isBipartite(G, 0))
System.out.println("Yes");
else
System.out.println("No");
//Yes
}
}
上述算法仅在图连通时才有效。在上面的代码中,我们总是源点0开始,并假设所有顶点可以通过这个点访问到。
一个重要的点是没有边的图也是二分图。请注意,二分条件表示所有边都应该从一组到另一组。
我们可以扩展上面的代码来处理图未连通的情况。这个想法是对所有尚未访问的顶点重复调用上述方法。
PART2:
/**
* 图不一定是连通的
*/
static class _2nd {
final static int V = 4;
public static boolean isBipartiteUtil(int[][] G, int src, int[] colors) {
colors[src] = 1;
Queue<Integer> q = new LinkedList<>();
q.offer(src);
while (!q.isEmpty()) {
int u = q.poll();
//自依赖
if (G[u][u] == 1) return false;
for (int v = 0; v < V; v++) {
//u与v之间存在一条边&& v没有被染色过
if (G[u][v] == 1 && colors[v] == -1) {
colors[v] = 1 - colors[u];
q.offer(v);
} else if (G[u][v] == 1 && colors[v] == colors[u]) {//u与v之间存在一条边,但是u和v的颜色相同,不满足二分图
return false;
}
}
}
return true;
}
public static boolean isBipartite(int[][] G) {
int[] colors = new int[V];
Arrays.fill(colors, -1);
for (int u = 0; u < V; u++) {
if (colors[u] == -1) {
if (!isBipartiteUtil(G, u, colors)) return false;
}
}
return true;
}
public static void main(String[] args) {
int G[][] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}};
if (isBipartite(G))
System.out.println("Yes");
else
System.out.println("No");
}
}
上述方法的时间复杂度与广度优先搜索相同。在上面的实现中是 O ( V 2 ) O(V^2) O(V2) 其中 V 是顶点数。如果使用邻接表表示图,则复杂度变为 O ( V + E ) O(V+E) O(V+E)。
如果使用邻接表表示图,时间复杂度将为 O ( V + E ) O(V+E) O(V+E)
适用于连通图和未连通图。
PART3:
//通用版本
static class _3rd {
static class Pair {
int v;//vertex
int c;//color
public Pair(int v, int c) {
this.v = v;
this.c = c;
}
}
public static boolean isBipartite(int V, List<List<Integer>> adj) {
int[] colors = new int[V];
Arrays.fill(colors, -1);
Queue<Pair> q = new LinkedList<>();
for (int i = 0; i < V; i++) {
if (colors[i] == -1) {//当前节点i未被着色
q.offer(new Pair(i, 0));//一开始赋一个颜色0
colors[i] = 0;//修改colors数组的值
while (!q.isEmpty()) {
Pair cur = q.poll();
int u = cur.v;//当前节点u
int c = cur.c;//当前节点u的颜色
for (int v : adj.get(u)) {
if (colors[v] == c) return false;//相邻的节点颜色相同,不符合二分图
if (colors[v] == -1) {
colors[v] = 1 - c;//修改colors数组的值,翻转与上一个节点的颜色
q.offer(new Pair(v, colors[v]));
}
}
}
}
}
return true;
}
public static void main(String args[]) {
int V, E;
V = 4;
E = 8;
// adjacency list for storing graph
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < V; i++) {
adj.add(new ArrayList<Integer>());
}
adj.get(0).add(1);
adj.get(0).add(3);
adj.get(1).add(0);
adj.get(1).add(2);
adj.get(2).add(1);
adj.get(2).add(3);
adj.get(3).add(0);
adj.get(3).add(2);
boolean ans = isBipartite(V, adj);
// returns 1 if bipatite graph is possible
if (ans)
System.out.println("Yes");
// returns 0 if bipartite graph is not possible
else
System.out.println("No");
}
}
PART4:DFS
static class _4th {
static final int V = 4;
public static boolean isBipartiteUtil(int[][] G, int[] colors, int u, int c) {
if (colors[u] != -1 && colors[u] != c) return false;
colors[u] = c;
boolean res = true;
for (int v = 0; v < V; v++) {
if (G[u][v] == 1) {
if (colors[v] == -1) {
res &= isBipartiteUtil(G, colors, v, 1 - c);
}
if (colors[v] != -1 && colors[v] != 1 - c) return false;
}
if (!res) return false;
}
return true;
}
static boolean isBipartite(int G[][]) {
int[] color = new int[V];
for (int i = 0; i < V; i++)
color[i] = -1;
// start is vertex 0;
int u = 0;
// two colors 1 and 0
return isBipartiteUtil(G, color, u, 1);
}
// Driver Code
public static void main(String[] args) {
int G[][] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}};
if (isBipartite(G))
System.out.print("Yes");
else
System.out.print("No");
}
}