java学习第35天,图的m着色问题

1,这是经典的回溯算法,万能的暴力解题法,大家一定要掌握!
2,调拭时注意 +1, -1 之类的下标控制.
3,由于它使用了图的连接性, 所以需要一部分图的代码。
图的代码:

package java31to40;

import java.util.Arrays;

import java21to30.D22_CircleObjectQueue;
import java21to30.D25_ObjectStack;

public class D32_Graph {
	D31_IntMatrix connectivityMatrix;

	public static void main(String[] args) {
		System.out.println("Hello!");
		D32_Graph tempGraph = new D32_Graph(3);
		System.out.println(tempGraph);
		getConnectivityTest();
	}

	public D32_Graph(int paraNumNodes) {
		connectivityMatrix = new D31_IntMatrix(paraNumNodes, paraNumNodes);
	}

	public D32_Graph(int[][] paraMatrix) {
		connectivityMatrix = new D31_IntMatrix(paraMatrix);
	}

	public String toString() {
		String resultString = "图的连通性矩阵.\r\n" + connectivityMatrix;
		return resultString;
	}

	public boolean getConnectivity() throws Exception {
		D31_IntMatrix tempConnectivityMatrix = D31_IntMatrix.getIdentityMatrix(connectivityMatrix.getData().length);
		D31_IntMatrix tempMultipliedMatrix = new D31_IntMatrix(connectivityMatrix);
		for (int i = 0; i < connectivityMatrix.getData().length - 1; i++) {
			tempConnectivityMatrix.add(tempMultipliedMatrix);
			tempMultipliedMatrix = D31_IntMatrix.multiply(tempMultipliedMatrix, connectivityMatrix);
		}
		System.out.println("连通性矩阵为: " + tempConnectivityMatrix);
		int[][] tempData = tempConnectivityMatrix.getData();
		for (int i = 0; i < tempData.length; i++) {
			for (int j = 0; j < tempData.length; j++) {
				if (tempData[i][j] == 0) {
					System.out.println("节点 " + i + " 不能到达 " + j);
					return false;
				}
			}
		}
		return true;
	}

	public static void getConnectivityTest() {
		int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
		D32_Graph tempGraph2 = new D32_Graph(tempMatrix);
		System.out.println(tempGraph2);
		boolean tempConnected = false;
		try {
			tempConnected = tempGraph2.getConnectivity();
		} catch (Exception ee) {
			System.out.println(ee);
		}
		System.out.println("图是否具有连通性? " + tempConnected);
		tempGraph2.connectivityMatrix.setValue(1, 0, 0);
		tempConnected = false;
		try {
			tempConnected = tempGraph2.getConnectivity();
		} catch (Exception ee) {
			System.out.println(ee);
		}
		System.out.println("图是否具有连通性? " + tempConnected);
	}

	public String breadthFirstTraversal(int paraStartIndex) {
		D22_CircleObjectQueue tempQueue = new D22_CircleObjectQueue();
		String resultString = "";
		int tempNumNodes = connectivityMatrix.getRows();
		boolean[] tempVisitedArray = new boolean[tempNumNodes];
		tempVisitedArray[paraStartIndex] = true;
		tempVisitedArray[paraStartIndex] = true;
		resultString += paraStartIndex;
		tempQueue.enqueue(new Integer(paraStartIndex));
		int tempIndex;
		Integer tempInteger = (Integer) tempQueue.dequeue();
		while (tempInteger != null) {
			tempIndex = tempInteger.intValue();
			for (int i = 0; i < tempNumNodes; i++) {
				if (tempVisitedArray[i]) {
					continue;
				}
				if (connectivityMatrix.getData()[tempIndex][i] == 0) {
					continue;
				}
				tempVisitedArray[i] = true;
				resultString += i;
				tempQueue.enqueue(new Integer(i));
			}
			tempInteger = (Integer) tempQueue.dequeue();
		}
		return resultString;
	}

