数据结构之图(带权图 迪杰斯特拉算法)

// 主要思想是: 每次寻找最小的边  这样的话从上一个节点 到这个节点的值 是最小的 当找到最小的边时,把final[v] = true 表示从原点到这个节点的最小值 已经找到了

<!DOCTYPE html>
<html>
<head>
    <title>实现函数</title>
    <meta charset="utf-8">
    <script>

    // 稠密图  邻接矩阵
    class DenseGraph{
        // n为定点个数  directed为无向或有向
        constructor(n, directed){
            this.n = n;
            this.m = 0;
            this.directed = directed
            this.data = []
            for(var i =0; i<n; i++){
                this.data[i] = []
                for(var j=0; j<n; j++){
                    this.data[i][j] = -1;
                }
            }
                
        }

        V() { return this.n }
        E() { return this.m }

        // 添加一条边
        addEdge(v, w, weight){
            
            this.data[v][w] = weight;
            
            
            this.m++;
        }

        // 判断是否有边
        hasEdge(v, w){
            console.log(this.data[v])
            return (this.data[v] && this.data[v][w] > 0);
        }

        DIJ(){
            // 为求得v0 顶点到其余顶点v的最短路径p[v]及其带权长度D[v]
            // 若p[v][w]为true,则w是v0到v的一个顶点
            // final[v] 为true,当且仅当 已经求得从v0到v的最短路径
            var p = [];
            var final = [];
            var D = [];

            for(var v=0; v<this.n; v++){
                final[v] = false; D[v] = this.data[0][v];
                p[v] = [];
                for(var w=0; w<this.n; w++) { p[v][w] = false; } // 设置空路径
                if(D[v] != -1){ p[v][0] = true; p[v][v] = true; } // 如果有值的话 那么v0到v就有路径
            }

            final[0] = true; D[0] = -1;

            // 开始循环 每次求得结果都加入到S集中(就是final[x] = true)
            for(var i=0; i<this.n; i++){
                // 每次求最小的值 并加入到S中
                var min = 99999;
                for(var w=0; w<this.n; w++){
                    if( D[w]<min && D[w] > 0 && !final[w]){
                        // 记录位置 和 值
                        v = w;
                        min = D[w];
                        
                    }
                }
                
                final[v] = true; //把v加入到s中
                for(var j=0; j<this.n; j++){
                    // 如果发现路径经过v节点 使得整体路径变小了 则更新
                    if(!final[j] && ((min + this.data[v][j]) < D[j] || D[j] == -1)&& this.data[v][j] > 0){
                        
                        D[j] = min + this.data[v][j];
                        
                        p[j] = p[v]; p[j][j] = true; // p[w] = p[v] + [w]都为true
                    }
                }
            }

            return {
                p,
                final,
                D
            }
        }
    }

    var dg = new DenseGraph(6, true);
    var  arr = [[0, 2, 10], [0, 4, 30], [0, 5, 100], [1, 2, 5], [2, 3, 50], [3, 5, 10], [4, 3, 20], [4, 5, 60]];
    for(var i=0,len=arr.length; i<len; i++){
        dg.addEdge(arr[i][0], arr[i][1], arr[i][2]);
    }
    console.log(dg.DIJ())
    
    </script>

</head>
<body>
    
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_39081958/article/details/82762999