代码来自闵老师”日撸 Java 三百行(31-40天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975772
拓扑排序:每次剪掉入度为一个0的节点。理解参考:https://www.jianshu.com/p/3347f54a3187
关键路径:活动的最早开始时间和最晚开始时间相等,则说明该活动是属于关键路径上的活动,即关键活动。理解参考:https://blog.csdn.net/fu_jian_ping/article/details/88962697
/**
* ******************************************************
* 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() {
int tempValue;
// Step 1. The in-degree of each node.
int[] tempInDegree = new int[numNodes];
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
if (weightMatrix.getValue(i, j) != -1) {
tempInDegree[j]++;
}//of if
}//of for j
}//of for i
System.out.println("In-degree of nodes: " + Arrays.toString(tempInDegree));
// Step 2. Topology sorting.
int[] tempEarliestTimeArray = new int[numNodes];
for (int i = 0; i < numNodes; i++) {
if (tempInDegree[i] > 0) {
continue;//Not the node that should be cut out.
}//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
}//of if
tempInDegree[j]--;
}//of for j
}//of for i
System.out.println("Earlest start time: " + Arrays.toString(tempEarliestTimeArray));
// Step 3. The out-degree of each node.
int[] tempOutDegree = new int[numNodes];
for (int i = 0; i < numNodes; i++) {
for (int j = 0; j < numNodes; j++) {
if (weightMatrix.getValue(i, j) != -1) {
tempOutDegree[i]++;
}//of if
}//of for j
}//of for i
System.out.println("Out-degree of nodes: " + Arrays.toString(tempOutDegree));
// Step 4. Reverse topology sorting.
int[] tempLatestTimeArray = new int[numNodes];
Arrays.fill(tempLatestTimeArray, tempEarliestTimeArray[numNodes - 1]);
for (int i = numNodes - 1; i >= 0; i--) {
if (tempOutDegree[i] > 0) {
continue;//Not the node that should be cut out.
}//of if
System.out.println("*****************");
System.out.println("Removing " + i);
System.out.println("*****************");
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
}//of if
tempOutDegree[j]--;
}//of for j
}//of for i
System.out.println("Latest start time: " + Arrays.toString(tempLatestTimeArray));
//Determine whether it is a critical node
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));
for (int i = 0; i < numNodes; i++) {
if (resultCriticalArray[i]) {
System.out.println(" " + i);
}//of if
}//of for i
System.out.println();
return resultCriticalArray;
}//of criticalPath
/**
* ****************************************************************
* The entrance of 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);
//
// int[] resultDistanceArray = new int[tempNet1.numNodes];
// resultDistanceArray = tempNet1.dijkstra(3);
// System.out.println("The distance result of dijkstra is: " + Arrays.toString(resultDistanceArray));
//
// int resultCost;
// resultCost = tempNet1.prim(0);
// System.out.println("The result cost of the Prim algorithm: " + resultCost);
//
// resultCost = tempNet1.prim();
// System.out.println("The result cost of the Prim algorithm: " + resultCost);
//
// 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();
// tempNet2.prim(0);
// 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、拓扑逆排序的时候,下标写错了。
2、最晚开始时间初始化自己写的时候还是写错了。应该用最后一个节点的开始时间,将最晚开始时间数组全部填充。
3、倒序应该是i–;否则前面的遍历是无效的。
4、求最早开始时间找最大值,求最晚发生时间逆序找最小值。对于关键路径来说,逆序减掉的越多(关键路径减的多),剩的越少,所以要逆序求最小值。