<E>(config: DijkstraConfig<E>): <N, T extends Kind = "directed">(
graph: Graph<N, E, T> | MutableGraph<N, E, T>
) => Option.Option<PathResult<E>>
<N, E, T extends Kind = "directed">(
graph: Graph<N, E, T> | MutableGraph<N, E, T>,
config: DijkstraConfig<E>
): Option.Option<PathResult<E>>Finds the shortest path from the configured source node to the target node using Dijkstra's algorithm.
Details
Edge costs must be non-negative. Returns Option.none() when the target is
not reachable, and throws a GraphError when either endpoint is missing or a
negative edge cost is encountered.
Example (Finding shortest paths with Dijkstra)
import { Graph } from "effect"
const graph = Graph.directed<string, number>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
const c = Graph.addNode(mutable, "C")
Graph.addEdge(mutable, a, b, 5)
Graph.addEdge(mutable, a, c, 10)
Graph.addEdge(mutable, b, c, 2)
})
const result = Graph.dijkstra(graph, {
source: 0,
target: 2,
cost: (edgeData) => edgeData
})
if (result._tag === "Some") {
console.log(result.value.path) // [0, 1, 2] - shortest path A->B->C
console.log(result.value.distance) // 7 - total distance
}export const const dijkstra: {
<E>(config: DijkstraConfig<E>): <
N,
T extends Kind = "directed"
>(
graph: Graph<N, E, T> | MutableGraph<N, E, T>
) => Option.Option<PathResult<E>>
<N, E, T extends Kind = "directed">(
graph: Graph<N, E, T> | MutableGraph<N, E, T>,
config: DijkstraConfig<E>
): Option.Option<PathResult<E>>
}
Finds the shortest path from the configured source node to the target node
using Dijkstra's algorithm.
Details
Edge costs must be non-negative. Returns Option.none() when the target is
not reachable, and throws a GraphError when either endpoint is missing or a
negative edge cost is encountered.
Example (Finding shortest paths with Dijkstra)
import { Graph } from "effect"
const graph = Graph.directed<string, number>((mutable) => {
const a = Graph.addNode(mutable, "A")
const b = Graph.addNode(mutable, "B")
const c = Graph.addNode(mutable, "C")
Graph.addEdge(mutable, a, b, 5)
Graph.addEdge(mutable, a, c, 10)
Graph.addEdge(mutable, b, c, 2)
})
const result = Graph.dijkstra(graph, {
source: 0,
target: 2,
cost: (edgeData) => edgeData
})
if (result._tag === "Some") {
console.log(result.value.path) // [0, 1, 2] - shortest path A->B->C
console.log(result.value.distance) // 7 - total distance
}
dijkstra: {
<function (type parameter) E in <E>(config: DijkstraConfig<E>): <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>) => Option.Option<PathResult<E>>E>(
config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config: interface DijkstraConfig<E>Configuration for finding a shortest path with Dijkstra's algorithm.
When to use
Use when configuring dijkstra to find a shortest path between two existing
node indices with non-negative edge costs.
Details
Specifies the source and target node indices, plus a cost function that maps
each edge's data to a non-negative numeric weight.
Gotchas
dijkstra throws a GraphError when either endpoint does not exist or when
the cost function returns a negative weight.
DijkstraConfig<function (type parameter) E in <E>(config: DijkstraConfig<E>): <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>) => Option.Option<PathResult<E>>E>
): <function (type parameter) N in <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>): Option.Option<PathResult<E>>N, function (type parameter) T in <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>): Option.Option<PathResult<E>>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, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>): Option.Option<PathResult<E>>N, function (type parameter) E in <E>(config: DijkstraConfig<E>): <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>) => Option.Option<PathResult<E>>E, function (type parameter) T in <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>): Option.Option<PathResult<E>>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, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>): Option.Option<PathResult<E>>N, function (type parameter) E in <E>(config: DijkstraConfig<E>): <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>) => Option.Option<PathResult<E>>E, function (type parameter) T in <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>): Option.Option<PathResult<E>>T>) => import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<interface PathResult<E>Result of a shortest path computation.
When to use
Use to read the successful source-to-target shortest path returned by
path-finding algorithms, including the ordered node indices, total distance,
and traversed edge data.
Details
Contains the node-index path, the total numeric distance, and the edge data
encountered along the path.
Gotchas
costs contains original edge data, not the numeric output of the cost
function unless the edge data is numeric.
PathResult<function (type parameter) E in <E>(config: DijkstraConfig<E>): <N, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>) => Option.Option<PathResult<E>>E>>
<function (type parameter) N in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>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>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>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>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>T>,
config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config: interface DijkstraConfig<E>Configuration for finding a shortest path with Dijkstra's algorithm.
When to use
Use when configuring dijkstra to find a shortest path between two existing
node indices with non-negative edge costs.
Details
Specifies the source and target node indices, plus a cost function that maps
each edge's data to a non-negative numeric weight.
Gotchas
dijkstra throws a GraphError when either endpoint does not exist or when
the cost function returns a negative weight.
DijkstraConfig<function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E>
): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<interface PathResult<E>Result of a shortest path computation.
When to use
Use to read the successful source-to-target shortest path returned by
path-finding algorithms, including the ordered node indices, total distance,
and traversed edge data.
Details
Contains the node-index path, the total numeric distance, and the edge data
encountered along the path.
Gotchas
costs contains original edge data, not the numeric output of the cost
function unless the edge data is numeric.
PathResult<function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E>>
} = import dualdual(2, <function (type parameter) N in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>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>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>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>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>N, function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E, function (type parameter) T in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>T>,
config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config: interface DijkstraConfig<E>Configuration for finding a shortest path with Dijkstra's algorithm.
When to use
Use when configuring dijkstra to find a shortest path between two existing
node indices with non-negative edge costs.
Details
Specifies the source and target node indices, plus a cost function that maps
each edge's data to a non-negative numeric weight.
Gotchas
dijkstra throws a GraphError when either endpoint does not exist or when
the cost function returns a negative weight.
DijkstraConfig<function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E>
): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<interface PathResult<E>Result of a shortest path computation.
When to use
Use to read the successful source-to-target shortest path returned by
path-finding algorithms, including the ordered node indices, total distance,
and traversed edge data.
Details
Contains the node-index path, the total numeric distance, and the edge data
encountered along the path.
Gotchas
costs contains original edge data, not the numeric output of the cost
function unless the edge data is numeric.
PathResult<function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E>> => {
// Validate that source and target nodes exist
if (!graph: Graph<N, E, T> | MutableGraph<N, E, T>graph.Proto<out N, out E>.nodes: Map<NodeIndex, N>nodes.Map<number, N>.has(key: number): booleanhas(config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.source: NodeIndexsource)) {
throw const missingNode: (
node: number
) => GraphError
missingNode(config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.source: NodeIndexsource)
}
if (!graph: Graph<N, E, T> | MutableGraph<N, E, T>graph.Proto<out N, out E>.nodes: Map<NodeIndex, N>nodes.Map<number, N>.has(key: number): booleanhas(config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.target: NodeIndextarget)) {
throw const missingNode: (
node: number
) => GraphError
missingNode(config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.target: NodeIndextarget)
}
const const edgeWeights: Map<number, number>edgeWeights = const validateNonNegativeEdgeWeights: <
N,
E,
T extends Kind = "directed"
>(
graph: Graph<N, E, T> | MutableGraph<N, E, T>,
cost: (edgeData: E) => number,
algorithm: string
) => Map<EdgeIndex, number>
validateNonNegativeEdgeWeights(graph: Graph<N, E, T> | MutableGraph<N, E, T>graph, config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.cost: (edgeData: E) => numbercost, "Dijkstra's algorithm")
// Early return if source equals target
if (config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.source: NodeIndexsource === config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.target: NodeIndextarget) {
return import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some({
path: number[]path: [config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.source: NodeIndexsource],
distance: numberdistance: 0,
costs: never[]costs: []
})
}
// Distance tracking and priority queue simulation
const const distances: Map<number, number>distances = new var Map: MapConstructor
new <number, number>(iterable?: Iterable<readonly [number, number]> | null | undefined) => Map<number, number> (+3 overloads)
Map<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>()
const const previous: Map<
number,
{ node: NodeIndex; edgeData: E } | null
>
previous = new var Map: MapConstructor
new <number, {
node: NodeIndex;
edgeData: E;
} | null>(iterable?: Iterable<readonly [number, {
node: NodeIndex;
edgeData: E;
} | null]> | null | undefined) => Map<number, {
node: NodeIndex;
edgeData: E;
} | null> (+3 overloads)
Map<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, { node: NodeIndexnode: 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; edgeData: EedgeData: function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E } | null>()
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>()
// Initialize distances
// Iterate directly over node keys
for (const const node: numbernode of graph: Graph<N, E, T> | MutableGraph<N, E, T>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()) {
const distances: Map<number, number>distances.Map<number, number>.set(key: number, value: number): Map<number, number>Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
set(const node: numbernode, const node: numbernode === config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.source: NodeIndexsource ? 0 : var Infinity: numberInfinity)
const previous: Map<
number,
{ node: NodeIndex; edgeData: E } | null
>
previous.function Map(key: number, value: { node: NodeIndex; edgeData: E } | null): Map<number, { node: NodeIndex; edgeData: E } | null>Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
set(const node: numbernode, null)
}
// Simple priority queue using array (can be optimized with proper heap)
const const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue: interface Array<T>Array<{ node: NodeIndexnode: 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; distance: numberdistance: number }> = [
{ node: numbernode: config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.source: NodeIndexsource, distance: numberdistance: 0 }
]
while (const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue.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) {
// Find minimum distance node (priority queue extract-min)
let let minIndex: numberminIndex = 0
for (let let i: numberi = 1; let i: numberi < const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue.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; let i: numberi++) {
if (const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue[let i: numberi].distance: numberdistance < const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue[let minIndex: numberminIndex].distance: numberdistance) {
let minIndex: numberminIndex = let i: numberi
}
}
const const current: {
node: NodeIndex
distance: number
}
current = const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue.Array<{ node: NodeIndex; distance: number; }>.splice(start: number, deleteCount?: number): {
node: NodeIndex;
distance: number;
}[] (+1 overload)
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
splice(let minIndex: numberminIndex, 1)[0]
const const currentNode: numbercurrentNode = const current: {
node: NodeIndex
distance: number
}
current.node: NodeIndexnode
// Skip if already visited (can happen with duplicate entries)
if (const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const currentNode: numbercurrentNode)) {
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 currentNode: numbercurrentNode)
// Early termination if we reached the target
if (const currentNode: numbercurrentNode === config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.target: NodeIndextarget) {
break
}
// Get current distance
const const currentDistance: numbercurrentDistance = const distances: Map<number, number>distances.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 currentNode: numbercurrentNode)!
// Examine all outgoing edges
const const adjacencyList: number[] | undefinedadjacencyList = graph: Graph<N, E, T> | MutableGraph<N, E, T>graph.Proto<out N, out E>.adjacency: Map<NodeIndex, Array<EdgeIndex>>adjacency.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 currentNode: numbercurrentNode)
if (const adjacencyList: number[] | undefinedadjacencyList !== var undefinedundefined) {
for (const const edgeIndex: numberedgeIndex of const adjacencyList: number[]adjacencyList) {
const const edge: Edge<E> | undefinededge = graph: Graph<N, E, T> | MutableGraph<N, E, T>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 neighbor: numberneighbor = const getTraversableNeighbor: <
E,
T extends Kind
>(
graph:
| Graph<unknown, E, T>
| MutableGraph<unknown, E, T>,
current: NodeIndex,
edge: Edge<E>
) => NodeIndex
getTraversableNeighbor(graph: Graph<N, E, T> | MutableGraph<N, E, T>graph, const currentNode: numbercurrentNode, 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)
const const cost: numbercost = const edgeWeights: Map<number, number>edgeWeights.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 edgeIndex: numberedgeIndex)!
const const newDistance: numbernewDistance = const currentDistance: numbercurrentDistance + const cost: numbercost
const const neighborDistance: numberneighborDistance = const distances: Map<number, number>distances.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 neighbor: numberneighbor)!
// Relaxation step
if (const newDistance: numbernewDistance < const neighborDistance: numberneighborDistance) {
const distances: Map<number, number>distances.Map<number, number>.set(key: number, value: number): Map<number, number>Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
set(const neighbor: numberneighbor, const newDistance: numbernewDistance)
const previous: Map<
number,
{ node: NodeIndex; edgeData: E } | null
>
previous.function Map(key: number, value: { node: NodeIndex; edgeData: E } | null): Map<number, { node: NodeIndex; edgeData: E } | null>Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.
set(const neighbor: numberneighbor, { node: numbernode: const currentNode: numbercurrentNode, edgeData: EedgeData: 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.data })
// Add to priority queue if not visited
if (!const visited: Set<number>visited.Set<number>.has(value: number): booleanhas(const neighbor: numberneighbor)) {
const priorityQueue: Array<{
node: NodeIndex
distance: number
}>
priorityQueue.function Array(...items: Array<{ node: NodeIndex; distance: number }>): numberAppends new elements to the end of an array, and returns the new length of the array.
push({ node: numbernode: const neighbor: numberneighbor, distance: numberdistance: const newDistance: numbernewDistance })
}
}
}
}
}
}
// Check if target is reachable
const const distance: numberdistance = const distances: Map<number, number>distances.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(config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.target: NodeIndextarget)!
if (const distance: numberdistance === var Infinity: numberInfinity) {
return import OptionOption.const none: <A = never>() => Option<A>Creates an Option representing the absence of a value.
When to use
Use to represent a missing or uninitialized value, such as returning "no
result" from a function.
Details
- Returns
Option<never>, which is a subtype of Option<A> for any A
- Always returns the same singleton instance
Example (Creating an empty Option)
import { Option } from "effect"
// ┌─── Option<never>
// ▼
const noValue = Option.none()
console.log(noValue)
// Output: { _id: 'Option', _tag: 'None' }
none() // No path exists
}
// Reconstruct path
const const path: Array<NodeIndex>path: 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 costs: E[]costs: interface Array<T>Array<function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E> = []
let let currentNode: NodeIndex | nullcurrentNode: 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 | null = config: DijkstraConfig<E>(parameter) config: {
source: NodeIndex;
target: NodeIndex;
cost: (edgeData: E) => number;
}
config.DijkstraConfig<E>.target: NodeIndextarget
while (let currentNode: NodeIndex | nullcurrentNode !== null) {
const path: Array<NodeIndex>path.Array<number>.unshift(...items: number[]): numberInserts new elements at the start of an array, and returns the new length of the array.
unshift(let currentNode: NodeIndex | nullcurrentNode)
const const prev: {
node: NodeIndex
edgeData: E
} | null
prev: { node: NodeIndexnode: 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; edgeData: EedgeData: function (type parameter) E in <N, E, T extends Kind = "directed">(graph: Graph<N, E, T> | MutableGraph<N, E, T>, config: DijkstraConfig<E>): Option.Option<PathResult<E>>E } | null = const previous: Map<
number,
{ node: NodeIndex; edgeData: E } | null
>
previous.function Map(key: number): { node: NodeIndex; edgeData: E } | null | 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(let currentNode: NodeIndex | nullcurrentNode)!
if (const prev: {
node: NodeIndex
edgeData: E
} | null
prev !== null) {
const costs: E[]costs.Array<E>.unshift(...items: E[]): numberInserts new elements at the start of an array, and returns the new length of the array.
unshift(const prev: {
node: NodeIndex
edgeData: E
} | null
prev.edgeData: EedgeData)
let currentNode: NodeIndex | nullcurrentNode = const prev: {
node: NodeIndex
edgeData: E
} | null
prev.node: NodeIndexnode
} else {
let currentNode: NodeIndex | nullcurrentNode = null
}
}
return import OptionOption.const some: <A>(value: A) => Option<A>Wraps the given value into an Option to represent its presence.
When to use
Use to wrap a known present value as Option
- Returning a successful result from a partial function
Details
- Always returns
Some<A>
- Does not filter
null or undefined; use
fromNullishOr
for that
Example (Wrapping a value)
import { Option } from "effect"
// ┌─── Option<number>
// ▼
const value = Option.some(1)
console.log(value)
// Output: { _id: 'Option', _tag: 'Some', value: 1 }
some({
path: number[]path,
distance: numberdistance,
costs: E[]costs
})
})