UNPKG

@sha1n/dagraph

Version:

Directed acyclic graph utility in TypeScript

44 lines (43 loc) 1.25 kB
interface Identifiable { readonly id: string; } declare class DAGraph<T extends Identifiable> { private readonly nodesById; /** * Adds the specified identifiable node to the graph. */ addNode(data: T): DAGraph<T>; /** * @returns the data node identified by the specified id if found, else returns undefined. */ getNode(id: string): T | undefined; /** * Adds an edge pointing from 'from' to 'to'. */ addEdge(from: T, to: T): DAGraph<T>; /** * Returns a generator that returns all the nodes in topological order. * Implements a depth-first-search algorithm. */ topologicalSort(): Iterable<T>; /** * A generator that returns the traverse roots of this graph. */ roots(): Iterable<T>; /** * A generator that returns all the nodes in the this graph. */ nodes(): Iterable<T>; /** * Returns a graph with the same edges pointing in the opposite direction. * * @returns a DAGraph */ reverse(): DAGraph<T>; private ensureNode; private isAcyclic; } declare function createDAG<T extends Identifiable>(): DAGraph<T>; export type { DAGraph, Identifiable }; export default createDAG; export { createDAG };