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>
相关推荐
大胖丫9 分钟前
vue 学习-vite api.js
开发语言·前端·javascript
孙桂月10 分钟前
ES6相关操作(2)
前端·javascript·es6
遇见很ok10 分钟前
js中 ES6 新特性详解
开发语言·javascript·es6
陈浩源同学10 分钟前
学习 TypeScript 栈和队列数据结构
前端·算法
我这一生如履薄冰~12 分钟前
简单封装一个websocket构造函数
前端·javascript·websocket
前端菜鸟日常27 分钟前
vue2和vue3的按需引入的详细对比通俗易懂
javascript·vue.js·ecmascript
夏末秋也凉1 小时前
力扣-回溯-491 非递减子序列
数据结构·算法·leetcode
penguin_bark1 小时前
三、动规_子数组系列
算法·leetcode
kyle~1 小时前
thread---基本使用和常见错误
开发语言·c++·算法
程楠楠&M1 小时前
uni-app(位置1)
前端·javascript·uni-app·node.js