import { DuplicateNodeError, InvalidInputError } from '../types/errors'; import { Edge, Node } from '../types/graph'; import { Option } from '../types/option'; import { Result } from '../types/result'; /** * Generic graph data structure supporting both directed and undirected graphs. * Uses adjacency list representation for efficient neighbor lookup (O(1) average case). * @template N - Node type (must extend Node interface with id and type fields) * @template E - Edge type (must extend Edge interface with source, target fields) * @example * ```typescript * type WorkNode = { id: string; type: 'work'; title: string }; * type CitationEdge = { id: string; source: string; target: string; type: 'citation' }; * * const graph = new Graph(true); // directed graph * graph.addNode({ id: 'W1', type: 'work', title: 'Paper A' }); * graph.addNode({ id: 'W2', type: 'work', title: 'Paper B' }); * graph.addEdge({ id: 'E1', source: 'W1', target: 'W2', type: 'citation' }); * ``` */ export declare class Graph { /** Node storage (ID → Node) */ private nodes; /** Edge storage (ID → Edge) */ private edges; /** Adjacency list (Node ID → Set of neighbor IDs) */ private adjacencyList; /** Graph directionality */ private directed; /** * Create a new graph. * @param directed - Whether the graph is directed (true) or undirected (false) */ constructor(directed: boolean); /** * Add a node to the graph. * @param node - Node to add * @returns Ok(void) if successful, Err(DuplicateNodeError) if node ID already exists * @example * ```typescript * const result = graph.addNode({ id: 'N1', type: 'test', label: 'Node 1' }); * if (!result.ok) { * console.error('Failed to add node:', result.error.message); * } * ``` */ addNode(node: N): Result; /** * Remove a node from the graph (and all incident edges). * @param id - Node ID to remove * @returns Ok(void) if successful, Err(InvalidInputError) if node not found */ removeNode(id: string): Result; /** * Check if node exists in graph. * @param id - Node ID to check * @returns true if node exists, false otherwise */ hasNode(id: string): boolean; /** * Get node by ID. * @param id - Node ID to retrieve * @returns Some(node) if found, None if not found * @example * ```typescript * const nodeOption = graph.getNode('N1'); * if (nodeOption.some) { * console.log('Found:', nodeOption.value); * } * ``` */ getNode(id: string): Option; /** * Add an edge to the graph. * @param edge - Edge to add * @returns Ok(void) if successful, Err(InvalidInputError) if source/target nodes don't exist * @example * ```typescript * const result = graph.addEdge({ id: 'E1', source: 'N1', target: 'N2', type: 'link' }); * if (!result.ok) { * console.error('Failed to add edge:', result.error.message); * } * ``` */ addEdge(edge: E): Result; /** * Remove an edge from the graph. * @param id - Edge ID to remove * @returns Ok(void) if successful, Err(InvalidInputError) if edge not found */ removeEdge(id: string): Result; /** * Get neighbor node IDs for a given node. * @param id - Node ID to get neighbors for * @returns Ok(neighbor IDs) if successful, Err(InvalidInputError) if node not found * @example * ```typescript * const result = graph.getNeighbors('N1'); * if (result.ok) { * console.log('Neighbors:', result.value); * } * ``` */ getNeighbors(id: string): Result; /** * Get total number of nodes in graph. * @returns Node count */ getNodeCount(): number; /** * Get total number of edges in graph. * @returns Edge count */ getEdgeCount(): number; /** * Check if graph is directed. * @returns true if directed, false if undirected */ isDirected(): boolean; /** * Get all nodes in the graph. * @returns Array of all nodes * @example * ```typescript * const nodes = graph.getAllNodes(); * console.log('Total nodes:', nodes.length); * ``` */ getAllNodes(): N[]; /** * Get all edges in the graph. * @returns Array of all edges * @example * ```typescript * const edges = graph.getAllEdges(); * console.log('Total edges:', edges.length); * ``` */ getAllEdges(): E[]; /** * Get an edge by its ID. * @param id - Edge ID to look up * @returns Option containing the edge, or None if not found * @example * ```typescript * const edge = graph.getEdge('E1'); * if (edge.some) { * console.log('Edge found:', edge.value); * } * ``` */ getEdge(id: string): Option; /** * Get all outgoing edges from a node. * * For directed graphs: Returns edges where node is the source. * For undirected graphs: Returns edges where node is either source or target. * @param id - Node ID to get outgoing edges from * @returns Result containing array of outgoing edges, or error if node not found * @example * ```typescript * const result = graph.getOutgoingEdges('N1'); * if (result.ok) { * console.log('Outgoing edges:', result.value); * } * ``` */ getOutgoingEdges(id: string): Result; } //# sourceMappingURL=graph.d.ts.map