0
点赞
收藏
分享

微信扫一扫

day 14

婉殇成长笔记 2022-04-23 阅读 156
java

第 34 天: 图的深度优先遍历

  1. 与二叉树的深度优先遍历类似. 但要难一点.
  2.  while (true)退出条件是栈为空.
  3. 需要在前面 import ObjectStack.
  4. 按例子自己理一遍逻辑会清晰很多.
    package day30;
    import day30.IntMatrix;
    import day20.ObjectStack;
    import day20.CircleObjectQueue;;
    public class Graph {
    
    	IntMatrix connectivityMatrix;
    	
    	public Graph(int paraNumbers) {
    		connectivityMatrix = new IntMatrix(paraNumbers,paraNumbers);
    	}// Of the first constructor
    	
    	public Graph(int[][] paraMatrix) {
    		connectivityMatrix = new IntMatrix(paraMatrix);
    	}// Of the second constructor
    	
    	public String toString() {
    		String resultString = "This is the connectivity matrix of the graph.\r\n" + connectivityMatrix;
    	    return resultString;
    	}//Of toString
    	
    	public boolean getConnectivity()throws Exception{
    		IntMatrix tempConnectivityMatrix = IntMatrix.getIdentityMatrix(connectivityMatrix.getData().length);
    		
    		IntMatrix tempMultipliedMatrix = new IntMatrix(connectivityMatrix);
    		
    		for(int i = 0; i < connectivityMatrix.getData().length - 1; i++) {
    			tempConnectivityMatrix.add(tempMultipliedMatrix);
    			tempMultipliedMatrix = IntMatrix.multiply(tempMultipliedMatrix, connectivityMatrix);
    		}//Of for i
    		System.out.println("The connectivity matrix is: " + 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("Node " + i + " cannot reach " + j);
    					return false;
    				}// Of if
    			} // Of for j
    		} // Of for i
    		
    		return true;
    	}// Of getConnectivity
    	
    	public static void getConnectivityTest() {
    		int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
    		Graph tempGraph2 = new Graph(tempMatrix);
    		System.out.println(tempGraph2);
    		
    		boolean tempConnected = false;
    		try {
    			tempConnected = tempGraph2.getConnectivity();
    		} catch (Exception ee) {
    			System.out.println(ee);
    		} // Of try
    		System.out.println("Is the graph connected? " + tempConnected);
    		tempGraph2.connectivityMatrix.setValue(1, 0, 0);
    		tempConnected = false;
    		try {
    			tempConnected = tempGraph2.getConnectivity();
    		} catch (Exception ee) {
    			System.out.println(ee);
    		}//Of try
    		System.out.println("Is the graph connected? " + tempConnected);
    	}// Of getConnectivityTest
    	
    	public String breadthFirstTraversal(int paraStartIndex) {
    		CircleObjectQueue tempQueue = new CircleObjectQueue();
    		String resultString = "";
    		
    		int tempNumNodes = connectivityMatrix.getRows();
    		boolean[] tempVisitedArray = new boolean[tempNumNodes];
    		
    		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;
    		    	}//Of if
    		    	if(connectivityMatrix.getData()[tempIndex][i] == 0) {
    		    		continue;
    		    	}//Of if
    		    	tempVisitedArray[i] = true;
    				resultString += i;
    				tempQueue.enqueue(new Integer(i));
    		    }//Of for i
    			
    			tempInteger = (Integer)tempQueue.dequeue();
    		}//Of while
    		return resultString;
    	}//Of breadthFirstTraversal
    	
    	public static void breadthFirstTraversalTest() {
    		int[][] tempMatrix =  { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1}, { 0, 1, 1, 0} };
    		Graph tempGraph = new Graph(tempMatrix);
    		System.out.println(tempGraph);
    		
    		String tempSequence = "";
    		try {
    			tempSequence = tempGraph.breadthFirstTraversal(2);
    		} catch (Exception ee) {
    			System.out.println(ee);
    		} // Of try
    		System.out.println("The breadth first order of visit: " + tempSequence);
    	}//Of breadthFirstTraversalTest
    	
    	public String depthFirstTraversal(int paraStartIndex) {
    		ObjectStack tempStack = new 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; //Already visited.
    				}//Of if
    				if (connectivityMatrix.getData()[tempIndex][i] == 0) {
    					continue; //Not directly connected.
    				}//Of if
    				tempVisitedArray[i] = true;
    				resultString += i;
    				tempStack.push(new Integer(i));
    				System.out.println("Push " + i);
    				tempNext = i;
    				break;
    			}//Of for i
    			
    			if(tempNext == -1) {
    				tempInteger = (Integer) tempStack.pop();
    				System.out.println("Pop " + tempInteger);
    				if (tempStack.isEmpty()) {
    					break;
    				}else {
    					tempInteger = (Integer) tempStack.pop();
    					tempIndex = tempInteger.intValue();
    					tempStack.push(tempInteger);
    				}// Of if
    			} else {
    				tempIndex = tempNext;
    			} // Of if
    		}//Of while
    		return resultString;
    	}//Of depthFirstTraversal
    	
    	public static void depthFirstTraversalTest() {
    		// Test an undirected graph.
    		int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 0}, { 0, 1, 0, 0} };
    		Graph tempGraph = new Graph(tempMatrix);
    		System.out.println(tempGraph);
    
    		String tempSequence = "";
    		try {
    			tempSequence = tempGraph.depthFirstTraversal(0);
    		} catch (Exception ee) {
    			System.out.println(ee);
    		} // Of try.
    
    		System.out.println("The depth first order of visit: " + tempSequence);
    	}//Of depthFirstTraversalTest
    	
    	public static void main(String[] args) {
    		System.out.println("Hello!");
    		Graph tempGraph = new Graph(3);
    		System.out.println(tempGraph);
    		getConnectivityTest();
    		breadthFirstTraversalTest();
    		depthFirstTraversalTest();
    	}//Of main
    
    }// Of class Graph

    运行截图:

 

 

 35 天: 图的 m 着色问题

