Graphity for TypeScript 3.0.0
    Preparing search index...

    A builder for a Graph, from nodes and edges added in any order.

    The nodes added so far have the node IDs 0..nodeCount-1, in the order they were added; GraphBuilder.addNode and GraphBuilder.addNodes return the IDs of new ones. Edges name their source and target by node ID, in any order and as often as the input has them. Here, the calls between the methods of a program are built from its call sites, one edge per call site; names holds the names of the methods:

    const builder = new GraphBuilder();
    builder.addNodes(names.length); // one node per method, numbered as in names
    for (const site of callSites) {
    builder.addEdge(site.caller, site.callee);
    }
    const calls = builder.build();
    // 14 call sites, 12 edges: run -> show and parse -> parse were added twice

    GraphBuilder.build sorts the edges into edge index order and merges duplicates. The builder stays usable afterwards: more nodes and edges can be added, and build() called again.

    Besides single edges, the builder takes edges in batches: the out-edges or in-edges of one node (GraphBuilder.addOutEdges, GraphBuilder.addInEdges), a path (GraphBuilder.addPath), pairs of node IDs (GraphBuilder.addEdgePairs), the edges of an algorithm result (GraphBuilder.addEdges) or a whole graph (GraphBuilder.addGraph). GraphBuilder.addChild, GraphBuilder.addParent and their plural forms add new nodes together with their edges. The out-edges, in-edges and paths take their node IDs as loose arguments or as an IntList, such as the SortedIntView that Graph.outNodes returns. So a result of this library goes in without being copied into an array first. A list is read during the call and not kept. Here, part of the dependencies between the packages of a program is built, with the node IDs of dependencies:

    const builder = new GraphBuilder();
    builder.addNodes(dependencies.nodeCount);
    builder.addOutEdges(0, 1, 2, 3); // loose arguments
    builder.addOutEdges(2, [3, 4, 7]); // an array or a typed array
    builder.addOutEdges(3, dependencies.outNodes(3)); // a SortedIntView, without a copy
    const someDependencies = builder.build();
    // app -> [cli, ui, service], ui -> [service, model, util],
    // service -> [model, storage]

    A method that takes the node ID of an existing node throws a RangeError if it is not in 0..nodeCount-1, so that an edge to a missing node fails at the call. A batch method that throws adds nothing. The methods that add edges between given nodes each have a twin with the suffix AndNodes, such as GraphBuilder.addEdgeAndNodes, which adds missing nodes instead. A twin raises the node count to one more than the highest node ID it is given, and the nodes in between are added too, without edges. So a graph can be built from input numbered elsewhere without counting its nodes first. However, a node ID that is too high adds nodes instead of failing.

    Nodes and edges cannot be removed. To leave nodes out, build the graph and take a subgraph with Graphs.remapped and NodeMapping.forSubgraph.

    Instances are mutable.

    Memory: 8 bytes per added edge, duplicates included, plus room to grow.

    A builder holds at most 2^31 - 10 nodes, as many as a graph can have. This number is the node limit. A method that would add more throws before it changes anything. A builder builds a graph from at most 2^31 - 9 added edges, duplicates included. This number is the edge limit. A batch method that would add more throws an Error and adds nothing. The single-edge methods GraphBuilder.addEdge, GraphBuilder.addEdgeAndNodes, GraphBuilder.addChild and GraphBuilder.addParent do not check the edge limit: beyond it, GraphBuilder.build throws an Error, and the single-edge methods throw once the engine cannot grow the edge storage any further (Java: OutOfMemoryError).

    Index
    • Adds a node with an edge from parent to it, and returns its node ID.

      Time: O(1) amortized.

      Parameters

      • parent: number

        node ID in 0..nodeCount-1

      Returns number

      node ID of the new node

      Error if the node count has reached the node limit (Java: IllegalStateException)

      RangeError if parent is out of range

    • Adds count nodes, each with an edge from parent to it, and returns the node ID of the first; the others follow in order.

      Time: O(count) amortized.

      Parameters

      • parent: number

        node ID in 0..nodeCount-1

      • count: number

        number of nodes to add

      Returns number

      node ID of the first new node

      Error if count is not an integer, is negative, or the nodes or edges would exceed their limit

      RangeError if parent is out of range

    • Adds the edge source -> target.

      An edge added more than once is still one edge in the graph: GraphBuilder.build merges duplicates.

      Time: O(1) amortized.

      Parameters

      • source: number

        node ID in 0..nodeCount-1

      • target: number

        node ID in 0..nodeCount-1

      Returns void

      RangeError if source or target is out of range

    • Adds the edge source -> target, and the missing nodes up to the higher of the two node IDs.

      Time: O(1) amortized.

      Parameters

      • source: number

        node ID, 0 or more

      • target: number

        node ID, 0 or more

      Returns void

      RangeError if source or target is negative or not below the node limit

    • Adds an edge for each pair of node IDs in endpoints: endpoints[0] -> endpoints[1], endpoints[2] -> endpoints[3], and so on.

      Time: O(number of endpoints) amortized.

      Parameters

      • endpoints: ArrayLike<number>

        node IDs in 0..nodeCount-1, source and target of each edge in turn, as one array or as loose arguments

      Returns void

      Error if endpoints has an odd length, or the edges would exceed the edge limit

      RangeError if a node of endpoints is out of range

    • Adds an edge for each pair of node IDs in endpoints: endpoints[0] -> endpoints[1], endpoints[2] -> endpoints[3], and so on.

      Time: O(number of endpoints) amortized.

      Parameters

      • ...endpoints: number[]

        node IDs in 0..nodeCount-1, source and target of each edge in turn, as one array or as loose arguments

      Returns void

      Error if endpoints has an odd length, or the edges would exceed the edge limit

      RangeError if a node of endpoints is out of range

    • Adds an edge for each pair of node IDs in endpoints, as GraphBuilder.addEdgePairs does, and the missing nodes up to the highest of these node IDs.

      Time: O(number of endpoints) amortized.

      Parameters

      • endpoints: ArrayLike<number>

        node IDs, 0 or more, source and target of each edge in turn, as one array or as loose arguments

      Returns void

      Error if endpoints has an odd length, or the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds an edge for each pair of node IDs in endpoints, as GraphBuilder.addEdgePairs does, and the missing nodes up to the highest of these node IDs.

      Time: O(number of endpoints) amortized.

      Parameters

      • ...endpoints: number[]

        node IDs, 0 or more, source and target of each edge in turn, as one array or as loose arguments

      Returns void

      Error if endpoints has an odd length, or the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds the edges of an algorithm result, such as BackEdges.of or Biconnectivity.bridges.

      Only the sources and targets are read: the edge indices of edges belong to the graph the algorithm ran on.

      Time: O(edges.count) amortized.

      Parameters

      • edges: Edges

        the edges to add, between nodes in 0..nodeCount-1

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a source or target is out of range

    • Adds the edges of an algorithm result, as GraphBuilder.addEdges does, and the missing nodes up to the highest of their node IDs.

      Time: O(edges.count) amortized.

      Parameters

      • edges: Edges

        the edges to add

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a source or target is not below the node limit

    • Adds the nodes and edges of graph after the nodes already added, and returns the offset of its node IDs.

      Node v of graph becomes node offset + v of this builder, where the offset is the node count before the call. Into an empty builder, the node IDs stay the same, which makes this the way to extend a graph: a Graph is immutable, but a builder that starts with it can add to it and build a new one. Here, the dependencies between the packages of a program are extended to a next version, in which ui also uses storage:

      const builder = new GraphBuilder();
      // into an empty builder: the node IDs stay the same
      builder.addGraph(dependencies);
      builder.addEdge(ui, storage);
      const nextVersion = builder.build();

      Time: O(graph.nodeCount + graph.edgeCount) amortized.

      Parameters

      • graph: Graph

        the graph to add

      Returns number

      the offset: node ID of node 0 of graph in this builder

      Error if the nodes or edges of graph would exceed their limit

    • Adds an edge from each node of sources to target.

      Time: O(number of sources) amortized.

      Parameters

      • target: number

        node ID in 0..nodeCount-1

      • sources: IntList

        node IDs in 0..nodeCount-1

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if target or a node of sources is out of range

    • Adds an edge from each node of sources to target.

      Time: O(sources.length) amortized.

      Parameters

      • target: number

        node ID in 0..nodeCount-1

      • ...sources: number[]

        node IDs in 0..nodeCount-1

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if target or a node of sources is out of range

    • Adds an edge from each node of sources to target, and the missing nodes up to the highest of these node IDs.

      Time: O(number of sources) amortized.

      Parameters

      • target: number

        node ID, 0 or more

      • sources: IntList

        node IDs, 0 or more

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds an edge from each node of sources to target, and the missing nodes up to the highest of these node IDs.

      Time: O(sources.length) amortized.

      Parameters

      • target: number

        node ID, 0 or more

      • ...sources: number[]

        node IDs, 0 or more

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds a node and returns its node ID, which is the next in order.

      Time: O(1).

      Returns number

      node ID of the new node

      Error if the node count has reached the node limit (Java: IllegalStateException)

    • Adds count nodes and returns the node ID of the first; the others follow in order.

      Time: O(1).

      Parameters

      • count: number

        number of nodes to add

      Returns number

      node ID of the first new node

      Error if count is not an integer, is negative, or the nodes would exceed the node limit

    • Adds an edge from source to each node of targets.

      Time: O(number of targets) amortized.

      Parameters

      • source: number

        node ID in 0..nodeCount-1

      • targets: IntList

        node IDs in 0..nodeCount-1

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if source or a node of targets is out of range

    • Adds an edge from source to each node of targets.

      Time: O(targets.length) amortized.

      Parameters

      • source: number

        node ID in 0..nodeCount-1

      • ...targets: number[]

        node IDs in 0..nodeCount-1

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if source or a node of targets is out of range

    • Adds an edge from source to each node of targets, and the missing nodes up to the highest of these node IDs.

      Time: O(number of targets) amortized.

      Parameters

      • source: number

        node ID, 0 or more

      • targets: IntList

        node IDs, 0 or more

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds an edge from source to each node of targets, and the missing nodes up to the highest of these node IDs.

      Time: O(targets.length) amortized.

      Parameters

      • source: number

        node ID, 0 or more

      • ...targets: number[]

        node IDs, 0 or more

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds a node with an edge from it to child, and returns its node ID.

      Time: O(1) amortized.

      Parameters

      • child: number

        node ID in 0..nodeCount-1

      Returns number

      node ID of the new node

      Error if the node count has reached the node limit (Java: IllegalStateException)

      RangeError if child is out of range

    • Adds count nodes, each with an edge from it to child, and returns the node ID of the first; the others follow in order.

      Time: O(count) amortized.

      Parameters

      • child: number

        node ID in 0..nodeCount-1

      • count: number

        number of nodes to add

      Returns number

      node ID of the first new node

      Error if count is not an integer, is negative, or the nodes or edges would exceed their limit

      RangeError if child is out of range

    • Adds a path through nodes: an edge from each node of the list to the next.

      Fewer than two nodes add no edge.

      Time: O(number of nodes) amortized.

      Parameters

      • nodes: IntList

        node IDs in 0..nodeCount-1, in the order of the path

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node of nodes is out of range

    • Adds a path through nodes: an edge from each node of the array to the next.

      Fewer than two nodes add no edge.

      Time: O(nodes.length) amortized.

      Parameters

      • ...nodes: number[]

        node IDs in 0..nodeCount-1, in the order of the path

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node of nodes is out of range

    • Adds a path through nodes, and the missing nodes up to the highest of these node IDs. The path has an edge from each node of the list to the next.

      Fewer than two nodes add no edge, but still add the nodes.

      Time: O(number of nodes) amortized.

      Parameters

      • nodes: IntList

        node IDs, 0 or more, in the order of the path

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Adds a path through nodes, and the missing nodes up to the highest of these node IDs. The path has an edge from each node of the array to the next.

      Fewer than two nodes add no edge, but still add the nodes.

      Time: O(nodes.length) amortized.

      Parameters

      • ...nodes: number[]

        node IDs, 0 or more, in the order of the path

      Returns void

      Error if the edges would exceed the edge limit

      RangeError if a node ID is negative or not below the node limit

    • Returns a graph with the nodes and edges added so far.

      The edges are sorted into edge index order (by source and then by target), and duplicates are merged, so an edge does not keep the position at which it was added; Graph.edgeIndexOf finds where it went. The builder does not change: more nodes and edges can be added and built into another graph.

      Time: O(nodeCount + k log d), where k is the number of added edges, duplicates included, and d the most of them with the same source. Memory: 4 x (nodeCount + edgeCount) bytes for the result; while it runs, briefly up to 4 x (nodeCount + k) bytes more.

      Returns Graph

      the graph

      Error if the edges added exceed the edge limit

    • Makes room for totalEdges edges in total, so that adding up to that many allocates nothing more.

      The argument counts all edges the builder will hold, duplicates included, not the ones still to come. It can be called at any time and does not change the graph that GraphBuilder.build returns.

      Time: O(totalEdges) if the room does not suffice yet, otherwise O(1). Memory: 8 x totalEdges bytes if the room does not suffice yet, otherwise none.

      Parameters

      • totalEdges: number

        the number of edges to make room for, duplicates included

      Returns void

      Error if totalEdges is not an integer, negative or above the edge limit