-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-dijkstraAlgorithm.js
More file actions
98 lines (81 loc) · 2.13 KB
/
Copy path7-dijkstraAlgorithm.js
File metadata and controls
98 lines (81 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* @Author: fangyong luo
* @Date: 2024-09-03 06:48:06
* @Description: 狄克斯特拉算法
* 适用于带有权重的有向图,计算起点到终点的权重最小路径
*
*/
// 图的散列表存储节点信息
const graph = {};
graph.start = {};
graph.start.a = 5;
graph.start.b = 0;
graph.a = {};
graph.a.c = 15;
graph.a.d = 20;
graph.b = {};
graph.b.c = 30;
graph.b.d = 35;
graph.c = {};
graph.c.end = 20;
graph.d = {};
graph.d.end = 10;
graph.end = {};
// 存储最小开销的散列表
const costs = {
a: 5,
b: 0,
end: Infinity
};
// 存储最小开销的父节点
const parents = {
a: 'start',
b: 'start',
};
const hasProcessedNode = [];
/**
* @description: 从未处理过的节点中找出最小开销节点
* @param {map} costs
* @return {node}
*/
function getLowestCostNode() {
let lowestCostNode = null;
let lowestCostValue = Infinity;
for(const node in costs) {
const value = costs[node];
if((value < lowestCostValue) && !hasProcessedNode.includes(node)) {
lowestCostValue = value;
lowestCostNode = node;
}
}
lowestCostNode && hasProcessedNode.push(lowestCostNode);
console.log(lowestCostNode, hasProcessedNode);
return lowestCostNode;
}
function searchLowestCostPath() {
let node = getLowestCostNode();
while(node) {
const cost = costs[node];
const neighbors = graph[node];
for(const neighbor in neighbors) {
const value = neighbors[neighbor];
const newValue = cost + value;
costs[neighbor] ||= Infinity;
if(newValue < costs[neighbor]) {
costs[neighbor] = newValue;
parents[neighbor] = node;
}
}
node = getLowestCostNode();
}
// 经过一系列的搜索,找到权重和最小的值与路径
console.log('the min weight sum is ', costs.end, costs);
const paths = ['end'];
let parent = parents['end'];
while(parent) {
paths.unshift(parent);
parent = parents[parent];
}
console.log('the min weight paths is ', paths, parents);
}
searchLowestCostPath();