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,28 +1,31 @@
|
||||
async function findShortestPath(graph, start, end) {
|
||||
const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
|
||||
|
||||
const distances = {};
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
|
||||
const dist = Object.keys(graph).reduce((acc, node) => ({ ...acc, [node]: Infinity }), {});
|
||||
dist[start] = 0;
|
||||
|
||||
for (const node in graph) distances[node] = Infinity;
|
||||
distances[start] = 0;
|
||||
pq.queue({ node: start, dist: 0 });
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a[1] - b[1] });
|
||||
pq.queue([start, 0]);
|
||||
|
||||
const visited = new Set();
|
||||
|
||||
while (pq.length) {
|
||||
const { node, dist } = pq.dequeue();
|
||||
const [node, d] = 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 (newDist < distances[neighbor]) {
|
||||
distances[neighbor] = newDist;
|
||||
pq.queue({ node: neighbor, dist: newDist });
|
||||
if (node === end) return d;
|
||||
|
||||
for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
|
||||
const newDist = d + weight;
|
||||
if (newDist < dist[neighbor]) {
|
||||
dist[neighbor] = newDist;
|
||||
pq.queue([neighbor, newDist]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return distances[end];
|
||||
return dist[end];
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -1,19 +1,15 @@
|
||||
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 dist = { [start]: 0 };
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => a[1] - b[1] });
|
||||
const visited = new Set();
|
||||
|
||||
const pq = new PQ({ comparator: (a, b) => a[1] - b[1] });
|
||||
pq.queue([start, 0]);
|
||||
|
||||
while (pq.length) {
|
||||
const [node, d] = pq.dequeue();
|
||||
|
||||
if (visited.has(node)) continue;
|
||||
visited.add(node);
|
||||
|
||||
if (node === end) return d;
|
||||
if (d > (dist[node] ?? Infinity)) continue;
|
||||
|
||||
for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
|
||||
const newDist = d + weight;
|
||||
|
||||
@@ -1,33 +1,32 @@
|
||||
const findShortestPath = async (graph, start, end) => {
|
||||
const cdn = 'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js';
|
||||
const { default: PriorityQueue } = await import(cdn);
|
||||
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 = Object.fromEntries(
|
||||
Object.keys(graph).map(node => [node, Infinity])
|
||||
);
|
||||
distances[start] = 0;
|
||||
const dist = {};
|
||||
const pq = new PriorityQueue({ comparator: (a, b) => dist[a] - dist[b] });
|
||||
|
||||
const pq = new PriorityQueue({
|
||||
comparator: (a, b) => a.priority - b.priority
|
||||
});
|
||||
pq.queue({ value: start, priority: 0 });
|
||||
for (const vertex in graph) {
|
||||
dist[vertex] = Infinity;
|
||||
}
|
||||
dist[startNode] = 0;
|
||||
|
||||
pq.queue(startNode);
|
||||
|
||||
while (pq.length) {
|
||||
const { value: current } = pq.dequeue();
|
||||
const u = pq.dequeue();
|
||||
|
||||
if (current === end) break;
|
||||
if (u === endNode) break;
|
||||
if (!graph[u] || dist[u] === Infinity) continue;
|
||||
|
||||
const neighbors = graph[current] || {};
|
||||
for (const [neighbor, weight] of Object.entries(neighbors)) {
|
||||
const newDist = distances[current] + weight;
|
||||
for (const v in graph[u]) {
|
||||
const newDist = dist[u] + graph[u][v];
|
||||
|
||||
if (newDist < distances[neighbor]) {
|
||||
distances[neighbor] = newDist;
|
||||
pq.queue({ value: neighbor, priority: newDist });
|
||||
if (newDist < dist[v]) {
|
||||
dist[v] = newDist;
|
||||
pq.queue(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return distances[end];
|
||||
};
|
||||
return dist[endNode] ?? Infinity;
|
||||
}
|
||||
export default findShortestPath;
|
||||
@@ -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;
|
||||
@@ -1,24 +1,30 @@
|
||||
async function findShortestPath(g,s,e){
|
||||
if(!g||!g[s]||!g[e])return Infinity
|
||||
const{default:PriorityQueue}=await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/js/priority-queue.min.js')
|
||||
const d={},v={}
|
||||
Object.keys(g).forEach(k=>d[k]=k===s?0:Infinity)
|
||||
const q=new PriorityQueue({comparator:(a,b)=>a.w-b.w})
|
||||
q.queue({n:s,w:0})
|
||||
while(q.length){
|
||||
const{n,w}=q.dequeue()
|
||||
if(v[n])continue
|
||||
v[n]=1
|
||||
if(n===e)return w
|
||||
const nbrs=g[n]
|
||||
for(const x in nbrs){
|
||||
const nw=w+nbrs[x]
|
||||
if(nw<d[x]){
|
||||
d[x]=nw
|
||||
q.queue({n:x,w:nw})
|
||||
const {default:PriorityQueue} = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/jsprioryqueue.min.js').catch(()=>({default:class{
|
||||
constructor(o){this.c=o.comparator||((a,b)=>a-b);this.q=[]}
|
||||
queue(v){this.q.push(v);this.q.sort(this.c)}
|
||||
dequeue(){return this.q.shift()}
|
||||
peek(){return this.q[0]}
|
||||
get length(){return this.q.length}
|
||||
}}));
|
||||
if(!g||!g[s]||!g[e])return Infinity;
|
||||
const d={},v={},pq=new PriorityQueue({comparator:(a,b)=>a.w-b.w});
|
||||
for(const n in g)d[n]=Infinity;
|
||||
d[s]=0;
|
||||
pq.queue({n:s,w:0});
|
||||
while(pq.length){
|
||||
const {n,w}=pq.dequeue();
|
||||
if(v[n])continue;
|
||||
v[n]=1;
|
||||
if(n===e)return w;
|
||||
const nb=g[n];
|
||||
for(const k in nb){
|
||||
const nw=w+nb[k];
|
||||
if(nw<d[k]){
|
||||
d[k]=nw;
|
||||
pq.queue({n:k,w:nw});
|
||||
}
|
||||
}
|
||||
}
|
||||
return Infinity
|
||||
return Infinity;
|
||||
}
|
||||
export default findShortestPath;
|
||||
Reference in New Issue
Block a user