9.普利姆算法(加点法)求最小生成树(JavaScript版)

普利姆算法(加点法)求最小生成树

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function Node(value) {
            this.value = value;
            this.neighbor = [];
            this.distance = [];
        }

        var nodeA = new Node("a");
        var nodeB = new Node("b");
        var nodeC = new Node("c");
        var nodeD = new Node("d");
        var nodeE = new Node("e");
        var nodeF = new Node("f");
        var nodeG = new Node("g");
        var nodeH = new Node("h");

        //存放所有节点的数组
        var pointSet = [nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, nodeG, nodeH];

        var max = Number.POSITIVE_INFINITY; //无穷大

        var distance = [ //点与点之间的距离
            //   a    b    c    d    e    f    g    h
            [0, 1, 2, max, max, max, max, max], //a
            [1, 0, max, 3, max, 5, max, max], //b
            [2, max, 0, 4, max, max, 7, max], //c
            [max, 3, 4, 0, 6, max, max, max], //d
            [max, max, max, 6, 0, 8, 9, max], //e
            [max, 5, max, max, 8, 0, max, 10], //f
            [max, max, 7, max, 9, max, 0, 11], //g
            [max, max, max, max, max, 10, 11, 0] //h
        ];

        //prim算法
        function prim(pointSet, distance, start) {
            var nowPointSet = [];
            nowPointSet.push(start);//将开始节点放入已连接数组中
            while (true) {
                //通过已连接节点,找到和它们相连接开销最小的节点
                var minDistanceNode = getMinDistanceNode(pointSet, distance, nowPointSet);
                nowPointSet.push(minDistanceNode);//将开销最小的节点加入已连接数组中
                if(nowPointSet.length == pointSet.length) break;//所有节点都连接,跳出循环
            }
            console.log(nowPointSet);   
        }

        function getMinDistanceNode(pointSet, distance, nowPointSet) {
            for (var i = 0; i < nowPointSet.length; i++) { //遍历已连接的点
                var pointIndex = getIndex(nowPointSet[i].value);//获取已连接节点在pointSet中的索引值
                var pointDistance = distance[pointIndex];//通过pointIndex找到该连接节点对应所有边的开销
                var minDistance = max;//最小距离默认为max
                var fromNode = null;//起始节点
                var endNode = null;//终止节点
                for (var j = 0; j < pointDistance.length; j++) { //遍历所有边的开销
                    if (nowPointSet.indexOf(pointSet[j]) < 0 && pointDistance[j] <
                        minDistance) { //最小距离连接的节点不能在nowPointSet中 && 要小于minDistance
                        minDistance = pointDistance[j];
                        fromNode = nowPointSet[i];
                        endNode = pointSet[j];
                    }
                }
            }

            fromNode.neighbor.push(endNode);//起始节点 将开销最小的节点加入
            fromNode.distance.push({//起始节点 将开销最小的节点的值和距离加入
                from: fromNode.value,
                to: endNode.value,
                distance: minDistance
            });
            endNode.neighbor.push(fromNode);
            endNode.distance.push({
                from: fromNode.value,
                to: endNode.value,
                distance: minDistance
            });
                     
            return endNode;//返回开销最小的节点
        }

        function getIndex(str) {//获取索引值
            for (var i = 0; i < pointSet.length; i++) {
                if (str == pointSet[i].value) {
                    return i;
                }
            }

            return -1;
        }

        prim(pointSet, distance, pointSet[2]);
    </script>
</body>

</html>
普利姆算法

 

posted @ 2020-06-26 17:24  lanshanxiao  阅读(456)  评论(0编辑  收藏  举报