javascript 使用迪杰斯特拉算法完成 n*m 网格中2点的寻路

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
    </style>
</head>
<body>
    <div id="box1"></div>
</body>
<script type="text/javascript">

    // dijkstra 函数
    // 它接受一个图和一个起始节点作为参数,并返回一个包含最短距离和前驱节点的对象。
    function dijkstra(graph, start) {
        const distances = {};
        const visited = {};
        const previous = {};
        const queue = [];

        for (let vertex in graph) {
        distances[vertex] = Infinity;
        previous[vertex] = null;
        queue.push(vertex);
        }

        distances[start] = 0;

        while (queue.length) {
        let current = null;

        for (let vertex of queue) {
          if (!current || distances[vertex] < distances[current]) {
            current = vertex;
          }
        }

        queue.splice(queue.indexOf(current), 1);
        visited[current] = true;

        for (let neighbor in graph[current]) {
          let distance = graph[current][neighbor];
          let totalDistance = distances[current] + distance;

          if (totalDistance < distances[neighbor]) {
            distances[neighbor] = totalDistance;
            previous[neighbor] = current;
          }
        }
        }

        return { distances, previous };
    }

    const graph = {
      A: { B: 5, C: 1 },
      B: { A: 5, C: 2, D: 1 },
      C: { A: 1, B: 2, D: 4, E: 8 },
      D: { B: 1, C: 4, E: 3, F: 6 },
      E: { C: 8, D: 3 },
      F: { D: 6 }
    };

    // 上述代码中,我们定义了一个图,并使用 dijkstra 函数计算了从节点A到其他节点的最短距离和前驱节点。
    // 最后,我们将结果打印到控制台上。
    var startPoint = 'A';
    var endPoint = 'F';
    const { distances, previous } = dijkstra(graph, startPoint);
    console.log(distances);
    // A到其他节点的最短距离
    // {
    //        A: 0,
    //        B: 5,
    //        C: 1,
    //        D: 6,
    //        E: 9,
    //        F: 12
    //   }

    console.log(previous);
    // A到其他节点的前驱结点:
    // {
    //       A: null,
    //       B: 'C',
    //       C: 'A',
    //       D: 'B',
    //       E: 'D',
    //       F: 'D'     A -> B -> D->F
    //   }


    console.log("xxxxxxxxxxxxx");
    // 打印出 startPoint 到 endPoint 的最短路径

    function getPath( previous ){
        var path=[];
        var target = endPoint;
        while( true ){
            path.push( target );
            var prev = previous[ target ];
            console.log( prev + " --> " +  target );
            if( prev == startPoint ){
                path.push( startPoint );
                break;
            }
            target = prev;
        }
        path = path.reverse();
        return path;
    }
    var path = getPath( previous );
    console.log( path );
</script>
</html>
相关推荐
阿珊和她的猫1 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
PAK向日葵3 小时前
【算法导论】PDD 0817笔试题题解
算法·面试
地平线开发者6 小时前
ReID/OSNet 算法模型量化转换实践
算法·自动驾驶
地平线开发者6 小时前
开发者说|EmbodiedGen:为具身智能打造可交互3D世界生成引擎
算法·自动驾驶
gnip6 小时前
vite和webpack打包结构控制
前端·javascript
星星火柴9367 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
艾莉丝努力练剑8 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
烛阴8 小时前
前端必会:如何创建一个可随时取消的定时器
前端·javascript·typescript
萌萌哒草头将军9 小时前
Oxc 最新 Transformer Alpha 功能速览! 🚀🚀🚀
前端·javascript·vue.js
C++、Java和Python的菜鸟9 小时前
第六章 统计初步
算法·机器学习·概率论