Class AbstractGraph<V, E, VO, EO>Abstract

Abstract graph over vertices and edges.

Time O(1), Space O(1)

examples will be generated by unit test

Type Parameters

Hierarchy (view full)

Implements

Constructors

Accessors

Methods

  • Internal hook to attach an edge into adjacency structures.

    Parameters

    • edge: EO

      Edge instance.

    Returns boolean

    true if inserted; otherwise false.

    Time O(1) avg, Space O(1)

  • Insert a pre-built vertex into the graph.

    Parameters

    • newVertex: VO

      Concrete vertex instance.

    Returns boolean

    true if inserted; false if key already exists.

    Time O(1) avg, Space O(1)

  • Create an empty graph instance of the same concrete species.

    Parameters

    • Optional_options: Partial<Record<string, unknown>>

      Snapshot options from _snapshotOptions().

    Returns this

    A new empty graph instance of this type.

    Time O(1), Space O(1)

  • Create a same-species graph populated with entries; preserves edges among kept vertices.

    Parameters

    • Optionaliter: Iterable<[VertexKey, undefined | V], any, any>

      Optional entries to seed the new graph.

    • Optionaloptions: Partial<Record<string, unknown>>

      Snapshot options.

    Returns this

    A new graph of this type.

    Time O(V + E), Space O(V + E)

  • Resolve a vertex key or instance to the concrete vertex instance.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex key or existing vertex.

    Returns undefined | VO

    Vertex instance or undefined.

    Time O(1), Space O(1)

  • Resolve a vertex key from a key or vertex instance.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex key or existing vertex.

    Returns VertexKey

    The vertex key.

    Time O(1), Space O(1)

  • Capture configuration needed to reproduce the current graph.

    Returns Record<string, unknown>

    Options bag (opaque to callers).

    Time O(1), Space O(1)

  • Bellman-Ford single-source shortest paths with option to scan negative cycles.

    Parameters

    • src: VertexKey | VO

      Source vertex or key.

    • OptionalscanNegativeCycle: boolean

      If true, also detect negative cycles.

    • OptionalgetMin: boolean

      If true, compute global minimum distance.

    • OptionalgenPath: boolean

      If true, generate path arrays via predecessor map.

    Returns {
        distMap: Map<VO, number>;
        hasNegativeCycle: undefined | boolean;
        min: number;
        minPath: VO[];
        paths: VO[][];
        preMap: Map<VO, VO>;
    }

    Result bag including distances, predecessors, and optional cycle flag.

    • distMap: Map<VO, number>
    • hasNegativeCycle: undefined | boolean
    • min: number
    • minPath: VO[]
    • paths: VO[][]
    • preMap: Map<VO, VO>

    Time O(V * E), Space O(V + E)

  • Create a new edge instance (implementation specific).

    Parameters

    • srcOrV1: VertexKey

      Source/endpoint A key.

    • destOrV2: VertexKey

      Destination/endpoint B key.

    • Optionalweight: number

      Edge weight (defaults may apply).

    • Optionalvalue: E

      Edge payload.

    Returns EO

    Concrete edge instance.

    Time O(1), Space O(1)

  • Create a new vertex instance (implementation specific).

    Parameters

    • key: VertexKey

      Vertex identifier.

    • Optionalvalue: V

      Optional payload.

    Returns VO

    Concrete vertex instance.

    Time O(1), Space O(1)

  • Degree of a vertex in this graph model.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex or key.

    Returns number

    Non-negative integer degree.

    Time O(1) avg, Space O(1)

  • Delete an edge by instance.

    Parameters

    • edge: EO

      Edge instance.

    Returns undefined | EO

    Removed edge or undefined.

    Time O(1) avg, Space O(1)

  • Delete a vertex and its incident edges.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex or key.

    Returns boolean

    true if removed; otherwise false.

    Time O(deg), Space O(1)

  • Dijkstra without heap (array-based selection).

    Parameters

    • src: VertexKey | VO

      Source vertex or key.

    • dest: undefined | VertexKey | VO = undefined

      Optional destination for early stop.

    • getMinDist: boolean = false

      If true, compute global minimum distance.

    • genPaths: boolean = false

      If true, also generate path arrays.

    Returns DijkstraResult<VO>

    Result bag or undefined if source missing.

    Time O(V^2 + E), Space O(V + E)

  • Incident edges of a vertex.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex or key.

    Returns EO[]

    Array of incident edges.

    Time O(deg), Space O(deg)

  • Test whether all entries satisfy the predicate.

    Parameters

    • predicate: EntryCallback<VertexKey, undefined | V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns boolean

    true if all pass; otherwise false.

    Time O(n), Space O(1)

  • Induced-subgraph filter: keep vertices where predicate(key, value) is true, and only keep edges whose endpoints both survive.

    Parameters

    • predicate: EntryCallback<VertexKey, undefined | V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns this

    A new graph of the same concrete class (this type).

    Time O(V + E), Space O(V + E)

  • Preserve the old behavior: return filtered entries as an array.

    Parameters

    • predicate: EntryCallback<VertexKey, undefined | V, boolean>
    • OptionalthisArg: any

    Returns [VertexKey, undefined | V][]

    Time O(V), Space O(V)

  • Find the first entry that matches a predicate.

    Parameters

    • callbackfn: EntryCallback<VertexKey, undefined | V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns undefined | [VertexKey, undefined | V]

    Matching [key, value] or undefined.

    Time O(n), Space O(1)

  • Floyd–Warshall all-pairs shortest paths.

    Returns {
        costs: number[][];
        predecessor: (undefined | VO)[][];
    }

    { costs, predecessor } matrices.

    • costs: number[][]
    • predecessor: (undefined | VO)[][]

    Time O(V^3), Space O(V^2)

  • Visit each entry, left-to-right.

    Parameters

    • callbackfn: EntryCallback<VertexKey, undefined | V, void>

      (key, value, index, self) => void.

    • OptionalthisArg: any

      Optional this for callback.

    Returns void

    Time O(n), Space O(1)

  • Enumerate simple paths up to a limit.

    Parameters

    • v1: VertexKey | VO

      Source vertex or key.

    • v2: VertexKey | VO

      Destination vertex or key.

    • limit: number = 1000

      Maximum number of paths to collect.

    Returns VO[][]

    Array of paths (each path is an array of vertices).

    Time O(paths) worst-case exponential, Space O(V + paths)

  • Enumerate simple cycles (may be expensive).

    Parameters

    • isInclude2Cycle: boolean = false

      If true, include 2-cycles when graph semantics allow.

    Returns VertexKey[][]

    Array of cycles (each as array of vertex keys).

    Time exponential in worst-case, Space O(V + E)

  • Get an edge between two vertices if present.

    Parameters

    • srcOrKey: VertexKey | VO

      Source/endpoint A vertex or key.

    • destOrKey: VertexKey | VO

      Destination/endpoint B vertex or key.

    Returns undefined | EO

    Edge instance or undefined.

    Time O(1) avg, Space O(1)

  • Resolve endpoints of an edge to vertex instances.

    Parameters

    • edge: EO

      Edge instance.

    Returns undefined | [VO, VO]

    [v1, v2] or undefined if missing.

    Time O(1), Space O(1)

  • Minimum hops/weight between two vertices.

    Parameters

    • v1: VertexKey | VO

      Source vertex or key.

    • v2: VertexKey | VO

      Destination vertex or key.

    • OptionalisWeight: boolean

      If true, compare by path weight; otherwise by hop count.

    Returns undefined | number

    Minimum cost or undefined if missing/unreachable.

    Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)

  • Minimum path (as vertex sequence) between two vertices.

    Parameters

    • v1: VertexKey | VO

      Source vertex or key.

    • v2: VertexKey | VO

      Destination vertex or key.

    • OptionalisWeight: boolean

      If true, compare by path weight; otherwise by hop count.

    • isDFS: boolean = false

      For weighted mode only: if true, brute-force all paths; if false, use Dijkstra.

    Returns undefined | VO[]

    Vertex sequence, or undefined/empty when unreachable depending on branch.

    Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)

  • One-step neighbors of a vertex.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex or key.

    Returns VO[]

    Array of neighbor vertices.

    Time O(deg), Space O(deg)

  • Sum the weights along a vertex path.

    Parameters

    • path: VO[]

      Sequence of vertices.

    Returns number

    Path weight sum (0 if empty or edge missing).

    Time O(L), Space O(1) where L is path length

  • Get vertex instance by key.

    Parameters

    • vertexKey: VertexKey

      Vertex key.

    Returns undefined | VO

    Vertex instance or undefined.

    Time O(1), Space O(1)

  • Whether an edge exists between two vertices.

    Parameters

    • v1: VertexKey | VO

      Endpoint A vertex or key.

    • v2: VertexKey | VO

      Endpoint B vertex or key.

    Returns boolean

    true if present; otherwise false.

    Time O(1) avg, Space O(1)

  • Whether a vertex exists.

    Parameters

    • vertexOrKey: VertexKey | VO

      Vertex or key.

    Returns boolean

    true if present, otherwise false.

    Time O(1) avg, Space O(1)

  • Type guard: check if a value is a valid vertex key.

    Parameters

    • potentialKey: any

      Value to test.

    Returns potentialKey is VertexKey

    true if string/number; else false.

    Time O(1), Space O(1)

  • Reduce entries into a single accumulator.

    Type Parameters

    • U

    Parameters

    • callbackfn: ReduceEntryCallback<VertexKey, undefined | V, U>

      (acc, value, key, index, self) => acc.

    • initialValue: U

      Initial accumulator.

    Returns U

    Final accumulator.

    Time O(n), Space O(1)

  • Delete multiple vertices.

    Parameters

    • vertexMap: VO[] | VertexKey[]

      Array of vertices or keys.

    Returns boolean

    true if any vertex was removed.

    Time O(sum(deg)), Space O(1)

  • Set the weight of an existing edge.

    Parameters

    • srcOrKey: VertexKey | VO

      Source vertex or key.

    • destOrKey: VertexKey | VO

      Destination vertex or key.

    • weight: number

      New weight.

    Returns boolean

    true if updated; otherwise false.

    Time O(1) avg, Space O(1)

  • Test whether any entry satisfies the predicate.

    Parameters

    • predicate: EntryCallback<VertexKey, undefined | V, boolean>

      (key, value, index, self) => boolean.

    • OptionalthisArg: any

      Optional this for callback.

    Returns boolean

    true if any passes; otherwise false.

    Time O(n), Space O(1)