其他分享
首页 > 其他分享> > day39

day39

作者:互联网

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 if
            }//of for j
        }//olf 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 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 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 cannnot 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 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.println("Critical nodes: ");
        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 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

标签:numNodes,int,day39,System,++,println,out
来源: https://blog.csdn.net/mitchellemin/article/details/118344402