UNPKG

@graphty/algorithms

Version:

Graph algorithms library for browser environments implemented in TypeScript

146 lines 4.75 kB
import { CSRGraph } from "./csr-graph.js"; /** * Adapter to convert standard Graph to CSR format * * Provides transparent conversion while maintaining API compatibility */ export class GraphAdapter { constructor(graph) { // Check if already a CSRGraph if (graph instanceof CSRGraph) { this.csrGraph = graph; } else { // Convert to CSR format this.csrGraph = this.convertToCSR(graph); } } /** * Convert standard graph to CSR format */ convertToCSR(graph) { const adjacencyList = new Map(); const weights = new Map(); // Handle both Graph and ReadonlyGraph interfaces if ("nodes" in graph && typeof graph.nodes === "function") { // Build adjacency list from Graph for (const node of graph.nodes()) { const nodeId = node.id; const neighbors = []; const graphTyped = graph; for (const neighborId of graphTyped.neighbors(nodeId)) { neighbors.push(neighborId); // Get edge weight if available if ("getEdge" in graph && typeof graph.getEdge === "function") { const edge = graphTyped.getEdge(nodeId, neighborId); if (edge?.weight !== undefined) { weights.set(`${String(nodeId)}-${String(neighborId)}`, edge.weight); } } } adjacencyList.set(nodeId, neighbors); } } else if ("nodeIds" in graph && typeof graph.nodeIds === "function") { const readonlyGraph = graph; for (const nodeId of readonlyGraph.nodeIds()) { adjacencyList.set(nodeId, []); } // Build adjacency from edges if ("edges" in readonlyGraph && typeof readonlyGraph.edges === "function") { for (const edge of readonlyGraph.edges()) { const neighbors = adjacencyList.get(edge.source); if (neighbors) { neighbors.push(edge.target); if (edge.weight !== undefined) { weights.set(`${String(edge.source)}-${String(edge.target)}`, edge.weight); } } } } } return new CSRGraph(adjacencyList, weights.size > 0 ? weights : undefined); } // Delegate all methods to CSR implementation nodeCount() { return this.csrGraph.nodeCount(); } edgeCount() { return this.csrGraph.edgeCount(); } hasNode(nodeId) { return this.csrGraph.hasNode(nodeId); } hasEdge(source, target) { return this.csrGraph.hasEdge(source, target); } neighbors(nodeId) { return this.csrGraph.neighbors(nodeId); } outDegree(nodeId) { return this.csrGraph.outDegree(nodeId); } nodes() { return this.csrGraph.nodes(); } /** * Get the underlying CSR graph */ getCSRGraph() { return this.csrGraph; } } /** * Configure optimizations globally * @deprecated This function is a no-op and will be removed in a future version */ // eslint-disable-next-line @typescript-eslint/no-unused-vars export function configureOptimizations(_config) { // No-op for backward compatibility } /** * Get current configuration * @deprecated This function returns empty config and will be removed in a future version */ export function getOptimizationConfig() { return {}; } /** * Check if a graph is already in CSR format */ export function isCSRGraph(graph) { return graph instanceof CSRGraph; } /** * Convert any graph to CSR format */ export function toCSRGraph(graph) { if (isCSRGraph(graph)) { return graph; } const adapter = new GraphAdapter(graph); return adapter.getCSRGraph(); } /** * Create an optimized graph from nodes and edges */ export function createOptimizedGraph(nodes, edges) { const adjacencyList = new Map(); const weights = new Map(); // Initialize nodes for (const node of nodes) { adjacencyList.set(node, []); } // Add edges for (const [source, target, weight] of edges) { const neighbors = adjacencyList.get(source); if (neighbors) { neighbors.push(target); if (weight !== undefined) { weights.set(`${String(source)}-${String(target)}`, weight); } } } return new CSRGraph(adjacencyList, weights.size > 0 ? weights : undefined); } //# sourceMappingURL=graph-adapter.js.map