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,33 +1,36 @@
|
||||
async function findShortestPath(graph, start, end) {
|
||||
const findShortestPath = async (graph, startNode, endNode) => {
|
||||
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
|
||||
|
||||
const distances = Object.fromEntries(
|
||||
Object.keys(graph).map(node => [node, Infinity])
|
||||
Object.keys(graph).map(node => [node, 1 / 0])
|
||||
);
|
||||
distances[start] = 0;
|
||||
distances[startNode] = 0;
|
||||
|
||||
const pq = new PriorityQueue({
|
||||
comparator: (a, b) => distances[b] - distances[a]
|
||||
comparator: (a, b) => a.priority - b.priority
|
||||
});
|
||||
pq.queue(start);
|
||||
pq.queue({ node: startNode, priority: 0 });
|
||||
|
||||
while (pq.length) {
|
||||
const currentNode = pq.dequeue();
|
||||
const { node: u, priority: dist } = pq.dequeue();
|
||||
|
||||
if (currentNode === end) break;
|
||||
if (dist > distances[u]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!graph[currentNode]) continue;
|
||||
if (u === endNode) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (const [neighbor, weight] of Object.entries(graph[currentNode])) {
|
||||
const newDist = distances[currentNode] + weight;
|
||||
|
||||
if (newDist < distances[neighbor]) {
|
||||
distances[neighbor] = newDist;
|
||||
pq.queue(neighbor);
|
||||
for (const [v, weight] of Object.entries(graph[u] ?? {})) {
|
||||
const newDist = distances[u] + weight;
|
||||
if (newDist < distances[v]) {
|
||||
distances[v] = newDist;
|
||||
pq.queue({ node: v, priority: newDist });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return distances[end];
|
||||
}
|
||||
return distances[endNode];
|
||||
};
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user