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>
相关推荐
dapeng287028 分钟前
分布式系统容错设计
开发语言·c++·算法
qq_4176950532 分钟前
代码热修复技术
开发语言·c++·算法
还是大剑师兰特7 小时前
Vue3 中的 defineExpose 完全指南
前端·javascript·vue.js
泯泷7 小时前
阶段一:从 0 看懂 JSVMP 架构,先在脑子里搭出一台最小 JSVM
前端·javascript·架构
Liu628887 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
AI科技星7 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘
华洛8 小时前
利好打工人,openclaw不是企业提效工具,而是个人助理
前端·javascript·产品经理
xkxnq8 小时前
第六阶段:Vue生态高级整合与优化(第93天)Element Plus进阶:自定义主题(变量覆盖)+ 全局配置与组件按需加载优化
前端·javascript·vue.js
条tiao条8 小时前
KMP 算法详解:告别暴力匹配,让字符串匹配 “永不回头”
开发语言·算法
干啥啥不行,秃头第一名8 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法