mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 00:27:55 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,34 +1,40 @@
|
||||
const findShortestPath = async (graph, start, end) => {
|
||||
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
|
||||
|
||||
const dists = Object.fromEntries(
|
||||
Object.keys(graph).map(node => [node, Infinity])
|
||||
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 distances = {};
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a.priority - b.priority });
|
||||
|
||||
dists[start] = 0;
|
||||
pq.queue({ node: start, priority: 0 });
|
||||
for (const node in graph) {
|
||||
distances[node] = Infinity;
|
||||
}
|
||||
distances[startNode] = 0;
|
||||
|
||||
pq.queue({ node: startNode, priority: 0 });
|
||||
|
||||
while (pq.length) {
|
||||
const { node: u, priority: currentDist } = pq.dequeue();
|
||||
const { node: currentNode, priority: currentPriority } = pq.dequeue();
|
||||
|
||||
if (currentDist > dists[u]) {
|
||||
if (currentPriority > distances[currentNode]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (u === end) break;
|
||||
if (currentNode === endNode) {
|
||||
return currentPriority;
|
||||
}
|
||||
|
||||
for (const [v, weight] of Object.entries(graph[u])) {
|
||||
const distThroughU = dists[u] + weight;
|
||||
for (const neighbor in graph[currentNode]) {
|
||||
const weight = graph[currentNode][neighbor];
|
||||
const newDistance = currentPriority + weight;
|
||||
|
||||
if (distThroughU < dists[v]) {
|
||||
dists[v] = distThroughU;
|
||||
pq.queue({ node: v, priority: distThroughU });
|
||||
if (newDistance < distances[neighbor]) {
|
||||
distances[neighbor] = newDistance;
|
||||
pq.queue({ node: neighbor, priority: newDistance });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dists[end];
|
||||
};
|
||||
return Infinity;
|
||||
}
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user