mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-13 16:17:54 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,21 +1,24 @@
|
||||
async function findShortestPath(graph, start, end) {
|
||||
const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
|
||||
const { default: PQ } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm');
|
||||
|
||||
const distances = {};
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
|
||||
|
||||
for (const node in graph) distances[node] = Infinity;
|
||||
const distances = Object.keys(graph).reduce((acc, node) => ({ ...acc, [node]: Infinity }), {});
|
||||
distances[start] = 0;
|
||||
|
||||
const pq = new PQ({ comparator: (a, b) => a.dist - b.dist });
|
||||
pq.queue({ node: start, dist: 0 });
|
||||
|
||||
const visited = new Set();
|
||||
|
||||
while (pq.length) {
|
||||
const { node, dist } = pq.dequeue();
|
||||
|
||||
if (node === end) return dist;
|
||||
if (dist > distances[node]) continue;
|
||||
if (visited.has(node)) continue;
|
||||
visited.add(node);
|
||||
|
||||
for (const neighbor in graph[node]) {
|
||||
const newDist = dist + graph[node][neighbor];
|
||||
if (node === end) return dist;
|
||||
|
||||
for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
|
||||
const newDist = dist + weight;
|
||||
if (newDist < distances[neighbor]) {
|
||||
distances[neighbor] = newDist;
|
||||
pq.queue({ node: neighbor, dist: newDist });
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
async function findShortestPath(graph, startNode, endNode) {
|
||||
const { default: PriorityQueue } = await import('https://esm.sh/js-priority-queue');
|
||||
async function findShortestPath(graph, start, end) {
|
||||
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
|
||||
|
||||
const distances = { [startNode]: 0 };
|
||||
const queue = new PriorityQueue({ comparator: (a, b) => a.priority - b.priority });
|
||||
const distances = Object.fromEntries(
|
||||
Object.keys(graph).map(node => [node, Infinity])
|
||||
);
|
||||
distances[start] = 0;
|
||||
|
||||
queue.queue({ node: startNode, priority: 0 });
|
||||
const pq = new PriorityQueue({
|
||||
comparator: (a, b) => distances[b] - distances[a]
|
||||
});
|
||||
pq.queue(start);
|
||||
|
||||
while (queue.length) {
|
||||
const { node: currentNode, priority: currentDist } = queue.dequeue();
|
||||
while (pq.length) {
|
||||
const currentNode = pq.dequeue();
|
||||
|
||||
if (currentDist > (distances[currentNode] ?? Infinity)) {
|
||||
continue;
|
||||
}
|
||||
if (currentNode === end) break;
|
||||
|
||||
if (currentNode === endNode) {
|
||||
return currentDist;
|
||||
}
|
||||
if (!graph[currentNode]) continue;
|
||||
|
||||
for (const neighbor in graph[currentNode] || {}) {
|
||||
const weight = graph[currentNode][neighbor];
|
||||
const newDist = currentDist + weight;
|
||||
for (const [neighbor, weight] of Object.entries(graph[currentNode])) {
|
||||
const newDist = distances[currentNode] + weight;
|
||||
|
||||
if (newDist < (distances[neighbor] ?? Infinity)) {
|
||||
if (newDist < distances[neighbor]) {
|
||||
distances[neighbor] = newDist;
|
||||
queue.queue({ node: neighbor, priority: newDist });
|
||||
pq.queue(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Infinity;
|
||||
return distances[end];
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -1,19 +0,0 @@
|
||||
async function findShortestPath(g,s,e){
|
||||
const {default:Q}=await import('https://cdn.skypack.dev/js-priority-queue')
|
||||
const d={[s]:0},q=new Q({comparator:(a,b)=>a.w-b.w})
|
||||
q.queue({n:s,w:0})
|
||||
while(q.length){
|
||||
const {n,w}=q.dequeue()
|
||||
if(w>(d[n]??Infinity))continue
|
||||
if(n===e)return w
|
||||
for(const k in g[n]||{}){
|
||||
const nw=w+g[n][k]
|
||||
if(nw<(d[k]??Infinity)){
|
||||
d[k]=nw
|
||||
q.queue({n:k,w:nw})
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity
|
||||
}
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user