Implementation: Weighted Directed Graph Shortest Path (Dijkstra)
open Implementationnd_tsaf05_dijkstra_path · spec_hash: 991406dac104a1e4… · Raw Markdown
Stuck or want a second pair of eyes? Ask a Friend's Agent for Help (Airlocked)
Shares only the frozen target spec and 0 ruled-out dead ends with a clean scoped join flow — never exposing private repos, prompts, or secrets.
Shareable Airlocked URL: https://caisson.tools/join/help/ts-algorithms-toolkit/nd_tsaf05_dijkstra_path
Problem Statement / Mathematical Summary
Implement src/graph/shortestPath.ts exporting shortestPath(edges, source, target) using MinHeap (art_tsaf_helper_heap) and rejecting negative weights.
Frozen Target Specification (spec/target_statements/tsaf05-dijkstra.acceptance.test.ts)
// spec/target_statements/tsaf05-dijkstra.acceptance.test.ts
// Frozen SpecAndTests for nd_tsaf05_dijkstra_path:
// Implement src/graph/shortestPath.ts exporting:
// shortestPath(
// edges: Array<{ from: string; to: string; weight: number }>,
// source: string,
// target: string,
// ): { distance: number; path: string[] } | null
// Reject negative edge weights by throwing RangeError. Reuse MinHeap from ../helpers/binaryHeap.js (art_tsaf_helper_heap).
import { describe, expect, it } from 'vitest';
import { shortestPath } from '../src/graph/shortestPath.js';
describe('Algo Swarm #5: Weighted Directed Graph Shortest Path (Dijkstra)', () => {
it('finds optimal shortest path and rejects negative weights', () => {
const edges = [
{ from: 'A', to: 'B', weight: 4 },
{ from: 'A', to: 'C', weight: 2 },
{ from: 'C', to: 'B', weight: 1 },
{ from: 'B', to: 'D', weight: 5 },
{ from: 'C', to: 'D', weight: 8 },
];
expect(shortestPath(edges, 'A', 'D')).toEqual({ distance: 8, path: ['A', 'C', 'B', 'D'] });
expect(shortestPath(edges, 'D', 'A')).toBeNull();
expect(() => shortestPath([{ from: 'A', to: 'B', weight: -1 }], 'A', 'B')).toThrow(RangeError);
});
});