该算法用于计算城市之间的最小最短距离。
连同所附文章,如果您想了解更多信息,我添加了另一个增强功能。
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>