mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 08:37:56 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,27 +1,33 @@
|
||||
async function findShortestPath(graph, startNode, endNode) {
|
||||
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
|
||||
const findShortestPath = async (graph, start, end) => {
|
||||
const cdn = 'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js';
|
||||
const { default: PriorityQueue } = await import(cdn);
|
||||
|
||||
const distances = { [startNode]: 0 };
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a.priority - b.priority });
|
||||
const distances = Object.fromEntries(
|
||||
Object.keys(graph).map(node => [node, Infinity])
|
||||
);
|
||||
distances[start] = 0;
|
||||
|
||||
pq.queue({ value: startNode, priority: 0 });
|
||||
const pq = new PriorityQueue({
|
||||
comparator: (a, b) => a.priority - b.priority
|
||||
});
|
||||
pq.queue({ value: start, priority: 0 });
|
||||
|
||||
while (pq.length) {
|
||||
const { value: currentNode } = pq.dequeue();
|
||||
const { value: current } = pq.dequeue();
|
||||
|
||||
if (currentNode === endNode) break;
|
||||
if (current === end) break;
|
||||
|
||||
const neighbors = graph[currentNode] ?? {};
|
||||
const neighbors = graph[current] || {};
|
||||
for (const [neighbor, weight] of Object.entries(neighbors)) {
|
||||
const newDist = distances[currentNode] + weight;
|
||||
const newDist = distances[current] + weight;
|
||||
|
||||
if (newDist < (distances[neighbor] ?? Infinity)) {
|
||||
if (newDist < distances[neighbor]) {
|
||||
distances[neighbor] = newDist;
|
||||
pq.queue({ value: neighbor, priority: newDist });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return distances[endNode] ?? Infinity;
|
||||
}
|
||||
return distances[end];
|
||||
};
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user