graph-typed 1.45.3 → 1.46.2

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.
@@ -9,15 +9,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.FibonacciHeap = exports.FibonacciHeapNode = exports.Heap = void 0;
10
10
  class Heap {
11
11
  constructor(options) {
12
- this._nodes = [];
12
+ this._elements = [];
13
13
  this._comparator = options.comparator;
14
- if (options.nodes && options.nodes.length > 0) {
15
- this._nodes = options.nodes;
14
+ if (options.elements && options.elements.length > 0) {
15
+ this._elements = options.elements;
16
16
  this.fix();
17
17
  }
18
18
  }
19
- get nodes() {
20
- return this._nodes;
19
+ get elements() {
20
+ return this._elements;
21
21
  }
22
22
  get comparator() {
23
23
  return this._comparator;
@@ -26,7 +26,7 @@ class Heap {
26
26
  * Get the size (number of elements) of the heap.
27
27
  */
28
28
  get size() {
29
- return this.nodes.length;
29
+ return this.elements.length;
30
30
  }
31
31
  /**
32
32
  * Get the last element in the heap, which is not necessarily a leaf node.
@@ -34,10 +34,10 @@ class Heap {
34
34
  */
35
35
  get leaf() {
36
36
  var _a;
37
- return (_a = this.nodes[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
37
+ return (_a = this.elements[this.size - 1]) !== null && _a !== void 0 ? _a : undefined;
38
38
  }
39
39
  /**
40
- * Static method that creates a binary heap from an array of nodes and a comparison function.
40
+ * Static method that creates a binary heap from an array of elements and a comparison function.
41
41
  * @returns A new Heap instance.
42
42
  * @param options
43
43
  */
@@ -45,11 +45,11 @@ class Heap {
45
45
  return new Heap(options);
46
46
  }
47
47
  /**
48
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
48
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
49
49
  * Space Complexity: O(1)
50
50
  */
51
51
  /**
52
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
52
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
53
53
  * Space Complexity: O(1)
54
54
  *
55
55
  * Insert an element into the heap and maintain the heap properties.
@@ -59,50 +59,49 @@ class Heap {
59
59
  return this.push(element);
60
60
  }
61
61
  /**
62
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
62
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
63
63
  * Space Complexity: O(1)
64
64
  */
65
65
  /**
66
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
66
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
67
67
  * Space Complexity: O(1)
68
68
  *
69
69
  * Insert an element into the heap and maintain the heap properties.
70
70
  * @param element - The element to be inserted.
71
71
  */
72
72
  push(element) {
73
- this.nodes.push(element);
74
- this.bubbleUp(this.nodes.length - 1);
73
+ this._elements.push(element);
74
+ this._bubbleUp(this.elements.length - 1);
75
75
  return this;
76
76
  }
77
77
  /**
78
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
78
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
79
79
  * Space Complexity: O(1)
80
80
  */
81
81
  /**
82
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
82
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
83
83
  * Space Complexity: O(1)
84
84
  *
85
85
  * Remove and return the top element (smallest or largest element) from the heap.
86
86
  * @returns The top element or undefined if the heap is empty.
87
87
  */
88
88
  poll() {
89
- if (this.nodes.length === 0) {
90
- return undefined;
91
- }
92
- if (this.nodes.length === 1) {
93
- return this.nodes.pop();
89
+ if (this.elements.length === 0)
90
+ return;
91
+ const value = this.elements[0];
92
+ const last = this.elements.pop();
93
+ if (this.elements.length) {
94
+ this.elements[0] = last;
95
+ this._sinkDown(0, this.elements.length >> 1);
94
96
  }
95
- const topValue = this.nodes[0];
96
- this.nodes[0] = this.nodes.pop();
97
- this.sinkDown(0);
98
- return topValue;
97
+ return value;
99
98
  }
100
99
  /**
101
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
100
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
102
101
  * Space Complexity: O(1)
103
102
  */
104
103
  /**
105
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
104
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
106
105
  * Space Complexity: O(1)
107
106
  *
108
107
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -116,10 +115,7 @@ class Heap {
116
115
  * @returns The top element or undefined if the heap is empty.
117
116
  */
118
117
  peek() {
119
- if (this.nodes.length === 0) {
120
- return undefined;
121
- }
122
- return this.nodes[0];
118
+ return this.elements[0];
123
119
  }
124
120
  /**
125
121
  * Check if the heap is empty.
@@ -129,32 +125,32 @@ class Heap {
129
125
  return this.size === 0;
130
126
  }
131
127
  /**
132
- * Reset the nodes of the heap. Make the nodes empty.
128
+ * Reset the elements of the heap. Make the elements empty.
133
129
  */
134
130
  clear() {
135
- this._nodes = [];
131
+ this._elements = [];
136
132
  }
137
133
  /**
138
- * Time Complexity: O(n), where n is the number of elements in the nodes array.
134
+ * Time Complexity: O(n), where n is the number of elements in the elements array.
139
135
  * Space Complexity: O(n)
140
136
  */
141
137
  /**
142
- * Time Complexity: O(n), where n is the number of elements in the nodes array.
138
+ * Time Complexity: O(n), where n is the number of elements in the elements array.
143
139
  * Space Complexity: O(n)
144
140
  *
145
- * Clear and add nodes of the heap
146
- * @param nodes
141
+ * Clear and add elements of the heap
142
+ * @param elements
147
143
  */
148
- refill(nodes) {
149
- this._nodes = nodes;
144
+ refill(elements) {
145
+ this._elements = elements;
150
146
  this.fix();
151
147
  }
152
148
  /**
153
- * Time Complexity: O(n), where n is the number of nodes in the heap.
149
+ * Time Complexity: O(n), where n is the number of elements in the heap.
154
150
  * Space Complexity: O(1)
155
151
  */
156
152
  /**
157
- * Time Complexity: O(n), where n is the number of nodes in the heap.
153
+ * Time Complexity: O(n), where n is the number of elements in the heap.
158
154
  * Space Complexity: O(1)
159
155
  *
160
156
  * Use a comparison function to check whether a binary heap contains a specific element.
@@ -162,14 +158,46 @@ class Heap {
162
158
  * @returns Returns true if the specified element is contained; otherwise, returns false.
163
159
  */
164
160
  has(element) {
165
- return this.nodes.includes(element);
161
+ return this.elements.includes(element);
166
162
  }
167
163
  /**
168
- * Time Complexity: O(n), where n is the number of nodes in the heap.
164
+ * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
165
+ * Space Complexity: O(1)
166
+ */
167
+ /**
168
+ * Time Complexity: O(n). The worst-case O(n), where n is the number of elements in the heap. This is because, in the worst case, the element to be deleted is located at the end of the heap (not the root), and after deletion, we may need to reorganize the elements by performing a sinkDown operation.
169
+ * Space Complexity: O(1)
170
+ *
171
+ * The `delete` function removes an element from an array-like data structure, maintaining the order
172
+ * and structure of the remaining elements.
173
+ * @param {E} element - The `element` parameter represents the element that you want to delete from
174
+ * the array `this.elements`.
175
+ * @returns The `delete` function is returning a boolean value. It returns `true` if the element was
176
+ * successfully deleted from the array, and `false` if the element was not found in the array.
177
+ */
178
+ delete(element) {
179
+ const index = this.elements.indexOf(element);
180
+ if (index < 0)
181
+ return false;
182
+ if (index === 0) {
183
+ this.pop();
184
+ }
185
+ else if (index === this.elements.length - 1) {
186
+ this.elements.pop();
187
+ }
188
+ else {
189
+ this.elements.splice(index, 1, this.elements.pop());
190
+ this._bubbleUp(index);
191
+ this._sinkDown(index, this.elements.length >> 1);
192
+ }
193
+ return true;
194
+ }
195
+ /**
196
+ * Time Complexity: O(n), where n is the number of elements in the heap.
169
197
  * Space Complexity: O(h), where h is the height of the heap.
170
198
  */
171
199
  /**
172
- * Time Complexity: O(n), where n is the number of nodes in the heap.
200
+ * Time Complexity: O(n), where n is the number of elements in the heap.
173
201
  * Space Complexity: O(h), where h is the height of the heap.
174
202
  *
175
203
  * Depth-first search (DFS) method, different traversal orders can be selected。
@@ -183,18 +211,18 @@ class Heap {
183
211
  if (index < this.size) {
184
212
  if (order === 'in') {
185
213
  dfsHelper(2 * index + 1);
186
- result.push(this.nodes[index]);
214
+ result.push(this.elements[index]);
187
215
  dfsHelper(2 * index + 2);
188
216
  }
189
217
  else if (order === 'pre') {
190
- result.push(this.nodes[index]);
218
+ result.push(this.elements[index]);
191
219
  dfsHelper(2 * index + 1);
192
220
  dfsHelper(2 * index + 2);
193
221
  }
194
222
  else if (order === 'post') {
195
223
  dfsHelper(2 * index + 1);
196
224
  dfsHelper(2 * index + 2);
197
- result.push(this.nodes[index]);
225
+ result.push(this.elements[index]);
198
226
  }
199
227
  }
200
228
  };
@@ -213,14 +241,7 @@ class Heap {
213
241
  * @returns An array containing the elements of the heap.
214
242
  */
215
243
  toArray() {
216
- return [...this.nodes];
217
- }
218
- /**
219
- * Time Complexity: O(1)
220
- * Space Complexity: O(1)
221
- */
222
- getNodes() {
223
- return this.nodes;
244
+ return [...this.elements];
224
245
  }
225
246
  /**
226
247
  * Time Complexity: O(n)
@@ -235,7 +256,7 @@ class Heap {
235
256
  */
236
257
  clone() {
237
258
  const clonedHeap = new Heap({ comparator: this.comparator });
238
- clonedHeap._nodes = [...this.nodes];
259
+ clonedHeap._elements = [...this.elements];
239
260
  return clonedHeap;
240
261
  }
241
262
  /**
@@ -264,35 +285,14 @@ class Heap {
264
285
  * Space Complexity: O(1)
265
286
  */
266
287
  /**
267
- * Time Complexity: O(log n)
288
+ * Time Complexity: O(n)
268
289
  * Space Complexity: O(1)
269
290
  *
270
- * Float operation to maintain heap properties after adding an element.
271
- * @param index - The index of the newly added element.
291
+ * Fix the entire heap to maintain heap properties.
272
292
  */
273
- bubbleUp(index) {
274
- // const element = this.nodes[index];
275
- // while (index > 0) {
276
- // const parentIndex = (index - 1) >> 1;
277
- // const parent = this.nodes[parentIndex];
278
- // if (this.comparator(element, parent) < 0) {
279
- // this.nodes[index] = parent;
280
- // this.nodes[parentIndex] = element;
281
- // index = parentIndex;
282
- // } else {
283
- // break;
284
- // }
285
- // }
286
- const item = this.nodes[index];
287
- while (index > 0) {
288
- const parent = (index - 1) >> 1;
289
- const parentItem = this.nodes[parent];
290
- if (this.comparator(parentItem, item) <= 0)
291
- break;
292
- this.nodes[index] = parentItem;
293
- index = parent;
294
- }
295
- this.nodes[index] = item;
293
+ fix() {
294
+ for (let i = Math.floor(this.size / 2); i >= 0; i--)
295
+ this._sinkDown(i, this.elements.length >> 1);
296
296
  }
297
297
  /**
298
298
  * Time Complexity: O(log n)
@@ -302,40 +302,50 @@ class Heap {
302
302
  * Time Complexity: O(log n)
303
303
  * Space Complexity: O(1)
304
304
  *
305
- * Sinking operation to maintain heap properties after removing the top element.
306
- * @param index - The index from which to start sinking.
305
+ * Float operation to maintain heap properties after adding an element.
306
+ * @param index - The index of the newly added element.
307
307
  */
308
- sinkDown(index) {
309
- const leftChildIndex = index << 1 | 1;
310
- const rightChildIndex = leftChildIndex + 1;
311
- const length = this.nodes.length;
312
- let targetIndex = index;
313
- if (leftChildIndex < length && this.comparator(this.nodes[leftChildIndex], this.nodes[targetIndex]) < 0) {
314
- targetIndex = leftChildIndex;
315
- }
316
- if (rightChildIndex < length && this.comparator(this.nodes[rightChildIndex], this.nodes[targetIndex]) < 0) {
317
- targetIndex = rightChildIndex;
318
- }
319
- if (targetIndex !== index) {
320
- const temp = this.nodes[index];
321
- this.nodes[index] = this.nodes[targetIndex];
322
- this.nodes[targetIndex] = temp;
323
- this.sinkDown(targetIndex);
308
+ _bubbleUp(index) {
309
+ const element = this.elements[index];
310
+ while (index > 0) {
311
+ const parent = (index - 1) >> 1;
312
+ const parentItem = this.elements[parent];
313
+ if (this._comparator(parentItem, element) <= 0)
314
+ break;
315
+ this.elements[index] = parentItem;
316
+ index = parent;
324
317
  }
318
+ this.elements[index] = element;
325
319
  }
326
320
  /**
327
321
  * Time Complexity: O(n)
328
322
  * Space Complexity: O(1)
329
323
  */
330
324
  /**
331
- * Time Complexity: O(n)
325
+ * Time Complexity: O(log n)
332
326
  * Space Complexity: O(1)
333
327
  *
334
- * Fix the entire heap to maintain heap properties.
335
- */
336
- fix() {
337
- for (let i = Math.floor(this.size / 2); i >= 0; i--)
338
- this.sinkDown(i);
328
+ * Sinking operation to maintain heap properties after removing the top element.
329
+ * @param index - The index from which to start sinking.
330
+ * @param halfLength
331
+ */
332
+ _sinkDown(index, halfLength) {
333
+ const element = this.elements[index];
334
+ while (index < halfLength) {
335
+ let left = index << 1 | 1;
336
+ const right = left + 1;
337
+ let minItem = this.elements[left];
338
+ if (right < this.elements.length &&
339
+ this._comparator(minItem, this.elements[right]) > 0) {
340
+ left = right;
341
+ minItem = this.elements[right];
342
+ }
343
+ if (this._comparator(minItem, element) >= 0)
344
+ break;
345
+ this.elements[index] = minItem;
346
+ index = left;
347
+ }
348
+ this.elements[index] = element;
339
349
  }
340
350
  }
341
351
  exports.Heap = Heap;
@@ -431,22 +441,22 @@ class FibonacciHeap {
431
441
  return this.min ? this.min.element : undefined;
432
442
  }
433
443
  /**
434
- * Time Complexity: O(n), where n is the number of nodes in the linked list.
444
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
435
445
  * Space Complexity: O(1)
436
446
  */
437
447
  /**
438
- * Time Complexity: O(n), where n is the number of nodes in the linked list.
448
+ * Time Complexity: O(n), where n is the number of elements in the linked list.
439
449
  * Space Complexity: O(1)
440
450
  *
441
451
  * Get the size (number of elements) of the heap.
442
452
  * @param {FibonacciHeapNode<E>} head - The head of the linked list.
443
453
  * @protected
444
- * @returns FibonacciHeapNode<E>[] - An array containing the nodes of the linked list.
454
+ * @returns FibonacciHeapNode<E>[] - An array containing the elements of the linked list.
445
455
  */
446
456
  consumeLinkedList(head) {
447
- const nodes = [];
457
+ const elements = [];
448
458
  if (!head)
449
- return nodes;
459
+ return elements;
450
460
  let node = head;
451
461
  let flag = false;
452
462
  while (true) {
@@ -455,11 +465,11 @@ class FibonacciHeap {
455
465
  else if (node === head)
456
466
  flag = true;
457
467
  if (node) {
458
- nodes.push(node);
468
+ elements.push(node);
459
469
  node = node.right;
460
470
  }
461
471
  }
462
- return nodes;
472
+ return elements;
463
473
  }
464
474
  /**
465
475
  * Time Complexity: O(1)
@@ -480,11 +490,11 @@ class FibonacciHeap {
480
490
  }
481
491
  }
482
492
  /**
483
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
493
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
484
494
  * Space Complexity: O(1)
485
495
  */
486
496
  /**
487
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
497
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
488
498
  * Space Complexity: O(1)
489
499
  *
490
500
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -494,11 +504,11 @@ class FibonacciHeap {
494
504
  return this.pop();
495
505
  }
496
506
  /**
497
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
507
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
498
508
  * Space Complexity: O(1)
499
509
  */
500
510
  /**
501
- * Time Complexity: O(log n), where n is the number of nodes in the heap.
511
+ * Time Complexity: O(log n), where n is the number of elements in the heap.
502
512
  * Space Complexity: O(1)
503
513
  *
504
514
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -509,8 +519,8 @@ class FibonacciHeap {
509
519
  return undefined;
510
520
  const z = this.min;
511
521
  if (z.child) {
512
- const nodes = this.consumeLinkedList(z.child);
513
- for (const node of nodes) {
522
+ const elements = this.consumeLinkedList(z.child);
523
+ for (const node of elements) {
514
524
  this.mergeWithRoot(node);
515
525
  node.parent = undefined;
516
526
  }
@@ -647,11 +657,11 @@ class FibonacciHeap {
647
657
  y.parent = x;
648
658
  }
649
659
  /**
650
- * Time Complexity: O(n log n), where n is the number of nodes in the heap.
660
+ * Time Complexity: O(n log n), where n is the number of elements in the heap.
651
661
  * Space Complexity: O(n)
652
662
  */
653
663
  /**
654
- * Time Complexity: O(n log n), where n is the number of nodes in the heap.
664
+ * Time Complexity: O(n log n), where n is the number of elements in the heap.
655
665
  * Space Complexity: O(n)
656
666
  *
657
667
  * Remove and return the top element (smallest or largest element) from the heap.
@@ -659,9 +669,9 @@ class FibonacciHeap {
659
669
  */
660
670
  consolidate() {
661
671
  const A = new Array(this.size);
662
- const nodes = this.consumeLinkedList(this.root);
672
+ const elements = this.consumeLinkedList(this.root);
663
673
  let x, y, d, t;
664
- for (const node of nodes) {
674
+ for (const node of elements) {
665
675
  x = node;
666
676
  d = x.degree;
667
677
  while (A[d]) {