	public static void breadthFirstTraversalTest() {
		int[][] matrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1 }, { 0, 1, 1, 0 } };
		D32_Graph graph = new D32_Graph(matrix);
		System.out.println(graph);
		String sequence = "";
		try {
			sequence = graph.breadthFirstTraversal(2);
		} catch (Exception ee) {
			System.out.println(ee);
		}
		System.out.println("图的广度优先遍历访问顺序: " + sequence);
	}

	public String depthFirstTraversal(int paraStartIndex) {
		D25_ObjectStack tempStack = new D25_ObjectStack();
		String resultString = "";
		int tempNumNodes = connectivityMatrix.getRows();
		boolean[] tempVisitedArray = new boolean[tempNumNodes];
		tempVisitedArray[paraStartIndex] = true;
		resultString += paraStartIndex;
		tempStack.push(new Integer(paraStartIndex));
		System.out.println("Push " + paraStartIndex);
		System.out.println("Visited " + resultString);
		int tempIndex = paraStartIndex;
		int tempNext;
		Integer tempInteger;
		while (true) {
			tempNext = -1;
			for (int i = 0; i < tempNumNodes; i++) {
				if (tempVisitedArray[i]) {
					continue;
				}
				if (connectivityMatrix.getData()[tempIndex][i] == 0) {
					continue;
				}
				tempVisitedArray[i] = true;
				resultString += i;
				tempStack.push(new Integer(i));
				System.out.println("Push " + i);
				tempNext = i;
				break;
			}
			if (tempNext == -1) {
				if (tempStack.isEmpty()) {
					break;
				}
				tempInteger = (Integer) tempStack.pop();
				System.out.println("Pop " + tempInteger);
				tempIndex = tempInteger.intValue();
			} else {
				tempIndex = tempNext;
			}
		}
		return resultString;
	}

	public static void depthFirstTraversalTest() {
		int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 0, 0 } };
		D32_Graph tempGraph = new D32_Graph(tempMatrix);
		System.out.println(tempGraph);
		String tempSequence = "";
		try {
			tempSequence = tempGraph.depthFirstTraversal(0);
		} catch (Exception ee) {
			System.out.println(ee);
		}
		System.out.println("图的深度优先遍历访问顺序:: " + tempSequence);
	}

	public void coloring(int paraNumColors) {
		int tempNumNodes = connectivityMatrix.getRows();
		int[] tempColorScheme = new int[tempNumNodes];
		Arrays.fill(tempColorScheme, -1);
		coloring(paraNumColors, 0, tempColorScheme);
	}

	public void coloring(int paraNumColors, int paraCurrentNumNodes, int[] paraCurrentColoring) {
		int tempNumNodes = connectivityMatrix.getRows();
		System.out.println("着色:色号 = " + paraNumColors + ", 当前节点色号 = "
				+ paraCurrentNumNodes + ", 当前颜色为" + Arrays.toString(paraCurrentColoring));
		if (paraCurrentNumNodes >= tempNumNodes) {
			System.out.println("找到颜色:" + Arrays.toString(paraCurrentColoring));
			return;
		}
		for (int i = 0; i < paraNumColors; i++) {
			paraCurrentColoring[paraCurrentNumNodes] = i;
			if (!colorConflict(paraCurrentNumNodes + 1, paraCurrentColoring)) {
				coloring(paraNumColors, paraCurrentNumNodes + 1, paraCurrentColoring);
			}
		}
	}

	public boolean colorConflict(int paraCurrentNumNodes, int[] paraColoring) {
		for (int i = 0; i < paraCurrentNumNodes - 1; i++) {
			if (connectivityMatrix.getValue(paraCurrentNumNodes - 1, i) == 0) {
				continue;
			}
			if (paraColoring[paraCurrentNumNodes - 1] == paraColoring[i]) {
				return true;
			}
		}
		return false;
	}

	public static void coloringTest() {
		int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 0, 0 } };
		D32_Graph tempGraph = new D32_Graph(tempMatrix);
		tempGraph.coloring(3);
	}
}

着色部分代码:

package java31to40;

public class D35_Colour {
	public static void main(String[] args) {
		System.out.println("Hello!");
		D32_Graph tempGraph = new D32_Graph(3);
		System.out.println(tempGraph);
		D32_Graph.getConnectivityTest();
		D32_Graph.breadthFirstTraversalTest();
		D32_Graph.depthFirstTraversalTest();
		D32_Graph.coloringTest();
	}
}

结果输出:

Hello!
着色:色号 = 3, 当前节点色号 = 0, 当前颜色为[-1, -1, -1, -1]
着色:色号 = 3, 当前节点色号 = 1, 当前颜色为[0, -1, -1, -1]
着色:色号 = 3, 当前节点色号 = 2, 当前颜色为[0, 1, -1, -1]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[0, 1, 1, -1]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 1, 1, 0]
找到颜色:[0, 1, 1, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 1, 1, 2]
找到颜色:[0, 1, 1, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[0, 1, 2, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 1, 2, 0]
找到颜色:[0, 1, 2, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 1, 2, 2]
找到颜色:[0, 1, 2, 2]
着色:色号 = 3, 当前节点色号 = 2, 当前颜色为[0, 2, 2, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[0, 2, 1, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 2, 1, 0]
找到颜色:[0, 2, 1, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 2, 1, 1]
找到颜色:[0, 2, 1, 1]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[0, 2, 2, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 2, 2, 0]
找到颜色:[0, 2, 2, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[0, 2, 2, 1]
找到颜色:[0, 2, 2, 1]
着色:色号 = 3, 当前节点色号 = 1, 当前颜色为[1, 2, 2, 2]
着色:色号 = 3, 当前节点色号 = 2, 当前颜色为[1, 0, 2, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[1, 0, 0, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 0, 0, 1]
找到颜色:[1, 0, 0, 1]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 0, 0, 2]
找到颜色:[1, 0, 0, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[1, 0, 2, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 0, 2, 1]
找到颜色:[1, 0, 2, 1]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 0, 2, 2]
找到颜色:[1, 0, 2, 2]
着色:色号 = 3, 当前节点色号 = 2, 当前颜色为[1, 2, 2, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[1, 2, 0, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 2, 0, 0]
找到颜色:[1, 2, 0, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 2, 0, 1]
找到颜色:[1, 2, 0, 1]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[1, 2, 2, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 2, 2, 0]
找到颜色:[1, 2, 2, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[1, 2, 2, 1]
找到颜色:[1, 2, 2, 1]
着色:色号 = 3, 当前节点色号 = 1, 当前颜色为[2, 2, 2, 2]
着色:色号 = 3, 当前节点色号 = 2, 当前颜色为[2, 0, 2, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[2, 0, 0, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 0, 0, 1]
找到颜色:[2, 0, 0, 1]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 0, 0, 2]
找到颜色:[2, 0, 0, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[2, 0, 1, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 0, 1, 1]
找到颜色:[2, 0, 1, 1]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 0, 1, 2]
找到颜色:[2, 0, 1, 2]
着色:色号 = 3, 当前节点色号 = 2, 当前颜色为[2, 1, 2, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[2, 1, 0, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 1, 0, 0]
找到颜色:[2, 1, 0, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 1, 0, 2]
找到颜色:[2, 1, 0, 2]
着色:色号 = 3, 当前节点色号 = 3, 当前颜色为[2, 1, 1, 2]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 1, 1, 0]
找到颜色:[2, 1, 1, 0]
着色:色号 = 3, 当前节点色号 = 4, 当前颜色为[2, 1, 1, 2]
找到颜色:[2, 1, 1, 2]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值