Class Graph<N, E>

Type Parameters

Hierarchy

  • Graph

Constructors

  • Create a new Graph instance.

    Type Parameters

    Parameters

    • Optional options: GraphOptions<N, E>

      The options to initialize a graph. See GraphOptions.

      const graph = new Graph({
      // Optional, initial nodes.
      nodes: [
      // Each node has a unique ID.
      { id: 'A', foo: 1 },
      { id: 'B', foo: 1 },
      ],
      // Optional, initial edges.
      edges: [
      { id: 'C', source: 'B', target: 'B', weight: 1 },
      ],
      // Optional, called with a GraphChangedEvent.
      onChanged: (event) => {
      console.log(event);
      }
      });

    Returns Graph<N, E>

Properties

batchCount: number = 0
changes: GraphChange<N, E>[] = []
edgeMap: Map<ID, Edge<E>> = ...
inEdgesMap: Map<ID, Set<Edge<E>>> = ...
nodeMap: Map<ID, Node<N>> = ...
onChanged: ((event: GraphChangedEvent<N, E>) => void) = ...

Type declaration

outEdgesMap: Map<ID, Set<Edge<E>>> = ...
treeIndices: TreeIndices<Node<N>> = ...

EdgeMethods

  • Add a single edge pointing from source to target into the graph.

    graph.addNode({ id: 'NodeA' });
    graph.addNode({ id: 'NodeB' });
    graph.addEdge({ id: 'EdgeA', source: 'NodeA', target: 'NodeB' });

    If source or target were not found in the current graph, it throws an Error.

    Parameters

    Returns void

  • Add all edges of the given iterable(an array, a set, etc.) into the graph.

    Parameters

    • edges: Iterable<Edge<E>>

    Returns void

  • Get the edge, the source node, and the target node by an edge ID.

    Parameters

    Returns {
        edge: Edge<E>;
        source: Node<N>;
        target: Node<N>;
    }

  • Parameters

    • id: ID
    • patch: Partial<E>

    Returns void

  • Remove a single edge of the given id.

    Parameters

    Returns void

  • Remove edges whose id was included in the given id list.

    Parameters

    • idList: ID[]

    Returns void

  • Update edge data.

    Type Parameters

    • P extends string | number | symbol

    Parameters

    • id: ID
    • propertyName: P
    • value: E[P]

    Returns void

  • Change the source of an edge. The source must be found in current graph.

    Parameters

    Returns void

  • Change the target of an edge. The target must be found in current graph.

    Parameters

    Returns void

NodeMethods

  • Add a single node into the graph.

    Parameters

    Returns void

  • Add all nodes of the given array, or iterable, into the graph.

    Parameters

    • nodes: Iterable<Node<N>>

    Returns void

  • Tell if two nodes are neighbors.

    Parameters

    • firstNodeId: ID
    • secondNodeId: ID

    Returns boolean

  • Get the degree of the given node.

    Parameters

    • id: ID
    • Optional direction: "in" | "out" | "both"

    Returns number

  • Given a node ID, find its neighbors.

    Parameters

    • id: ID

      ID of the node

    Returns Node<N>[]

  • Given a node ID, find all edges of the node.

    Parameters

    • id: ID

      ID of the node

    • Optional direction: "in" | "out" | "both"

      Edge direction, defaults to 'both'.

    Returns Edge<E>[]

  • Check if an edge exists in the graph.

    Parameters

    Returns boolean

  • Check if a node exists in the graph.

    Parameters

    Returns boolean

  • Remove a single node and its attached edges from the graph.

    Parameters

    Returns void

  • Remove nodes and their attached edges from the graph.

    Parameters

    • idList: ID[]

    Returns void

  • Update node data.

    Type Parameters

    • P extends string | number | symbol

    Parameters

    • id: ID
    • propertyName: P
    • value: N[P]

    Returns void

