/** ********************* * Coloring. Output all possible schemes. * * @param paraNumColors The number of colors. ********************* */ publicvoidcoloring(int paraNumColors) { // Step 1. Initialize. inttempNumNodes= connectivityMatrix.getRows(); int[] tempColorScheme = newint[tempNumNodes]; Arrays.fill(tempColorScheme, -1);
coloring(paraNumColors, 0, tempColorScheme); }// Of coloring
/** ********************* * Coloring. Output all possible schemes. * * @param paraNumColors The number of colors. * @param paraCurrentNumNodes The number of nodes that have been colored. * @param paraCurrentColoring The array recording the coloring scheme. ********************* */ publicvoidcoloring(int paraNumColors, int paraCurrentNumNodes, int[] paraCurrentColoring) { // Step 1. Initialize. inttempNumNodes= 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
// Try all possible colors. for (inti=0; i < paraNumColors; i++) { paraCurrentColoring[paraCurrentNumNodes] = i; if (!colorConflict(paraCurrentNumNodes + 1, paraCurrentColoring)) { coloring(paraNumColors, paraCurrentNumNodes + 1, paraCurrentColoring); } // Of if } // Of for i }// Of coloring
/** ********************* * Coloring conflict or not. Only compare the current last node with previous * ones. * * @param paraCurrentNumNodes The current number of nodes. * @param paraColoring The current coloring scheme. * @return Conflict or not. ********************* */ publicbooleancolorConflict(int paraCurrentNumNodes, int[] paraColoring) { for (inti=0; i < paraCurrentNumNodes - 1; i++) { // No direct connection. if (connectivityMatrix.getValue(paraCurrentNumNodes - 1, i) == 0) { continue; } // Of if
if (paraColoring[paraCurrentNumNodes - 1] == paraColoring[i]) { returntrue; } // Of if } // Of for i returnfalse; }// Of colorConflict
/** * Adjacency list for directed graph. * * @author ShiHuai Wen shihuaiwen@outlook.com. */ publicclassAdjacencyList { /** * An inner class for adjacent node. */ classAdjacencyNode { /** * The column index. */ int column;
/** * The next adjacent node. */ AdjacencyNode next;
/** ********************* * The first constructor. * * @param paraColumn The column. ********************* */ publicAdjacencyNode(int paraColumn) { column = paraColumn; next = null; }// Of AdjacencyNode }// Of class AdjacencyNode
/** * The number of nodes. This member variable may be redundant since it is always * equal to headers.length. */ int numNodes;
/** * The headers for each row. */ AdjacencyNode[] headers;
/** ********************* * The first constructor. * * @param paraMatrix The the matrix indicating the graph. ********************* */ publicAdjacencyList(int[][] paraMatrix) { numNodes = paraMatrix.length;
// Step 1. Initialize. The data in the headers are not meaningful. AdjacencyNode tempPreviousNode, tempNode;
headers = newAdjacencyNode[numNodes]; for (inti=0; i < numNodes; i++) { headers[i] = newAdjacencyNode(-1); tempPreviousNode = headers[i]; for (intj=0; j < numNodes; j++) { if (paraMatrix[i][j] == 0) { continue; } // Of if
// Create a new node. tempNode = newAdjacencyNode(j);
// Link. tempPreviousNode.next = tempNode; tempPreviousNode = tempNode; } // Of for j } // Of for i }// Of class AdjacentTable
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ public String toString() { StringresultString="";
AdjacencyNode tempNode; for (inti=0; i < numNodes; i++) { tempNode = headers[i].next;
while (tempNode != null) { resultString += " (" + i + ", " + tempNode.column + ")"; tempNode = tempNode.next; } // Of while resultString += "\r\n"; } // Of for i
return resultString; }// Of toString
/** ********************* * Breadth first traversal. * * @param paraStartIndex The start index. * @return The sequence of the visit. ********************* */ public String breadthFirstTraversal(int paraStartIndex) { CircleObjectQueuetempQueue=newCircleObjectQueue(); StringresultString="";
// Initialize the queue. // Visit before enqueue. tempVisitedArray[paraStartIndex] = true; resultString += paraStartIndex; tempQueue.enqueue(Integer.valueOf(paraStartIndex));
// Now visit the rest of the graph. int tempIndex; IntegertempInteger= (Integer) tempQueue.dequeue(); AdjacencyNode tempNode; while (tempInteger != null) { tempIndex = tempInteger.intValue();
// Enqueue all its unvisited neighbors. The neighbors are linked // already. tempNode = headers[tempIndex].next; while (tempNode != null) { if (!tempVisitedArray[tempNode.column]) { // Visit before enqueue. tempVisitedArray[tempNode.column] = true; resultString += tempNode.column; tempQueue.enqueue(Integer.valueOf(tempNode.column)); } // Of if tempNode = tempNode.next; } // Of for i
// Take out one from the head. tempInteger = (Integer) tempQueue.dequeue(); } // Of while
return resultString; }// Of breadthFirstTraversal
/** ********************* * Unit test for breadthFirstTraversal. The same as the one in class Graph. ********************* */ publicstaticvoidbreadthFirstTraversalTest() { // Test an undirected graph. int[][] tempMatrix = { { 0, 1, 1, 0 }, { 1, 0, 0, 1 }, { 1, 0, 0, 1 }, { 0, 1, 1, 0 } }; GraphtempGraph=newGraph(tempMatrix); System.out.println(tempGraph); AdjacencyListtempAdjList=newAdjacencyList(tempMatrix);