/**
 * Tarjan's strongly connected components algorithm.
 * Operates on a CodeGraph instance.
 *
 * @returns SCCs with length > 1 (cycles)
 */
export function tarjan(graph) {
    let index = 0;
    const stack = [];
    const onStack = new Set();
    const indices = new Map();
    const lowlinks = new Map();
    const sccs = [];
    function strongconnect(v) {
        indices.set(v, index);
        lowlinks.set(v, index);
        index++;
        stack.push(v);
        onStack.add(v);
        for (const w of graph.successors(v)) {
            if (!indices.has(w)) {
                strongconnect(w);
                lowlinks.set(v, Math.min(lowlinks.get(v), lowlinks.get(w)));
            }
            else if (onStack.has(w)) {
                lowlinks.set(v, Math.min(lowlinks.get(v), indices.get(w)));
            }
        }
        if (lowlinks.get(v) === indices.get(v)) {
            const scc = [];
            let w;
            do {
                w = stack.pop();
                onStack.delete(w);
                scc.push(w);
            } while (w !== v);
            if (scc.length > 1)
                sccs.push(scc);
        }
    }
    for (const id of graph.nodeIds()) {
        if (!indices.has(id))
            strongconnect(id);
    }
    return sccs;
}
//# sourceMappingURL=tarjan.js.map