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,27 +1,24 @@
|
||||
async function findShortestPath(graph, start, end) {
|
||||
const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
|
||||
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm');
|
||||
|
||||
const distances = {};
|
||||
const visited = new Set();
|
||||
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 PriorityQueue({ 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 (visited.has(node)) continue;
|
||||
if (node === end) return dist;
|
||||
|
||||
visited.add(node);
|
||||
|
||||
for (const neighbor in graph[node]) {
|
||||
if (visited.has(neighbor)) continue;
|
||||
|
||||
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 });
|
||||
@@ -29,6 +26,6 @@ async function findShortestPath(graph, start, end) {
|
||||
}
|
||||
}
|
||||
|
||||
return Infinity;
|
||||
return distances[end];
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -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;
|
||||
@@ -0,0 +1,22 @@
|
||||
const findShortestPath = async (g, s, e) => {
|
||||
const { default: Q } = await import('https://esm.sh/js-priority-queue')
|
||||
const q = new Q({ comparator: (a, b) => a.w - b.w })
|
||||
const d = { [s]: 0 }
|
||||
q.queue({ n: s, w: 0 })
|
||||
|
||||
while (q.length) {
|
||||
const { n, w } = q.dequeue()
|
||||
if (n === e) return w
|
||||
if (w > (d[n] ?? Infinity)) continue
|
||||
|
||||
for (const v in g[n]) {
|
||||
const k = w + g[n][v]
|
||||
if (k < (d[v] ?? Infinity)) {
|
||||
d[v] = k
|
||||
q.queue({ n: v, w: k })
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -1,19 +0,0 @@
|
||||
const findShortestPath = async (g, s, e) => {
|
||||
const { default: P } = await import('https://cdn.skypack.dev/js-priority-queue')
|
||||
const d = { [s]: 0 }, q = new P({ comparator: (a, b) => a.w - b.w })
|
||||
q.queue({ w: 0, n: s })
|
||||
while (q.length) {
|
||||
const { w, n } = q.dequeue()
|
||||
if (n === e) return w
|
||||
if (w > (d[n] ?? 1 / 0)) continue
|
||||
for (const v in g[n]) {
|
||||
const t = w + g[n][v]
|
||||
if (t < (d[v] ?? 1 / 0)) {
|
||||
d[v] = t
|
||||
q.queue({ w: t, n: v })
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1 / 0
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -1,24 +1,28 @@
|
||||
async function findShortestPath(graph, start, end) {
|
||||
const { default: Q } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/index.min.js');
|
||||
const dist = { [start]: 0 }, visited = new Set();
|
||||
const pq = new Q({ comparator: (a, b) => dist[a] - dist[b] });
|
||||
pq.queue(start);
|
||||
export async function findShortestPath(graph, start, end) {
|
||||
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/index.min.js');
|
||||
|
||||
if (!graph[start] || !graph[end]) return Infinity;
|
||||
if (start === end) return 0;
|
||||
|
||||
const dist = { [start]: 0 };
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a[0] - b[0] });
|
||||
pq.queue([0, start]);
|
||||
|
||||
while (pq.length) {
|
||||
const node = pq.dequeue();
|
||||
if (node === end) return dist[end];
|
||||
if (visited.has(node)) continue;
|
||||
visited.add(node);
|
||||
const nodeDist = dist[node];
|
||||
for (const [adj, weight] of Object.entries(graph[node] || {})) {
|
||||
if (visited.has(adj)) continue;
|
||||
const newDist = nodeDist + weight;
|
||||
if (newDist < (dist[adj] ?? Infinity)) {
|
||||
dist[adj] = newDist;
|
||||
pq.queue(adj);
|
||||
const [d, node] = pq.dequeue();
|
||||
|
||||
if (node === end) return d;
|
||||
if (d > (dist[node] ?? Infinity)) continue;
|
||||
|
||||
for (const [n, w] of Object.entries(graph[node] || {})) {
|
||||
const nd = d + w;
|
||||
if (nd < (dist[n] ?? Infinity)) {
|
||||
dist[n] = nd;
|
||||
pq.queue([nd, n]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Infinity;
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -1,27 +1,30 @@
|
||||
const src='https://esm.sh/js-priority-queue@1'
|
||||
let load
|
||||
const getPQ=()=>load||(load=import(src).then(m=>m.default))
|
||||
const findShortestPath=async(g,s,e)=>{
|
||||
const PQ=await getPQ()
|
||||
const pq=new PQ({comparator:(a,b)=>a[0]-b[0]})
|
||||
const dist=new Map([[s,0]])
|
||||
const done=new Set
|
||||
pq.queue([0,s])
|
||||
while(pq.length){
|
||||
const [d,n]=pq.dequeue()
|
||||
if(done.has(n))continue
|
||||
done.add(n)
|
||||
if(n===e)return d
|
||||
const edges=g[n]
|
||||
if(!edges)continue
|
||||
for(const k in edges){
|
||||
const w=d+edges[k]
|
||||
if(w<(dist.get(k)??Infinity)){
|
||||
dist.set(k,w)
|
||||
pq.queue([w,k])
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity
|
||||
let pqPromise
|
||||
const getPQ=()=>pqPromise??=import('https://cdn.jsdelivr.net/npm/js-priority-queue@latest/+esm')
|
||||
|
||||
async function findShortestPath(g,s,t){
|
||||
if(!g||!(s in g)||!(t in g)) return Infinity
|
||||
if(s===t) return 0
|
||||
const {default:PriorityQueue}=await getPQ()
|
||||
const d={[s]:0}
|
||||
const seen=new Set
|
||||
const q=new PriorityQueue({comparator:(a,b)=>a[0]-b[0]})
|
||||
q.queue([0,s])
|
||||
while(q.length){
|
||||
const [w,n]=q.dequeue()
|
||||
if(seen.has(n)) continue
|
||||
seen.add(n)
|
||||
if(n===t) return w
|
||||
const edges=g[n]
|
||||
if(!edges) continue
|
||||
for(const k in edges){
|
||||
if(seen.has(k)) continue
|
||||
const nw=w+edges[k]
|
||||
if(nw<(d[k]??Infinity)){
|
||||
d[k]=nw
|
||||
q.queue([nw,k])
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -1,23 +1,24 @@
|
||||
async function findShortestPath(graph,start,end){
|
||||
const{default:PriorityQueue}=await import('https://cdn.skypack.dev/js-priority-queue');
|
||||
const {PriorityQueue}=await import('https://cdn.skypack.dev/js-priority-queue');
|
||||
if(start===end)return 0;
|
||||
const dist={};
|
||||
const pq=new PriorityQueue({comparator:(x,y)=>x.d-y.d});
|
||||
for(const n of Object.keys(graph))dist[n]=Infinity;
|
||||
dist[start]=0;
|
||||
pq.queue({n:start,d:0});
|
||||
const pq=new PriorityQueue({comparator:(a,b)=>a.dist-b.dist});
|
||||
pq.queue({node:start,dist:0});
|
||||
while(!pq.isEmpty()){
|
||||
const{n:n,d:d}=pq.dequeue();
|
||||
if(d>dist[n])continue;
|
||||
if(n===end)return d;
|
||||
for(let nei in graph[n]){
|
||||
const w=graph[n][nei];
|
||||
const alt=d+w;
|
||||
if(alt<dist[nei]){
|
||||
dist[nei]=alt;
|
||||
pq.queue({n:nei,d:alt});
|
||||
const u=pq.dequeue();
|
||||
if(u.dist>dist[u.node])continue;
|
||||
const neighbors=graph[u.node];
|
||||
if(!neighbors)continue;
|
||||
for(const v in neighbors){
|
||||
const w=neighbors[v];
|
||||
const alt=dist[u.node]+w;
|
||||
if(alt<(dist[v]??Infinity)){
|
||||
dist[v]=alt;
|
||||
pq.queue({node:v,dist:alt});
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity;
|
||||
return dist[end]??Infinity;
|
||||
}
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user