Skip to content

Function: addEdge()

ts
function addEdge(
   g: Graph, 
   tail: Node, 
   head: Node, 
   name?: string): Edge;

Defined in: api/edge-ops.ts:96

Create and insert an edge from tail to head in graph g.

In a strict graph, at most one edge between any (tail, head) pair exists (symmetric for undirected). When a match is found the existing edge is returned without modification. This matches C agedge with cflag=1.

Most callers building a graph programmatically should prefer createGraph().addEdge(...) (the GvGraphBuilder method), which accepts node names or handles. Use this lower-level addEdge when you already hold Graph/Node references — e.g. edges added onto a graph returned by parse().

Parameters

g

Graph

Owning graph or subgraph; root derived via g.root.

tail

Node

Source node (AGTAIL).

Node

Destination node (AGHEAD).

name?

string

Edge key; defaults to empty string for anonymous edges. Ignored for strict-graph dedup (wildcard match).

Returns

Edge

The new (or existing, for strict graphs) edge.

See

  • lib/cgraph/cgraph.h:AGTAIL
  • lib/cgraph/cgraph.h:AGHEAD
  • lib/cgraph/edge.c:agedge

Example

ts
import { parse, addEdge } from '@knowvah/dot-engine';

const g = parse('digraph { a; b; }');
const a = g.nodes.get('a')!;
const b = g.nodes.get('b')!;
const edge = addEdge(g, a, b, 'ab1');
// edge.tail === a, edge.head === b