欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java学习第34天,图的深度优先遍历

程序员文章站 2022-05-24 09:49:47
...

与二叉树的深度优先遍历类似。不过比二叉树的深度优先遍历要复杂一些。
1,使用到了图连通性检测代码。

package java31to40;

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 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;
	}
}

2,图的深度优先遍历代码

package java31to40;

public class D34_DepthFirstTraversal {
	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 static void main(String[] args) {
		System.out.println("Hello!");
		D32_Graph tempGraph = new D32_Graph(3);
		System.out.println(tempGraph);
		D32_Graph.getConnectivityTest();
		D33_BreadthFirstTraversal.breadthFirstTraversalTest();
		depthFirstTraversalTest();
	}
}

结果输出:

Hello!
图的连通性矩阵.
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
图的连通性矩阵.
[[0, 1, 0], [1, 0, 1], [0, 1, 0]]
连通性矩阵为: [[2, 1, 1], [1, 3, 1], [1, 1, 2]]
图是否具有连通性? true
连通性矩阵为: [[1, 1, 1], [0, 2, 1], [0, 1, 2]]
节点 1 不能到达 0
图是否具有连通性? false
图的连通性矩阵.
[[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]]
图的广度优先遍历访问顺序: 2031
图的连通性矩阵.
[[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]]
Push 0
Visited 0
Push 1
Push 3
Pop 3
Pop 1
Pop 0
Push 2
Pop 2
图的深度优先遍历访问顺序:: 0132