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>
相关推荐
wclass-zhengge27 分钟前
数据结构篇(绪论)
java·数据结构·算法
何事驚慌28 分钟前
2024/10/5 数据结构打卡
java·数据结构·算法
结衣结衣.29 分钟前
C++ 类和对象的初步介绍
java·开发语言·数据结构·c++·笔记·学习·算法
汪子熙40 分钟前
Angular 服务器端应用 ng-state tag 的作用介绍
前端·javascript·angular.js
大二转专业3 小时前
408算法题leetcode--第24天
考研·算法·leetcode
凭栏落花侧3 小时前
决策树:简单易懂的预测模型
人工智能·算法·决策树·机器学习·信息可视化·数据挖掘·数据分析
hong_zc4 小时前
算法【Java】—— 二叉树的深搜
java·算法
吱吱鼠叔5 小时前
MATLAB计算与建模常见函数:5.曲线拟合
算法·机器学习·matlab
嵌入式AI的盲6 小时前
数组指针和指针数组
数据结构·算法
昨天;明天。今天。6 小时前
案例-表白墙简单实现
前端·javascript·css