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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值