Graphity for TypeScript 3.0.0
    Preparing search index...

    A Graph that also holds the in-nodes of every node, for algorithms that follow edges against their direction.

    BiAdjacentGraph.of builds the in-adjacency of a graph - the in-nodes of every node. The result has the node IDs and edge indices of the graph and shares the out-nodes of every node, called its out-adjacency. Algorithms that need in-nodes, such as Dominators, take a BiAdjacentGraph, so that the in-adjacency is built once and serves all of them. BiAdjacentGraph.transposed reverses every edge without copying, and BiAdjacentGraph.uniAdjacentGraph drops the in-adjacency again.

    The in-nodes of a node are in ascending order, like its out-nodes. A BiAdjacentGraph equals a Graph with the same nodes and edges. Instances are immutable.

    Memory: 8 x (nodeCount + edgeCount) bytes, half of it for the out-adjacency, which is shared with the graph it was built from.

    Hierarchy (View Summary)

    Index
    • get isEmpty(): boolean

      Whether this graph has no nodes. Equivalent to nodeCount === 0.

      Time: O(1).

      Returns boolean

    • Returns whether node has an edge to itself, called a self-loop. Equivalent to hasOutNode(node, node).

      Time: O(log outDegree(node)).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns boolean

      true if the edge node -> node exists

      RangeError if node is out of range

    • Returns the degree of node. Equivalent to inDegree(node) + outDegree(node).

      It counts edges, not neighbors: a self-loop counts twice, and so do the edges node -> v and v -> node, although v is one neighbor.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      degree

      RangeError if node is out of range

    • Returns the edge index of the edge source -> target, or -1 if there is none.

      GraphBuilder sorts the edges and merges duplicates, so an edge does not keep the position at which it was added; this method finds where it went. Here, calls was built with one edge per call site, and the call sites of each call are counted:

      const siteCount = new Int32Array(calls.edgeCount);
      for (const site of callSites) {
      siteCount[calls.edgeIndexOf(site.caller, site.callee)]++;
      }
      // 2 for run -> show and for parse -> parse, 1 for every other edge

      Time: O(log outDegree(source)).

      Parameters

      • source: number

        node ID in 0..nodeCount-1

      • target: number

        node ID; a value out of range yields -1

      Returns number

      edge index in 0..edgeCount-1, or -1

      RangeError if source is out of range

    • Returns the source of edge edgeIndex.

      Unlike the target, the source is searched for; Graph.forEachEdge gives every edge with its source without a search.

      Time: O(log nodeCount).

      Parameters

      • edgeIndex: number

        edge index in 0..edgeCount-1

      Returns number

      source node ID

      RangeError if edgeIndex is out of range

    • Returns the target of edge edgeIndex.

      Time: O(1).

      Parameters

      • edgeIndex: number

        edge index in 0..edgeCount-1

      Returns number

      target node ID

      RangeError if edgeIndex is out of range

    • Returns whether other is a graph with the same nodes and edges (Java: equals(Object)).

      The in-adjacency does not count: a BiAdjacentGraph equals a Graph with the same nodes and edges.

      Time: O(nodeCount + edgeCount).

      Parameters

      • other: unknown

      Returns boolean

    • Returns a hash code over the nodes and edges, consistent with Graph.equals. It has the same value as in Java.

      It is computed on every call, so each lookup of a graph by this hash walks the whole graph. Graphs.topologyHash gives a 64-bit hash to compute once and keep.

      Time: O(nodeCount + edgeCount).

      Returns number

    • Returns whether the edge inNode -> node exists.

      Time: O(log inDegree(node)).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      • inNode: number

        node ID; a value out of range yields false

      Returns boolean

      true if the edge exists

      RangeError if node is out of range

    • Returns whether neighbor is a neighbor of node. Equivalent to hasOutNode(node, neighbor) || hasInNode(node, neighbor).

      Time: O(log outDegree(node) + log inDegree(node)).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      • neighbor: number

        node ID; a value out of range yields false

      Returns boolean

      true if neighbor is an in-node or an out-node of node

      RangeError if node is out of range

    • Returns whether the edge node -> outNode exists.

      Time: O(log outDegree(node)).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      • outNode: number

        node ID; a value out of range yields false

      Returns boolean

      true if the edge exists

      RangeError if node is out of range

    • Returns the in-degree of node. That is the number of edges with node as target.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      in-degree

      RangeError if node is out of range

    • Returns the in-nodes of node in ascending order. These are the sources of the edges with node as target.

      The result is a view of this graph's own array, not a copy.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns SortedIntView

      in-nodes

      RangeError if node is out of range

    • Returns whether node is a root node. Equivalent to inDegree(node) === 0.

      Graphs.rootNodes returns all root nodes.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns boolean

      true if no edge has node as target

      RangeError if node is out of range

    • Returns the neighbors of node in ascending order and without duplicates. These are its in-nodes and out-nodes.

      Unlike BiAdjacentGraph.inNodes and Graph.outNodes, which are views, the result is a copy, merged on every call.

      Time: O(inDegree(node) + outDegree(node)). Memory: up to 4 x (inDegree(node) + outDegree(node)) bytes for the result; while it runs, up to as much again if a neighbor is both an in-node and an out-node.

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns SortedIntView

      neighbors

      RangeError if node is out of range

    • Returns the node IDs 0..nodeCount-1 as a generator, in ascending order (Java: an IntStream).

      Time: O(1) to create the generator.

      Returns Generator<number, void, undefined>

      node IDs

    • Returns the out-degree of node. That is the number of edges with node as source.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      out-degree

      RangeError if node is out of range

    • Returns the end, exclusive, of the edge index range of the out-edges of node; see Graph.outEdgeStart.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      end of the range, exclusive

      RangeError if node is out of range

    • Returns the start of the edge index range of the out-edges of node. The out-edges are the edges with node as source.

      Their edge indices are outEdgeStart(node)..outEdgeEnd(node)-1, in the order of Graph.outNodes; the range is empty if node has no out-edges. A loop over the range reads the data of each edge from arrays indexed by edge index. Here, it lists the methods that run calls, with the number of call sites of each call in siteCount:

      for (let edge = calls.outEdgeStart(run); edge < calls.outEdgeEnd(run); edge++) {
      console.log(`${names[calls.edgeTarget(edge)]}: ${siteCount[edge]}`);
      }
      // load: 1
      // save: 1
      // show: 2

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      start of the range

      RangeError if node is out of range

    • Returns the out-nodes of node in ascending order. These are the targets of the edges with node as source.

      The result is a view of this graph's own array, not a copy.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns SortedIntView

      out-nodes

      RangeError if node is out of range

    • Returns the node and edge counts and the out-nodes of the first nodes, for debuggers and logs. For the control flow of a method with a loop:

      const text = cfg.toString();
      // Graph[nodes=6, edges=7] {0 -> [1], 1 -> [2, 5], 2 -> [3, 4], 3 -> [4], 4 -> [1],
      // 5 -> []}

      The text stays short however large the graph: it lists at most 16 nodes and 64 out-nodes in total. ... marks where it is cut - after the 16th node, or inside the list that would go beyond 64 out-nodes. A BiAdjacentGraph is named as such and lists its out-nodes. Graphs.adjacencyListOf gives the whole graph as text.

      Time: O(1), as the text is bounded.

      Returns string

    • Returns the transposed graph as a view of this graph's arrays. The transposed graph has every edge reversed.

      Node IDs stay the same, edge indices do not: edge e of the transposed graph is in general not the reverse of edge e of this graph. BiAdjacentGraph.transposedEdgeIndex translates one edge index, and Graphs.transposedEdgeMapping translates all of them.

      A plain Graph needs its in-adjacency first: BiAdjacentGraph.of(graph).transposed(). Here, the packages of a program that depend on model, directly or indirectly, are the ones reachable from it against the direction of the dependencies:

      const model = 4;
      const usedBy = BiAdjacentGraph.of(dependencies).transposed();
      const affected = Graphs.reachableFrom(usedBy, model);
      // affected: [0, 2, 3, 4, 5] - app, ui, service, model and storage

      Time: O(1).

      Returns BiAdjacentGraph

      the transposed graph

    • Returns the edge index that edge edgeIndex has in the transposed graph.

      Called on BiAdjacentGraph.transposed, it translates back: transposed().transposedEdgeIndex(transposedEdgeIndex(e)) === e. To translate every edge, Graphs.transposedEdgeMapping needs one pass instead of a search per edge; it maps from the transposed graph to this one.

      Time: O(log nodeCount).

      Parameters

      • edgeIndex: number

        edge index in 0..edgeCount-1

      Returns number

      edge index in the transposed graph

      RangeError if edgeIndex is out of range

    • Returns a Graph with the same nodes and edges, without the in-adjacency.

      The result shares the out-adjacency of this graph; nothing is copied. Once only the result is kept, the in-adjacency can be garbage-collected.

      Time: O(1).

      Returns Graph

      a graph over the out-adjacency of this graph

    • Writes this graph to out, for Graph.readFrom to read back with the same node IDs and edge indices.

      Only the structure is written. Data in arrays indexed by node ID or edge index stays valid for the graph read back and can follow it in the same file or stream. Here, the names of the methods of a program follow the calls between them:

      const fd = openSync(file, "w");
      try {
      calls.writeTo({ write: (chunk) => writeSync(fd, chunk) });
      writeSync(fd, JSON.stringify(names));
      } finally {
      closeSync(fd);
      }

      A BiAdjacentGraph writes its out-adjacency only.

      The format is the two arrays that Graph.of takes, preceded by the node count and the edge count. Every field is a signed 32-bit integer in little-endian byte order:

      offset in bytes size in bytes field
      0 4 nodeCount
      4 4 edgeCount
      8 4 x (nodeCount + 1) outAdjacencyStart
      8 + 4 x (nodeCount + 1) 4 x edgeCount outAdjacency

      The arrays follow the rules of Graph.of. Nothing else is written: no padding, checksum, magic number or version. So a graph takes exactly 8 + 4 x (nodeCount + 1 + edgeCount) bytes, and Graph.readFrom recognizes foreign data only where it does not form a valid graph. Every implementation of Graphity reads and writes this format, so a graph written in one language can be read in another.

      Passes the header and then the arrays. Where the platform is little-endian, each array goes in one chunk, which is a view of the graph's own array, so nothing is copied. Otherwise the chunks are little-endian copies in blocks of 64 KiB. A file stream therefore needs no buffering of its own.

      Time: O(nodeCount + edgeCount). Memory: 8 bytes for the header; where the platform is big-endian, 4 x (nodeCount + 1 + edgeCount) bytes for the copies.

      Parameters

      • out: ByteSink

        where the bytes go, for instance a file stream of Node.js; not closed

      Returns void

      Error what out.write throws (Java: IOException); only a write that throws fails the call. A stream reports its errors through its own event or promise, which the caller must watch (see ByteSink)

    • Returns a BiAdjacentGraph with the nodes and edges of graph, building its in-adjacency.

      Node IDs and edge indices stay the same, and the out-adjacency is shared with graph, not copied. The in-adjacency is built anew on every call, not cached: keep the result to use it more than once. If graph already is a BiAdjacentGraph, returns it.

      Time: O(nodeCount + edgeCount), or O(1) if already bi-adjacent. Memory: +4 x (nodeCount + edgeCount) bytes, or none if already bi-adjacent.

      Parameters

      • graph: Graph

        the graph to build the in-adjacency for

      Returns BiAdjacentGraph

      the graph with in-adjacency

    • Returns a Graph from the two arrays of its CSR layout, without in-adjacency. This is Graph.of, inherited as Java's static methods are.

      Parameters

      • outAdjacencyStart: ArrayLike<number>
      • outAdjacency: ArrayLike<number>

      Returns Graph

    • Reads a graph that Graph.writeTo wrote, with the same node IDs and edge indices.

      Reads the graph at the start of bytes. Data written after the graph follows it at offset 8 + 4 x (nodeCount + 1 + edgeCount); the bytes from there on are not read. Here, that data is the names of the methods of a program:

      const bytes = readFileSync(file);
      // equal to the graph written, edge indices included
      const calls = Graph.readFrom(bytes);
      // The names follow the graph, which takes 8 + 4 x (nodeCount + 1 + edgeCount) bytes.
      const namesStart = 8 + 4 * (calls.nodeCount + 1 + calls.edgeCount);
      const names: string[] = JSON.parse(bytes.subarray(namesStart).toString("utf8"));

      The result has no in-adjacency, even if the data was written from a BiAdjacentGraph and even when called as BiAdjacentGraph.readFrom(bytes); BiAdjacentGraph.of builds it again.

      The arrays are checked as by Graph.of, but not copied. Where the platform is little-endian and an array lies 4-byte aligned in the buffer of bytes, the graph keeps a view of that buffer, which must not change afterwards. Otherwise it keeps a converted copy. The length of the data is checked against the header before anything is allocated, so a header that announces more than the data holds ends in an Error, not in an allocation of the announced size.

      Time: O(nodeCount + edgeCount). Memory: none for views, which keep the whole buffer of bytes reachable as long as the graph is; otherwise 4 x (nodeCount + edgeCount) bytes for the result. To keep only the graph, copy it with Graph.of, or pass a copy of its bytes: bytes.slice(0, 8 + 4 * (nodeCount + 1 + edgeCount)).

      Parameters

      • bytes: Uint8Array

        the data to read, for instance the content of a file

      Returns Graph

      the graph

      Error if the data ends before the graph does (Java: EOFException), a count in the header is negative or larger than a graph can have, or the arrays do not form a graph that Graph.of accepts, whose error is then the cause (Java: IOException)