max-priority-queue-typed 2.2.2 → 2.2.4
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/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -5
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +98 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +103 -7
- package/dist/types/data-structures/binary-tree/bst.d.ts +202 -39
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +86 -37
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +7 -7
- package/dist/types/data-structures/graph/directed-graph.d.ts +126 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +160 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +110 -27
- package/dist/types/data-structures/heap/heap.d.ts +107 -58
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +72 -404
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +121 -5
- package/dist/types/data-structures/queue/deque.d.ts +95 -67
- package/dist/types/data-structures/queue/queue.d.ts +90 -34
- package/dist/types/data-structures/stack/stack.d.ts +58 -40
- package/dist/types/data-structures/trie/trie.d.ts +109 -47
- package/dist/types/interfaces/binary-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +5 -5
- package/dist/umd/max-priority-queue-typed.js.map +1 -1
- package/dist/umd/max-priority-queue-typed.min.js.map +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +1 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +7 -8
- package/src/data-structures/binary-tree/avl-tree.ts +100 -7
- package/src/data-structures/binary-tree/binary-tree.ts +117 -7
- package/src/data-structures/binary-tree/bst.ts +431 -93
- package/src/data-structures/binary-tree/red-black-tree.ts +85 -37
- package/src/data-structures/binary-tree/tree-counter.ts +5 -7
- package/src/data-structures/binary-tree/tree-multi-map.ts +9 -10
- package/src/data-structures/graph/directed-graph.ts +126 -1
- package/src/data-structures/graph/undirected-graph.ts +160 -1
- package/src/data-structures/hash/hash-map.ts +110 -27
- package/src/data-structures/heap/heap.ts +107 -58
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -404
- package/src/data-structures/linked-list/singly-linked-list.ts +121 -5
- package/src/data-structures/queue/deque.ts +95 -67
- package/src/data-structures/queue/queue.ts +90 -34
- package/src/data-structures/stack/stack.ts +58 -40
- package/src/data-structures/trie/trie.ts +109 -47
- package/src/interfaces/binary-tree.ts +2 -0
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult,
|
|
8
|
+
import type { BinaryTreeDeleteResult, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
9
|
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
@@ -112,48 +112,97 @@ export declare class RedBlackTreeNode<K = any, V = any> {
|
|
|
112
112
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
113
113
|
*
|
|
114
114
|
* @example
|
|
115
|
-
* //
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
115
|
+
* // basic Red-Black Tree with simple number keys
|
|
116
|
+
* // Create a simple Red-Black Tree with numeric keys
|
|
117
|
+
* const tree = new RedBlackTree([5, 2, 8, 1, 9]);
|
|
118
|
+
*
|
|
119
|
+
* tree.print();
|
|
120
|
+
* // _2___
|
|
121
|
+
* // / \
|
|
122
|
+
* // 1 _8_
|
|
123
|
+
* // / \
|
|
124
|
+
* // 5 9
|
|
125
|
+
*
|
|
126
|
+
* // Verify the tree maintains sorted order
|
|
127
|
+
* console.log([...tree.keys()]); // [1, 2, 5, 8, 9];
|
|
128
|
+
*
|
|
129
|
+
* // Check size
|
|
130
|
+
* console.log(tree.size); // 5;
|
|
131
|
+
* @example
|
|
132
|
+
* // Red-Black Tree with key-value pairs for lookups
|
|
133
|
+
* interface Employee {
|
|
134
|
+
* id: number;
|
|
135
|
+
* name: string;
|
|
121
136
|
* }
|
|
122
137
|
*
|
|
123
|
-
* //
|
|
124
|
-
* const
|
|
125
|
-
* {
|
|
126
|
-
* {
|
|
127
|
-
* {
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
138
|
+
* // Create tree with employee data
|
|
139
|
+
* const employees = new RedBlackTree<number, Employee>([
|
|
140
|
+
* [1, { id: 1, name: 'Alice' }],
|
|
141
|
+
* [3, { id: 3, name: 'Charlie' }],
|
|
142
|
+
* [2, { id: 2, name: 'Bob' }]
|
|
143
|
+
* ]);
|
|
144
|
+
*
|
|
145
|
+
* // Retrieve employee by ID
|
|
146
|
+
* const alice = employees.get(1);
|
|
147
|
+
* console.log(alice?.name); // 'Alice';
|
|
148
|
+
*
|
|
149
|
+
* // Verify sorted order by ID
|
|
150
|
+
* console.log([...employees.keys()]); // [1, 2, 3];
|
|
151
|
+
* @example
|
|
152
|
+
* // Red-Black Tree range search for filtering
|
|
153
|
+
* interface Product {
|
|
154
|
+
* name: string;
|
|
155
|
+
* price: number;
|
|
156
|
+
* }
|
|
131
157
|
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
158
|
+
* const products = new RedBlackTree<number, Product>([
|
|
159
|
+
* [10, { name: 'Item A', price: 10 }],
|
|
160
|
+
* [25, { name: 'Item B', price: 25 }],
|
|
161
|
+
* [40, { name: 'Item C', price: 40 }],
|
|
162
|
+
* [50, { name: 'Item D', price: 50 }]
|
|
163
|
+
* ]);
|
|
134
164
|
*
|
|
135
|
-
* //
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
* toEntryFn: stockRecord => [
|
|
139
|
-
* stockRecord.price, // Use stock price as the key
|
|
140
|
-
* {
|
|
141
|
-
* ...stockRecord,
|
|
142
|
-
* lastUpdated: new Date() // Add a timestamp for when the record was indexed
|
|
143
|
-
* }
|
|
144
|
-
* ]
|
|
165
|
+
* // Find products in price range [20, 45]
|
|
166
|
+
* const pricesInRange = products.rangeSearch([20, 45], node => {
|
|
167
|
+
* return products.get(node)?.name;
|
|
145
168
|
* });
|
|
146
169
|
*
|
|
147
|
-
* //
|
|
148
|
-
*
|
|
149
|
-
*
|
|
170
|
+
* console.log(pricesInRange); // ['Item B', 'Item C'];
|
|
171
|
+
* @example
|
|
172
|
+
* // Red-Black Tree as database index for stock market data
|
|
173
|
+
* interface StockPrice {
|
|
174
|
+
* symbol: string;
|
|
175
|
+
* volume: number;
|
|
176
|
+
* timestamp: Date;
|
|
177
|
+
* }
|
|
178
|
+
*
|
|
179
|
+
* // Simulate real-time stock price index
|
|
180
|
+
* const priceIndex = new RedBlackTree<number, StockPrice>([
|
|
181
|
+
* [142.5, { symbol: 'AAPL', volume: 1000000, timestamp: new Date() }],
|
|
182
|
+
* [335.2, { symbol: 'MSFT', volume: 800000, timestamp: new Date() }],
|
|
183
|
+
* [3285.04, { symbol: 'AMZN', volume: 500000, timestamp: new Date() }],
|
|
184
|
+
* [267.98, { symbol: 'META', volume: 750000, timestamp: new Date() }],
|
|
185
|
+
* [234.57, { symbol: 'GOOGL', volume: 900000, timestamp: new Date() }]
|
|
186
|
+
* ]);
|
|
187
|
+
*
|
|
188
|
+
* // Find highest-priced stock
|
|
189
|
+
* const maxPrice = priceIndex.getRightMost();
|
|
190
|
+
* console.log(priceIndex.get(maxPrice)?.symbol); // 'AMZN';
|
|
191
|
+
*
|
|
192
|
+
* // Find stocks in price range [200, 400] for portfolio balancing
|
|
193
|
+
* const stocksInRange = priceIndex.rangeSearch([200, 400], node => {
|
|
194
|
+
* const stock = priceIndex.get(node);
|
|
195
|
+
* return {
|
|
196
|
+
* symbol: stock?.symbol,
|
|
197
|
+
* price: node,
|
|
198
|
+
* volume: stock?.volume
|
|
199
|
+
* };
|
|
200
|
+
* });
|
|
150
201
|
*
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
* );
|
|
156
|
-
* console.log(stocksInRange); // ['GOOGL', 'META', 'MSFT']
|
|
202
|
+
* console.log(stocksInRange.length); // 3;
|
|
203
|
+
* console.log(stocksInRange.some((s: any) => s.symbol === 'GOOGL')); // true;
|
|
204
|
+
* console.log(stocksInRange.some((s: any) => s.symbol === 'META')); // true;
|
|
205
|
+
* console.log(stocksInRange.some((s: any) => s.symbol === 'MSFT')); // true;
|
|
157
206
|
*/
|
|
158
207
|
export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
159
208
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | RedBlackTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: RedBlackTreeOptions<K, V, R>);
|
|
@@ -212,7 +261,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
212
261
|
* @param [thisArg] - See parameter type for details.
|
|
213
262
|
* @returns A new RedBlackTree with mapped entries.
|
|
214
263
|
*/
|
|
215
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
264
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<RedBlackTreeOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
216
265
|
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): this;
|
|
217
266
|
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<RedBlackTreeOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
218
267
|
protected _setRoot(v: RedBlackTreeNode<K, V> | undefined): void;
|
|
@@ -5,8 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult,
|
|
9
|
-
import { BSTOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
10
9
|
import { BSTNode } from './bst';
|
|
11
10
|
import { IBinaryTree } from '../../interfaces';
|
|
12
11
|
import { RedBlackTree } from './red-black-tree';
|
|
@@ -188,7 +187,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
188
187
|
* @param [thisArg] - Value for `this` inside the callback.
|
|
189
188
|
* @returns A new TreeCounter with mapped entries.
|
|
190
189
|
*/
|
|
191
|
-
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<
|
|
190
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<TreeCounterOptions<MK, MV, MR>>, thisArg?: unknown): TreeCounter<MK, MV, MR>;
|
|
192
191
|
/**
|
|
193
192
|
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
194
193
|
* @remarks Time O(N), Space O(N)
|
|
@@ -204,7 +203,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
204
203
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
205
204
|
* @returns An empty like-kind instance.
|
|
206
205
|
*/
|
|
207
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
206
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeCounterOptions<TK, TV, TR>>): this;
|
|
208
207
|
/**
|
|
209
208
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
210
209
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -215,7 +214,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
215
214
|
* @param [options] - Options merged with the current snapshot.
|
|
216
215
|
* @returns A like-kind TreeCounter built from the iterable.
|
|
217
216
|
*/
|
|
218
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
217
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeCounterOptions<TK, TV, TR>>): TreeCounter<TK, TV, TR>;
|
|
219
218
|
/**
|
|
220
219
|
* (Protected) Normalize input into a node plus its effective value and count.
|
|
221
220
|
* @remarks Time O(1), Space O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor,
|
|
8
|
+
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -116,7 +116,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
|
|
|
116
116
|
*
|
|
117
117
|
* @example
|
|
118
118
|
* // players ranked by score with their equipment
|
|
119
|
-
*
|
|
119
|
+
* type Equipment = {
|
|
120
120
|
* name: string; // Equipment name
|
|
121
121
|
* quality: 'legendary' | 'epic' | 'rare' | 'common';
|
|
122
122
|
* level: number;
|
|
@@ -277,7 +277,7 @@ export declare class TreeMultiMapNode<K = any, V = any> {
|
|
|
277
277
|
* // },
|
|
278
278
|
* // { name: 'Level 3 Backpack', quality: 'epic', level: 80 }
|
|
279
279
|
* // ]
|
|
280
|
-
* // ]
|
|
280
|
+
* // ];
|
|
281
281
|
*/
|
|
282
282
|
export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
283
283
|
/**
|
|
@@ -307,8 +307,8 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
307
307
|
* @returns True if the value was removed; false if not found.
|
|
308
308
|
*/
|
|
309
309
|
deleteValue(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
|
|
310
|
-
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<
|
|
311
|
-
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<
|
|
310
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<TreeMultiMapOptions<MK, MVArr, MR>>, thisArg?: unknown): TreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
311
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<TreeMultiMapOptions<MK, MV, MR>>, thisArg?: unknown): RedBlackTree<MK, MV, MR>;
|
|
312
312
|
/**
|
|
313
313
|
* (Protected) Create an empty instance of the same concrete class.
|
|
314
314
|
* @remarks Time O(1), Space O(1)
|
|
@@ -318,7 +318,7 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
318
318
|
* @param [options] - Optional constructor options for the like-kind instance.
|
|
319
319
|
* @returns An empty like-kind instance.
|
|
320
320
|
*/
|
|
321
|
-
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<
|
|
321
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): this;
|
|
322
322
|
/**
|
|
323
323
|
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
324
324
|
* @remarks Time O(N log N), Space O(N)
|
|
@@ -329,5 +329,5 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
329
329
|
* @param [options] - Options merged with the current snapshot.
|
|
330
330
|
* @returns A like-kind RedBlackTree built from the iterable.
|
|
331
331
|
*/
|
|
332
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<
|
|
332
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | RedBlackTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<TreeMultiMapOptions<TK, TV, TR>>): RedBlackTree<TK, TV, TR>;
|
|
333
333
|
}
|
|
@@ -23,7 +23,132 @@ export declare class DirectedEdge<E = any> extends AbstractEdge<E> {
|
|
|
23
23
|
* @template VO - Concrete vertex class (extends AbstractVertex<V>).
|
|
24
24
|
* @template EO - Concrete edge class (extends AbstractEdge<E>).
|
|
25
25
|
* @remarks Time O(1), Space O(1)
|
|
26
|
-
* @example
|
|
26
|
+
* @example
|
|
27
|
+
* // basic DirectedGraph vertex and edge creation
|
|
28
|
+
* // Create a simple directed graph
|
|
29
|
+
* const graph = new DirectedGraph<string>();
|
|
30
|
+
*
|
|
31
|
+
* // Add vertices
|
|
32
|
+
* graph.addVertex('A');
|
|
33
|
+
* graph.addVertex('B');
|
|
34
|
+
* graph.addVertex('C');
|
|
35
|
+
*
|
|
36
|
+
* // Verify vertices exist
|
|
37
|
+
* console.log(graph.hasVertex('A')); // true;
|
|
38
|
+
* console.log(graph.hasVertex('B')); // true;
|
|
39
|
+
* console.log(graph.hasVertex('C')); // true;
|
|
40
|
+
* console.log(graph.hasVertex('D')); // false;
|
|
41
|
+
*
|
|
42
|
+
* // Check vertex count
|
|
43
|
+
* console.log(graph.size); // 3;
|
|
44
|
+
* @example
|
|
45
|
+
* // DirectedGraph edge operations
|
|
46
|
+
* const graph = new DirectedGraph<string>();
|
|
47
|
+
*
|
|
48
|
+
* // Add vertices
|
|
49
|
+
* graph.addVertex('A');
|
|
50
|
+
* graph.addVertex('B');
|
|
51
|
+
* graph.addVertex('C');
|
|
52
|
+
*
|
|
53
|
+
* // Add directed edges
|
|
54
|
+
* graph.addEdge('A', 'B', 1);
|
|
55
|
+
* graph.addEdge('B', 'C', 2);
|
|
56
|
+
* graph.addEdge('A', 'C', 3);
|
|
57
|
+
*
|
|
58
|
+
* // Verify edges exist
|
|
59
|
+
* console.log(graph.hasEdge('A', 'B')); // true;
|
|
60
|
+
* console.log(graph.hasEdge('B', 'C')); // true;
|
|
61
|
+
* console.log(graph.hasEdge('C', 'B')); // false; // Graph is directed
|
|
62
|
+
*
|
|
63
|
+
* // Get neighbors of A
|
|
64
|
+
* const neighborsA = graph.getNeighbors('A');
|
|
65
|
+
* console.log(neighborsA[0].key); // 'B';
|
|
66
|
+
* console.log(neighborsA[1].key); // 'C';
|
|
67
|
+
* @example
|
|
68
|
+
* // DirectedGraph deleteEdge and vertex operations
|
|
69
|
+
* const graph = new DirectedGraph<string>();
|
|
70
|
+
*
|
|
71
|
+
* // Build a small graph
|
|
72
|
+
* graph.addVertex('X');
|
|
73
|
+
* graph.addVertex('Y');
|
|
74
|
+
* graph.addVertex('Z');
|
|
75
|
+
* graph.addEdge('X', 'Y', 1);
|
|
76
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
77
|
+
*
|
|
78
|
+
* // Delete an edge
|
|
79
|
+
* graph.deleteEdgeSrcToDest('X', 'Y');
|
|
80
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
81
|
+
*
|
|
82
|
+
* // Edge in other direction should not exist
|
|
83
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
84
|
+
*
|
|
85
|
+
* // Other edges should remain
|
|
86
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
87
|
+
*
|
|
88
|
+
* // Delete a vertex
|
|
89
|
+
* graph.deleteVertex('Y');
|
|
90
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
91
|
+
* console.log(graph.size); // 2;
|
|
92
|
+
* @example
|
|
93
|
+
* // DirectedGraph topologicalSort for task scheduling
|
|
94
|
+
* const graph = new DirectedGraph<string>();
|
|
95
|
+
*
|
|
96
|
+
* // Build a DAG (Directed Acyclic Graph) for task dependencies
|
|
97
|
+
* graph.addVertex('Design');
|
|
98
|
+
* graph.addVertex('Implement');
|
|
99
|
+
* graph.addVertex('Test');
|
|
100
|
+
* graph.addVertex('Deploy');
|
|
101
|
+
*
|
|
102
|
+
* // Add dependency edges
|
|
103
|
+
* graph.addEdge('Design', 'Implement', 1); // Design must come before Implement
|
|
104
|
+
* graph.addEdge('Implement', 'Test', 1); // Implement must come before Test
|
|
105
|
+
* graph.addEdge('Test', 'Deploy', 1); // Test must come before Deploy
|
|
106
|
+
*
|
|
107
|
+
* // Topological sort gives valid execution order
|
|
108
|
+
* const executionOrder = graph.topologicalSort();
|
|
109
|
+
* console.log(executionOrder); // defined;
|
|
110
|
+
* console.log(executionOrder); // ['Design', 'Implement', 'Test', 'Deploy'];
|
|
111
|
+
*
|
|
112
|
+
* // All vertices should be included
|
|
113
|
+
* console.log(executionOrder?.length); // 4;
|
|
114
|
+
* @example
|
|
115
|
+
* // DirectedGraph dijkstra shortest path for network routing
|
|
116
|
+
* // Build a weighted directed graph representing network nodes and costs
|
|
117
|
+
* const network = new DirectedGraph<string>();
|
|
118
|
+
*
|
|
119
|
+
* // Add network nodes
|
|
120
|
+
* network.addVertex('Router-A');
|
|
121
|
+
* network.addVertex('Router-B');
|
|
122
|
+
* network.addVertex('Router-C');
|
|
123
|
+
* network.addVertex('Router-D');
|
|
124
|
+
* network.addVertex('Router-E');
|
|
125
|
+
*
|
|
126
|
+
* // Add weighted edges (network latency costs)
|
|
127
|
+
* network.addEdge('Router-A', 'Router-B', 5);
|
|
128
|
+
* network.addEdge('Router-A', 'Router-C', 10);
|
|
129
|
+
* network.addEdge('Router-B', 'Router-D', 3);
|
|
130
|
+
* network.addEdge('Router-C', 'Router-D', 2);
|
|
131
|
+
* network.addEdge('Router-D', 'Router-E', 4);
|
|
132
|
+
* network.addEdge('Router-B', 'Router-E', 12);
|
|
133
|
+
*
|
|
134
|
+
* // Find shortest path from Router-A to Router-E
|
|
135
|
+
* const { minDist, minPath } = network.dijkstra('Router-A', 'Router-E', true, true) || {
|
|
136
|
+
* minDist: undefined,
|
|
137
|
+
* minPath: undefined
|
|
138
|
+
* };
|
|
139
|
+
*
|
|
140
|
+
* // Verify shortest path is found
|
|
141
|
+
* console.log(minDist); // defined;
|
|
142
|
+
* console.log(minPath); // defined;
|
|
143
|
+
*
|
|
144
|
+
* // Shortest path should be A -> B -> D -> E with cost 5+3+4=12
|
|
145
|
+
* // Or A -> C -> D -> E with cost 10+2+4=16
|
|
146
|
+
* // So the minimum is 12
|
|
147
|
+
* console.log(minDist); // <= 16;
|
|
148
|
+
*
|
|
149
|
+
* // Verify path is valid (includes start and end)
|
|
150
|
+
* console.log(minPath?.[0].key); // 'Router-A';
|
|
151
|
+
* console.log(minPath?.[minPath.length - 1].key); // 'Router-E';
|
|
27
152
|
*/
|
|
28
153
|
export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V> = DirectedVertex<V>, EO extends DirectedEdge<E> = DirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
|
|
29
154
|
/**
|
|
@@ -22,7 +22,166 @@ export declare class UndirectedEdge<E = number> extends AbstractEdge<E> {
|
|
|
22
22
|
* @template VO - Concrete vertex class (extends AbstractVertex<V>).
|
|
23
23
|
* @template EO - Concrete edge class (extends AbstractEdge<E>).
|
|
24
24
|
* @remarks Time O(1), Space O(1)
|
|
25
|
-
* @example
|
|
25
|
+
* @example
|
|
26
|
+
* // basic UndirectedGraph vertex and edge creation
|
|
27
|
+
* // Create a simple undirected graph
|
|
28
|
+
* const graph = new UndirectedGraph<string>();
|
|
29
|
+
*
|
|
30
|
+
* // Add vertices
|
|
31
|
+
* graph.addVertex('A');
|
|
32
|
+
* graph.addVertex('B');
|
|
33
|
+
* graph.addVertex('C');
|
|
34
|
+
* graph.addVertex('D');
|
|
35
|
+
*
|
|
36
|
+
* // Verify vertices exist
|
|
37
|
+
* console.log(graph.hasVertex('A')); // true;
|
|
38
|
+
* console.log(graph.hasVertex('B')); // true;
|
|
39
|
+
* console.log(graph.hasVertex('E')); // false;
|
|
40
|
+
*
|
|
41
|
+
* // Check vertex count
|
|
42
|
+
* console.log(graph.size); // 4;
|
|
43
|
+
* @example
|
|
44
|
+
* // UndirectedGraph edge operations (bidirectional)
|
|
45
|
+
* const graph = new UndirectedGraph<string>();
|
|
46
|
+
*
|
|
47
|
+
* // Add vertices
|
|
48
|
+
* graph.addVertex('A');
|
|
49
|
+
* graph.addVertex('B');
|
|
50
|
+
* graph.addVertex('C');
|
|
51
|
+
*
|
|
52
|
+
* // Add undirected edges (both directions automatically)
|
|
53
|
+
* graph.addEdge('A', 'B', 1);
|
|
54
|
+
* graph.addEdge('B', 'C', 2);
|
|
55
|
+
* graph.addEdge('A', 'C', 3);
|
|
56
|
+
*
|
|
57
|
+
* // Verify edges exist in both directions
|
|
58
|
+
* console.log(graph.hasEdge('A', 'B')); // true;
|
|
59
|
+
* console.log(graph.hasEdge('B', 'A')); // true; // Bidirectional!
|
|
60
|
+
*
|
|
61
|
+
* console.log(graph.hasEdge('C', 'B')); // true;
|
|
62
|
+
* console.log(graph.hasEdge('B', 'C')); // true; // Bidirectional!
|
|
63
|
+
*
|
|
64
|
+
* // Get neighbors of A
|
|
65
|
+
* const neighborsA = graph.getNeighbors('A');
|
|
66
|
+
* console.log(neighborsA[0].key); // 'B';
|
|
67
|
+
* console.log(neighborsA[1].key); // 'C';
|
|
68
|
+
* @example
|
|
69
|
+
* // UndirectedGraph deleteEdge and vertex operations
|
|
70
|
+
* const graph = new UndirectedGraph<string>();
|
|
71
|
+
*
|
|
72
|
+
* // Build a simple undirected graph
|
|
73
|
+
* graph.addVertex('X');
|
|
74
|
+
* graph.addVertex('Y');
|
|
75
|
+
* graph.addVertex('Z');
|
|
76
|
+
* graph.addEdge('X', 'Y', 1);
|
|
77
|
+
* graph.addEdge('Y', 'Z', 2);
|
|
78
|
+
* graph.addEdge('X', 'Z', 3);
|
|
79
|
+
*
|
|
80
|
+
* // Delete an edge
|
|
81
|
+
* graph.deleteEdge('X', 'Y');
|
|
82
|
+
* console.log(graph.hasEdge('X', 'Y')); // false;
|
|
83
|
+
*
|
|
84
|
+
* // Bidirectional deletion confirmed
|
|
85
|
+
* console.log(graph.hasEdge('Y', 'X')); // false;
|
|
86
|
+
*
|
|
87
|
+
* // Other edges should remain
|
|
88
|
+
* console.log(graph.hasEdge('Y', 'Z')); // true;
|
|
89
|
+
* console.log(graph.hasEdge('Z', 'Y')); // true;
|
|
90
|
+
*
|
|
91
|
+
* // Delete a vertex
|
|
92
|
+
* graph.deleteVertex('Y');
|
|
93
|
+
* console.log(graph.hasVertex('Y')); // false;
|
|
94
|
+
* console.log(graph.size); // 2;
|
|
95
|
+
* @example
|
|
96
|
+
* // UndirectedGraph connectivity and neighbors
|
|
97
|
+
* const graph = new UndirectedGraph<string>();
|
|
98
|
+
*
|
|
99
|
+
* // Build a friendship network
|
|
100
|
+
* const people = ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'];
|
|
101
|
+
* for (const person of people) {
|
|
102
|
+
* graph.addVertex(person);
|
|
103
|
+
* }
|
|
104
|
+
*
|
|
105
|
+
* // Add friendships (undirected edges)
|
|
106
|
+
* graph.addEdge('Alice', 'Bob', 1);
|
|
107
|
+
* graph.addEdge('Alice', 'Charlie', 1);
|
|
108
|
+
* graph.addEdge('Bob', 'Diana', 1);
|
|
109
|
+
* graph.addEdge('Charlie', 'Eve', 1);
|
|
110
|
+
* graph.addEdge('Diana', 'Eve', 1);
|
|
111
|
+
*
|
|
112
|
+
* // Get friends of each person
|
|
113
|
+
* const aliceFriends = graph.getNeighbors('Alice');
|
|
114
|
+
* console.log(aliceFriends[0].key); // 'Bob';
|
|
115
|
+
* console.log(aliceFriends[1].key); // 'Charlie';
|
|
116
|
+
* console.log(aliceFriends.length); // 2;
|
|
117
|
+
*
|
|
118
|
+
* const dianaFriends = graph.getNeighbors('Diana');
|
|
119
|
+
* console.log(dianaFriends[0].key); // 'Bob';
|
|
120
|
+
* console.log(dianaFriends[1].key); // 'Eve';
|
|
121
|
+
* console.log(dianaFriends.length); // 2;
|
|
122
|
+
*
|
|
123
|
+
* // Verify bidirectional friendship
|
|
124
|
+
* const bobFriends = graph.getNeighbors('Bob');
|
|
125
|
+
* console.log(bobFriends[0].key); // 'Alice'; // Alice -> Bob -> Alice ✓
|
|
126
|
+
* console.log(bobFriends[1].key); // 'Diana';
|
|
127
|
+
* @example
|
|
128
|
+
* // UndirectedGraph for social network connectivity analysis
|
|
129
|
+
* interface Person {
|
|
130
|
+
* id: number;
|
|
131
|
+
* name: string;
|
|
132
|
+
* location: string;
|
|
133
|
+
* }
|
|
134
|
+
*
|
|
135
|
+
* // UndirectedGraph is perfect for modeling symmetric relationships
|
|
136
|
+
* // (friendships, collaborations, partnerships)
|
|
137
|
+
* const socialNetwork = new UndirectedGraph<number, Person>();
|
|
138
|
+
*
|
|
139
|
+
* // Add people as vertices
|
|
140
|
+
* const people: [number, Person][] = [
|
|
141
|
+
* [1, { id: 1, name: 'Alice', location: 'New York' }],
|
|
142
|
+
* [2, { id: 2, name: 'Bob', location: 'San Francisco' }],
|
|
143
|
+
* [3, { id: 3, name: 'Charlie', location: 'Boston' }],
|
|
144
|
+
* [4, { id: 4, name: 'Diana', location: 'New York' }],
|
|
145
|
+
* [5, { id: 5, name: 'Eve', location: 'Seattle' }]
|
|
146
|
+
* ];
|
|
147
|
+
*
|
|
148
|
+
* for (const [id] of people) {
|
|
149
|
+
* socialNetwork.addVertex(id);
|
|
150
|
+
* }
|
|
151
|
+
*
|
|
152
|
+
* // Add friendships (automatically bidirectional)
|
|
153
|
+
* socialNetwork.addEdge(1, 2, 1); // Alice <-> Bob
|
|
154
|
+
* socialNetwork.addEdge(1, 3, 1); // Alice <-> Charlie
|
|
155
|
+
* socialNetwork.addEdge(2, 4, 1); // Bob <-> Diana
|
|
156
|
+
* socialNetwork.addEdge(3, 5, 1); // Charlie <-> Eve
|
|
157
|
+
* socialNetwork.addEdge(4, 5, 1); // Diana <-> Eve
|
|
158
|
+
*
|
|
159
|
+
* console.log(socialNetwork.size); // 5;
|
|
160
|
+
*
|
|
161
|
+
* // Find direct connections for Alice
|
|
162
|
+
* const aliceConnections = socialNetwork.getNeighbors(1);
|
|
163
|
+
* console.log(aliceConnections[0].key); // 2;
|
|
164
|
+
* console.log(aliceConnections[1].key); // 3;
|
|
165
|
+
* console.log(aliceConnections.length); // 2;
|
|
166
|
+
*
|
|
167
|
+
* // Verify bidirectional connections
|
|
168
|
+
* console.log(socialNetwork.hasEdge(1, 2)); // true;
|
|
169
|
+
* console.log(socialNetwork.hasEdge(2, 1)); // true; // Friendship works both ways!
|
|
170
|
+
*
|
|
171
|
+
* // Remove a person from network
|
|
172
|
+
* socialNetwork.deleteVertex(2); // Bob leaves
|
|
173
|
+
* console.log(socialNetwork.hasVertex(2)); // false;
|
|
174
|
+
* console.log(socialNetwork.size); // 4;
|
|
175
|
+
*
|
|
176
|
+
* // Alice loses Bob as a friend
|
|
177
|
+
* const updatedAliceConnections = socialNetwork.getNeighbors(1);
|
|
178
|
+
* console.log(updatedAliceConnections[0].key); // 3;
|
|
179
|
+
* console.log(updatedAliceConnections[1]); // undefined;
|
|
180
|
+
*
|
|
181
|
+
* // Diana loses Bob as a friend
|
|
182
|
+
* const dianaConnections = socialNetwork.getNeighbors(4);
|
|
183
|
+
* console.log(dianaConnections[0].key); // 5;
|
|
184
|
+
* console.log(dianaConnections[1]); // undefined;
|
|
26
185
|
*/
|
|
27
186
|
export declare class UndirectedGraph<V = any, E = any, VO extends UndirectedVertex<V> = UndirectedVertex<V>, EO extends UndirectedEdge<E> = UndirectedEdge<E>> extends AbstractGraph<V, E, VO, EO> implements IGraph<V, E, VO, EO> {
|
|
28
187
|
/**
|