(nodeIndex: NodeIndex, direction: Direction): <N, E>(
graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">
) => Array<NodeIndex>
<N, E>(
graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">,
nodeIndex: NodeIndex,
direction: Direction
): Array<NodeIndex>Gets directed neighbors of a node in a specific direction.
When to use
Use when maintaining existing code that already passes an explicit traversal
direction. New code should prefer successors or predecessors.
Gotchas
Throws a GraphError when used with an undirected graph.
Example (Traversing directed neighbors)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
Graph.addEdge(mutable, a, b, "A->B")
})
const nodeA = 0
const nodeB = 1
// Get outgoing neighbors (nodes that nodeA points to)
const outgoing = Graph.neighborsDirected(graph, nodeA, "outgoing")
// Get incoming neighbors (nodes that point to nodeB)
const incoming = Graph.neighborsDirected(graph, nodeB, "incoming")export const const neighborsDirected: {
(nodeIndex: NodeIndex, direction: Direction): <
N,
E
>(
graph:
| Graph<N, E, "directed">
| MutableGraph<N, E, "directed">
) => Array<NodeIndex>
<N, E>(
graph:
| Graph<N, E, "directed">
| MutableGraph<N, E, "directed">,
nodeIndex: NodeIndex,
direction: Direction
): Array<NodeIndex>
}
Gets directed neighbors of a node in a specific direction.
When to use
Use when maintaining existing code that already passes an explicit traversal
direction. New code should prefer successors or predecessors.
Gotchas
Throws a GraphError when used with an undirected graph.
Example (Traversing directed neighbors)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
Graph.addEdge(mutable, a, b, "A->B")
})
const nodeA = 0
const nodeB = 1
// Get outgoing neighbors (nodes that nodeA points to)
const outgoing = Graph.neighborsDirected(graph, nodeA, "outgoing")
// Get incoming neighbors (nodes that point to nodeB)
const incoming = Graph.neighborsDirected(graph, nodeB, "incoming")
neighborsDirected: {
(
nodeIndex: NodeIndexnodeIndex: 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,
direction: Directiondirection: type Direction = "outgoing" | "incoming"Direction for graph traversal, indicating which edges to follow.
Example (Traversing by direction)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
Graph.addEdge(mutable, a, b, "A->B")
})
// Follow outgoing edges (normal direction)
const outgoingNodes = Array.from(
Graph.indices(Graph.dfs(graph, { start: [0], direction: "outgoing" }))
)
// Follow incoming edges (reverse direction)
const incomingNodes = Array.from(
Graph.indices(Graph.dfs(graph, { start: [1], direction: "incoming" }))
)
Direction
): <function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<NodeIndex>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): 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<NodeIndex>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): 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<NodeIndex>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">): Array<NodeIndex>E, "directed">) => 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>
<function (type parameter) N in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">, nodeIndex: NodeIndex, direction: Direction): 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">, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">, nodeIndex: NodeIndex, direction: Direction): 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">, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E>(graph: Graph<N, E, "directed"> | MutableGraph<N, E, "directed">, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>E, "directed">,
nodeIndex: NodeIndexnodeIndex: 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,
direction: Directiondirection: type Direction = "outgoing" | "incoming"Direction for graph traversal, indicating which edges to follow.
Example (Traversing by direction)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
Graph.addEdge(mutable, a, b, "A->B")
})
// Follow outgoing edges (normal direction)
const outgoingNodes = Array.from(
Graph.indices(Graph.dfs(graph, { start: [0], direction: "outgoing" }))
)
// Follow incoming edges (reverse direction)
const incomingNodes = Array.from(
Graph.indices(Graph.dfs(graph, { start: [1], direction: "incoming" }))
)
Direction
): 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>
} = import dualdual(3, <function (type parameter) N in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>T extends 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 = "directed">(
graph: Graph<N, E, T> | MutableGraph<N, E, T>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, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>T> | 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, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>T>,
nodeIndex: NodeIndexnodeIndex: 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,
direction: Directiondirection: type Direction = "outgoing" | "incoming"Direction for graph traversal, indicating which edges to follow.
Example (Traversing by direction)
import { Graph } from "effect"
const graph = Graph.directed<string, string>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
Graph.addEdge(mutable, a, b, "A->B")
})
// Follow outgoing edges (normal direction)
const outgoingNodes = Array.from(
Graph.indices(Graph.dfs(graph, { start: [0], direction: "outgoing" }))
)
// Follow incoming edges (reverse direction)
const incomingNodes = Array.from(
Graph.indices(Graph.dfs(graph, { start: [1], direction: "incoming" }))
)
Direction
): 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, T> | MutableGraph<N, E, T>graph.type: T extends Kind = "directed"type === "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 get directed neighbors of undirected graph" })
}
return const getDirectedNeighbors: <N, E>(
graph:
| Graph<N, E, "directed">
| MutableGraph<N, E, "directed">,
nodeIndex: NodeIndex,
direction: Direction
) => Array<NodeIndex>
getDirectedNeighbors(graph: Graph<N, E, T> | MutableGraph<N, E, T>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, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): 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, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, nodeIndex: NodeIndex, direction: Direction): Array<NodeIndex>E, "directed">, nodeIndex: NodeIndexnodeIndex, direction: Directiondirection)
})