graphwise 1.8.0 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adjacency-map-BtKzcuJq.js +229 -0
- package/dist/adjacency-map-BtKzcuJq.js.map +1 -0
- package/dist/adjacency-map-JqBnMNkF.cjs +234 -0
- package/dist/adjacency-map-JqBnMNkF.cjs.map +1 -0
- package/dist/async/index.cjs +15 -242
- package/dist/async/index.js +2 -229
- package/dist/expansion/index.cjs +43 -0
- package/dist/expansion/index.js +2 -0
- package/dist/expansion-ClDhlMK8.js +1704 -0
- package/dist/expansion-ClDhlMK8.js.map +1 -0
- package/dist/expansion-DaTroIyv.cjs +1949 -0
- package/dist/expansion-DaTroIyv.cjs.map +1 -0
- package/dist/extraction/index.cjs +630 -0
- package/dist/extraction/index.cjs.map +1 -0
- package/dist/extraction/index.js +621 -0
- package/dist/extraction/index.js.map +1 -0
- package/dist/gpu/csr.d.ts +29 -30
- package/dist/gpu/csr.d.ts.map +1 -1
- package/dist/gpu/dispatch.d.ts +31 -0
- package/dist/gpu/dispatch.d.ts.map +1 -0
- package/dist/gpu/dispatch.unit.test.d.ts +5 -0
- package/dist/gpu/dispatch.unit.test.d.ts.map +1 -0
- package/dist/gpu/index.cjs +15 -410
- package/dist/gpu/index.d.ts +3 -1
- package/dist/gpu/index.d.ts.map +1 -1
- package/dist/gpu/index.js +2 -400
- package/dist/gpu/kernels/bfs/kernel.d.ts +59 -0
- package/dist/gpu/kernels/bfs/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/bfs/logic.d.ts +47 -0
- package/dist/gpu/kernels/bfs/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/bfs/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/bfs/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/degree-histogram/kernel.d.ts +32 -0
- package/dist/gpu/kernels/degree-histogram/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/degree-histogram/logic.d.ts +45 -0
- package/dist/gpu/kernels/degree-histogram/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/degree-histogram/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/degree-histogram/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/jaccard/kernel.d.ts +40 -0
- package/dist/gpu/kernels/jaccard/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/jaccard/logic.d.ts +43 -0
- package/dist/gpu/kernels/jaccard/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/jaccard/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/jaccard/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/pagerank/kernel.d.ts +44 -0
- package/dist/gpu/kernels/pagerank/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/pagerank/logic.d.ts +50 -0
- package/dist/gpu/kernels/pagerank/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/pagerank/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/pagerank/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/kernels/spmv/kernel.d.ts +43 -0
- package/dist/gpu/kernels/spmv/kernel.d.ts.map +1 -0
- package/dist/gpu/kernels/spmv/logic.d.ts +31 -0
- package/dist/gpu/kernels/spmv/logic.d.ts.map +1 -0
- package/dist/gpu/kernels/spmv/logic.unit.test.d.ts +2 -0
- package/dist/gpu/kernels/spmv/logic.unit.test.d.ts.map +1 -0
- package/dist/gpu/operations.d.ts +76 -0
- package/dist/gpu/operations.d.ts.map +1 -0
- package/dist/gpu/operations.unit.test.d.ts +5 -0
- package/dist/gpu/operations.unit.test.d.ts.map +1 -0
- package/dist/gpu/root.d.ts +53 -0
- package/dist/gpu/root.d.ts.map +1 -0
- package/dist/gpu/root.unit.test.d.ts +2 -0
- package/dist/gpu/root.unit.test.d.ts.map +1 -0
- package/dist/gpu/types.d.ts +3 -8
- package/dist/gpu/types.d.ts.map +1 -1
- package/dist/gpu-CHiCN0wa.js +16945 -0
- package/dist/gpu-CHiCN0wa.js.map +1 -0
- package/dist/gpu-Y6owRVMi.cjs +17028 -0
- package/dist/gpu-Y6owRVMi.cjs.map +1 -0
- package/dist/graph/index.cjs +2 -229
- package/dist/graph/index.js +1 -228
- package/dist/index/index.cjs +141 -4040
- package/dist/index/index.js +15 -3917
- package/dist/jaccard-3rCdilwm.js +39 -0
- package/dist/jaccard-3rCdilwm.js.map +1 -0
- package/dist/jaccard-Bys9_dGW.cjs +50 -0
- package/dist/jaccard-Bys9_dGW.cjs.map +1 -0
- package/dist/{kmeans-BIgSyGKu.cjs → kmeans-B8x9D1kt.cjs} +1 -1
- package/dist/{kmeans-BIgSyGKu.cjs.map → kmeans-B8x9D1kt.cjs.map} +1 -1
- package/dist/{kmeans-87ExSUNZ.js → kmeans-DKkL9rAN.js} +1 -1
- package/dist/{kmeans-87ExSUNZ.js.map → kmeans-DKkL9rAN.js.map} +1 -1
- package/dist/ops-djAsQQSh.cjs +277 -0
- package/dist/ops-djAsQQSh.cjs.map +1 -0
- package/dist/ops-upIi6JIi.js +212 -0
- package/dist/ops-upIi6JIi.js.map +1 -0
- package/dist/priority-queue-BIiD1L0k.cjs +148 -0
- package/dist/priority-queue-BIiD1L0k.cjs.map +1 -0
- package/dist/priority-queue-CFDd5cBg.js +143 -0
- package/dist/priority-queue-CFDd5cBg.js.map +1 -0
- package/dist/ranking/index.cjs +43 -0
- package/dist/ranking/index.js +4 -0
- package/dist/ranking/mi/index.cjs +581 -0
- package/dist/ranking/mi/index.cjs.map +1 -0
- package/dist/ranking/mi/index.js +555 -0
- package/dist/ranking/mi/index.js.map +1 -0
- package/dist/ranking-3ez5m67U.js +1016 -0
- package/dist/ranking-3ez5m67U.js.map +1 -0
- package/dist/ranking-DVvajgUZ.cjs +1093 -0
- package/dist/ranking-DVvajgUZ.cjs.map +1 -0
- package/dist/seeds/index.cjs +1 -1
- package/dist/seeds/index.js +1 -1
- package/dist/structures/index.cjs +2 -143
- package/dist/structures/index.js +1 -142
- package/dist/utils/index.cjs +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils-BodeE2Mo.js +22 -0
- package/dist/utils-BodeE2Mo.js.map +1 -0
- package/dist/utils-CDtCcsyF.cjs +33 -0
- package/dist/utils-CDtCcsyF.cjs.map +1 -0
- package/package.json +3 -1
- package/dist/async/index.cjs.map +0 -1
- package/dist/async/index.js.map +0 -1
- package/dist/gpu/context.d.ts +0 -118
- package/dist/gpu/context.d.ts.map +0 -1
- package/dist/gpu/context.unit.test.d.ts +0 -2
- package/dist/gpu/context.unit.test.d.ts.map +0 -1
- package/dist/gpu/index.cjs.map +0 -1
- package/dist/gpu/index.js.map +0 -1
- package/dist/graph/index.cjs.map +0 -1
- package/dist/graph/index.js.map +0 -1
- package/dist/index/index.cjs.map +0 -1
- package/dist/index/index.js.map +0 -1
- package/dist/structures/index.cjs.map +0 -1
- package/dist/structures/index.js.map +0 -1
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
//#region src/graph/adjacency-map.ts
|
|
2
|
+
/**
|
|
3
|
+
* Graph implementation using adjacency map data structure.
|
|
4
|
+
*
|
|
5
|
+
* Uses Map<NodeId, N> for node storage and Map<NodeId, Map<NodeId, E>>
|
|
6
|
+
* for adjacency representation. This provides O(1) average-case lookup
|
|
7
|
+
* for nodes and edges, with memory proportional to V + E.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam N - Node data type, must extend NodeData
|
|
10
|
+
* @typeParam E - Edge data type, must extend EdgeData
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a directed citation graph
|
|
15
|
+
* const graph = AdjacencyMapGraph.directed<AuthorNode, CitationEdge>()
|
|
16
|
+
* .addNode({ id: 'A1', name: 'Alice' })
|
|
17
|
+
* .addNode({ id: 'B1', name: 'Bob' })
|
|
18
|
+
* .addEdge({ source: 'A1', target: 'B1', year: 2024 });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
var AdjacencyMapGraph = class AdjacencyMapGraph {
|
|
22
|
+
directed;
|
|
23
|
+
nodes;
|
|
24
|
+
adjacency;
|
|
25
|
+
reverseAdjacency;
|
|
26
|
+
_edgeCount;
|
|
27
|
+
constructor(directed) {
|
|
28
|
+
this.directed = directed;
|
|
29
|
+
this.nodes = /* @__PURE__ */ new Map();
|
|
30
|
+
this.adjacency = /* @__PURE__ */ new Map();
|
|
31
|
+
this.reverseAdjacency = directed ? /* @__PURE__ */ new Map() : null;
|
|
32
|
+
this._edgeCount = 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a new directed graph.
|
|
36
|
+
*
|
|
37
|
+
* In a directed graph, edges have direction from source to target.
|
|
38
|
+
* The `neighbours` method with direction 'out' returns successors,
|
|
39
|
+
* and direction 'in' returns predecessors.
|
|
40
|
+
*
|
|
41
|
+
* @typeParam N - Node data type
|
|
42
|
+
* @typeParam E - Edge data type
|
|
43
|
+
* @returns A new empty directed graph
|
|
44
|
+
*/
|
|
45
|
+
static directed() {
|
|
46
|
+
return new AdjacencyMapGraph(true);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create a new undirected graph.
|
|
50
|
+
*
|
|
51
|
+
* In an undirected graph, edges have no direction. Adding an edge
|
|
52
|
+
* from A to B automatically creates the connection from B to A.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam N - Node data type
|
|
55
|
+
* @typeParam E - Edge data type
|
|
56
|
+
* @returns A new empty undirected graph
|
|
57
|
+
*/
|
|
58
|
+
static undirected() {
|
|
59
|
+
return new AdjacencyMapGraph(false);
|
|
60
|
+
}
|
|
61
|
+
get nodeCount() {
|
|
62
|
+
return this.nodes.size;
|
|
63
|
+
}
|
|
64
|
+
get edgeCount() {
|
|
65
|
+
return this._edgeCount;
|
|
66
|
+
}
|
|
67
|
+
hasNode(id) {
|
|
68
|
+
return this.nodes.has(id);
|
|
69
|
+
}
|
|
70
|
+
getNode(id) {
|
|
71
|
+
return this.nodes.get(id);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Iterate over all node identifiers in the graph.
|
|
75
|
+
*
|
|
76
|
+
* @returns An iterable of all node IDs
|
|
77
|
+
*/
|
|
78
|
+
*nodeIds() {
|
|
79
|
+
yield* this.nodes.keys();
|
|
80
|
+
}
|
|
81
|
+
neighbours(id, direction = "out") {
|
|
82
|
+
if (!this.nodes.has(id)) return [];
|
|
83
|
+
if (this.directed) {
|
|
84
|
+
if (direction === "out") return this.adjacency.get(id)?.keys() ?? [];
|
|
85
|
+
if (direction === "in") return this.reverseAdjacency?.get(id)?.keys() ?? [];
|
|
86
|
+
return this.iterateBothDirections(id);
|
|
87
|
+
}
|
|
88
|
+
return this.adjacency.get(id)?.keys() ?? [];
|
|
89
|
+
}
|
|
90
|
+
*iterateBothDirections(id) {
|
|
91
|
+
const seen = /* @__PURE__ */ new Set();
|
|
92
|
+
const outNeighbours = this.adjacency.get(id);
|
|
93
|
+
if (outNeighbours !== void 0) {
|
|
94
|
+
for (const neighbour of outNeighbours.keys()) if (!seen.has(neighbour)) {
|
|
95
|
+
seen.add(neighbour);
|
|
96
|
+
yield neighbour;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const inNeighbours = this.reverseAdjacency?.get(id);
|
|
100
|
+
if (inNeighbours !== void 0) {
|
|
101
|
+
for (const neighbour of inNeighbours.keys()) if (!seen.has(neighbour)) {
|
|
102
|
+
seen.add(neighbour);
|
|
103
|
+
yield neighbour;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
degree(id, direction = "out") {
|
|
108
|
+
if (!this.nodes.has(id)) return 0;
|
|
109
|
+
if (this.directed) {
|
|
110
|
+
if (direction === "out") return this.adjacency.get(id)?.size ?? 0;
|
|
111
|
+
if (direction === "in") return this.reverseAdjacency?.get(id)?.size ?? 0;
|
|
112
|
+
return (this.adjacency.get(id)?.size ?? 0) + (this.reverseAdjacency?.get(id)?.size ?? 0);
|
|
113
|
+
}
|
|
114
|
+
return this.adjacency.get(id)?.size ?? 0;
|
|
115
|
+
}
|
|
116
|
+
getEdge(source, target) {
|
|
117
|
+
const forward = this.adjacency.get(source)?.get(target);
|
|
118
|
+
if (forward !== void 0) return forward;
|
|
119
|
+
if (!this.directed) return this.adjacency.get(target)?.get(source);
|
|
120
|
+
}
|
|
121
|
+
*edges() {
|
|
122
|
+
const emitted = /* @__PURE__ */ new Set();
|
|
123
|
+
for (const [, neighbours] of this.adjacency) for (const [, edge] of neighbours) if (this.directed) yield edge;
|
|
124
|
+
else {
|
|
125
|
+
const key = this.edgeKey(edge.source, edge.target);
|
|
126
|
+
if (!emitted.has(key)) {
|
|
127
|
+
emitted.add(key);
|
|
128
|
+
yield edge;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
edgeKey(source, target) {
|
|
133
|
+
const [a, b] = source < target ? [source, target] : [target, source];
|
|
134
|
+
return `${a}::${b}`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Add a node to the graph (builder pattern).
|
|
138
|
+
*
|
|
139
|
+
* If a node with the same ID already exists, it is not replaced.
|
|
140
|
+
*
|
|
141
|
+
* @param node - The node data to add
|
|
142
|
+
* @returns this (for method chaining)
|
|
143
|
+
*/
|
|
144
|
+
addNode(node) {
|
|
145
|
+
if (!this.nodes.has(node.id)) this.nodes.set(node.id, node);
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Add an edge to the graph (builder pattern).
|
|
150
|
+
*
|
|
151
|
+
* @param edge - The edge data to add
|
|
152
|
+
* @returns this (for method chaining)
|
|
153
|
+
* @throws Error if either endpoint node does not exist
|
|
154
|
+
*/
|
|
155
|
+
addEdge(edge) {
|
|
156
|
+
if (!this.nodes.has(edge.source) || !this.nodes.has(edge.target)) throw new Error(`Cannot add edge: nodes ${edge.source} and/or ${edge.target} do not exist`);
|
|
157
|
+
if (!this.directed) {
|
|
158
|
+
const [cSource, cTarget] = edge.source < edge.target ? [edge.source, edge.target] : [edge.target, edge.source];
|
|
159
|
+
if (this.adjacency.get(cSource)?.get(cTarget) !== void 0) {
|
|
160
|
+
this.adjacency.get(cSource)?.set(cTarget, edge);
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
let forwardMap = this.adjacency.get(edge.source);
|
|
165
|
+
if (forwardMap === void 0) {
|
|
166
|
+
forwardMap = /* @__PURE__ */ new Map();
|
|
167
|
+
this.adjacency.set(edge.source, forwardMap);
|
|
168
|
+
}
|
|
169
|
+
const isNewEdge = !forwardMap.has(edge.target);
|
|
170
|
+
forwardMap.set(edge.target, edge);
|
|
171
|
+
if (this.directed) {
|
|
172
|
+
let reverseMap = this.reverseAdjacency?.get(edge.target);
|
|
173
|
+
if (reverseMap === void 0) {
|
|
174
|
+
reverseMap = /* @__PURE__ */ new Map();
|
|
175
|
+
this.reverseAdjacency?.set(edge.target, reverseMap);
|
|
176
|
+
}
|
|
177
|
+
reverseMap.set(edge.source, edge);
|
|
178
|
+
} else {
|
|
179
|
+
let reverseMap = this.adjacency.get(edge.target);
|
|
180
|
+
if (reverseMap === void 0) {
|
|
181
|
+
reverseMap = /* @__PURE__ */ new Map();
|
|
182
|
+
this.adjacency.set(edge.target, reverseMap);
|
|
183
|
+
}
|
|
184
|
+
reverseMap.set(edge.source, edge);
|
|
185
|
+
}
|
|
186
|
+
if (isNewEdge) this._edgeCount++;
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
removeNode(id) {
|
|
190
|
+
if (!this.nodes.has(id)) return false;
|
|
191
|
+
const outNeighbours = [...this.adjacency.get(id)?.keys() ?? []];
|
|
192
|
+
for (const neighbour of outNeighbours) this.removeEdgeInternal(id, neighbour);
|
|
193
|
+
if (this.directed && this.reverseAdjacency !== null) {
|
|
194
|
+
const inNeighbours = [...this.reverseAdjacency.get(id)?.keys() ?? []];
|
|
195
|
+
for (const neighbour of inNeighbours) this.removeEdgeFromDirected(neighbour, id);
|
|
196
|
+
}
|
|
197
|
+
this.nodes.delete(id);
|
|
198
|
+
this.adjacency.delete(id);
|
|
199
|
+
this.reverseAdjacency?.delete(id);
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Remove an edge from a directed graph, updating both adjacency maps.
|
|
204
|
+
* This handles the case where we're removing an edge that points TO the removed node.
|
|
205
|
+
*/
|
|
206
|
+
removeEdgeFromDirected(source, target) {
|
|
207
|
+
if (this.adjacency.get(source)?.delete(target) === true) this._edgeCount--;
|
|
208
|
+
this.reverseAdjacency?.get(target)?.delete(source);
|
|
209
|
+
}
|
|
210
|
+
removeEdgeInternal(source, target) {
|
|
211
|
+
if (this.adjacency.get(source)?.delete(target) === true) this._edgeCount--;
|
|
212
|
+
if (this.directed) this.reverseAdjacency?.get(target)?.delete(source);
|
|
213
|
+
else this.adjacency.get(target)?.delete(source);
|
|
214
|
+
}
|
|
215
|
+
removeEdge(source, target) {
|
|
216
|
+
if (!this.hasEdgeInternal(source, target)) return false;
|
|
217
|
+
this.removeEdgeInternal(source, target);
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
hasEdgeInternal(source, target) {
|
|
221
|
+
if (this.adjacency.get(source)?.has(target) === true) return true;
|
|
222
|
+
if (!this.directed) return this.adjacency.get(target)?.has(source) === true;
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
//#endregion
|
|
227
|
+
export { AdjacencyMapGraph as t };
|
|
228
|
+
|
|
229
|
+
//# sourceMappingURL=adjacency-map-BtKzcuJq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adjacency-map-BtKzcuJq.js","names":[],"sources":["../src/graph/adjacency-map.ts"],"sourcesContent":["/**\n * Adjacency map graph implementation.\n *\n * This module provides a flexible graph implementation using nested Maps\n * for efficient adjacency list representation. It supports both directed\n * and undirected graphs with builder pattern for convenient construction.\n */\n\nimport type { NodeId, NodeData, EdgeData, Direction } from \"./types\";\nimport type { MutableGraph } from \"./interfaces\";\n\n/**\n * Graph implementation using adjacency map data structure.\n *\n * Uses Map<NodeId, N> for node storage and Map<NodeId, Map<NodeId, E>>\n * for adjacency representation. This provides O(1) average-case lookup\n * for nodes and edges, with memory proportional to V + E.\n *\n * @typeParam N - Node data type, must extend NodeData\n * @typeParam E - Edge data type, must extend EdgeData\n *\n * @example\n * ```typescript\n * // Create a directed citation graph\n * const graph = AdjacencyMapGraph.directed<AuthorNode, CitationEdge>()\n * .addNode({ id: 'A1', name: 'Alice' })\n * .addNode({ id: 'B1', name: 'Bob' })\n * .addEdge({ source: 'A1', target: 'B1', year: 2024 });\n * ```\n */\nexport class AdjacencyMapGraph<\n\tN extends NodeData = NodeData,\n\tE extends EdgeData = EdgeData,\n> implements MutableGraph<N, E> {\n\treadonly directed: boolean;\n\n\tprivate readonly nodes: Map<NodeId, N>;\n\tprivate readonly adjacency: Map<NodeId, Map<NodeId, E>>;\n\tprivate readonly reverseAdjacency: Map<NodeId, Map<NodeId, E>> | null;\n\tprivate _edgeCount: number;\n\n\tprivate constructor(directed: boolean) {\n\t\tthis.directed = directed;\n\t\tthis.nodes = new Map();\n\t\tthis.adjacency = new Map();\n\t\tthis.reverseAdjacency = directed ? new Map() : null;\n\t\tthis._edgeCount = 0;\n\t}\n\n\t/**\n\t * Create a new directed graph.\n\t *\n\t * In a directed graph, edges have direction from source to target.\n\t * The `neighbours` method with direction 'out' returns successors,\n\t * and direction 'in' returns predecessors.\n\t *\n\t * @typeParam N - Node data type\n\t * @typeParam E - Edge data type\n\t * @returns A new empty directed graph\n\t */\n\tstatic directed<\n\t\tN extends NodeData = NodeData,\n\t\tE extends EdgeData = EdgeData,\n\t>(): AdjacencyMapGraph<N, E> {\n\t\treturn new AdjacencyMapGraph<N, E>(true);\n\t}\n\n\t/**\n\t * Create a new undirected graph.\n\t *\n\t * In an undirected graph, edges have no direction. Adding an edge\n\t * from A to B automatically creates the connection from B to A.\n\t *\n\t * @typeParam N - Node data type\n\t * @typeParam E - Edge data type\n\t * @returns A new empty undirected graph\n\t */\n\tstatic undirected<\n\t\tN extends NodeData = NodeData,\n\t\tE extends EdgeData = EdgeData,\n\t>(): AdjacencyMapGraph<N, E> {\n\t\treturn new AdjacencyMapGraph<N, E>(false);\n\t}\n\n\tget nodeCount(): number {\n\t\treturn this.nodes.size;\n\t}\n\n\tget edgeCount(): number {\n\t\treturn this._edgeCount;\n\t}\n\n\thasNode(id: NodeId): boolean {\n\t\treturn this.nodes.has(id);\n\t}\n\n\tgetNode(id: NodeId): N | undefined {\n\t\treturn this.nodes.get(id);\n\t}\n\n\t/**\n\t * Iterate over all node identifiers in the graph.\n\t *\n\t * @returns An iterable of all node IDs\n\t */\n\t*nodeIds(): Iterable<NodeId> {\n\t\tyield* this.nodes.keys();\n\t}\n\n\tneighbours(id: NodeId, direction: Direction = \"out\"): Iterable<NodeId> {\n\t\tif (!this.nodes.has(id)) {\n\t\t\treturn [];\n\t\t}\n\n\t\tif (this.directed) {\n\t\t\tif (direction === \"out\") {\n\t\t\t\treturn this.adjacency.get(id)?.keys() ?? [];\n\t\t\t}\n\t\t\tif (direction === \"in\") {\n\t\t\t\treturn this.reverseAdjacency?.get(id)?.keys() ?? [];\n\t\t\t}\n\t\t\t// direction === 'both'\n\t\t\treturn this.iterateBothDirections(id);\n\t\t}\n\n\t\t// Undirected: all neighbours are in adjacency\n\t\treturn this.adjacency.get(id)?.keys() ?? [];\n\t}\n\n\tprivate *iterateBothDirections(id: NodeId): Iterable<NodeId> {\n\t\tconst seen = new Set<NodeId>();\n\n\t\t// Yield outgoing neighbours\n\t\tconst outNeighbours = this.adjacency.get(id);\n\t\tif (outNeighbours !== undefined) {\n\t\t\tfor (const neighbour of outNeighbours.keys()) {\n\t\t\t\tif (!seen.has(neighbour)) {\n\t\t\t\t\tseen.add(neighbour);\n\t\t\t\t\tyield neighbour;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Yield incoming neighbours\n\t\tconst inNeighbours = this.reverseAdjacency?.get(id);\n\t\tif (inNeighbours !== undefined) {\n\t\t\tfor (const neighbour of inNeighbours.keys()) {\n\t\t\t\tif (!seen.has(neighbour)) {\n\t\t\t\t\tseen.add(neighbour);\n\t\t\t\t\tyield neighbour;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tdegree(id: NodeId, direction: Direction = \"out\"): number {\n\t\tif (!this.nodes.has(id)) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (this.directed) {\n\t\t\tif (direction === \"out\") {\n\t\t\t\treturn this.adjacency.get(id)?.size ?? 0;\n\t\t\t}\n\t\t\tif (direction === \"in\") {\n\t\t\t\treturn this.reverseAdjacency?.get(id)?.size ?? 0;\n\t\t\t}\n\t\t\t// direction === 'both': count unique neighbours\n\t\t\tconst outSize = this.adjacency.get(id)?.size ?? 0;\n\t\t\tconst inSize = this.reverseAdjacency?.get(id)?.size ?? 0;\n\t\t\t// Simple sum is sufficient as edges are stored separately\n\t\t\treturn outSize + inSize;\n\t\t}\n\n\t\t// Undirected\n\t\treturn this.adjacency.get(id)?.size ?? 0;\n\t}\n\n\tgetEdge(source: NodeId, target: NodeId): E | undefined {\n\t\t// For undirected, try both orders\n\t\tconst forward = this.adjacency.get(source)?.get(target);\n\t\tif (forward !== undefined) {\n\t\t\treturn forward;\n\t\t}\n\n\t\tif (!this.directed) {\n\t\t\treturn this.adjacency.get(target)?.get(source);\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t*edges(): Iterable<E> {\n\t\tconst emitted = new Set<string>();\n\n\t\tfor (const [, neighbours] of this.adjacency) {\n\t\t\tfor (const [, edge] of neighbours) {\n\t\t\t\tif (this.directed) {\n\t\t\t\t\tyield edge;\n\t\t\t\t} else {\n\t\t\t\t\t// For undirected, avoid emitting duplicate edges\n\t\t\t\t\tconst key = this.edgeKey(edge.source, edge.target);\n\t\t\t\t\tif (!emitted.has(key)) {\n\t\t\t\t\t\temitted.add(key);\n\t\t\t\t\t\tyield edge;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate edgeKey(source: NodeId, target: NodeId): string {\n\t\t// Create a canonical key for undirected edges\n\t\tconst [a, b] = source < target ? [source, target] : [target, source];\n\t\treturn `${a}::${b}`;\n\t}\n\n\t/**\n\t * Add a node to the graph (builder pattern).\n\t *\n\t * If a node with the same ID already exists, it is not replaced.\n\t *\n\t * @param node - The node data to add\n\t * @returns this (for method chaining)\n\t */\n\taddNode(node: N): this {\n\t\tif (!this.nodes.has(node.id)) {\n\t\t\tthis.nodes.set(node.id, node);\n\t\t}\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add an edge to the graph (builder pattern).\n\t *\n\t * @param edge - The edge data to add\n\t * @returns this (for method chaining)\n\t * @throws Error if either endpoint node does not exist\n\t */\n\taddEdge(edge: E): this {\n\t\t// Ensure both nodes exist\n\t\tif (!this.nodes.has(edge.source) || !this.nodes.has(edge.target)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot add edge: nodes ${edge.source} and/or ${edge.target} do not exist`,\n\t\t\t);\n\t\t}\n\n\t\tif (!this.directed) {\n\t\t\t// Canonical direction: source < target (prevents duplicate storage)\n\t\t\tconst [cSource, cTarget] =\n\t\t\t\tedge.source < edge.target\n\t\t\t\t\t? [edge.source, edge.target]\n\t\t\t\t\t: [edge.target, edge.source];\n\t\t\t// Check if edge already exists before incrementing edgeCount\n\t\t\tconst existingEdge = this.adjacency.get(cSource)?.get(cTarget);\n\t\t\tif (existingEdge !== undefined) {\n\t\t\t\t// Edge already exists — update data but don't increment count\n\t\t\t\tthis.adjacency.get(cSource)?.set(cTarget, edge);\n\t\t\t\treturn this;\n\t\t\t}\n\t\t}\n\n\t\t// Store in forward adjacency\n\t\tlet forwardMap = this.adjacency.get(edge.source);\n\t\tif (forwardMap === undefined) {\n\t\t\tforwardMap = new Map();\n\t\t\tthis.adjacency.set(edge.source, forwardMap);\n\t\t}\n\n\t\tconst isNewEdge = !forwardMap.has(edge.target);\n\t\tforwardMap.set(edge.target, edge);\n\n\t\tif (this.directed) {\n\t\t\t// Store reverse reference for efficient predecessor lookup\n\t\t\tlet reverseMap = this.reverseAdjacency?.get(edge.target);\n\t\t\tif (reverseMap === undefined) {\n\t\t\t\treverseMap = new Map();\n\t\t\t\tthis.reverseAdjacency?.set(edge.target, reverseMap);\n\t\t\t}\n\t\t\treverseMap.set(edge.source, edge);\n\t\t} else {\n\t\t\t// For undirected, also store in reverse direction\n\t\t\tlet reverseMap = this.adjacency.get(edge.target);\n\t\t\tif (reverseMap === undefined) {\n\t\t\t\treverseMap = new Map();\n\t\t\t\tthis.adjacency.set(edge.target, reverseMap);\n\t\t\t}\n\t\t\treverseMap.set(edge.source, edge);\n\t\t}\n\n\t\tif (isNewEdge) {\n\t\t\tthis._edgeCount++;\n\t\t}\n\t\treturn this;\n\t}\n\n\tremoveNode(id: NodeId): boolean {\n\t\tif (!this.nodes.has(id)) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Remove all outgoing edges from this node\n\t\tconst outNeighbours = [...(this.adjacency.get(id)?.keys() ?? [])];\n\t\tfor (const neighbour of outNeighbours) {\n\t\t\tthis.removeEdgeInternal(id, neighbour);\n\t\t}\n\n\t\t// For directed graphs, also remove incoming edges to this node\n\t\tif (this.directed && this.reverseAdjacency !== null) {\n\t\t\tconst inNeighbours = [...(this.reverseAdjacency.get(id)?.keys() ?? [])];\n\t\t\tfor (const neighbour of inNeighbours) {\n\t\t\t\t// Remove the edge from neighbour -> id\n\t\t\t\tthis.removeEdgeFromDirected(neighbour, id);\n\t\t\t}\n\t\t}\n\n\t\t// Remove the node itself\n\t\tthis.nodes.delete(id);\n\t\tthis.adjacency.delete(id);\n\t\tthis.reverseAdjacency?.delete(id);\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Remove an edge from a directed graph, updating both adjacency maps.\n\t * This handles the case where we're removing an edge that points TO the removed node.\n\t */\n\tprivate removeEdgeFromDirected(source: NodeId, target: NodeId): void {\n\t\t// Remove from forward adjacency (source -> target)\n\t\tconst forwardMap = this.adjacency.get(source);\n\t\tif (forwardMap?.delete(target) === true) {\n\t\t\tthis._edgeCount--;\n\t\t}\n\n\t\t// Remove from reverse adjacency\n\t\tthis.reverseAdjacency?.get(target)?.delete(source);\n\t}\n\n\tprivate removeEdgeInternal(source: NodeId, target: NodeId): void {\n\t\t// Remove from forward adjacency\n\t\tconst forwardMap = this.adjacency.get(source);\n\t\tif (forwardMap?.delete(target) === true) {\n\t\t\tthis._edgeCount--;\n\t\t}\n\n\t\tif (this.directed) {\n\t\t\t// Remove from reverse adjacency\n\t\t\tthis.reverseAdjacency?.get(target)?.delete(source);\n\t\t} else {\n\t\t\t// For undirected, remove both directions\n\t\t\tthis.adjacency.get(target)?.delete(source);\n\t\t}\n\t}\n\n\tremoveEdge(source: NodeId, target: NodeId): boolean {\n\t\t// Check if edge exists\n\t\tif (!this.hasEdgeInternal(source, target)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tthis.removeEdgeInternal(source, target);\n\t\treturn true;\n\t}\n\n\tprivate hasEdgeInternal(source: NodeId, target: NodeId): boolean {\n\t\tconst forward = this.adjacency.get(source)?.has(target) === true;\n\t\tif (forward) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (!this.directed) {\n\t\t\treturn this.adjacency.get(target)?.has(source) === true;\n\t\t}\n\n\t\treturn false;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8BA,IAAa,oBAAb,MAAa,kBAGmB;CAC/B;CAEA;CACA;CACA;CACA;CAEA,YAAoB,UAAmB;AACtC,OAAK,WAAW;AAChB,OAAK,wBAAQ,IAAI,KAAK;AACtB,OAAK,4BAAY,IAAI,KAAK;AAC1B,OAAK,mBAAmB,2BAAW,IAAI,KAAK,GAAG;AAC/C,OAAK,aAAa;;;;;;;;;;;;;CAcnB,OAAO,WAGsB;AAC5B,SAAO,IAAI,kBAAwB,KAAK;;;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AI,UAAU;AAIvC,MAAI,KAAK,YAAY,KAAK,qBAAqB,MAAM;GACpD,MAAM,eAAe,CAAC,GAAI,KAAK,iBAAiB,IAAI,GAAG,EAAE,MAAM,IAAI,EAAE,CAAE;AACvE,QAAK,MAAM,aAAa,aAEvB,MAAK,uBAAuB,WAAW,GAAG;;AAK5C,OAAK,MAAM,OAAO,GAAG;AACrB,OAAK,UAAU,OAAO,GAAG;AACzB,OAAK,kBAAkB,OAAO,GAAG;AAEjC,SAAO;;;;;;CAOR,uBAA+B,QAAgB,QAAsB;AAGpE,MADmB,KAAK,UAAU,IAAI,OAAO,EAC7B,OAAO,OAAO,KAAK,KAClC,MAAK;AAIN,OAAK,kBAAkB,IAAI,OAAO,EAAE,OAAO,OAAO;;CAGnD,mBAA2B,QAAgB,QAAsB;AAGhE,MADmB,KAAK,UAAU,IAAI,OAAO,EAC7B,OAAO,OAAO,KAAK,KAClC,MAAK;AAGN,MAAI,KAAK,SAER,MAAK,kBAAkB,IAAI,OAAO,EAAE,OAAO,OAAO;MAGlD,MAAK,UAAU,IAAI,OAAO,EAAE,OAAO,OAAO;;CAI5C,WAAW,QAAgB,QAAyB;AAEnD,MAAI,CAAC,KAAK,gBAAgB,QAAQ,OAAO,CACxC,QAAO;AAGR,OAAK,mBAAmB,QAAQ,OAAO;AACvC,SAAO;;CAGR,gBAAwB,QAAgB,QAAyB;AAEhE,MADgB,KAAK,UAAU,IAAI,OAAO,EAAE,IAAI,OAAO,KAAK,KAE3D,QAAO;AAGR,MAAI,CAAC,KAAK,SACT,QAAO,KAAK,UAAU,IAAI,OAAO,EAAE,IAAI,OAAO,KAAK;AAGpD,SAAO"}
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
//#region src/graph/adjacency-map.ts
|
|
2
|
+
/**
|
|
3
|
+
* Graph implementation using adjacency map data structure.
|
|
4
|
+
*
|
|
5
|
+
* Uses Map<NodeId, N> for node storage and Map<NodeId, Map<NodeId, E>>
|
|
6
|
+
* for adjacency representation. This provides O(1) average-case lookup
|
|
7
|
+
* for nodes and edges, with memory proportional to V + E.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam N - Node data type, must extend NodeData
|
|
10
|
+
* @typeParam E - Edge data type, must extend EdgeData
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Create a directed citation graph
|
|
15
|
+
* const graph = AdjacencyMapGraph.directed<AuthorNode, CitationEdge>()
|
|
16
|
+
* .addNode({ id: 'A1', name: 'Alice' })
|
|
17
|
+
* .addNode({ id: 'B1', name: 'Bob' })
|
|
18
|
+
* .addEdge({ source: 'A1', target: 'B1', year: 2024 });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
var AdjacencyMapGraph = class AdjacencyMapGraph {
|
|
22
|
+
directed;
|
|
23
|
+
nodes;
|
|
24
|
+
adjacency;
|
|
25
|
+
reverseAdjacency;
|
|
26
|
+
_edgeCount;
|
|
27
|
+
constructor(directed) {
|
|
28
|
+
this.directed = directed;
|
|
29
|
+
this.nodes = /* @__PURE__ */ new Map();
|
|
30
|
+
this.adjacency = /* @__PURE__ */ new Map();
|
|
31
|
+
this.reverseAdjacency = directed ? /* @__PURE__ */ new Map() : null;
|
|
32
|
+
this._edgeCount = 0;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a new directed graph.
|
|
36
|
+
*
|
|
37
|
+
* In a directed graph, edges have direction from source to target.
|
|
38
|
+
* The `neighbours` method with direction 'out' returns successors,
|
|
39
|
+
* and direction 'in' returns predecessors.
|
|
40
|
+
*
|
|
41
|
+
* @typeParam N - Node data type
|
|
42
|
+
* @typeParam E - Edge data type
|
|
43
|
+
* @returns A new empty directed graph
|
|
44
|
+
*/
|
|
45
|
+
static directed() {
|
|
46
|
+
return new AdjacencyMapGraph(true);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create a new undirected graph.
|
|
50
|
+
*
|
|
51
|
+
* In an undirected graph, edges have no direction. Adding an edge
|
|
52
|
+
* from A to B automatically creates the connection from B to A.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam N - Node data type
|
|
55
|
+
* @typeParam E - Edge data type
|
|
56
|
+
* @returns A new empty undirected graph
|
|
57
|
+
*/
|
|
58
|
+
static undirected() {
|
|
59
|
+
return new AdjacencyMapGraph(false);
|
|
60
|
+
}
|
|
61
|
+
get nodeCount() {
|
|
62
|
+
return this.nodes.size;
|
|
63
|
+
}
|
|
64
|
+
get edgeCount() {
|
|
65
|
+
return this._edgeCount;
|
|
66
|
+
}
|
|
67
|
+
hasNode(id) {
|
|
68
|
+
return this.nodes.has(id);
|
|
69
|
+
}
|
|
70
|
+
getNode(id) {
|
|
71
|
+
return this.nodes.get(id);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Iterate over all node identifiers in the graph.
|
|
75
|
+
*
|
|
76
|
+
* @returns An iterable of all node IDs
|
|
77
|
+
*/
|
|
78
|
+
*nodeIds() {
|
|
79
|
+
yield* this.nodes.keys();
|
|
80
|
+
}
|
|
81
|
+
neighbours(id, direction = "out") {
|
|
82
|
+
if (!this.nodes.has(id)) return [];
|
|
83
|
+
if (this.directed) {
|
|
84
|
+
if (direction === "out") return this.adjacency.get(id)?.keys() ?? [];
|
|
85
|
+
if (direction === "in") return this.reverseAdjacency?.get(id)?.keys() ?? [];
|
|
86
|
+
return this.iterateBothDirections(id);
|
|
87
|
+
}
|
|
88
|
+
return this.adjacency.get(id)?.keys() ?? [];
|
|
89
|
+
}
|
|
90
|
+
*iterateBothDirections(id) {
|
|
91
|
+
const seen = /* @__PURE__ */ new Set();
|
|
92
|
+
const outNeighbours = this.adjacency.get(id);
|
|
93
|
+
if (outNeighbours !== void 0) {
|
|
94
|
+
for (const neighbour of outNeighbours.keys()) if (!seen.has(neighbour)) {
|
|
95
|
+
seen.add(neighbour);
|
|
96
|
+
yield neighbour;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const inNeighbours = this.reverseAdjacency?.get(id);
|
|
100
|
+
if (inNeighbours !== void 0) {
|
|
101
|
+
for (const neighbour of inNeighbours.keys()) if (!seen.has(neighbour)) {
|
|
102
|
+
seen.add(neighbour);
|
|
103
|
+
yield neighbour;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
degree(id, direction = "out") {
|
|
108
|
+
if (!this.nodes.has(id)) return 0;
|
|
109
|
+
if (this.directed) {
|
|
110
|
+
if (direction === "out") return this.adjacency.get(id)?.size ?? 0;
|
|
111
|
+
if (direction === "in") return this.reverseAdjacency?.get(id)?.size ?? 0;
|
|
112
|
+
return (this.adjacency.get(id)?.size ?? 0) + (this.reverseAdjacency?.get(id)?.size ?? 0);
|
|
113
|
+
}
|
|
114
|
+
return this.adjacency.get(id)?.size ?? 0;
|
|
115
|
+
}
|
|
116
|
+
getEdge(source, target) {
|
|
117
|
+
const forward = this.adjacency.get(source)?.get(target);
|
|
118
|
+
if (forward !== void 0) return forward;
|
|
119
|
+
if (!this.directed) return this.adjacency.get(target)?.get(source);
|
|
120
|
+
}
|
|
121
|
+
*edges() {
|
|
122
|
+
const emitted = /* @__PURE__ */ new Set();
|
|
123
|
+
for (const [, neighbours] of this.adjacency) for (const [, edge] of neighbours) if (this.directed) yield edge;
|
|
124
|
+
else {
|
|
125
|
+
const key = this.edgeKey(edge.source, edge.target);
|
|
126
|
+
if (!emitted.has(key)) {
|
|
127
|
+
emitted.add(key);
|
|
128
|
+
yield edge;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
edgeKey(source, target) {
|
|
133
|
+
const [a, b] = source < target ? [source, target] : [target, source];
|
|
134
|
+
return `${a}::${b}`;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Add a node to the graph (builder pattern).
|
|
138
|
+
*
|
|
139
|
+
* If a node with the same ID already exists, it is not replaced.
|
|
140
|
+
*
|
|
141
|
+
* @param node - The node data to add
|
|
142
|
+
* @returns this (for method chaining)
|
|
143
|
+
*/
|
|
144
|
+
addNode(node) {
|
|
145
|
+
if (!this.nodes.has(node.id)) this.nodes.set(node.id, node);
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Add an edge to the graph (builder pattern).
|
|
150
|
+
*
|
|
151
|
+
* @param edge - The edge data to add
|
|
152
|
+
* @returns this (for method chaining)
|
|
153
|
+
* @throws Error if either endpoint node does not exist
|
|
154
|
+
*/
|
|
155
|
+
addEdge(edge) {
|
|
156
|
+
if (!this.nodes.has(edge.source) || !this.nodes.has(edge.target)) throw new Error(`Cannot add edge: nodes ${edge.source} and/or ${edge.target} do not exist`);
|
|
157
|
+
if (!this.directed) {
|
|
158
|
+
const [cSource, cTarget] = edge.source < edge.target ? [edge.source, edge.target] : [edge.target, edge.source];
|
|
159
|
+
if (this.adjacency.get(cSource)?.get(cTarget) !== void 0) {
|
|
160
|
+
this.adjacency.get(cSource)?.set(cTarget, edge);
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
let forwardMap = this.adjacency.get(edge.source);
|
|
165
|
+
if (forwardMap === void 0) {
|
|
166
|
+
forwardMap = /* @__PURE__ */ new Map();
|
|
167
|
+
this.adjacency.set(edge.source, forwardMap);
|
|
168
|
+
}
|
|
169
|
+
const isNewEdge = !forwardMap.has(edge.target);
|
|
170
|
+
forwardMap.set(edge.target, edge);
|
|
171
|
+
if (this.directed) {
|
|
172
|
+
let reverseMap = this.reverseAdjacency?.get(edge.target);
|
|
173
|
+
if (reverseMap === void 0) {
|
|
174
|
+
reverseMap = /* @__PURE__ */ new Map();
|
|
175
|
+
this.reverseAdjacency?.set(edge.target, reverseMap);
|
|
176
|
+
}
|
|
177
|
+
reverseMap.set(edge.source, edge);
|
|
178
|
+
} else {
|
|
179
|
+
let reverseMap = this.adjacency.get(edge.target);
|
|
180
|
+
if (reverseMap === void 0) {
|
|
181
|
+
reverseMap = /* @__PURE__ */ new Map();
|
|
182
|
+
this.adjacency.set(edge.target, reverseMap);
|
|
183
|
+
}
|
|
184
|
+
reverseMap.set(edge.source, edge);
|
|
185
|
+
}
|
|
186
|
+
if (isNewEdge) this._edgeCount++;
|
|
187
|
+
return this;
|
|
188
|
+
}
|
|
189
|
+
removeNode(id) {
|
|
190
|
+
if (!this.nodes.has(id)) return false;
|
|
191
|
+
const outNeighbours = [...this.adjacency.get(id)?.keys() ?? []];
|
|
192
|
+
for (const neighbour of outNeighbours) this.removeEdgeInternal(id, neighbour);
|
|
193
|
+
if (this.directed && this.reverseAdjacency !== null) {
|
|
194
|
+
const inNeighbours = [...this.reverseAdjacency.get(id)?.keys() ?? []];
|
|
195
|
+
for (const neighbour of inNeighbours) this.removeEdgeFromDirected(neighbour, id);
|
|
196
|
+
}
|
|
197
|
+
this.nodes.delete(id);
|
|
198
|
+
this.adjacency.delete(id);
|
|
199
|
+
this.reverseAdjacency?.delete(id);
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Remove an edge from a directed graph, updating both adjacency maps.
|
|
204
|
+
* This handles the case where we're removing an edge that points TO the removed node.
|
|
205
|
+
*/
|
|
206
|
+
removeEdgeFromDirected(source, target) {
|
|
207
|
+
if (this.adjacency.get(source)?.delete(target) === true) this._edgeCount--;
|
|
208
|
+
this.reverseAdjacency?.get(target)?.delete(source);
|
|
209
|
+
}
|
|
210
|
+
removeEdgeInternal(source, target) {
|
|
211
|
+
if (this.adjacency.get(source)?.delete(target) === true) this._edgeCount--;
|
|
212
|
+
if (this.directed) this.reverseAdjacency?.get(target)?.delete(source);
|
|
213
|
+
else this.adjacency.get(target)?.delete(source);
|
|
214
|
+
}
|
|
215
|
+
removeEdge(source, target) {
|
|
216
|
+
if (!this.hasEdgeInternal(source, target)) return false;
|
|
217
|
+
this.removeEdgeInternal(source, target);
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
hasEdgeInternal(source, target) {
|
|
221
|
+
if (this.adjacency.get(source)?.has(target) === true) return true;
|
|
222
|
+
if (!this.directed) return this.adjacency.get(target)?.has(source) === true;
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
//#endregion
|
|
227
|
+
Object.defineProperty(exports, "AdjacencyMapGraph", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
get: function() {
|
|
230
|
+
return AdjacencyMapGraph;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
//# sourceMappingURL=adjacency-map-JqBnMNkF.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adjacency-map-JqBnMNkF.cjs","names":[],"sources":["../src/graph/adjacency-map.ts"],"sourcesContent":["/**\n * Adjacency map graph implementation.\n *\n * This module provides a flexible graph implementation using nested Maps\n * for efficient adjacency list representation. It supports both directed\n * and undirected graphs with builder pattern for convenient construction.\n */\n\nimport type { NodeId, NodeData, EdgeData, Direction } from \"./types\";\nimport type { MutableGraph } from \"./interfaces\";\n\n/**\n * Graph implementation using adjacency map data structure.\n *\n * Uses Map<NodeId, N> for node storage and Map<NodeId, Map<NodeId, E>>\n * for adjacency representation. This provides O(1) average-case lookup\n * for nodes and edges, with memory proportional to V + E.\n *\n * @typeParam N - Node data type, must extend NodeData\n * @typeParam E - Edge data type, must extend EdgeData\n *\n * @example\n * ```typescript\n * // Create a directed citation graph\n * const graph = AdjacencyMapGraph.directed<AuthorNode, CitationEdge>()\n * .addNode({ id: 'A1', name: 'Alice' })\n * .addNode({ id: 'B1', name: 'Bob' })\n * .addEdge({ source: 'A1', target: 'B1', year: 2024 });\n * ```\n */\nexport class AdjacencyMapGraph<\n\tN extends NodeData = NodeData,\n\tE extends EdgeData = EdgeData,\n> implements MutableGraph<N, E> {\n\treadonly directed: boolean;\n\n\tprivate readonly nodes: Map<NodeId, N>;\n\tprivate readonly adjacency: Map<NodeId, Map<NodeId, E>>;\n\tprivate readonly reverseAdjacency: Map<NodeId, Map<NodeId, E>> | null;\n\tprivate _edgeCount: number;\n\n\tprivate constructor(directed: boolean) {\n\t\tthis.directed = directed;\n\t\tthis.nodes = new Map();\n\t\tthis.adjacency = new Map();\n\t\tthis.reverseAdjacency = directed ? new Map() : null;\n\t\tthis._edgeCount = 0;\n\t}\n\n\t/**\n\t * Create a new directed graph.\n\t *\n\t * In a directed graph, edges have direction from source to target.\n\t * The `neighbours` method with direction 'out' returns successors,\n\t * and direction 'in' returns predecessors.\n\t *\n\t * @typeParam N - Node data type\n\t * @typeParam E - Edge data type\n\t * @returns A new empty directed graph\n\t */\n\tstatic directed<\n\t\tN extends NodeData = NodeData,\n\t\tE extends EdgeData = EdgeData,\n\t>(): AdjacencyMapGraph<N, E> {\n\t\treturn new AdjacencyMapGraph<N, E>(true);\n\t}\n\n\t/**\n\t * Create a new undirected graph.\n\t *\n\t * In an undirected graph, edges have no direction. Adding an edge\n\t * from A to B automatically creates the connection from B to A.\n\t *\n\t * @typeParam N - Node data type\n\t * @typeParam E - Edge data type\n\t * @returns A new empty undirected graph\n\t */\n\tstatic undirected<\n\t\tN extends NodeData = NodeData,\n\t\tE extends EdgeData = EdgeData,\n\t>(): AdjacencyMapGraph<N, E> {\n\t\treturn new AdjacencyMapGraph<N, E>(false);\n\t}\n\n\tget nodeCount(): number {\n\t\treturn this.nodes.size;\n\t}\n\n\tget edgeCount(): number {\n\t\treturn this._edgeCount;\n\t}\n\n\thasNode(id: NodeId): boolean {\n\t\treturn this.nodes.has(id);\n\t}\n\n\tgetNode(id: NodeId): N | undefined {\n\t\treturn this.nodes.get(id);\n\t}\n\n\t/**\n\t * Iterate over all node identifiers in the graph.\n\t *\n\t * @returns An iterable of all node IDs\n\t */\n\t*nodeIds(): Iterable<NodeId> {\n\t\tyield* this.nodes.keys();\n\t}\n\n\tneighbours(id: NodeId, direction: Direction = \"out\"): Iterable<NodeId> {\n\t\tif (!this.nodes.has(id)) {\n\t\t\treturn [];\n\t\t}\n\n\t\tif (this.directed) {\n\t\t\tif (direction === \"out\") {\n\t\t\t\treturn this.adjacency.get(id)?.keys() ?? [];\n\t\t\t}\n\t\t\tif (direction === \"in\") {\n\t\t\t\treturn this.reverseAdjacency?.get(id)?.keys() ?? [];\n\t\t\t}\n\t\t\t// direction === 'both'\n\t\t\treturn this.iterateBothDirections(id);\n\t\t}\n\n\t\t// Undirected: all neighbours are in adjacency\n\t\treturn this.adjacency.get(id)?.keys() ?? [];\n\t}\n\n\tprivate *iterateBothDirections(id: NodeId): Iterable<NodeId> {\n\t\tconst seen = new Set<NodeId>();\n\n\t\t// Yield outgoing neighbours\n\t\tconst outNeighbours = this.adjacency.get(id);\n\t\tif (outNeighbours !== undefined) {\n\t\t\tfor (const neighbour of outNeighbours.keys()) {\n\t\t\t\tif (!seen.has(neighbour)) {\n\t\t\t\t\tseen.add(neighbour);\n\t\t\t\t\tyield neighbour;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Yield incoming neighbours\n\t\tconst inNeighbours = this.reverseAdjacency?.get(id);\n\t\tif (inNeighbours !== undefined) {\n\t\t\tfor (const neighbour of inNeighbours.keys()) {\n\t\t\t\tif (!seen.has(neighbour)) {\n\t\t\t\t\tseen.add(neighbour);\n\t\t\t\t\tyield neighbour;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tdegree(id: NodeId, direction: Direction = \"out\"): number {\n\t\tif (!this.nodes.has(id)) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tif (this.directed) {\n\t\t\tif (direction === \"out\") {\n\t\t\t\treturn this.adjacency.get(id)?.size ?? 0;\n\t\t\t}\n\t\t\tif (direction === \"in\") {\n\t\t\t\treturn this.reverseAdjacency?.get(id)?.size ?? 0;\n\t\t\t}\n\t\t\t// direction === 'both': count unique neighbours\n\t\t\tconst outSize = this.adjacency.get(id)?.size ?? 0;\n\t\t\tconst inSize = this.reverseAdjacency?.get(id)?.size ?? 0;\n\t\t\t// Simple sum is sufficient as edges are stored separately\n\t\t\treturn outSize + inSize;\n\t\t}\n\n\t\t// Undirected\n\t\treturn this.adjacency.get(id)?.size ?? 0;\n\t}\n\n\tgetEdge(source: NodeId, target: NodeId): E | undefined {\n\t\t// For undirected, try both orders\n\t\tconst forward = this.adjacency.get(source)?.get(target);\n\t\tif (forward !== undefined) {\n\t\t\treturn forward;\n\t\t}\n\n\t\tif (!this.directed) {\n\t\t\treturn this.adjacency.get(target)?.get(source);\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t*edges(): Iterable<E> {\n\t\tconst emitted = new Set<string>();\n\n\t\tfor (const [, neighbours] of this.adjacency) {\n\t\t\tfor (const [, edge] of neighbours) {\n\t\t\t\tif (this.directed) {\n\t\t\t\t\tyield edge;\n\t\t\t\t} else {\n\t\t\t\t\t// For undirected, avoid emitting duplicate edges\n\t\t\t\t\tconst key = this.edgeKey(edge.source, edge.target);\n\t\t\t\t\tif (!emitted.has(key)) {\n\t\t\t\t\t\temitted.add(key);\n\t\t\t\t\t\tyield edge;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate edgeKey(source: NodeId, target: NodeId): string {\n\t\t// Create a canonical key for undirected edges\n\t\tconst [a, b] = source < target ? [source, target] : [target, source];\n\t\treturn `${a}::${b}`;\n\t}\n\n\t/**\n\t * Add a node to the graph (builder pattern).\n\t *\n\t * If a node with the same ID already exists, it is not replaced.\n\t *\n\t * @param node - The node data to add\n\t * @returns this (for method chaining)\n\t */\n\taddNode(node: N): this {\n\t\tif (!this.nodes.has(node.id)) {\n\t\t\tthis.nodes.set(node.id, node);\n\t\t}\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add an edge to the graph (builder pattern).\n\t *\n\t * @param edge - The edge data to add\n\t * @returns this (for method chaining)\n\t * @throws Error if either endpoint node does not exist\n\t */\n\taddEdge(edge: E): this {\n\t\t// Ensure both nodes exist\n\t\tif (!this.nodes.has(edge.source) || !this.nodes.has(edge.target)) {\n\t\t\tthrow new Error(\n\t\t\t\t`Cannot add edge: nodes ${edge.source} and/or ${edge.target} do not exist`,\n\t\t\t);\n\t\t}\n\n\t\tif (!this.directed) {\n\t\t\t// Canonical direction: source < target (prevents duplicate storage)\n\t\t\tconst [cSource, cTarget] =\n\t\t\t\tedge.source < edge.target\n\t\t\t\t\t? [edge.source, edge.target]\n\t\t\t\t\t: [edge.target, edge.source];\n\t\t\t// Check if edge already exists before incrementing edgeCount\n\t\t\tconst existingEdge = this.adjacency.get(cSource)?.get(cTarget);\n\t\t\tif (existingEdge !== undefined) {\n\t\t\t\t// Edge already exists — update data but don't increment count\n\t\t\t\tthis.adjacency.get(cSource)?.set(cTarget, edge);\n\t\t\t\treturn this;\n\t\t\t}\n\t\t}\n\n\t\t// Store in forward adjacency\n\t\tlet forwardMap = this.adjacency.get(edge.source);\n\t\tif (forwardMap === undefined) {\n\t\t\tforwardMap = new Map();\n\t\t\tthis.adjacency.set(edge.source, forwardMap);\n\t\t}\n\n\t\tconst isNewEdge = !forwardMap.has(edge.target);\n\t\tforwardMap.set(edge.target, edge);\n\n\t\tif (this.directed) {\n\t\t\t// Store reverse reference for efficient predecessor lookup\n\t\t\tlet reverseMap = this.reverseAdjacency?.get(edge.target);\n\t\t\tif (reverseMap === undefined) {\n\t\t\t\treverseMap = new Map();\n\t\t\t\tthis.reverseAdjacency?.set(edge.target, reverseMap);\n\t\t\t}\n\t\t\treverseMap.set(edge.source, edge);\n\t\t} else {\n\t\t\t// For undirected, also store in reverse direction\n\t\t\tlet reverseMap = this.adjacency.get(edge.target);\n\t\t\tif (reverseMap === undefined) {\n\t\t\t\treverseMap = new Map();\n\t\t\t\tthis.adjacency.set(edge.target, reverseMap);\n\t\t\t}\n\t\t\treverseMap.set(edge.source, edge);\n\t\t}\n\n\t\tif (isNewEdge) {\n\t\t\tthis._edgeCount++;\n\t\t}\n\t\treturn this;\n\t}\n\n\tremoveNode(id: NodeId): boolean {\n\t\tif (!this.nodes.has(id)) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// Remove all outgoing edges from this node\n\t\tconst outNeighbours = [...(this.adjacency.get(id)?.keys() ?? [])];\n\t\tfor (const neighbour of outNeighbours) {\n\t\t\tthis.removeEdgeInternal(id, neighbour);\n\t\t}\n\n\t\t// For directed graphs, also remove incoming edges to this node\n\t\tif (this.directed && this.reverseAdjacency !== null) {\n\t\t\tconst inNeighbours = [...(this.reverseAdjacency.get(id)?.keys() ?? [])];\n\t\t\tfor (const neighbour of inNeighbours) {\n\t\t\t\t// Remove the edge from neighbour -> id\n\t\t\t\tthis.removeEdgeFromDirected(neighbour, id);\n\t\t\t}\n\t\t}\n\n\t\t// Remove the node itself\n\t\tthis.nodes.delete(id);\n\t\tthis.adjacency.delete(id);\n\t\tthis.reverseAdjacency?.delete(id);\n\n\t\treturn true;\n\t}\n\n\t/**\n\t * Remove an edge from a directed graph, updating both adjacency maps.\n\t * This handles the case where we're removing an edge that points TO the removed node.\n\t */\n\tprivate removeEdgeFromDirected(source: NodeId, target: NodeId): void {\n\t\t// Remove from forward adjacency (source -> target)\n\t\tconst forwardMap = this.adjacency.get(source);\n\t\tif (forwardMap?.delete(target) === true) {\n\t\t\tthis._edgeCount--;\n\t\t}\n\n\t\t// Remove from reverse adjacency\n\t\tthis.reverseAdjacency?.get(target)?.delete(source);\n\t}\n\n\tprivate removeEdgeInternal(source: NodeId, target: NodeId): void {\n\t\t// Remove from forward adjacency\n\t\tconst forwardMap = this.adjacency.get(source);\n\t\tif (forwardMap?.delete(target) === true) {\n\t\t\tthis._edgeCount--;\n\t\t}\n\n\t\tif (this.directed) {\n\t\t\t// Remove from reverse adjacency\n\t\t\tthis.reverseAdjacency?.get(target)?.delete(source);\n\t\t} else {\n\t\t\t// For undirected, remove both directions\n\t\t\tthis.adjacency.get(target)?.delete(source);\n\t\t}\n\t}\n\n\tremoveEdge(source: NodeId, target: NodeId): boolean {\n\t\t// Check if edge exists\n\t\tif (!this.hasEdgeInternal(source, target)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tthis.removeEdgeInternal(source, target);\n\t\treturn true;\n\t}\n\n\tprivate hasEdgeInternal(source: NodeId, target: NodeId): boolean {\n\t\tconst forward = this.adjacency.get(source)?.has(target) === true;\n\t\tif (forward) {\n\t\t\treturn true;\n\t\t}\n\n\t\tif (!this.directed) {\n\t\t\treturn this.adjacency.get(target)?.has(source) === true;\n\t\t}\n\n\t\treturn false;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8BA,IAAa,oBAAb,MAAa,kBAGmB;CAC/B;CAEA;CACA;CACA;CACA;CAEA,YAAoB,UAAmB;AACtC,OAAK,WAAW;AAChB,OAAK,wBAAQ,IAAI,KAAK;AACtB,OAAK,4BAAY,IAAI,KAAK;AAC1B,OAAK,mBAAmB,2BAAW,IAAI,KAAK,GAAG;AAC/C,OAAK,aAAa;;;;;;;;;;;;;CAcnB,OAAO,WAGsB;AAC5B,SAAO,IAAI,kBAAwB,KAAK;;;;;;;;;;;;CAazC,OAAO,aAGsB;AAC5B,SAAO,IAAI,kBAAwB,MAAM;;CAG1C,IAAI,YAAoB;AACvB,SAAO,KAAK,MAAM;;CAGnB,IAAI,YAAoB;AACvB,SAAO,KAAK;;CAGb,QAAQ,IAAqB;AAC5B,SAAO,KAAK,MAAM,IAAI,GAAG;;CAG1B,QAAQ,IAA2B;AAClC,SAAO,KAAK,MAAM,IAAI,GAAG;;;;;;;CAQ1B,CAAC,UAA4B;AAC5B,SAAO,KAAK,MAAM,MAAM;;CAGzB,WAAW,IAAY,YAAuB,OAAyB;AACtE,MAAI,CAAC,KAAK,MAAM,IAAI,GAAG,CACtB,QAAO,EAAE;AAGV,MAAI,KAAK,UAAU;AAClB,OAAI,cAAc,MACjB,QAAO,KAAK,UAAU,IAAI,GAAG,EAAE,MAAM,IAAI,EAAE;AAE5C,OAAI,cAAc,KACjB,QAAO,KAAK,kBAAkB,IAAI,GAAG,EAAE,MAAM,IAAI,EAAE;AAGpD,UAAO,KAAK,sBAAsB,GAAG;;AAItC,SAAO,KAAK,UAAU,I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