Graphity for TypeScript 3.0.0
    Preparing search index...

    The distances of the nodes of a graph from one or several sources, with a shortest path to every node reached.

    The sources are the nodes where the search starts. The distance of a node is the number of edges on a shortest path to it from the nearest source. A source has distance 0, and a node that no source reaches has distance -1. Edges are followed in their direction. For distances against their direction, pass the transposed graph: BiAdjacentGraph.of(graph).transposed(). Here, calls are the calls between the methods of a program, and the search finds a shortest chain of calls from main to parse:

    const fromMain = BfsDistances.of(calls, main);
    const depth = fromMain.distanceOf(parse);
    const chain = fromMain.pathTo(parse);
    // depth: 3
    // chain: [0, 2, 3, 6] - main -> run -> load -> parse

    Uses a breadth-first search that takes the sources in list order and the out-nodes of each node in ascending order. For the reachable nodes alone, Graphs.reachableFrom needs less memory.

    Instances are immutable.

    Memory: 8 x nodeCount bytes: the distance of each node and the node before it on its shortest path.

    Index
    • get maxDistance(): number

      The largest distance of a node that a source reaches, or -1 if there are no sources (Java: maxDistance()).

      Time: O(1).

      Returns number

    • Returns the distance of node, or -1 if no source reaches it. The distance is the number of edges on a shortest path to node from the nearest source.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns number

      distance, or -1

      RangeError if node is out of range

    • Returns whether a source reaches node. Equivalent to distanceOf(node) >= 0.

      Time: O(1).

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns boolean

      true if a source reaches node

      RangeError if node is out of range

    • Returns a shortest path to node from the nearest source, or an empty array if no source reaches node.

      The path has distanceOf(node) + 1 nodes, with the source first and node last. Of several shortest paths, this method returns the one that the search finds first.

      Time: O(distanceOf(node)). Memory: 4 x (distanceOf(node) + 1) bytes for the result.

      Parameters

      • node: number

        node ID in 0..nodeCount-1

      Returns Int32Array

      the node IDs of the path, or an empty array; a new array that must not be modified (Java: unmodifiable)

      RangeError if node is out of range

    • Returns the distances of the nodes of graph from source. Equivalent to of(graph, [source]).

      Time: O(nodeCount + edgeCount). Memory: as for the form with a list of sources.

      Parameters

      • graph: Graph

        the graph to search

      • source: number

        node ID in 0..nodeCount-1

      Returns BfsDistances

      the distances

      RangeError if source is out of range

    • Returns the distances of the nodes of graph from the nearest of sources.

      A source given twice counts once, and with no sources, no node is reachable.

      Time: O(number of sources + nodeCount + edgeCount). Memory: 8 x nodeCount bytes for the result; while it runs, 4 x nodeCount bytes more.

      Parameters

      • graph: Graph

        the graph to search

      • sources: IntList

        node IDs, each in 0..nodeCount-1

      Returns BfsDistances

      the distances

      RangeError if a source is out of range