day38

package datastructure.gragh;

import java.util.Arrays;

import matrix.IntMatrix;


/**
 * ******************************************
 *Weighted graphs are called nets.
 *
 * @author Michelle Min MitchelleMin@163.com
 * @date 2021-06-28
 * ******************************************
 */
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 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);
        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

            //fot 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

        //steo 3. output for debug.
        System.out.println("Finally");
        System.out.println("The distance to each nodes: " + 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 ca 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++){
            // stepp 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[i]){
                    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

                // Attentiion: 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 nodes: " + 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);

        //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, 15 }, {MAX_DISTANCE, 7, 5, 15, 0 }};
        Net tempNet2 = new Net(tempMatrix2);
        tempNet2.prim();
    }//of main
}//of class net

254行似乎多了一个“,”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值