Taking the first entry instead of the cheapest breaks the guarantee.
Acceptance criteria
- 1.#out should show cost 3.
Taking the first entry instead of the cheapest breaks the guarantee.
const graph = new Map([["a", [["c", 4], ["b", 1]]], ["b", [["c", 2]]], ["c", []]]); const dist = new Map([["a", 0]]); const done = new Set(); for (;;) { let best = null; for (const [node] of dist) if (!done.has(node)) { best = node; break; } if (best === null) break; done.add(best); for (const [next, w] of graph.get(best) || []) { const cand = dist.get(best) + w; if (!dist.has(next)) dist.set(next, cand); } } document.querySelector('#out').textContent = "cost " + dist.get("c");
Output ready when you are
Press Run to render your code, or Submit to validate your solution.