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,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;
|
||||
Reference in New Issue
Block a user