TreeMethods

  • Traverse the given tree data, add each node into the graph, then attach the tree structure.

    graph.addTree({
    id: 1,
    children: [
    { id: 2 },
    { id: 3 },
    ],
    }, 'Inheritance');
    graph.getRoots('Inheritance'); // [1]
    graph.getChildren(1, 'Inheritance'); // [2, 3]
    graph.getAllNodes(); // [1, 2, 3]
    graph.getAllEdges(); // []

    Parameters

    Returns void

  • Attach a new tree structure representing the hierarchy of all nodes in the graph.

    Parameters

    • Optional treeKey: string

      A unique key of the tree structure. You can attach multiple tree structures with different keys.

      const graph = new Graph({
      nodes: [{ id: 1 }, { id: 2 }, { id: 3 }],
      });
      graph.attachTreeStructure('Inheritance');
      graph.setParent(2, 1, 'Inheritance');
      graph.setParent(3, 1, 'Inheritance');
      graph.getRoots('Inheritance'); // [1]
      graph.getChildren(1, 'Inheritance'); // [2,3]

    Returns void

  • Detach the tree structure of the given tree key from the graph.

    graph.detachTreeStructure('Inheritance');
    graph.getRoots('Inheritance'); // Error!

    Parameters

    • Optional treeKey: string

    Returns void

  • Given a node ID and an optional tree key, get the children of the node in the specified tree structure.

    Parameters

    • id: ID
    • Optional treeKey: string

    Returns Node<N>[]

  • Given a node ID and an optional tree key, get the parent of the node in the specified tree structure. If the given node is one of the tree roots, this returns null.

    Parameters

    • id: ID
    • Optional treeKey: string

    Returns null | Node<N>

  • Get the root nodes of an attached tree structure.

    Consider a graph with the following tree structure attached:

    Tree structure:
    O 3
    / \ |
    1 2 4

    graph.getRoots() takes all nodes without a parent, therefore [0, 3] was returned.

    Newly added nodes are also unparented. So they are counted as roots.

    graph.addNode({ id: 5 });
    graph.getRoots(); // [0, 3, 5]

    Here is how the tree structure looks like:

    Tree structure:
    O 3 5
    / \ |
    1 2 4

    By setting a parent, a root node no more be a root.

    graph.setParent(5, 2);
    graph.getRoots(); // [0, 3]

    The tree structure now becomes:

    Tree structure:
    O 3
    / \ |
    1 2 4
    |
    5

    Removing a node forces its children to be unparented, or roots.

    graph.removeNode(0);
    graph.getRoots(); // [1, 2, 3]

    You might draw the the structure as follow:

    Tree structure:
    1 2 3
    | |
    5 4

    Parameters

    • Optional treeKey: string

    Returns Node<N>[]

  • Set node parent. If this operation causes a circle, it fails with an error.

    Parameters

    • id: ID

      ID of the child node.

    • parent: ID

      ID of the parent node.

    • Optional treeKey: string

      Which tree structure the relation is applied to.

    Returns void

Methods

  • Batch several graph changes into one.

    Make several changes, but dispatch only one ChangedEvent at the end of batch:

    graph.batch(() => {
    graph.addNodes([]);
    graph.addEdges([]);
    });

    Batches can be nested. Only the outermost batch will dispatch a ChangedEvent:

    graph.batch(() => {
    graph.addNodes([]);
    graph.batch(() => {
    graph.addEdges([]);
    });
    });

    Parameters

    • fn: (() => void)
        • (): void
        • Returns void

    Returns void

  • Parameters

    • id: ID
    • fn: ((node: Node<N>) => void)
        • (node: Node<N>): void
        • Parameters

          Returns void

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Parameters

    • treeKey: undefined | string

    Returns void

  • Reset changes and dispatch a ChangedEvent.

    Returns void

  • Parameters

    • id: ID
    • fn: ((node: Node<N>) => void)
        • (node: Node<N>): void
        • Parameters

          Returns void

    Returns void

  • Parameters

    Returns void

  • Parameters

    Returns void

  • Get all edges in the graph as an array.

    Returns Edge<E>[]

  • Get all nodes in the graph as an array.

    Returns Node<N>[]

  • Get all predecessors of the given node.

    Parameters

    Returns Node<N>[]

  • Get all successors of the given node.

    Parameters

    Returns Node<N>[]

  • Like Object.assign, merge all properties of path to the node data.

    Parameters

    • id: ID

      Node ID.

    • patch: Partial<N>

      A data object to merge.

    Returns void

Generated using TypeDoc