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,24 +1,29 @@
|
||||
let u
|
||||
const o=()=>u||(u=import('https://cdn.skypack.dev/js-priority-queue').then(r=>r.default||r))
|
||||
async function findShortestPath(g,s,t){
|
||||
const PQ=await o()
|
||||
if(s===t)return 0
|
||||
const d={},q=new PQ({comparator:(a,b)=>a[1]-b[1]})
|
||||
for(const k in g)d[k]=Infinity
|
||||
d[s]=0
|
||||
q.queue([s,0])
|
||||
while(q.length){
|
||||
const[n,w]=q.dequeue()
|
||||
if(w>d[n])continue
|
||||
if(n===t)return w
|
||||
for(const[nb,c]of Object.entries(g[n]||{})){
|
||||
const nw=w+c
|
||||
if(nw<d[nb]){
|
||||
d[nb]=nw
|
||||
q.queue([nb,nw])
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity
|
||||
let pq;
|
||||
const loadPQ=()=>pq||(pq=import('https://cdn.skypack.dev/js-priority-queue').then(m=>m.default));
|
||||
|
||||
async function findShortestPath(graph,start,end){
|
||||
const PQ=await loadPQ();
|
||||
if(start===end) return 0;
|
||||
if(!graph||typeof graph!=='object') return Infinity;
|
||||
const d=new Map([[start,0]]);
|
||||
const q=new PQ({comparator:(a,b)=>a[0]-b[0]});
|
||||
q.queue([0,start]);
|
||||
while(q.length){
|
||||
const [w,n]=q.dequeue();
|
||||
if(w>(d.get(n)??Infinity)) continue;
|
||||
if(n===end) return w;
|
||||
const edges=graph[n];
|
||||
if(!edges||typeof edges!=='object') continue;
|
||||
for(const k of Object.keys(edges)){
|
||||
const c=edges[k];
|
||||
if(typeof c!=='number'||c<0||!Number.isFinite(c)) continue;
|
||||
const nw=w+c;
|
||||
if(nw<(d.get(k)??Infinity)){
|
||||
d.set(k,nw);
|
||||
q.queue([nw,k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity;
|
||||
}
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user