卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章75948本站已运行4316

使用 Javascript 的 Dijkstra 算法

使用 javascript 的 dijkstra 算法

该算法用于计算城市之间的最小最短距离。

连同所附文章,如果您想了解更多信息,我添加了另一个增强功能。

  1. 我计算了之前的路径,从那里我们可以得到它到达那里的完整路径。

const dijkstra = (graph) => {
    const vertex = graph.length;
    const path = new Array(vertex).fill(false);
    const distance = new Array(vertex).fill(Infinity);
    const prev = [-1];
    distance[0] = 0; // source Node

    const getMinDistanceIndex = (path, distance) => {
        let min = Infinity;
        let minIndex = -1;

        for (let j = 0; j distance[j]) {
                min = distance[j];
                minIndex = j;
            }    
        }    

        return minIndex;
    }

    for (let i = 0; i  0 && distance[minDistanceIndex] + graph[minDistanceIndex][j] 



<p>如果您有任何疑问,请随时联系我</p>

<p><strong>参考</strong><br>
https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/</p>


          

            
        
卓越飞翔博客
上一篇: “备份表”包
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