mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-03-17 03:11:01 +00:00
Docs: Update benchmark for test 12
This commit is contained in:
@@ -0,0 +1,58 @@
|
|||||||
|
async function computeMST(tomlString) {
|
||||||
|
const [
|
||||||
|
{ parse },
|
||||||
|
{ default: Heap },
|
||||||
|
textTable
|
||||||
|
] = await Promise.all([
|
||||||
|
import('https://esm.sh/smol-toml'),
|
||||||
|
import('https://esm.sh/mnemonist/heap'),
|
||||||
|
import('https://esm.sh/text-table')
|
||||||
|
]);
|
||||||
|
|
||||||
|
const { edges } = parse(tomlString);
|
||||||
|
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
const nodes = new Set();
|
||||||
|
|
||||||
|
for (const e of edges) {
|
||||||
|
heap.push(e);
|
||||||
|
nodes.add(e.from);
|
||||||
|
nodes.add(e.to);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = {}, rank = {};
|
||||||
|
for (const n of nodes) {
|
||||||
|
parent[n] = n;
|
||||||
|
rank[n] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const find = x => parent[x] === x ? x : (parent[x] = find(parent[x]));
|
||||||
|
|
||||||
|
const union = (x, y) => {
|
||||||
|
const px = find(x), py = find(y);
|
||||||
|
if (px === py) return false;
|
||||||
|
if (rank[px] < rank[py]) parent[px] = py;
|
||||||
|
else if (rank[px] > rank[py]) parent[py] = px;
|
||||||
|
else { parent[py] = px; rank[px]++; }
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let total = 0;
|
||||||
|
const target = nodes.size - 1;
|
||||||
|
|
||||||
|
while (mst.length < target && heap.size) {
|
||||||
|
const { from, to, weight } = heap.pop();
|
||||||
|
if (union(from, to)) {
|
||||||
|
mst.push([from, to, String(weight)]);
|
||||||
|
total += weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const table = textTable([['From', 'To', 'Weight'], ...mst]);
|
||||||
|
|
||||||
|
return { table, totalWeight: total };
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 6.257s
|
||||||
|
// Result: FAIL
|
||||||
69
tests/12_mst_pipeline/outputs/anthropic_claude-opus-4.6.js
Normal file
69
tests/12_mst_pipeline/outputs/anthropic_claude-opus-4.6.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
async function computeMST(tomlString) {
|
||||||
|
const [
|
||||||
|
{ parse },
|
||||||
|
{ default: Heap },
|
||||||
|
textTable
|
||||||
|
] = await Promise.all([
|
||||||
|
import("https://esm.sh/smol-toml"),
|
||||||
|
import("https://esm.sh/mnemonist/heap"),
|
||||||
|
import("https://esm.sh/text-table")
|
||||||
|
]);
|
||||||
|
|
||||||
|
const table = textTable.default || textTable;
|
||||||
|
const { edges } = parse(tomlString);
|
||||||
|
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
const nodes = new Set();
|
||||||
|
|
||||||
|
for (const e of edges) {
|
||||||
|
heap.push(e);
|
||||||
|
nodes.add(e.from);
|
||||||
|
nodes.add(e.to);
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = {};
|
||||||
|
const rank = {};
|
||||||
|
for (const n of nodes) {
|
||||||
|
parent[n] = n;
|
||||||
|
rank[n] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const find = (x) => {
|
||||||
|
while (parent[x] !== x) {
|
||||||
|
parent[x] = parent[parent[x]];
|
||||||
|
x = parent[x];
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
const union = (a, b) => {
|
||||||
|
let ra = find(a), rb = find(b);
|
||||||
|
if (ra === rb) return false;
|
||||||
|
if (rank[ra] < rank[rb]) [ra, rb] = [rb, ra];
|
||||||
|
parent[rb] = ra;
|
||||||
|
if (rank[ra] === rank[rb]) rank[ra]++;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let totalWeight = 0;
|
||||||
|
const target = nodes.size - 1;
|
||||||
|
|
||||||
|
while (mst.length < target && heap.size) {
|
||||||
|
const { from, to, weight } = heap.pop();
|
||||||
|
if (union(from, to)) {
|
||||||
|
mst.push([from, to, String(weight)]);
|
||||||
|
totalWeight += weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = [["From", "To", "Weight"], ...mst];
|
||||||
|
|
||||||
|
return {
|
||||||
|
table: table(rows),
|
||||||
|
totalWeight
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 7.267s
|
||||||
|
// Result: PASS
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
async function computeMST(tomlStr) {
|
||||||
|
const [{ parse }, { Heap }, textTable] = await Promise.all([
|
||||||
|
import('https://cdn.jsdelivr.net/npm/smol-toml@1/dist/index.mjs'),
|
||||||
|
import('https://cdn.jsdelivr.net/npm/mnemonist@0.39.8/+esm'),
|
||||||
|
import('https://cdn.jsdelivr.net/npm/text-table@0.2.0/+esm')
|
||||||
|
]);
|
||||||
|
|
||||||
|
const { edges } = parse(tomlStr);
|
||||||
|
|
||||||
|
const nodes = new Set();
|
||||||
|
edges.forEach(({ from, to }) => {
|
||||||
|
nodes.add(from);
|
||||||
|
nodes.add(to);
|
||||||
|
});
|
||||||
|
|
||||||
|
const parent = {};
|
||||||
|
const rank = {};
|
||||||
|
nodes.forEach(n => {
|
||||||
|
parent[n] = n;
|
||||||
|
rank[n] = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
const find = x => {
|
||||||
|
if (parent[x] !== x) parent[x] = find(parent[x]);
|
||||||
|
return parent[x];
|
||||||
|
};
|
||||||
|
|
||||||
|
const union = (x, y) => {
|
||||||
|
const rx = find(x);
|
||||||
|
const ry = find(y);
|
||||||
|
if (rx === ry) return false;
|
||||||
|
if (rank[rx] < rank[ry]) {
|
||||||
|
parent[rx] = ry;
|
||||||
|
} else if (rank[rx] > rank[ry]) {
|
||||||
|
parent[ry] = rx;
|
||||||
|
} else {
|
||||||
|
parent[ry] = rx;
|
||||||
|
rank[rx]++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
edges.forEach(e => heap.push(e));
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let totalWeight = 0;
|
||||||
|
const targetSize = nodes.size - 1;
|
||||||
|
|
||||||
|
while (mst.length < targetSize && heap.size > 0) {
|
||||||
|
const edge = heap.pop();
|
||||||
|
if (union(edge.from, edge.to)) {
|
||||||
|
mst.push(edge);
|
||||||
|
totalWeight += edge.weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = [
|
||||||
|
['From', 'To', 'Weight'],
|
||||||
|
...mst.map(({ from, to, weight }) => [from, to, String(weight)])
|
||||||
|
];
|
||||||
|
|
||||||
|
return {
|
||||||
|
table: textTable.default(rows),
|
||||||
|
totalWeight
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 7.981s
|
||||||
|
// Result: FAIL
|
||||||
62
tests/12_mst_pipeline/outputs/deepseek_deepseek-v3.2.js
Normal file
62
tests/12_mst_pipeline/outputs/deepseek_deepseek-v3.2.js
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
const computeMST = async tomlString => {
|
||||||
|
const [{parse}, {Heap}, {default: table}] = await Promise.all([
|
||||||
|
import('https://esm.sh/smol-toml@0.9.1'),
|
||||||
|
import('https://esm.sh/mnemonist@0.11.4/heap'),
|
||||||
|
import('https://esm.sh/text-table@0.2.0')
|
||||||
|
]);
|
||||||
|
|
||||||
|
const config = parse(tomlString);
|
||||||
|
const edges = config.edges || [];
|
||||||
|
const nodes = new Set();
|
||||||
|
edges.forEach(e => {
|
||||||
|
nodes.add(e.from);
|
||||||
|
nodes.add(e.to);
|
||||||
|
});
|
||||||
|
|
||||||
|
const ids = new Map([...nodes].map((n, i) => [n, i]));
|
||||||
|
const parent = [...ids.values()];
|
||||||
|
const rank = parent.map(() => 0);
|
||||||
|
const find = x => {
|
||||||
|
while (parent[x] !== x) {
|
||||||
|
parent[x] = parent[parent[x]];
|
||||||
|
x = parent[x];
|
||||||
|
}
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
const union = (x, y) => {
|
||||||
|
let rx = find(x);
|
||||||
|
let ry = find(y);
|
||||||
|
if (rx === ry) return false;
|
||||||
|
if (rank[rx] < rank[ry]) parent[rx] = ry;
|
||||||
|
else if (rank[rx] > rank[ry]) parent[ry] = rx;
|
||||||
|
else {
|
||||||
|
parent[ry] = rx;
|
||||||
|
rank[rx]++;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
edges.forEach(e => heap.push(e));
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let totalWeight = 0;
|
||||||
|
while (heap.size && mst.length < nodes.size - 1) {
|
||||||
|
const edge = heap.pop();
|
||||||
|
const idxFrom = ids.get(edge.from);
|
||||||
|
const idxTo = ids.get(edge.to);
|
||||||
|
if (union(idxFrom, idxTo)) {
|
||||||
|
mst.push([edge.from, edge.to, String(edge.weight)]);
|
||||||
|
totalWeight += edge.weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const output = [['From', 'To', 'Weight'], ...mst];
|
||||||
|
return {
|
||||||
|
table: table(output, {align: ['l', 'l', 'r']}),
|
||||||
|
totalWeight
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 17.592s
|
||||||
|
// Result: FAIL
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
async function computeMST(tomlStr) {
|
||||||
|
const [toml, { Heap }, table] = await Promise.all([
|
||||||
|
import('https://cdn.skypack.dev/smol-toml'),
|
||||||
|
import('https://cdn.skypack.dev/mnemonist'),
|
||||||
|
import('https://cdn.skypack.dev/text-table')
|
||||||
|
]);
|
||||||
|
|
||||||
|
const { edges } = toml.parse(tomlStr);
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
const nodes = new Set();
|
||||||
|
|
||||||
|
edges.forEach(e => {
|
||||||
|
heap.push(e);
|
||||||
|
nodes.add(e.from);
|
||||||
|
nodes.add(e.to);
|
||||||
|
});
|
||||||
|
|
||||||
|
const parent = {};
|
||||||
|
const rank = {};
|
||||||
|
nodes.forEach(n => {
|
||||||
|
parent[n] = n;
|
||||||
|
rank[n] = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
const find = (i) => (parent[i] === i ? i : (parent[i] = find(parent[i])));
|
||||||
|
|
||||||
|
const union = (i, j) => {
|
||||||
|
let rootI = find(i), rootJ = find(j);
|
||||||
|
if (rootI !== rootJ) {
|
||||||
|
if (rank[rootI] < rank[rootJ]) [rootI, rootJ] = [rootJ, rootI];
|
||||||
|
parent[rootJ] = rootI;
|
||||||
|
if (rank[rootI] === rank[rootJ]) rank[rootI]++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let totalWeight = 0;
|
||||||
|
const target = nodes.size - 1;
|
||||||
|
|
||||||
|
while (heap.size && mst.length < target) {
|
||||||
|
const { from, to, weight } = heap.pop();
|
||||||
|
if (union(from, to)) {
|
||||||
|
mst.push([from, to, String(weight)]);
|
||||||
|
totalWeight += weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
table: table.default([['From', 'To', 'Weight'], ...mst]),
|
||||||
|
totalWeight
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 13.942s
|
||||||
|
// Result: PASS
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
const computeMST = async (tomlInput) => {
|
||||||
|
const [{ parse }, { default: Heap }, { default: table }] = await Promise.all([
|
||||||
|
import('https://esm.sh/smol-toml'),
|
||||||
|
import('https://esm.sh/mnemonist/heap'),
|
||||||
|
import('https://esm.sh/text-table')
|
||||||
|
])
|
||||||
|
|
||||||
|
const { edges } = parse(tomlInput)
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight)
|
||||||
|
const parent = new Map(), rank = new Map()
|
||||||
|
|
||||||
|
edges.forEach(e => {
|
||||||
|
heap.push(e);
|
||||||
|
[e.from, e.to].forEach(n => {
|
||||||
|
if (!parent.has(n)) parent.set(n, n), rank.set(n, 0)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
const find = n => parent.get(n) === n ? n : (parent.set(n, find(parent.get(n))), parent.get(n))
|
||||||
|
|
||||||
|
const union = (a, b) => {
|
||||||
|
const rootA = find(a), rootB = find(b)
|
||||||
|
if (rootA === rootB) return false
|
||||||
|
const rA = rank.get(rootA), rB = rank.get(rootB)
|
||||||
|
if (rA < rB) parent.set(rootA, rootB)
|
||||||
|
else parent.set(rootB, rootA), rA === rB && rank.set(rootA, rA + 1)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = [['From', 'To', 'Weight']]
|
||||||
|
let totalWeight = 0
|
||||||
|
|
||||||
|
while (heap.size) {
|
||||||
|
const { from, to, weight } = heap.pop()
|
||||||
|
if (union(from, to)) {
|
||||||
|
rows.push([from, to, String(weight)])
|
||||||
|
totalWeight += weight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { table: table(rows), totalWeight }
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 37.108s
|
||||||
|
// Result: PASS
|
||||||
13
tests/12_mst_pipeline/outputs/moonshotai_kimi-k2.5.js
Normal file
13
tests/12_mst_pipeline/outputs/moonshotai_kimi-k2.5.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
const computeMST=async s=>{
|
||||||
|
const[{parse:P},{Heap:H},T]=await Promise.all([import('https://esm.sh/smol-toml'),import('https://esm.sh/mnemonist'),import('https://esm.sh/text-table')]);
|
||||||
|
const d=P(s),e=d.edges||[],q=new H((x,y)=>x.w-y.w),u=new Set;
|
||||||
|
for(const c of e){q.push({f:c.from,t:c.to,w:c.weight});u.add(c.from);u.add(c.to)}
|
||||||
|
const n=u.size,a={},b={},f=x=>a[x]===x?x:(a[x]=f(a[x])),m=(x,y)=>{let r=f(x),s=f(y);if(r===s)return 0;if(b[r]<b[s])[r,s]=[s,r];a[s]=r;b[r]===b[s]&&b[r]++;return 1};
|
||||||
|
for(const k of u){a[k]=k;b[k]=0}
|
||||||
|
const v=[];let t=0;
|
||||||
|
while(v.length<n-1&&q.size){const c=q.pop();if(m(c.f,c.t)){v.push([c.f,c.t,String(c.w)]);t+=c.w}}
|
||||||
|
return{table:(T.default||T)([['From','To','Weight'],...v]),totalWeight:t}
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 66.902s
|
||||||
|
// Result: PASS
|
||||||
50
tests/12_mst_pipeline/outputs/openrouter_pony-alpha.js
Normal file
50
tests/12_mst_pipeline/outputs/openrouter_pony-alpha.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
async function computeMST(tomlStr) {
|
||||||
|
const [{ parse }, { Heap }, { default: textTable }] = await Promise.all([
|
||||||
|
import('https://esm.sh/smol-toml'),
|
||||||
|
import('https://esm.sh/mnemonist'),
|
||||||
|
import('https://esm.sh/text-table')
|
||||||
|
]);
|
||||||
|
|
||||||
|
const { edges } = parse(tomlStr);
|
||||||
|
const nodes = new Set(edges.flatMap(e => [e.from, e.to]));
|
||||||
|
const parent = {}, rank = {};
|
||||||
|
|
||||||
|
for (const n of nodes) {
|
||||||
|
parent[n] = n;
|
||||||
|
rank[n] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const find = x => parent[x] === x ? x : (parent[x] = find(parent[x]));
|
||||||
|
|
||||||
|
const union = (a, b) => {
|
||||||
|
const rootA = find(a), rootB = find(b);
|
||||||
|
if (rootA === rootB) return false;
|
||||||
|
if (rank[rootA] < rank[rootB]) parent[rootA] = rootB;
|
||||||
|
else if (rank[rootA] > rank[rootB]) parent[rootB] = rootA;
|
||||||
|
else { parent[rootB] = rootA; rank[rootA]++; }
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const heap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
edges.forEach(e => heap.push(e));
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let totalWeight = 0;
|
||||||
|
|
||||||
|
while (heap.size) {
|
||||||
|
const { from, to, weight } = heap.pop();
|
||||||
|
if (union(from, to)) {
|
||||||
|
mst.push([from, to, String(weight)]);
|
||||||
|
totalWeight += weight;
|
||||||
|
if (mst.length === nodes.size - 1) break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
table: textTable([['From', 'To', 'Weight'], ...mst]),
|
||||||
|
totalWeight
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 200.325s
|
||||||
|
// Result: PASS
|
||||||
75
tests/12_mst_pipeline/outputs/x-ai_grok-4.js
Normal file
75
tests/12_mst_pipeline/outputs/x-ai_grok-4.js
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
async function computeMST(toml) {
|
||||||
|
const [smol, mnemo, ttable] = await Promise.all([
|
||||||
|
import('https://cdn.jsdelivr.net/npm/smol-toml'),
|
||||||
|
import('https://cdn.jsdelivr.net/npm/mnemonist'),
|
||||||
|
import('https://cdn.jsdelivr.net/npm/text-table')
|
||||||
|
]);
|
||||||
|
const {parse} = smol;
|
||||||
|
const {Heap} = mnemo;
|
||||||
|
const textTable = ttable.default;
|
||||||
|
|
||||||
|
const config = parse(toml);
|
||||||
|
const edges = config.edges;
|
||||||
|
|
||||||
|
const nodes = new Set();
|
||||||
|
for (const e of edges) {
|
||||||
|
nodes.add(e.from);
|
||||||
|
nodes.add(e.to);
|
||||||
|
}
|
||||||
|
const numNodes = nodes.size;
|
||||||
|
|
||||||
|
const parent = new Map();
|
||||||
|
const rank = new Map();
|
||||||
|
for (const node of nodes) {
|
||||||
|
parent.set(node, node);
|
||||||
|
rank.set(node, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function find(p, x) {
|
||||||
|
if (p.get(x) !== x) {
|
||||||
|
p.set(x, find(p, p.get(x)));
|
||||||
|
}
|
||||||
|
return p.get(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
function union(p, r, x, y) {
|
||||||
|
let px = find(p, x);
|
||||||
|
let py = find(p, y);
|
||||||
|
if (px === py) return false;
|
||||||
|
if (r.get(px) < r.get(py)) {
|
||||||
|
p.set(px, py);
|
||||||
|
} else if (r.get(px) > r.get(py)) {
|
||||||
|
p.set(py, px);
|
||||||
|
} else {
|
||||||
|
p.set(py, px);
|
||||||
|
r.set(px, r.get(px) + 1);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const edgeHeap = new Heap((a, b) => a.weight - b.weight);
|
||||||
|
for (const edge of edges) {
|
||||||
|
edgeHeap.push(edge);
|
||||||
|
}
|
||||||
|
|
||||||
|
const mst = [];
|
||||||
|
let totalWeight = 0;
|
||||||
|
while (mst.length < numNodes - 1 && edgeHeap.size > 0) {
|
||||||
|
const edge = edgeHeap.pop();
|
||||||
|
if (union(parent, rank, edge.from, edge.to)) {
|
||||||
|
mst.push(edge);
|
||||||
|
totalWeight += edge.weight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = [['From', 'To', 'Weight']];
|
||||||
|
for (const e of mst) {
|
||||||
|
rows.push([e.from, e.to, String(e.weight)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableStr = textTable(rows);
|
||||||
|
return { table: tableStr, totalWeight };
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 49.564s
|
||||||
|
// Result: FAIL
|
||||||
52
tests/12_mst_pipeline/outputs/z-ai_glm-4.7.js
Normal file
52
tests/12_mst_pipeline/outputs/z-ai_glm-4.7.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
async function computeMST(t) {
|
||||||
|
const [
|
||||||
|
{ parse: P },
|
||||||
|
{ Heap: H },
|
||||||
|
{ default: T }
|
||||||
|
] = await Promise.all([
|
||||||
|
import('https://esm.sh/smol-toml'),
|
||||||
|
import('https://esm.sh/mnemonist'),
|
||||||
|
import('https://esm.sh/text-table')
|
||||||
|
]);
|
||||||
|
|
||||||
|
const E = P(t).edges;
|
||||||
|
const p = new Map(), r = new Map();
|
||||||
|
|
||||||
|
const f = (x) => {
|
||||||
|
if (!p.has(x)) { p.set(x, x); r.set(x, 0); return x; }
|
||||||
|
if (p.get(x) !== x) p.set(x, f(p.get(x)));
|
||||||
|
return p.get(x);
|
||||||
|
};
|
||||||
|
|
||||||
|
const u = (x, y) => {
|
||||||
|
const a = f(x), b = f(y);
|
||||||
|
if (a === b) return 0;
|
||||||
|
if (r.get(a) < r.get(b)) p.set(a, b);
|
||||||
|
else if (r.get(a) > r.get(b)) p.set(b, a);
|
||||||
|
else { p.set(b, a); r.set(a, r.get(a) + 1); }
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
const h = new H((a, b) => a[2] - b[2]);
|
||||||
|
E.forEach(e => h.push([e.from, e.to, e.weight]));
|
||||||
|
|
||||||
|
const M = [];
|
||||||
|
let S = 0;
|
||||||
|
const N = new Set(E.flatMap(e => [e.from, e.to]));
|
||||||
|
|
||||||
|
while (h.size && M.length < N.size - 1) {
|
||||||
|
const [i, j, w] = h.pop();
|
||||||
|
if (u(i, j)) {
|
||||||
|
M.push([i, j, String(w)]);
|
||||||
|
S += w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
table: T([['From', 'To', 'Weight'], ...M]),
|
||||||
|
totalWeight: S
|
||||||
|
};
|
||||||
|
}
|
||||||
|
export default computeMST;
|
||||||
|
// Generation time: 62.217s
|
||||||
|
// Result: PASS
|
||||||
Reference in New Issue
Block a user