Options
All
  • Public
  • Public/Protected
  • All
Menu

A DirectedGraph is similar a Graph but with additional functionality.

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

  • new DirectedGraph(nodeIdentity?: (node: T) => string): DirectedGraph

Properties

Protected adjacency

adjacency: Array<Array<Edge>>

Protected Optional hasCycle

hasCycle: undefined | false | true

Caches if the graph contains a cycle. If undefined then it is unknown.

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, skipUpdatingCyclicality?: boolean): void
  • Add a directed edge to the graph.

    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.

    • Default value skipUpdatingCyclicality: boolean = false

      This boolean indicates if the cache of the cyclicality of the graph should be updated. If false is passed the cached will be invalidated because we can not assure that a cycle has not been created.

    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

  • getSubGraphStartingFrom(startNodeIdentity: string): DirectedGraph<T>

indegreeOfNode

  • indegreeOfNode(nodeID: string): number
  • The indegree of a node is the number of edges that point to it. This will always be an integer.

    Throws a NodeDoesntExistError the node does not exist.

    Parameters

    • nodeID: string

      The string of the node identity of the node to calculate indegree for.

    Returns number

insert

  • insert(node: T): string
  • Add a node to the graph if it doesn't already exist. If it does, throw a NodeAlreadyExistsError.

    Parameters

    • node: T

      The node to be added

    Returns string

    A string that is the identity of the newly inserted node. This is created by applying the nodeIdentity.

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

Private subAdj

  • subAdj(include: T[]): Array<Array<Edge>>

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

Legend

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

Generated using TypeDoc