-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
94 lines (84 loc) · 2.06 KB
/
utils.js
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
/**
* @param {string} startXY
* @param {string} endXY
* @param {Map<string, string[]>} graph
* @returns {false|number[]}
*/
export function bfs(startNode, endNode, graph) {
const queue = [startNode];
const came_from = new Map();
came_from.set(startNode, null);
while (queue.length > 0) {
const currentNode = queue.shift();
if (currentNode == endNode) break;
// Search side paths
graph.get(currentNode).forEach(e => {
if (!came_from.has(e)) {
queue.push(e);
came_from.set(e, currentNode);
}
});
}
let currentNode = endNode;
const path = [];
while (currentNode != startNode) {
path.push(currentNode);
currentNode = came_from.get(currentNode);
if (!currentNode) return false;
}
return path;
}
/**
* Dijkstra's Algorithm
* @param {string} startNode
* @param {string} endNode
* @param {Map<string, {node: string, cost: number}[]} graph
* @returns {false|number[]}
*/
export function ucs(startNode, endNode, graph) {
const queue = [[startNode, 0]]; // Needs to be PriorityQueue
// add start with a value of 0
const came_from = new Map();
const cost_so_far = new Map();
came_from.set(startNode, null);
cost_so_far.set(startNode, 0);
console.log(queue);
/**/
while (queue.length > 0) {
const currentNode = queue.shift(); //Depend son PQ
if (currentNode == endNode) break;
// Search side paths
graph.get(currentNode).forEach(e => {
const node = e.node;
const cost = e.cost;
const new_cost = cost_so_far.get(currentNode) + cost; // How to handle the cost bit?
if (!cost_so_far.has(node) || new_cost < cost_so_far.get(node)) {
cost_so_far.set(node, new_cost);
queue.push([node, new_cost]);
queue.sort((a, b) => b[1] - a[1]);
came_from.set(node, currentNode);
}
});
}
/*
*/
}
/**
* @param {number} a
* @param {number} b
* @returns
*/
export function gcd(a, b) {
var t = 0;
a < b && (t = b, b = a, a = t); // swap them if a < b
t = a % b;
return t ? gcd(b, t) : b;
}
/**
* @param {number} a
* @param {number} b
* @returns
*/
export function lcm(a, b) {
return a / gcd(a, b) * b;
}