Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Graph

A graph composed of Nodes and Edges, representing 2-D spatial points and links between them. Provides methods for reading and analyzing its data.

The graph and all its data are immutable. No method will modify the Graph instance on which it is called, although some will return new instances.

New Graph instances are created using Graph.create, which takes SimpleNodes and SimpleEdges as arguments. Upon construction, the graph creates corresponding Node and Edge instances which contain additional information. All Graph methods will return these Nodes rather than the original SimpleNodes, and likewise for edges.

Example usage:

import Graph from "graphs-and-paths";

const nodes = [
    { id: "A", location: { x: 0, y: 0 } },
    { id: "B", location: { x: 3, y: 0 } },
    { id: "C", location: { x: 0, y: 4 } }
];
const edges = [
    { id: "AB", startNodeId: "A", endNodeId: "B" },
    { id: "BC", startNodeId: "B", endNodeId: "C" },
    { id: "CA", startNodeId: "C", endNodeId: "A" }
];
const graph = Graph.create(nodes, edges);

graph.getNode("A");
// { id: "A", location: { x: 0, y: 0 }, edgeIds: ["AB", "CA"] }

graph.getLocation("AB", 2);
// { x: 2, y: 0 }

graph.getShortestPath(
    { edgeId: "CA", distance: 3 },
    { edgeId: "BC", distance: 1 }
).locations;
// [
//     { x: 0, y: 1 },
//     { x: 0, y: 0 },
//     { x: 3, y: 0 },
//     { x: 2.4, y: 0.8 }
// ]

Hierarchy

  • Graph

Index

Methods

coalesced

  • Creates a new Graph instance with the same shape but with a reduced number of nodes and edges. Each instance of multiple nodes and edges in a chain with no forks is converted into a single edge, where the removed nodes are converted into inner locations of the new edge. In particular, the new graph will have no nodes of degree 2 except for nodes with only one edge connecting to themselves. This may significantly increase the speed of certain calculations, such as getShortestPath.

    A newly created edge will have the lowest ID of the edges which were combined to form it, where numbers are considered lower than strings.

    Returns Graph

    A new coalesced Graph instance.

getAllEdges

  • getAllEdges(): Edge[]
  • Returns Edge[]

    All the edges present in this graph, in the order that they were originally provided to Graph.create.

getAllNodes

  • getAllNodes(): Node[]
  • Returns Node[]

    All the nodes present in this graph, in the order that they were originally provided to Graph.create.

getClosestPoint

  • Returns the closest point on the graph to a given location. This requires the graph to have a spatial index. To enable this, first use withClosestPointMesh to obtain a new graph instance with an index enabled. Calling this method on a graph with no index will throw.

    Parameters

    Returns EdgePoint

    The point on the graph closest to the given location, up to a precision determined by the graph's mesh.

getConnectedComponentOfNode

  • Parameters

    Returns Graph

    A new Graph instance representing the connected component containing the node with the given ID.

getConnectedComponents

  • getConnectedComponents(): Graph[]
  • Returns Graph[]

    A list of new Graph instances, each representing a single connected component of this instance.

getEdge

  • Parameters

    Returns Edge

    The Edge associated with the given ID, or undefined if none exists. Note that the type signature is a lie (it claims to be non-nullable). This is for convenience. Lookup will usually be of known edge IDs, and this is consistent with the typings for index lookup in objects.

getEdgesOfNode

  • Parameters

    Returns Edge[]

    All edges which have the node with the given ID as an endpoint.

getEndpointsOfEdge

  • Parameters

    Returns [Node, Node]

    The two nodes which are endpoints of the edge with the given ID.

getLocation

  • Returns the Cartesian coordinates of the point a certain distance along an edge. Does not throw on out-of-bounds distances. Instead negative distances return the start of the edge and distances greater than the length return the end of the edge. This is to avoid unexpected behavior due to floating-point imprecision issues.

    Parameters

    • edgePoint: EdgePoint

      A point specified as a certain distance along an edge.

    Returns Location

    The Cartesian coordinates of the given point.

getNeighbors

  • Parameters

    Returns Node[]

    All nodes which are connected to the node with the given ID by an edge.

getNode

  • Parameters

    Returns Node

    The Node associated with the given ID, or undefined if none exists. Note that the type signature is a lie (it claims to be non-nullable). This is for convenience. Lookup will usually be of known node IDs, and this is consistent with the typings for index lookup in objects.

getOtherEndpoint

  • Given an edge and one of its endpoints, return the other endpoint. Throws if either of the IDs is nonexistent, or if the node is not an endpoint of the edge.

    Parameters

    • edgeId: EdgeId

      An edge ID.

    • nodeId: NodeId

      A node ID, referencing one of the endpoints of the edge.

    Returns Node

    The node which is the other endpoint of the edge with the given ID.

getShortestPath

  • Returns the shortest path between two points on the graph. Throws if no such path exists.

    Parameters

    Returns Path

    The shortest path from the first point to the second.

withClosestPointMesh

  • withClosestPointMesh(precision: number): Graph
  • Does preprocessing to enable getClosestPoint by creating a spatial index of points along the graph.

    Parameters

    • precision: number

      How fine the mesh should be. Lower precision is more accurate but takes more time to precompute and more memory. As a rule-of-thumb, getClosestPoint will be accurate to within precision distance.

    Returns Graph

    A new Graph instance with a spatial index enabled.

Static advanceAlongLocations

  • Helper function for computing progress down a path after advancing along it for a given distance. If the distance is greater than the total length of the path, then advances to the end of the path. Throws on negative distances.

    Parameters

    • locations: Location[]

      A list of locations representing a path.

    • distance: number

      A distance down the path.

    Returns Location[]

    A new list of locations representing the remaining part of locations after advancing distance along the path.

Static advanceAlongPath

  • advanceAlongPath(path: Path, distance: number): Path
  • Returns a new path obtained by truncating the given path by the provided distance. That is, the start point of the path moves forward, and any nodes and edges that it passes are dropped. Throws an error if distance is negative.

    Parameters

    • path: Path

      A path.

    • distance: number

      A distance to travel along the path.

    Returns Path

    The remaining portion of path after traveling distance along it.

Static create

  • Parameters

    Returns Graph

    A new Graph instance with the specified nodes and edges.

Static distance

  • Parameters

    Returns number

    The straight-line distance between location1 and location2.

Generated using TypeDoc