Dijkstra's algorithm All In One
Dijkstra's algorithm All In One
迪杰斯特拉
算法
Dijkstra
Dijkstra's algorithm (/ˈdaɪkstrəz/ DYKE-strəz) is an algorithm for finding the shortest paths
between nodes in a weighted graph
, which may represent, for example, road networks.
Dijkstra 算法是一种用于查找加权图
中节点之间最短路径
的算法,该算法可以表示例如道路网络
。
https://en.wikipedia.org/wiki/Dijkstra's_algorithm
https://zh.wikipedia.org/wiki/戴克斯特拉算法
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra
demos
leetcode
https://leetcode.com/problems/path-with-maximum-probability
function maxProbability(n: number, edges: number[][], succProb: number[], start: number, end: number): number {
let map = {};
for (let i = 0; i < edges.length; i++) {
let [f, t] = edges[i];
if (map[f] === undefined) {
map[f] = {};
}
if (map[t] === undefined) {
map[t] = {};
}
map[f][t] = succProb[i];
map[t][f] = succProb[i];
}
if(map[end] === undefined) {
return 0;
}
let res = dijkstra(n, map, start, end);
return res;
};
// 迪克斯特拉
let dijkstra = (n: number, map: any, s: number, d: number) => {
let visited = new Array(n).fill(0);
let costs = new Array(n).fill(0);
costs[s] = 1;
while(true) {
let node;
for(let i=0; i<visited.length; i++) {
if(visited[i]) {
continue;
}
if(node === undefined) {
node = i;
} else {
node = costs[node] < costs[i] ? i: node;
}
}
if(node === undefined) {
break;
}
if(node === d) {
return costs[d];
}
visited[node] = 1;
if(map[node] === undefined) {
continue;
}
let adjNodes = Object.keys(map[node]);
for(let adj of adjNodes) {
if(visited[adj]) {
continue;
}
let w = map[node][adj] * costs[node];
costs[adj] = Math.max(costs[adj], w);
}
}
return costs[d];
}
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2024-08-27
* @modified
*
* @description 1514. Path with Maximum Probability
* @description 1514. 概率最大的路径
* @difficulty Hard
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/path-with-maximum-probability
* @link https://leetcode.cn/problems/path-with-maximum-probability
* @solutions
*
* @best_solutions
*
*/
export {};
const log = console.log;
function maxProbability(n: number, edges: number[][], succProb: number[], start: number, end: number): number {
let map = {};
for (let i = 0; i < edges.length; i++) {
let [f, t] = edges[i];
if (map[f] === undefined) {
map[f] = {};
}
if (map[t] === undefined) {
map[t] = {};
}
map[f][t] = succProb[i];
map[t][f] = succProb[i];
}
if(map[end] === undefined) {
return 0;
}
let res = dijkstra(n, map, start, end);
return res;
};
// 迪克斯特拉
let dijkstra = (n: number, map: any, s: number, d: number) => {
let visited = new Array(n).fill(0);
let costs = new Array(n).fill(0);
costs[s] = 1;
while(true) {
let node;
for(let i=0; i<visited.length; i++) {
if(visited[i]) {
continue;
}
if(node === undefined) {
node = i;
} else {
node = costs[node] < costs[i] ? i: node;
}
}
if(node === undefined) {
break;
}
if(node === d) {
return costs[d];
}
visited[node] = 1;
if(map[node] === undefined) {
continue;
}
let adjNodes = Object.keys(map[node]);
for(let adj of adjNodes) {
if(visited[adj]) {
continue;
}
let w = map[node][adj] * costs[node];
costs[adj] = Math.max(costs[adj], w);
}
}
return costs[d];
}
/*
undirected weighted graph
无向加权图
*/
/*
https://leetcode.com/problems/path-with-maximum-probability/description/?envType=daily-question&envId=2024-08-27
*/
refs
https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
https://medium.com/@alejandro.itoaramendia/a-guide-to-dijkstras-algorithm-all-you-need-99635dcd6d94
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18383730
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2023-08-27 Authorization token types All In One
2023-08-27 Google Analytic 4 All In One
2023-08-27 node-fetch Advanced Usage All In One
2023-08-27 ESM import.meta All In One
2022-08-27 iPhone 外接移动固态硬盘 All In One
2022-08-27 三星固态硬盘 All In One
2022-08-27 2022 最新中国电影票房排行榜 All In One