本文共 30654 字,大约阅读时间需要 102 分钟。
广度优先搜索遍历过程
(1)从某个顶点V出发,访问该顶点的所有邻接点V1,V2…VN
(2)从邻接点V1,V2…VN出发,再访问他们各自的所有邻接点
(3)重复上述步骤,直到所有的顶点都被访问过
若此时图中还有顶点未被访问,则在外控算法的控制下,另选一个未曾被访问的顶点作为起始点,重复上述过程,直到图中所有顶点都被访问完为止。
/** ********************* * Breadth first traversal. * * @param paraStartIndex The start index. * @return The sequence of the visit. ********************* */ public String breadthFirstTraversal(int paraStartIndex) { CircleObjectQueue tempQueue = new CircleObjectQueue(); String resultString = ""; int tempNumNodes = connectivityMatrix.getRows(); boolean[] tempVisitedArray = new boolean[tempNumNodes]; tempVisitedArray[paraStartIndex] = true; //Initialize the queue. //Visit before enqueue. tempVisitedArray[paraStartIndex] = true; resultString += paraStartIndex; tempQueue.enqueue(new Integer(paraStartIndex)); //Now visit the rest of the graph. int tempIndex; Integer tempInteger = (Integer)tempQueue.dequeue(); while (tempInteger != null) { tempIndex = tempInteger.intValue(); //Enqueue all its unvisited neighbors. 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 //Visit before enqueue. tempVisitedArray[i] = true; resultString += i; tempQueue.enqueue(new Integer(i)); }//Of for i //Take out one from the head. tempInteger = (Integer)tempQueue.dequeue(); }//Of while return resultString; }//Of breadthFirstTraversal /** ********************* * Unit test for breadthFirstTraversal. ********************* */ public static void breadthFirstTraversalTest() { // Test an undirected graph. 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 /** ********************* * The entrance of the program. * * @param args * Not used now. ********************* */ public static void main(String args[]) { System.out.println("Hello!"); Graph tempGraph = new Graph(3); System.out.println(tempGraph); // Unit test. getConnectivityTest(); breadthFirstTraversalTest(); }// Of main
是一头扎到底的玩法。我们选择一条支路,尽可能不断地深入,如果遇到死路就往回退,回退过程中如果遇到没探索过的支路,就进入该支路继续深入。
在图中,我们首先选择景点1的这条路,继续深入到景点4、景点5、景点3、景点6,终于发现走不动了 于是,我们退回到景点1,然后探索景点7,景点8,又走到了死胡同。于是,退回到景点7,探索景点10: 按照这个思路,我们再退回到景点1,探索景点9,最后再退回到景点0,后续依次探索景点2,终于玩遍了整个游乐场:/** ********************* * Depth first traversal. * * @param paraStartIndex The start index. * @return The sequence of the visit. ********************* */ public String depthFirstTraversal(int paraStartIndex) { ObjectStack tempStack = new ObjectStack(); String resultString = ""; int tempNumNodes = connectivityMatrix.getRows(); boolean[] tempVisitedArray = new boolean[tempNumNodes]; //Initialize the stack. //Visit before push. tempVisitedArray[paraStartIndex] = true; resultString += paraStartIndex; tempStack.push(new Integer(paraStartIndex)); System.out.println("Push " + paraStartIndex); System.out.println("Visited " + resultString); //Now visit the rest of the graph. int tempIndex = paraStartIndex; int tempNext; Integer tempInteger; while (true){ //Find an unvisited neighbor. 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 //Visit this one. tempVisitedArray[i] = true; resultString += i; tempStack.push(new Integer(i)); System.out.println("Push " + i); tempNext = i; //One is enough. break; }//Of for i if (tempNext == -1) { //No unvisited neighbor. Backtracking to the last one stored in the stack. //Attention: This is the terminate condition! if (tempStack.isEmpty()) { break; }//Of if tempInteger = (Integer)tempStack.pop(); System.out.println("Pop " + tempInteger); tempIndex = tempInteger.intValue(); } else { tempIndex = tempNext; }//Of if }//Of while return resultString; }//Of depthFirstTraversal /** ********************* * Unit test for 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 /** ********************* * The entrance of the program. * * @param args * Not used now. ********************* */ public static void main(String args[]) { System.out.println("Hello!"); Graph tempGraph = new Graph(3); System.out.println(tempGraph); // Unit test. getConnectivityTest(); breadthFirstTraversalTest(); depthFirstTraversalTest(); }// Of main
图的m-着色判定问题——给定无向连通图G和m种不同的颜色。用这些颜色为图G的各顶点着色,每个顶点着一种颜色,是否有一种着色法使G中任意相邻的2个顶点着不同颜色?
图的m-着色优化问题——若一个图最少需要m种颜色才能使图中任意相邻的2个顶点着不同颜色,则称这个数m为该图的色数。求一个图的最小色数m的问题称为m-着色优化问题。/** ********************* * Coloring. Output all possible schemes. * * @param paraNumColors The number of colors. ********************* */ public void coloring(int paraNumColors) { // Step 1. Initialize. int tempNumNodes = connectivityMatrix.getRows(); int[] tempColorScheme = new int[tempNumNodes]; Arrays.fill(tempColorScheme, -1); coloring(paraNumColors, 0, tempColorScheme); }// Of coloring /** ********************* * Coloring. Output all possible approaches. * * @param paraNumColors The number of colors. * @return The sequence of the visit. ********************* */ 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 // Try all possible colors. 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 /** ********************* * 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. ********************* */ 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 /** ********************* * Coloring test. ********************* */ 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 /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String args[]) { System.out.println("Hello!"); Graph tempGraph = new Graph(3); System.out.println(tempGraph); // Unit test. getConnectivityTest(); breadthFirstTraversalTest(); depthFirstTraversalTest(); coloringTest(); }// Of main
邻接表中每个单链表的第一个结点存放有关顶点的信息,把这一结点看成链表的表头,其余结点存放有关边的信息,这样邻接表便由两部分组成:表头结点和边表
顶点: 按编号顺序将顶点数据存储在一维数组中 关联同一顶点的边(以顶点为尾的弧): 用线性链表存储无向图的邻接表
特点:
有向图的邻接表
邻接表特点:
逆邻接表特点:
package datastructure.graph;import datastructure.queue.CircleObjectQueue;/** * Adjacent table for directed graph. * * @author hengyuzuo. */public class AdjacencyList { /** * An inner class for adjacent node. */ class AdjacencyNode { /** * The column index. */ int column; /** * The next adjacent node. */ AdjacencyNode next; /** ********************* * The first constructor. * * @param paraColumn The column. ********************* */ public AdjacencyNode(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. ********************* */ public AdjacencyList(int[][] paraMatrix) { numNodes = paraMatrix.length; // Step 1. Initialize. The data in the headers are not meaningful. AdjacencyNode tempPreviousNode, tempNode; headers = new AdjacencyNode[numNodes]; for (int i = 0; i < numNodes; i++) { headers[i] = new AdjacencyNode(-1); tempPreviousNode = headers[i]; for (int j = 0; j < numNodes; j++) { if (paraMatrix[i][j] == 0) { continue; } // Of if // Create a new node. tempNode = new AdjacencyNode(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() { String resultString = ""; AdjacencyNode tempNode; for (int i = 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) { CircleObjectQueue tempQueue = new CircleObjectQueue(); String resultString = ""; boolean[] tempVisitedArray = new boolean[numNodes]; tempVisitedArray[paraStartIndex] = true; // Initialize the queue. // Visit before enqueue. tempVisitedArray[paraStartIndex] = true; resultString += paraStartIndex; tempQueue.enqueue(new Integer(paraStartIndex)); // Now visit the rest of the graph. int tempIndex; Integer tempInteger = (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]) { continue; // Already visited. } // Of if // Visit before enqueue. tempVisitedArray[tempNode.column] = true; resultString += tempNode.column; tempQueue.enqueue(new Integer(tempNode.column)); 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. ********************* */ public static void breadthFirstTraversalTest() { // Test an undirected graph. 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 /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String args[]) { int[][] tempMatrix = { { 0, 1, 0 }, { 1, 0, 1 }, { 0, 1, 0 } }; AdjacencyList tempTable = new AdjacencyList(tempMatrix); System.out.println("The data are:\r\n" + tempTable); breadthFirstTraversalTest(); }// Of main}// Of class AdjacentTable
为了解决有向图和无向图的缺点,我们又有了十字链表和邻接多重表
十字链表是有向图的另一种链式存储结构。我们也可以把它看成是有向图的邻接表和逆邻接表结合起来形成的一种链表。 有向图中的每一条弧对应十字链表的一个弧结点,同时有向图中的每个顶点在十字链表中对应有一个结点,叫做顶点结点package datastructure.graph;/** * Orthogonal List for directed graph. * * @author hengyuzuo. */public class OrthogonalList { /** * An inner class for adjacent node. */ class OrthogonalNode { /** * The row index. */ int row; /** * The column index. */ int column; /** * The next out node. */ OrthogonalNode nextOut; /** * The next in node. */ OrthogonalNode nextIn; /** ********************* * The first constructor. * * @param paraRow The row. * @param paraColumn The column. ********************* */ public OrthogonalNode(int paraRow, int paraColumn) { row = paraRow; column = paraColumn; nextOut = null; nextIn = null; }// Of OrthogonalNode }// Of class OrthogonalNode /** * 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. */ OrthogonalNode[] headers; /** ********************* * The first constructor. * * @param paraMatrix The matrix indicating the graph. ********************* */ public OrthogonalList(int[][] paraMatrix) { numNodes = paraMatrix.length; // Step 1. Initialize. The data in the headers are not meaningful. OrthogonalNode tempPreviousNode, tempNode; headers = new OrthogonalNode[numNodes]; // Step 2. Link to its out nodes. for (int i = 0; i < numNodes; i++) { headers[i] = new OrthogonalNode(i, -1); tempPreviousNode = headers[i]; for (int j = 0; j < numNodes; j++) { if (paraMatrix[i][j] == 0) { continue; } // Of if // Create a new node. tempNode = new OrthogonalNode(i, j); // Link. tempPreviousNode.nextOut = tempNode; tempPreviousNode = tempNode; } // Of for j } // Of for i // Step 3. Link to its in nodes. This step is harder. OrthogonalNode[] tempColumnNodes = new OrthogonalNode[numNodes]; for (int i = 0; i < numNodes; i++) { tempColumnNodes[i] = headers[i]; } // Of for i for (int i = 0; i < numNodes; i++) { tempNode = headers[i].nextOut; while (tempNode != null) { tempColumnNodes[tempNode.column].nextIn = tempNode; tempColumnNodes[tempNode.column] = tempNode; tempNode = tempNode.nextOut; } // Of while } // Of for i }// Of the constructor /** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ public String toString() { String resultString = "Out arcs: "; OrthogonalNode tempNode; for (int i = 0; i < numNodes; i++) { tempNode = headers[i].nextOut; while (tempNode != null) { resultString += " (" + tempNode.row + ", " + tempNode.column + ")"; tempNode = tempNode.nextOut; } // Of while resultString += "\r\n"; } // Of for i resultString += "\r\nIn arcs: "; for (int i = 0; i < numNodes; i++) { tempNode = headers[i].nextIn; while (tempNode != null) { resultString += " (" + tempNode.row + ", " + tempNode.column + ")"; tempNode = tempNode.nextIn; } // Of while resultString += "\r\n"; } // Of for i return resultString; }// Of toString /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String args[]) { int[][] tempMatrix = { { 0, 1, 0, 0 }, { 0, 0, 0, 1 }, { 1, 0, 0, 0 }, { 0, 1, 1, 0 } }; OrthogonalList tempList = new OrthogonalList(tempMatrix); System.out.println("The data are:\r\n" + tempList); }// Of main}// Of class OrthogonalList
prim算法
prim算法是一个最小生成树算法,它运用的是贪心原理(在这里不再证明),设置两个点集合,一个集合为要求的生成树的点集合A,另一个集合为未加入生成树的点B,它的具体实现过程是:第1步:所有的点都在集合B中,A集合为空。
第2步:任意以一个点为开始,把这个初始点加入集合A中,从集合B中减去这个点(代码实现很简单,也就是设置一个标示数组,为false表示这个点在B中,为true表示这个点在A中),寻找与它相邻的点中路径最短的点,如后把这个点也加入集合A中,从集合B中减去这个点(代码实现同上)。 第3步:集合A中已经有了多个点,这时两个集合A和B,只要找到A集合中的点到B集合中的点的最短边,可以是A集合中的与B集合中的点的任意组合,把这条最短边有两个顶点,把在集合B中的顶点加入到集合A中,(代码实现的时候有点技巧,不需要枚举所有情况,也就是更新操作)。 第4步:重复上述过程。一直到所有的点都在A集合中结束。Dijkstra 算法
它的目的是求某个源点到目的点的最短距离,总的来说,dijkstra算法也就是求某个源点到目的点的最短路,求解的过程也就是求源点到整个图的最短距离,次短距离,第三短距离等等(这些距离都是源点到某个点的最短距离)。。。。。求出来的每个距离都对应着一个点,也就是到这个点的最短距离,求的也就是原点到所有点的最短距离,并且存在了一个二维数组中,最后给出目的点就能直接通过查表获得最短距离。 第1步:以源点(假设是s1)为开始点,求最短距离,如何求呢? 与这个源点相邻的点与源点的距离全部放在一个数组dist[]中,如果不可达,dist[]中为最大值,这里说一下,为什么要是一维数组,原因是默认的是从源点到这个一维数组下标的值,只需要目的点作为下标就可以,这时从源点到其他点的最短的“一”条路径有了,只要选出dist[]中最小的就行(得到最短路径的另一个端点假设是s2)。 第2步:这时要寻找源点(假设是s1)到另外点的次短距离,这个距离或者是dist[]里面的值,或者是从第1步中选择的那个最短距离 + 从找到点(假设是s2)出发到其他点的距离(其实这里也是一个更新操作,更新的是dist[]里面的值),如果最短距离 + 从这点(假设是s2)到其他点的距离,小于dist[]里面的值,就可以更新dist[]数组了,然后再从dist[]数组中选一个值最小的,也就是第“二”短路径(次短路径)。 第3步:寻找第“三”短路径,这时同上,第二短路径的端点(s3)更新与之相邻其他的点的dist[]数组里面的值。 第4步:重复上述过程n - 1次(n指的是节点个数),得出结果,其实把源点到所有点的最短路径求出来了,都填在了dist[]表中,要找源点到哪个点的最短路,就只需要查表了。package datastructure.graph;import java.util.Arrays;import matrix.IntMatrix;/** * Weighted graphs are called nets. * * @author hengyuzuo. */public class Net { /** * The maximal distance. Do not use Integer.MAX_VALUE. */ public static final int MAX_DISTANCE = 10000; /** * The number of nodes. */ int numNodes; /** * The weight matrix. We use int to represent weight for simplicity. */ IntMatrix weightMatrix; /** ********************* * The first constructor. * * @param paraNumNodes * The number of nodes in the graph. ********************* */ public Net(int paraNumNodes) { numNodes = paraNumNodes; weightMatrix = new IntMatrix(numNodes, numNodes); for (int i = 0; i < numNodes; i++) { // For better readability, you may need to write fill() in class // IntMatrix. Arrays.fill(weightMatrix.getData()[i], MAX_DISTANCE); } // Of for i }// Of the first constructor /** ********************* * The second constructor. * * @param paraMatrix * The data matrix. ********************* */ public Net(int[][] paraMatrix) { weightMatrix = new IntMatrix(paraMatrix); numNodes = weightMatrix.getRows(); }// Of the second constructor /** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ public String toString() { String resultString = "This is the weight matrix of the graph.\r\n" + weightMatrix; return resultString; }// Of toString /** ********************* * The Dijkstra algorithm: shortest path from the source to all nodes. * * @param paraSource * The source node. * @return The distances to all nodes. ********************* */ public int[] dijkstra(int paraSource) { // Step 1. Initialize. int[] tempDistanceArray = new int[numNodes]; for (int i = 0; i < numNodes; i++) { tempDistanceArray[i] = weightMatrix.getValue(paraSource, i); } // Of for i int[] tempParentArray = new int[numNodes]; Arrays.fill(tempParentArray, paraSource); // -1 for no parent. tempParentArray[paraSource] = -1; // Visited nodes will not be considered further. boolean[] tempVisitedArray = new boolean[numNodes]; tempVisitedArray[paraSource] = true; // Step 2. Main loops. int tempMinDistance; int tempBestNode = -1; for (int i = 0; i < numNodes - 1; i++) { // Step 2.1 Find out the best next node. tempMinDistance = Integer.MAX_VALUE; for (int j = 0; j < numNodes; j++) { // This node is visited. if (tempVisitedArray[j]) { continue; } // Of if if (tempMinDistance > tempDistanceArray[j]) { tempMinDistance = tempDistanceArray[j]; tempBestNode = j; } // Of if } // Of for j tempVisitedArray[tempBestNode] = true; // Step 2.2 Prepare for the next round. for (int j = 0; j < numNodes; j++) { // This node is visited. if (tempVisitedArray[j]) { continue; } // Of if // This node cannot be reached. if (weightMatrix.getValue(tempBestNode, j) >= MAX_DISTANCE) { continue; } // Of if if (tempDistanceArray[j] > tempDistanceArray[tempBestNode] + weightMatrix.getValue(tempBestNode, j)) { // Change the distance. tempDistanceArray[j] = tempDistanceArray[tempBestNode] + weightMatrix.getValue(tempBestNode, j); // Change the parent. tempParentArray[j] = tempBestNode; } // Of if } // Of for j // For test System.out.println("The distance to each node: " + Arrays.toString(tempDistanceArray)); System.out.println("The parent of each node: " + Arrays.toString(tempParentArray)); } // Of for i // Step 3. Output for debug. System.out.println("Finally"); System.out.println("The distance to each node: " + Arrays.toString(tempDistanceArray)); System.out.println("The parent of each node: " + Arrays.toString(tempParentArray)); return tempDistanceArray; }// Of dijkstra /** ********************* * The minimal spanning tree. * * @return The total cost of the tree. ********************* */ public int prim() { // Step 1. Initialize. // Any node can be the source. int tempSource = 0; int[] tempDistanceArray = new int[numNodes]; for (int i = 0; i < numNodes; i++) { tempDistanceArray[i] = weightMatrix.getValue(tempSource, i); } // Of for i int[] tempParentArray = new int[numNodes]; Arrays.fill(tempParentArray, tempSource); // -1 for no parent. tempParentArray[tempSource] = -1; // Visited nodes will not be considered further. boolean[] tempVisitedArray = new boolean[numNodes]; tempVisitedArray[tempSource] = true; // Step 2. Main loops. int tempMinDistance; int tempBestNode = -1; for (int i = 0; i < numNodes - 1; i++) { // Step 2.1 Find out the best next node. tempMinDistance = Integer.MAX_VALUE; for (int j = 0; j < numNodes; j++) { // This node is visited. if (tempVisitedArray[j]) { continue; } // Of if if (tempMinDistance > tempDistanceArray[j]) { tempMinDistance = tempDistanceArray[j]; tempBestNode = j; } // Of if } // Of for j tempVisitedArray[tempBestNode] = true; // Step 2.2 Prepare for the next round. for (int j = 0; j < numNodes; j++) { // This node is visited. if (tempVisitedArray[j]) { continue; } // Of if // This node cannot be reached. if (weightMatrix.getValue(tempBestNode, j) >= MAX_DISTANCE) { continue; } // Of if // Attention: the difference from the Dijkstra algorithm. if (tempDistanceArray[j] > weightMatrix.getValue(tempBestNode, j)) { // Change the distance. tempDistanceArray[j] = weightMatrix.getValue(tempBestNode, j); // Change the parent. tempParentArray[j] = tempBestNode; } // Of if } // Of for j // For test System.out.println( "The selected distance for each node: " + Arrays.toString(tempDistanceArray)); System.out.println("The parent of each node: " + Arrays.toString(tempParentArray)); } // Of for i int resultCost = 0; for (int i = 0; i < numNodes; i++) { resultCost += tempDistanceArray[i]; } // Of for i // Step 3. Output for debug. System.out.println("Finally"); System.out.println("The parent of each node: " + Arrays.toString(tempParentArray)); System.out.println("The total cost: " + resultCost); return resultCost; }// Of prim /** ********************* * The entrance of the program. * * @param args * Not used now. ********************* */ public static void main(String args[]) { Net tempNet0 = new Net(3); System.out.println(tempNet0); int[][] tempMatrix1 = { { 0, 9, 3, 6 }, { 5, 0, 2, 4 }, { 3, 2, 0, 1 }, { 2, 8, 7, 0 } }; Net tempNet1 = new Net(tempMatrix1); System.out.println(tempNet1); // Dijkstra tempNet1.dijkstra(1); // An undirected net is required. int[][] tempMatrix2 = { { 0, 7, MAX_DISTANCE, 5, MAX_DISTANCE }, { 7, 0, 8, 9, 7 }, { MAX_DISTANCE, 8, 0, MAX_DISTANCE, 5 }, { 5, 9, MAX_DISTANCE, 0, 15 }, { MAX_DISTANCE, 7, 5, 15, 0 } }; Net tempNet2 = new Net(tempMatrix2); tempNet2.prim(); }// Of main}// Of class Net
在图论中,拓扑排序(Topological Sorting)是一个有向无环图(DAG, Directed Acyclic Graph)的所有顶点的线性序列。且该序列必须满足下面两个条件:
有向无环图(DAG)才有拓扑排序,非DAG图没有拓扑排序一说。
例如,下面这个图:
它是一个 DAG 图,那么如何写出它的拓扑排序呢?这里说一种比较常用的方法:于是,得到拓扑排序后的结果是 { 1, 2, 4, 3, 5 }。通常,一个有向无环图可以有一个或多个拓扑排序序列。
/** ********************* * Critical path. Net validity checks such as loop check not implemented. * The source should be 0 and the destination should be n-1. * * @return The node sequence of the path. ********************* */ public boolean[] criticalPath() { // One more value to save simple computation. int tempValue; // Step 1. The in-degree of each node. int[] tempInDegrees = new int[numNodes]; for (int i = 0; i < numNodes; i++) { for (int j = 0; j < numNodes; j++) { if (weightMatrix.getValue(i, j) != -1) { tempInDegrees[j]++; } // Of for if } // Of for j } // Of for i System.out.println("In-degree of nodes: " + Arrays.toString(tempInDegrees)); // Step 2. Topology sorting. int[] tempEarliestTimeArray = new int[numNodes]; for (int i = 0; i < numNodes; i++) { // This node cannot be removed. if (tempInDegrees[i] > 0) { continue; } // Of if System.out.println("Removing " + i); for (int j = 0; j < numNodes; j++) { if (weightMatrix.getValue(i, j) != -1) { tempValue = tempEarliestTimeArray[i] + weightMatrix.getValue(i, j); if (tempEarliestTimeArray[j] < tempValue) { tempEarliestTimeArray[j] = tempValue; } // Of if tempInDegrees[j]--; } // Of for if } // Of for j } // Of for i System.out.println("Earlest start time: " + Arrays.toString(tempEarliestTimeArray)); // Step 3. The out-degree of each node. int[] tempOutDegrees = new int[numNodes]; for (int i = 0; i < numNodes; i++) { for (int j = 0; j < numNodes; j++) { if (weightMatrix.getValue(i, j) != -1) { tempOutDegrees[i]++; } // Of for if } // Of for j } // Of for i System.out.println("Out-degree of nodes: " + Arrays.toString(tempOutDegrees)); // Step 4. Reverse topology sorting. int[] tempLatestTimeArray = new int[numNodes]; for (int i = 0; i < numNodes; i++) { tempLatestTimeArray[i] = tempEarliestTimeArray[numNodes - 1]; } // Of for i for (int i = numNodes - 1; i >= 0; i--) { // This node cannot be removed. if (tempOutDegrees[i] > 0) { continue; } // Of if System.out.println("Removing " + i); for (int j = 0; j < numNodes; j++) { if (weightMatrix.getValue(j, i) != -1) { tempValue = tempLatestTimeArray[i] - weightMatrix.getValue(j, i); if (tempLatestTimeArray[j] > tempValue) { tempLatestTimeArray[j] = tempValue; } // Of if tempOutDegrees[j]--; System.out.println("The out-degree of " + j + " decreases by 1."); } // Of for if } // Of for j } // Of for i System.out.println("Latest start time: " + Arrays.toString(tempLatestTimeArray)); boolean[] resultCriticalArray = new boolean[numNodes]; for (int i = 0; i < numNodes; i++) { if (tempEarliestTimeArray[i] == tempLatestTimeArray[i]) { resultCriticalArray[i] = true; } // Of if } // Of for i System.out.println("Critical array: " + Arrays.toString(resultCriticalArray)); System.out.print("Critical nodes: "); for (int i = 0; i < numNodes; i++) { if (resultCriticalArray[i]) { System.out.print(" " + i); } // Of if } // Of for i System.out.println(); return resultCriticalArray; }// Of criticalPath /** ********************* * The entrance of the program. * * @param args * Not used now. ********************* */ public static void main(String args[]) { Net tempNet0 = new Net(3); System.out.println(tempNet0); int[][] tempMatrix1 = { { 0, 9, 3, 6 }, { 5, 0, 2, 4 }, { 3, 2, 0, 1 }, { 2, 8, 7, 0 } }; Net tempNet1 = new Net(tempMatrix1); System.out.println(tempNet1); // Dijkstra tempNet1.dijkstra(1); // An undirected net is required. int[][] tempMatrix2 = { { 0, 7, MAX_DISTANCE, 5, MAX_DISTANCE }, { 7, 0, 8, 9, 7 }, { MAX_DISTANCE, 8, 0, MAX_DISTANCE, 5 }, { 5, 9, MAX_DISTANCE, 0, 15, }, { MAX_DISTANCE, 7, 5, 15, 0 } }; Net tempNet2 = new Net(tempMatrix2); tempNet2.prim(); // A directed net without loop is required. // Node cannot reach itself. It is indicated by -1. int[][] tempMatrix3 = { { -1, 3, 2, -1, -1, -1 }, { -1, -1, -1, 2, 3, -1 }, { -1, -1, -1, 4, -1, 3 }, { -1, -1, -1, -1, -1, 2 }, { -1, -1, -1, -1, -1, 1 }, { -1, -1, -1, -1, -1, -1 } }; Net tempNet3 = new Net(tempMatrix3); System.out.println("-------critical path"); tempNet3.criticalPath(); }// Of main
树
定义: 无圈连通图称为树。每个连通片皆为树的不连通图称为森林;树上次数为1的顶称为叶;如果一个树T是图G的生成子图,则称T是G的生成树。G-E(T)称为树余生成树的个数(注意:以顶点序来区分生成树,而不考虑同构情况)
用τ(G)表示生成树个数,则: τ(G) = nn-2 一个与之相关的定理: e是连通图G中的一条边,则τ(G)=τ(G-e)+τ(G•e),其中G•e是把边e的长度收缩成零,e的两个端点重合成一个顶形成的图求生成树——搜索算法: BFS / DFS
其中比较重要的一类生成树是最小生成树(也有人叫最优树),求解算法有Prim / Kruskal算法Dijkstra算法:用于最短路径,如图
转载地址:http://flmdi.baihongyu.com/