Options
All
  • Public
  • Public/Protected
  • All
Menu

A DirectedAcyclicGraph is builds on a DirectedGraph but enforces acyclicality. You cannot add an edge to a DirectedAcyclicGraph that would create a cycle.

Type parameters

  • T

    T is the node type of the graph. Nodes can be anything in all the included examples they are simple objects.

Hierarchy

Index

Constructors

constructor

Properties

Private Optional _topologicallySortedNodes

_topologicallySortedNodes: Array<T>

Protected adjacency

adjacency: Array<Array<Edge>>

Protected hasCycle

hasCycle: boolean = false

Protected nodeIdentity

nodeIdentity: (t: T) => string

Type declaration

    • (t: T): string
    • Parameters

      • t: T

      Returns string

Protected nodes

nodes: Map<string, T>

Methods

addEdge

  • addEdge(fromNodeIdentity: string, toNodeIdentity: string): void
  • Adds an edge to the graph similarly to DirectedGraph.addEdge but maintains correctness of the acyclic graph. Thows a CycleError if adding the requested edge would create a cycle. Adding an edge invalidates the cache of topologically sorted nodes, rather than updating it.

    Parameters

    • fromNodeIdentity: string

      The identity string of the node the edge should run from.

    • toNodeIdentity: string

      The identity string of the node the edge should run to.

    Returns void

canReachFrom

  • canReachFrom(startNode: string, endNode: string): boolean
  • Depth first search to see if one node is reachable from another following the directed edges.

    Caveat: This will return false if startNode and endNode are the same node and the is not a cycle or a loop edge connecting them.

    Parameters

    • startNode: string

      The string identity of the node to start at.

    • endNode: string

      The string identity of the node we are attempting to reach.

    Returns boolean

getNode

  • getNode(nodeIdentity: string): T | undefined
  • Returns a specific node given the node identity returned from the insert function

    Parameters

    • nodeIdentity: string

    Returns T | undefined

getNodes

  • getNodes(compareFunc?: undefined | ((a: T, b: T) => number)): T[]
  • This simply returns all the nodes stored in the graph

    Parameters

    • Optional compareFunc: undefined | ((a: T, b: T) => number)

      An optional function that indicates the sort order of the returned array

    Returns T[]

getSubGraphStartingFrom

indegreeOfNode

  • indegreeOfNode(nodeID: string): number

insert

  • insert(node: T): string
  • Inserts a node into the graph and maintains topologic sort cache by prepending the node (since all newly created nodes have an indegree of zero.)

    Parameters

    • node: T

      The node to insert

    Returns string

isAcyclic

  • isAcyclic(): boolean
  • Returns true if there are no cycles in the graph. This relies on a cached value so calling it multiple times without adding edges to the graph should be O(1) after the first call. Non-cached calls are potentially expensive, the implementation is based on Kahn's algorithim which is O(|EdgeCount| + |NodeCount|).

    Returns boolean

replace

  • replace(node: T): void
  • This replaces an existing node in the graph with an updated version. Throws a [[NodeDoesNotExistsError]] if no node with the same identity already exists.

    __Caveat_:_ The default identity function means that this will never work since if the node changes it will have a different hash.

    Parameters

    • node: T

      The new node that is replacing the old one.

    Returns void

topologicallySortedNodes

  • topologicallySortedNodes(): Array<T>
  • Topologically sort the nodes using Kahn's algorithim. Uses a cache which means that repeated calls should be O(1) after the first call. Non-cached calls are potentially expensive, Kahn's algorithim is O(|EdgeCount| + |NodeCount|). There may be more than one valid topological sort order for a single graph, so just because two graphs are the same does not mean that order of the resultant arrays will be.

    Returns Array<T>

    An array of nodes sorted by the topological order.

upsert

  • upsert(node: T): string
  • This essentially combines the behavior of insert and replace. If the node doesn't exist, create it. If the node already exists, replace it with the updated version.

    Parameters

    • node: T

      The node to insert or update

    Returns string

    The identity string of the node inserted or updated.

wouldAddingEdgeCreateCyle

  • wouldAddingEdgeCreateCyle(fromNodeIdentity: string, toNodeIdentity: string): boolean
  • Checks if adding the specified edge would create a cycle. Returns true in O(1) if the graph already contains a known cycle, or if fromNodeIdentity and toNodeIdentity are the same.

    Parameters

    • fromNodeIdentity: string

      The string identity of the node the edge is from.

    • toNodeIdentity: string

      The string identity of the node the edge is to.

    Returns boolean

Static fromDirectedGraph

Legend

  • Constructor
  • Property
  • Method
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Private property
  • Private method
  • Protected property
  • Static property

Generated using TypeDoc