Hyperlinkv0.8.0-beta.28

Graph

Graph.reverseconsteffect/Graph.ts:1116
<N, E, T extends Kind = "directed">(mutable: MutableGraph<N, E, T>): void

Swaps source and target nodes for every edge in a mutable graph.

Example (Reversing edge directions)

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, 1) // A -> B
  Graph.addEdge(mutable, b, c, 2) // B -> C
  Graph.reverse(mutable) // Now B -> A, C -> B
})

const edge0 = Graph.getEdge(graph, 0)
console.log(edge0) // Option.some(new Graph.Edge({ source: 1, target: 0, data: 1 }))
transforming
Source effect/Graph.ts:111624 lines
export const reverse = <N, E, T extends Kind = "directed">(
  mutable: MutableGraph<N, E, T>
): void => {
  if (mutable.type === "undirected") {
    return
  }

  // Reverse all edges by swapping source and target
  for (const [index, edgeData] of mutable.edges) {
    mutable.edges.set(
      index,
      new Edge({
        source: edgeData.target,
        target: edgeData.source,
        data: edgeData.data
      })
    )
  }

  rebuildAdjacency(mutable)

  // Invalidate cycle flag since edge directions changed
  mutable.acyclic = Option.none()
}