Coloring problem of graphs (bipartite graphs)

Related concepts:

  • The problem of coloring adjacent vertices into different colors is called graph coloring problem.
  • The minimum number of colors required for coloring a graph is called the minimum number of colors. A graph with a minimum coloring number of 2 is called a bipartite graph.
  • - bipartite graph: given a graph with n vertices, each vertex on the graph should be colored, and the colors of adjacent vertices should be different. If you want to use at most two colors for coloring, there is no double edge (two adjacent vertices have two sides) and self ring.

Solutions:

                    . Deep search traverses every point, tries every color in turn; records the marked colors in the process of deep search.

The code is as follows:

Adjacency table class:

import java.util.ArrayList;
import java.util.List;

/*
 * Head node of adjacency table
 */
public class GraphNodeAl {
	public int val;  //Represents the location of the node
	public List<GraphNodeAl> neighbors; //The linked list of nodes, which stores all nodes adjacent to the node

	
	public GraphNodeAl(int val){
		this.val = val;
	}
	
	//Get the node i adjacent to the node
	public GraphNodeAl getNeighbor(int i){
		return neighbors.get(i);
	}
	
	//Add nodes adjacent to nodes
	public void add(GraphNodeAl node){
		if(this.neighbors == null){
			this.neighbors =  new  ArrayList<GraphNodeAl>();
		}
		neighbors.add(node);
	}
	
	//Returns the number of nodes adjacent to the node
	public int size(){
		return this.neighbors.size();
	}
}

Solution code:

import Representation of Graphs.GraphNodeAl;

/*
 * Judge whether a given graph is a bipartite graph
 */
public class Bipartite graph {
	public static void main(String[] args) {
		//The nodes of the construction diagram, here counting from 1
		MyNode n1 = new MyNode(1);
		MyNode n2 = new MyNode(2);
		MyNode n3 = new MyNode(3);
		MyNode n4 = new MyNode(4);
		
		n1.add(n2);
		n1.add(n4);
		
		n2.add(n1);
		n2.add(n3);
		
		n3.add(n2);
		n3.add(n4);
		
		n4.add(n1);
		n4.add(n3);
		
		//You can traverse from any vertex
		System.out.println(dfs(n1,1));
	}

	//Traverse the vertices of a graph to determine whether it can form a bipartite graph. Use 1 and 2 for two colors, and 0 for no coloring
	private static boolean dfs(MyNode n1, int c) {
		n1.color = c;
		//Try to search the adjacent vertices in turn
		for(int j=0; j<n1.size(); j++){
			MyNode nei = (MyNode)n1.getNeighbor(j);
			if(nei.color==c){   //Same color as adjacent nodes
				return false;
			}
			if(nei.color==0){  //No color
				if(!dfs(nei, f(c))){ //Mark neighbors with opposite colors
					return false;
				}
			}
		}
		return true;
	}

	//Take the opposite color
	private static int f(int c) {
		if(c==1){
			return 2;
		}
		return 1;
	}
}

class MyNode extends GraphNodeAl{

	public  int color; 
	
	public MyNode(int val) {
		super(val);
	}
	
	public MyNode(int val, int color){
		super(val);
		this.color = color;
	}
}

Solution 2:
                      .
In fact, we can fill the color in the way of deep search and backtracking without using it as a graph. That is to say, each node is marked with color in order, and the color of each node is judged once to see whether there is color conflict between adjacent nodes.
   if there is a color conflict between adjacent nodes, it will go back to the previous node and try another color.
From the perspective of deep tree searching, a binary tree will be formed, because each node has two color choices.

The code is as follows:

import Representation of Graphs.GraphNodeAl;

/*
 * Judge whether a given graph is a bipartite graph
 */
public class Dichotomous graphic method II {
	private static MyNode[] arr = new MyNode[5];
	public static void main(String[] args) {
		//The nodes of the construction diagram, here counting from 1
		arr[1] = new MyNode(1);
		arr[2] = new MyNode(2);
		arr[3] = new MyNode(3);
		arr[4] = new MyNode(4);
		
		arr[1].add(arr[2]);
		arr[1].add(arr[4]);
		
		arr[2].add(arr[1]);
		arr[2].add(arr[3]);
		
		arr[3].add(arr[2]);
		arr[3].add(arr[4]);
		
		arr[4].add(arr[1]);
		arr[4].add(arr[3]);
		
		
		//You can traverse from any vertex
		System.out.println(dfs(arr,1));
	}

	private static boolean dfs(MyNode[] arr, int i) {
		
		for(int j=1; j<=2; j++){
			if(check(arr,i,j)){
				arr[i].color = j;
				if(i==arr.length-1){  //exit
					return true;
				}
				return dfs(arr, i+1);
			}
		}
		return false;
	}

	//Judge whether there is color conflict between adjacent nodes of node i
	private static boolean check(MyNode[] arr, int i, int j) {
		//Traverse adjacent nodes and compare colors
		for(int k=0; k<arr[i].size(); k++){
			MyNode temp = (MyNode)arr[i].getNeighbor(k);
			if(j==temp.color){
				return false;
			}
		}
		return true;
	}

}

Tags: Java

Posted on Thu, 11 Jun 2020 01:05:15 -0400 by ngubie