<N, E>(
graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">
): Array<Array<NodeIndex>>Finds strongly connected components in a directed graph using Kosaraju's algorithm. Each SCC is represented as an array of node indices.
Gotchas
Throws a GraphError when used with an undirected graph.
Example (Finding strongly connected components)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
const c = Graph.addNode(mutable, "C")
Graph.addEdge(mutable, a, b, "A->B")
Graph.addEdge(mutable, b, c, "B->C")
Graph.addEdge(mutable, c, a, "C->A") // Creates SCC: A-B-C
})
const sccs = Graph.stronglyConnectedComponents(graph)
console.log(sccs) // [[0, 1, 2]]export const const stronglyConnectedComponents: <N, E>(
graph:
| Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
) => Array<Array<NodeIndex>>
Finds strongly connected components in a directed graph using Kosaraju's algorithm.
Each SCC is represented as an array of node indices.
Gotchas
Throws a GraphError when used with an undirected graph.
Example (Finding strongly connected components)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
const c = Graph.addNode(mutable, "C")
Graph.addEdge(mutable, a, b, "A->B")
Graph.addEdge(mutable, b, c, "B->C")
Graph.addEdge(mutable, c, a, "C->A") // Creates SCC: A-B-C
})
const sccs = Graph.stronglyConnectedComponents(graph)
console.log(sccs) // [[0, 1, 2]]
stronglyConnectedComponents = <function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>E>(
graph: | Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
graph: interface Graph<out N, out E, T extends Kind = "directed">Immutable graph interface.
When to use
Use as the immutable graph model for code that queries, traverses,
transforms, or analyzes graph structure without mutating it.
Graph<function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>E, "directed"> | interface MutableGraph<out N, out E, T extends Kind = "directed">Mutable graph interface.
When to use
Use when adding, removing, or updating nodes and edges inside a graph
mutation scope.
MutableGraph<function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>E, "directed">
): interface Array<T>Array<interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex>> => {
if ((graph: | Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
graph as interface Graph<out N, out E, T extends Kind = "directed">Immutable graph interface.
When to use
Use as the immutable graph model for code that queries, traverses,
transforms, or analyzes graph structure without mutating it.
Graph<function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>E, type Kind = "directed" | "undirected"Graph type for distinguishing directed and undirected graphs.
When to use
Use when writing graph-polymorphic types or helpers that need to preserve
whether a graph is directed or undirected.
Kind> | interface MutableGraph<out N, out E, T extends Kind = "directed">Mutable graph interface.
When to use
Use when adding, removing, or updating nodes and edges inside a graph
mutation scope.
MutableGraph<function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<Array<NodeIndex>>E, type Kind = "directed" | "undirected"Graph type for distinguishing directed and undirected graphs.
When to use
Use when writing graph-polymorphic types or helpers that need to preserve
whether a graph is directed or undirected.
Kind>).type: Kindtype === "undirected") {
throw new constructor GraphError(): GraphErrorError thrown by graph operations when the requested graph structure is
invalid, such as referencing a missing node or using unsupported edge
weights.
When to use
Use when handling failures thrown by graph operations that reject invalid
graph structure or unsupported algorithm inputs.
GraphError({ message: stringmessage: "Cannot find strongly connected components of undirected graph" })
}
const const visited: Set<number>visited = new var Set: SetConstructor
new <number>(iterable?: Iterable<number> | null | undefined) => Set<number> (+1 overload)
Set<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex>()
const const finishOrder: Array<NodeIndex>finishOrder: interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex> = []
// Iterate directly over node keys
// Step 1: Stack-safe DFS on original graph to get finish times
// Stack entry: [node, neighbors, neighborIndex, isFirstVisit]
type type DfsStackEntry = [
number,
number[],
number,
boolean
]
DfsStackEntry = [type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex, interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex>, number, boolean]
for (const const startNode: numberstartNode of graph: | Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
graph.Proto<out N, out E>.nodes: Map<NodeIndex, N>nodes.Map<number, N>.keys(): MapIterator<number>Returns an iterable of keys in the map
keys()) {
if (const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const startNode: numberstartNode)) {
continue
}
const const stack: DfsStackEntry[]stack: interface Array<T>Array<type DfsStackEntry = [
number,
number[],
number,
boolean
]
DfsStackEntry> = [[const startNode: numberstartNode, [], 0, true]]
while (const stack: DfsStackEntry[]stack.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0) {
const [const node: numbernode, const nodeNeighbors: number[]nodeNeighbors, const neighborIndex: numberneighborIndex, const isFirstVisit: booleanisFirstVisit] = const stack: DfsStackEntry[]stack[const stack: DfsStackEntry[]stack.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1]
if (const isFirstVisit: booleanisFirstVisit) {
if (const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const node: numbernode)) {
const stack: DfsStackEntry[]stack.Array<DfsStackEntry>.pop(): DfsStackEntry | undefinedRemoves the last element from an array and returns it.
If the array is empty, undefined is returned and the array is not modified.
pop()
continue
}
const visited: Set<number>visited.Set<number>.add(value: number): Set<number>Appends a new element with a specified value to the end of the Set.
add(const node: numbernode)
const const nodeNeighborsList: number[]nodeNeighborsList = const getDirectedNeighbors: <N, E>(
graph:
| Graph<N, E, "directed">
| MutableGraph<N, E, "directed">,
nodeIndex: NodeIndex,
direction: Direction
) => Array<NodeIndex>
getDirectedNeighbors(graph: | Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
graph, const node: numbernode, "outgoing")
const stack: DfsStackEntry[]stack[const stack: DfsStackEntry[]stack.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1] = [const node: numbernode, const nodeNeighborsList: number[]nodeNeighborsList, 0, false]
continue
}
// Process next neighbor
if (const neighborIndex: numberneighborIndex < const nodeNeighbors: number[]nodeNeighbors.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length) {
const const neighbor: numberneighbor = const nodeNeighbors: number[]nodeNeighbors[const neighborIndex: numberneighborIndex]
const stack: DfsStackEntry[]stack[const stack: DfsStackEntry[]stack.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1] = [const node: numbernode, const nodeNeighbors: number[]nodeNeighbors, const neighborIndex: numberneighborIndex + 1, false]
if (!const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const neighbor: numberneighbor)) {
const stack: DfsStackEntry[]stack.Array<DfsStackEntry>.push(...items: DfsStackEntry[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push([const neighbor: numberneighbor, [], 0, true])
}
} else {
// Done with this node - add to finish order (post-order)
const finishOrder: Array<NodeIndex>finishOrder.Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const node: numbernode)
const stack: DfsStackEntry[]stack.Array<DfsStackEntry>.pop(): DfsStackEntry | undefinedRemoves the last element from an array and returns it.
If the array is empty, undefined is returned and the array is not modified.
pop()
}
}
}
// Step 2: Stack-safe DFS on transpose graph in reverse finish order
const visited: Set<number>visited.Set<number>.clear(): voidclear()
const const sccs: Array<Array<NodeIndex>>sccs: interface Array<T>Array<interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex>> = []
for (let let i: numberi = const finishOrder: Array<NodeIndex>finishOrder.Array<number>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length - 1; let i: numberi >= 0; let i: numberi--) {
const const startNode: numberstartNode = const finishOrder: Array<NodeIndex>finishOrder[let i: numberi]
if (const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const startNode: numberstartNode)) {
continue
}
const const scc: Array<NodeIndex>scc: interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex> = []
const const stack: Array<NodeIndex>stack: interface Array<T>Array<type NodeIndex = numberNode index for node identification using plain numbers.
When to use
Use when storing or passing the stable identifier of a graph node between
Graph operations.
Details
addNode allocates node identifiers from the graph's next node index.
Gotchas
A NodeIndex is an identifier, not an array offset. Removed node identifiers
are not reused.
NodeIndex> = [const startNode: numberstartNode]
while (const stack: Array<NodeIndex>stack.Array<number>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length > 0) {
const const node: numbernode = const stack: Array<NodeIndex>stack.Array<number>.pop(): number | undefinedRemoves the last element from an array and returns it.
If the array is empty, undefined is returned and the array is not modified.
pop()!
if (const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const node: numbernode)) {
continue
}
const visited: Set<number>visited.Set<number>.add(value: number): Set<number>Appends a new element with a specified value to the end of the Set.
add(const node: numbernode)
const scc: Array<NodeIndex>scc.Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const node: numbernode)
// Use reverse adjacency (transpose graph)
const const reverseAdjacency:
| number[]
| undefined
reverseAdjacency = graph: | Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
graph.Proto<out N, out E>.reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>reverseAdjacency.Map<number, number[]>.get(key: number): number[] | undefinedReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
get(const node: numbernode)
if (const reverseAdjacency:
| number[]
| undefined
reverseAdjacency !== var undefinedundefined) {
for (const const edgeIndex: numberedgeIndex of const reverseAdjacency: number[]reverseAdjacency) {
const const edge: Edge<E> | undefinededge = graph: | Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
graph.Proto<out N, out E>.edges: Map<EdgeIndex, Edge<E>>edges.Map<number, Edge<E>>.get(key: number): Edge<E> | undefinedReturns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.
get(const edgeIndex: numberedgeIndex)
if (const edge: Edge<E> | undefinededge !== var undefinedundefined) {
const const predecessor: anypredecessor = const edge: Edge<E>const edge: {
source: number;
target: number;
data: E;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
edge.source
if (!const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const predecessor: anypredecessor)) {
const stack: Array<NodeIndex>stack.Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const predecessor: anypredecessor)
}
}
}
}
}
const sccs: Array<Array<NodeIndex>>sccs.Array<number[]>.push(...items: number[][]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const scc: Array<NodeIndex>scc)
}
return const sccs: Array<Array<NodeIndex>>sccs
}