<N, E, T extends Kind = "directed">(
mutable: MutableGraph<N, E, T>,
nodeIndex: NodeIndex
): voidRemoves a node and all its incident edges from a mutable graph.
Example (Removing a node)
import { Graph } from "effect"
const result = Graph.mutate(Graph.directed<string, number>(), (mutable) => {
const nodeA = Graph.addNode(mutable, "Node A")
const nodeB = Graph.addNode(mutable, "Node B")
Graph.addEdge(mutable, nodeA, nodeB, 42)
// Remove nodeA and all edges connected to it
Graph.removeNode(mutable, nodeA)
})export const const removeNode: <
N,
E,
T extends Kind = "directed"
>(
mutable: MutableGraph<N, E, T>,
nodeIndex: NodeIndex
) => void
Removes a node and all its incident edges from a mutable graph.
Example (Removing a node)
import { Graph } from "effect"
const result = Graph.mutate(Graph.directed<string, number>(), (mutable) => {
const nodeA = Graph.addNode(mutable, "Node A")
const nodeB = Graph.addNode(mutable, "Node B")
Graph.addEdge(mutable, nodeA, nodeB, 42)
// Remove nodeA and all edges connected to it
Graph.removeNode(mutable, nodeA)
})
removeNode = <function (type parameter) N in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, nodeIndex: NodeIndex): voidN, function (type parameter) E in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, nodeIndex: NodeIndex): voidE, function (type parameter) T in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, nodeIndex: NodeIndex): voidT 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">(
mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable: 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">(mutable: MutableGraph<N, E, T>, nodeIndex: NodeIndex): voidN, function (type parameter) E in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, nodeIndex: NodeIndex): voidE, function (type parameter) T in <N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>, nodeIndex: NodeIndex): voidT>,
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
): void => {
// Check if node exists
if (!mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable.Proto<N, E>.nodes: Map<NodeIndex, N>nodes.Map<number, N>.has(key: number): booleanhas(nodeIndex: NodeIndexnodeIndex)) {
return // Node doesn't exist, nothing to remove
}
// Collect all incident edges for removal
const const edgesToRemove: Array<EdgeIndex>edgesToRemove: interface Array<T>Array<type EdgeIndex = numberEdge index for edge identification using plain numbers.
When to use
Use when you need to keep the identifier for a graph edge so you can later
read, update, remove, or compare that edge.
Gotchas
An EdgeIndex is an identifier, not an array offset. Removed edge
identifiers are not reused.
EdgeIndex> = []
// Get outgoing edges
const const outgoingEdges: number[] | undefinedoutgoingEdges = mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable.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(nodeIndex: NodeIndexnodeIndex)
if (const outgoingEdges: number[] | undefinedoutgoingEdges !== var undefinedundefined) {
for (const const edge: numberedge of const outgoingEdges: number[]outgoingEdges) {
const edgesToRemove: Array<EdgeIndex>edgesToRemove.Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const edge: numberedge)
}
}
// Get incoming edges
const const incomingEdges: number[] | undefinedincomingEdges = mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable.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(nodeIndex: NodeIndexnodeIndex)
if (const incomingEdges: number[] | undefinedincomingEdges !== var undefinedundefined) {
for (const const edge: numberedge of const incomingEdges: number[]incomingEdges) {
const edgesToRemove: Array<EdgeIndex>edgesToRemove.Array<number>.push(...items: number[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const edge: numberedge)
}
}
// Remove all incident edges
for (const const edgeIndex: numberedgeIndex of const edgesToRemove: Array<EdgeIndex>edgesToRemove) {
const removeEdgeInternal: <
N,
E,
T extends Kind = "directed"
>(
mutable: MutableGraph<N, E, T>,
edgeIndex: EdgeIndex
) => boolean
removeEdgeInternal(mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable, const edgeIndex: numberedgeIndex)
}
// Remove the node itself
mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable.Proto<N, E>.nodes: Map<NodeIndex, N>nodes.Map<number, N>.delete(key: number): booleandelete(nodeIndex: NodeIndexnodeIndex)
mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable.Proto<out N, out E>.adjacency: Map<NodeIndex, Array<EdgeIndex>>adjacency.Map<number, number[]>.delete(key: number): booleandelete(nodeIndex: NodeIndexnodeIndex)
mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable.Proto<out N, out E>.reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>reverseAdjacency.Map<number, number[]>.delete(key: number): booleandelete(nodeIndex: NodeIndexnodeIndex)
// Only invalidate cycle flag if the graph wasn't already known to be acyclic
// Removing nodes cannot introduce cycles in an acyclic graph
const invalidateCycleFlagOnRemoval: <
N,
E,
T extends Kind = "directed"
>(
mutable: MutableGraph<N, E, T>
) => void
invalidateCycleFlagOnRemoval(mutable: MutableGraph<N, E, T>(parameter) mutable: {
type: T;
mutable: true;
nodes: Map<NodeIndex, N>;
edges: Map<EdgeIndex, Edge<E>>;
adjacency: Map<NodeIndex, Array<EdgeIndex>>;
reverseAdjacency: Map<NodeIndex, Array<EdgeIndex>>;
nextNodeIndex: NodeIndex;
nextEdgeIndex: EdgeIndex;
acyclic: Option.Option<boolean>;
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; <…;
toString: () => string;
toJSON: () => unknown;
}
mutable)
}