78 lines
4.1 KiB
TypeScript
78 lines
4.1 KiB
TypeScript
/**
|
|
* A vertex (node) in a directed graph, holding metadata and a list of connected edges.
|
|
* @typeParam VMetadata - The type of metadata stored on vertices
|
|
* @typeParam EMetadata - The type of metadata stored on edges
|
|
*/
|
|
export type Vertex<VMetadata = null, EMetadata = null> = {
|
|
metadata: VMetadata;
|
|
edges: Array<Edge<EMetadata, VMetadata>>;
|
|
};
|
|
/**
|
|
* A directed edge connecting two vertices, with its own metadata.
|
|
* @typeParam EMetadata - The type of metadata stored on edges
|
|
* @typeParam VMetadata - The type of metadata stored on the connected vertices
|
|
*/
|
|
export type Edge<EMetadata = null, VMetadata = null> = {
|
|
metadata: EMetadata;
|
|
from: Vertex<VMetadata, EMetadata>;
|
|
to: Vertex<VMetadata, EMetadata>;
|
|
};
|
|
/**
|
|
* A directed graph data structure supporting vertex/edge management and graph traversal algorithms
|
|
* including breadth-first search, reverse BFS, and shortest path computation.
|
|
*
|
|
* @typeParam VMetadata - The type of metadata stored on vertices
|
|
* @typeParam EMetadata - The type of metadata stored on edges
|
|
*/
|
|
export declare class Graph<VMetadata = null, EMetadata = null> {
|
|
private readonly vertices;
|
|
constructor();
|
|
/**
|
|
* Serializes the graph to a JSON string for debugging.
|
|
* @param metadataRepr - Optional function to transform metadata values before serialization
|
|
* @returns A pretty-printed JSON string of the graph structure
|
|
*/
|
|
dump(metadataRepr?: (metadata: VMetadata | EMetadata) => any): string;
|
|
/**
|
|
* Adds a new vertex to the graph, optionally connecting it to existing vertices via edges.
|
|
* @param metadata - The metadata to attach to the new vertex
|
|
* @param fromEdges - Edges pointing from existing vertices to this new vertex
|
|
* @param toEdges - Edges pointing from this new vertex to existing vertices
|
|
* @returns The newly created vertex
|
|
*/
|
|
addVertex(metadata: VMetadata, fromEdges: Array<Omit<Edge<EMetadata, VMetadata>, 'to'>>, toEdges: Array<Omit<Edge<EMetadata, VMetadata>, 'from'>>): Vertex<VMetadata, EMetadata>;
|
|
/**
|
|
* Returns a generator that yields all vertices matching the predicate.
|
|
* @param predicate - A function to test each vertex
|
|
* @returns A generator of matching vertices
|
|
*/
|
|
findVertex(predicate: (vertex: Vertex<VMetadata, EMetadata>) => boolean): Generator<Vertex<VMetadata, EMetadata>, null>;
|
|
/**
|
|
* Adds a directed edge between two existing vertices.
|
|
* @param metadata - The metadata to attach to the edge
|
|
* @param from - The source vertex
|
|
* @param to - The destination vertex
|
|
* @returns The newly created edge
|
|
*/
|
|
addEdge(metadata: EMetadata, from: Vertex<VMetadata, EMetadata>, to: Vertex<VMetadata, EMetadata>): Edge<EMetadata, VMetadata>;
|
|
/**
|
|
* Performs a breadth-first traversal following outgoing edges from the starting vertex or vertices.
|
|
* @param from - A starting vertex, or a predicate to select multiple starting vertices
|
|
* @returns A generator yielding vertices in BFS order
|
|
*/
|
|
breadthFirstSearch(from: Vertex<VMetadata, EMetadata> | ((vertex: Vertex<VMetadata, EMetadata>) => boolean)): Generator<Vertex<VMetadata, EMetadata>, null>;
|
|
/**
|
|
* Performs a reverse breadth-first traversal following incoming edges from the starting vertex or vertices.
|
|
* @param to - A starting vertex, or a predicate to select multiple starting vertices
|
|
* @returns A generator yielding vertices in reverse BFS order
|
|
*/
|
|
reverseBreadthFirstSearch(to: Vertex<VMetadata, EMetadata> | ((vertex: Vertex<VMetadata, EMetadata>) => boolean)): Generator<Vertex<VMetadata, EMetadata>, null>;
|
|
/**
|
|
* Finds the shortest path (by edge count) between two vertices using BFS.
|
|
* @param from - The starting vertex, or a predicate to select starting vertices
|
|
* @param to - The target vertex, or a predicate to identify target vertices
|
|
* @returns An array of edges forming the shortest path, or `null` if no path exists
|
|
*/
|
|
shortestPath(from: Vertex<VMetadata, EMetadata> | ((vertex: Vertex<VMetadata, EMetadata>) => boolean), to: Vertex<VMetadata, EMetadata> | ((vertex: Vertex<VMetadata, EMetadata>) => boolean)): Array<Edge<EMetadata, VMetadata>> | null;
|
|
}
|