xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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 算法是一种用于查找加权图中节点之间最短路径的算法,该算法可以表示例如道路网络

image

https://en.wikipedia.org/wiki/Dijkstra's_algorithm

https://zh.wikipedia.org/wiki/戴克斯特拉算法

image

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, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 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
点击右上角即可分享
微信分享提示