package day30;
import day30.IntMatrix;
import day20.ObjectStack;

import java.util.Arrays;

import day20.CircleObjectQueue;;
public class Graph {

	IntMatrix connectivityMatrix;
	
	public Graph(int paraNumbers) {
		connectivityMatrix = new IntMatrix(paraNumbers,paraNumbers);
	}// Of the first constructor
	
	public Graph(int[][] paraMatrix) {
		connectivityMatrix = new IntMatrix(paraMatrix);
	}// Of the second constructor
	
	public String toString() {
		String resultString = "This is the connectivity matrix of the graph.\r\n" + connectivityMatrix;
	    return resultString;
	}//Of toString
	
	public boolean getConnectivity()throws Exception{
		IntMatrix tempConnectivityMatrix = IntMatrix.getIdentityMatrix(connectivityMatrix.getData().length);
		
		IntMatrix tempMultipliedMatrix = new IntMatrix(connectivityMatrix);
		
		for(int i = 0; i < connectivityMatrix.getData().length - 1; i++) {
			tempConnectivityMatrix.add(tempMultipliedMatrix);
			tempMultipliedMatrix = IntMatrix.multiply(tempMultipliedMatrix, connectivityMatrix);
		}//Of for i
		System.out.println("The connectivity matrix is: " + 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("Node " + i + " cannot reach " + j);
					return false;
				}// Of if
			} // Of for j
		} // Of for i
		
		return true;
	}// Of getConnectivity
	
	public static void getConnectivityTest() {
		int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } };
		Graph tempGraph2 = new Graph(tempMatrix);
		System.out.println(tempGraph2);
		
		boolean tempConnected = false;
		try {
			tempConnected = tempGraph2.getConnectivity();
		} catch (Exception ee) {
			System.out.println(ee);
		} // Of try
		System.out.println("Is the graph connected? " + tempConnected);
		tempGraph2.connectivityMatrix.setValue(1, 0, 0);
		tempConnected = false;
		try {
			tempConnected = tempGraph2.getConnectivity();
		} catch (Exception ee) {
			System.out.println(ee);
		}//Of try
		System.out.println("Is the graph connected? " + tempConnected);
	}// Of getConnectivityTest
	
	public String breadthFirstTraversal(int paraStartIndex) {
		CircleObjectQueue tempQueue = new CircleObjectQueue();
		String resultString = "";
		
		int tempNumNodes = connectivityMatrix.getRows();
		boolean[] tempVisitedArray = new boolean[tempNumNodes];
		
		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;
		    	}//Of if
		    	if(connectivityMatrix.getData()[tempIndex][i] == 0) {
		    		continue;
		    	}//Of if
		    	tempVisitedArray[i] = true;
				resultString += i;
				tempQueue.enqueue(new Integer(i));
		    }//Of for i
			
			tempInteger = (Integer)tempQueue.dequeue();
		}//Of while
		return resultString;
	}//Of breadthFirstTraversal
	
	public static void breadthFirstTraversalTest() {
		int[][] tempMatrix =  { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1}, { 0, 1, 1, 0} };
		Graph tempGraph = new Graph(tempMatrix);
		System.out.println(tempGraph);
		
		String tempSequence = "";
		try {
			tempSequence = tempGraph.breadthFirstTraversal(2);
		} catch (Exception ee) {
			System.out.println(ee);
		} // Of try
		System.out.println("The breadth first order of visit: " + tempSequence);
	}//Of breadthFirstTraversalTest
	
	public String depthFirstTraversal(int paraStartIndex) {
		ObjectStack tempStack = new 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; //Already visited.
				}//Of if
				if (connectivityMatrix.getData()[tempIndex][i] == 0) {
					continue; //Not directly connected.
				}//Of if
				tempVisitedArray[i] = true;
				resultString += i;
				tempStack.push(new Integer(i));
				System.out.println("Push " + i);
				tempNext = i;
				break;
			}//Of for i
			
			if(tempNext == -1) {
				tempInteger = (Integer) tempStack.pop();
				System.out.println("Pop " + tempInteger);
				if (tempStack.isEmpty()) {
					break;
				}else {
					tempInteger = (Integer) tempStack.pop();
					tempIndex = tempInteger.intValue();
					tempStack.push(tempInteger);
				}// Of if
			} else {
				tempIndex = tempNext;
			} // Of if
		}//Of while
		return resultString;
	}//Of depthFirstTraversal
	
	public static void depthFirstTraversalTest() {
		// Test an undirected graph.
		int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 0}, { 0, 1, 0, 0} };
		Graph tempGraph = new Graph(tempMatrix);
		System.out.println(tempGraph);

		String tempSequence = "";
		try {
			tempSequence = tempGraph.depthFirstTraversal(0);
		} catch (Exception ee) {
			System.out.println(ee);
		} // Of try.

		System.out.println("The depth first order of visit: " + tempSequence);
	}//Of depthFirstTraversalTest
	
	public void coloring(int paraNumColors) {
		int tempNumNodes = connectivityMatrix.getRows();
		int[] tempColorScheme = new int[tempNumNodes];
		Arrays.fill(tempColorScheme, -1);
	    
		coloring(paraNumColors, 0, tempColorScheme);
	}// Of coloring
	
	public void coloring(int paraNumColors, int paraCurrentNumNodes, int[] paraCurrentColoring) {
		// Step 1. Initialize.
		int tempNumNodes = connectivityMatrix.getRows();
				
		System.out.println("coloring: paraNumColors = " + paraNumColors + ", paraCurrentNumNodes = " + paraCurrentNumNodes + ", paraCurrentColoring" + Arrays.toString(paraCurrentColoring));
		// A complete scheme.
		if (paraCurrentNumNodes >= tempNumNodes) {
			System.out.println("Find one:" + Arrays.toString(paraCurrentColoring));
			return;
		} // Of if
		for(int i = 0; i < paraNumColors; i++) {
			paraCurrentColoring[paraCurrentNumNodes] = i;
			if (!colorConflict(paraCurrentNumNodes + 1, paraCurrentColoring)) {
				coloring(paraNumColors, paraCurrentNumNodes + 1, paraCurrentColoring);
			} // Of if
		} // Of for i
	}// Of coloring

	public boolean colorConflict(int paraCurrentNumNodes, int[] paraColoring) {
		for (int i = 0; i < paraCurrentNumNodes - 1; i++) {
			// No direct connection.
			if (connectivityMatrix.getValue(paraCurrentNumNodes - 1, i) == 0) {
				continue;
			} // Of if

			if (paraColoring[paraCurrentNumNodes - 1] == paraColoring[i]) {
				return true;
			} // Of if
		} // Of for i
		return false;
	}// Of colorConflict

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

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

}// Of class Graph

运行截图:

 图的代码一直都有点复杂,要多加练习和复习。

举报

相关推荐

day_14

day14

Python-day14

day14-异常

Day14 IO

day14--栈

Oracle day14

DAY 14 | 自学前端第14天

Day 14 Homework2

0 条评论