Skip to main content
🧩 Algorithm Projects

What you're building

A dijkstra(graph, start) function that computes the shortest distance from start to every reachable node in a weighted directed graph.

The graph is an adjacency list: { nodeId: [[neighborId, weight], ...] }.

const graph = {
  A: [["B", 1], ["C", 4]],
  B: [["C", 2], ["D", 5]],
  C: [["D", 1]],
  D: [],
};
dijkstra(graph, "A");
// { A: 0, B: 1, C: 3, D: 4 }

Requirements

  • Accept graph (adjacency list object) and start (string node id).
  • Return an object mapping each reachable node to its minimum cost from start.
  • Unreachable nodes should be omitted (or mapped to Infinity — both are valid).
  • All edge weights are non-negative integers.
  • Use a greedy approach: always process the unvisited node with the smallest known distance.

Stretch goals

  • Return the actual shortest path (not just distances) using a previous map.
  • Implement a proper min-heap for O((V+E) log V) performance instead of the naive O(V²) scan.
  • Add support for bidirectional edges.

What this proves

Dijkstra is the foundation of GPS routing, network packet forwarding (OSPF), and game AI pathfinding. Implementing it from scratch demonstrates mastery of greedy algorithms and graph traversal — a staple of technical interviews at every level.

⚡ 50 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
basic shortest distances
start node has distance 0
unreachable node is omitted
prefers cheaper path

Run your code to check these.