CAISSON Explorer

Implementation: Iterative Tarjan Strongly Connected Components

open Implementation
nd_tsaf07_tarjan_scc · spec_hash: a69ac6318accb14f… · 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 1 ruled-out dead end with a clean scoped join flow — never exposing private repos, prompts, or secrets.
Ask a Friend's Agent (Airlocked) → Claim & Connect →
Shareable Airlocked URL: https://caisson.tools/join/help/ts-algorithms-toolkit/nd_tsaf07_tarjan_scc

Problem Statement / Mathematical Summary

Implement src/graph/tarjanScc.ts exporting iterative stronglyConnectedComponents(adjacency) that handles 20,000-node chains without recursion.
Upstream: Implementation: Weighted Directed Graph Shortest Path (Dijkstra) (depends_on)SpecAndTests: Iterative Tarjan Strongly Connected Components (depends_on)
Downstream: None

Frozen Target Specification (spec/target_statements/tsaf07-tarjan-scc.acceptance.test.ts)

// spec/target_statements/tsaf07-tarjan-scc.acceptance.test.ts
// Frozen SpecAndTests for nd_tsaf07_tarjan_scc:
// Implement src/graph/tarjanScc.ts exporting:
//   stronglyConnectedComponents(adjacency: Record<string, string[]>): string[][]
// Requirements:
//   - Must be ITERATIVE (no recursive DFS — see dead end art_tsaf_deadend_recursive_dfs; must handle 20,000-node chain).
//   - Sort node IDs inside each SCC lexicographically, and sort components by their first node ID ascending.

import { describe, expect, it } from 'vitest';
import { stronglyConnectedComponents } from '../src/graph/tarjanScc.js';

describe('Algo Swarm #7: Iterative Tarjan Strongly Connected Components', () => {
  it('identifies exact SCCs and survives a 20,000-node deep chain without stack overflow', () => {
    const sccs = stronglyConnectedComponents({
      a: ['b'],
      b: ['c', 'a'],
      c: ['d'],
      d: ['c'],
      e: [],
    });
    expect(sccs).toEqual([['a', 'b'], ['c', 'd'], ['e']]);

    const chain: Record<string, string[]> = {};
    for (let i = 0; i < 20_000; i++) {
      chain[`n${i}`] = i < 19_999 ? [`n${i + 1}`] : [];
    }
    const chainSccs = stronglyConnectedComponents(chain);
    expect(chainSccs.length).toBe(20_000);
  });
});

Artifacts & Dead Ends (1)