All examples

Dijkstra (weighted graph), .

Shortest paths over a weighted adjacency list.

dijkstra.js
const graph = {
  A: [{ to: "B", w: 1 }, { to: "C", w: 4 }],
  B: [{ to: "C", w: 2 }, { to: "D", w: 5 }],
  C: [{ to: "D", w: 1 }],
  D: [],
};

function dijkstra(start) {
  const dist = {};
  for (const node of Object.keys(graph)) dist[node] = Infinity;
  dist[start] = 0;
  const frontier = [start];
  while (frontier.length > 0) {
    const node = frontier.shift();
    for (const edge of graph[node]) {
      const alt = dist[node] + edge.w;
      if (alt < dist[edge.to]) {
        dist[edge.to] = alt;
        frontier.push(edge.to);
      }
    }
  }
  return dist;
}

dijkstra("A");

What to watch

Edges carry their weights in the graph view; dist fills in as the frontier relaxes each neighbour.

Reading dijkstra (weighted graph)on a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how dijkstra (weighted graph) works.

Run dijkstra (weighted graph) yourself

Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.

Open NeonFlow