data-structure-typed 2.0.4 → 2.1.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/CHANGELOG.md +3 -1
- package/COMMANDS.md +17 -0
- package/README.md +11 -11
- package/benchmark/report.html +13 -77
- package/benchmark/report.json +158 -190
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/cjs/data-structures/base/iterable-element-base.js +149 -107
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/cjs/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/base/linear-base.d.ts +250 -192
- package/dist/cjs/data-structures/base/linear-base.js +137 -274
- package/dist/cjs/data-structures/base/linear-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +171 -205
- package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +208 -195
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +608 -875
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/cjs/data-structures/binary-tree/bst.js +505 -481
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +114 -209
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/cjs/data-structures/binary-tree/tree-counter.js +172 -203
- package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -85
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/cjs/data-structures/graph/abstract-graph.js +267 -237
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/cjs/data-structures/graph/directed-graph.js +146 -233
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/cjs/data-structures/graph/map-graph.js +56 -59
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/cjs/data-structures/graph/undirected-graph.js +129 -149
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/cjs/data-structures/hash/hash-map.js +270 -457
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +214 -289
- package/dist/cjs/data-structures/heap/heap.js +340 -349
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/cjs/data-structures/heap/max-heap.js +11 -66
- package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
- package/dist/cjs/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/cjs/data-structures/heap/min-heap.js +11 -66
- package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +368 -494
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +447 -466
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +227 -254
- package/dist/cjs/data-structures/queue/deque.js +309 -348
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +180 -201
- package/dist/cjs/data-structures/queue/queue.js +265 -248
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +124 -102
- package/dist/cjs/data-structures/stack/stack.js +181 -125
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +164 -165
- package/dist/cjs/data-structures/trie/trie.js +189 -172
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +56 -6
- package/dist/cjs/interfaces/graph.d.ts +16 -0
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/cjs/types/utils/utils.d.ts +6 -6
- package/dist/cjs/utils/utils.d.ts +110 -49
- package/dist/cjs/utils/utils.js +148 -73
- package/dist/cjs/utils/utils.js.map +1 -1
- package/dist/esm/data-structures/base/iterable-element-base.d.ts +186 -83
- package/dist/esm/data-structures/base/iterable-element-base.js +155 -107
- package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/esm/data-structures/base/iterable-entry-base.d.ts +95 -119
- package/dist/esm/data-structures/base/iterable-entry-base.js +59 -116
- package/dist/esm/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/esm/data-structures/base/linear-base.d.ts +250 -192
- package/dist/esm/data-structures/base/linear-base.js +137 -274
- package/dist/esm/data-structures/base/linear-base.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +171 -212
- package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +133 -94
- package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +138 -149
- package/dist/esm/data-structures/binary-tree/avl-tree.js +206 -200
- package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +476 -632
- package/dist/esm/data-structures/binary-tree/binary-tree.js +613 -885
- package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/bst.d.ts +258 -306
- package/dist/esm/data-structures/binary-tree/bst.js +507 -487
- package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +107 -179
- package/dist/esm/data-structures/binary-tree/red-black-tree.js +114 -215
- package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +132 -154
- package/dist/esm/data-structures/binary-tree/tree-counter.js +175 -209
- package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
- package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js +103 -92
- package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/esm/data-structures/graph/abstract-graph.d.ts +238 -233
- package/dist/esm/data-structures/graph/abstract-graph.js +267 -237
- package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/directed-graph.d.ts +108 -224
- package/dist/esm/data-structures/graph/directed-graph.js +145 -233
- package/dist/esm/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/map-graph.d.ts +49 -55
- package/dist/esm/data-structures/graph/map-graph.js +56 -59
- package/dist/esm/data-structures/graph/map-graph.js.map +1 -1
- package/dist/esm/data-structures/graph/undirected-graph.d.ts +103 -146
- package/dist/esm/data-structures/graph/undirected-graph.js +128 -149
- package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/esm/data-structures/hash/hash-map.d.ts +164 -338
- package/dist/esm/data-structures/hash/hash-map.js +270 -457
- package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
- package/dist/esm/data-structures/heap/heap.d.ts +214 -289
- package/dist/esm/data-structures/heap/heap.js +329 -349
- package/dist/esm/data-structures/heap/heap.js.map +1 -1
- package/dist/esm/data-structures/heap/max-heap.d.ts +11 -47
- package/dist/esm/data-structures/heap/max-heap.js +11 -66
- package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
- package/dist/esm/data-structures/heap/min-heap.d.ts +12 -47
- package/dist/esm/data-structures/heap/min-heap.js +11 -66
- package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
- package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js +368 -495
- package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +261 -310
- package/dist/esm/data-structures/linked-list/singly-linked-list.js +448 -467
- package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -107
- package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -100
- package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js +11 -78
- package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js +10 -79
- package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -61
- package/dist/esm/data-structures/priority-queue/priority-queue.js +8 -83
- package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/esm/data-structures/queue/deque.d.ts +227 -254
- package/dist/esm/data-structures/queue/deque.js +313 -348
- package/dist/esm/data-structures/queue/deque.js.map +1 -1
- package/dist/esm/data-structures/queue/queue.d.ts +180 -201
- package/dist/esm/data-structures/queue/queue.js +263 -248
- package/dist/esm/data-structures/queue/queue.js.map +1 -1
- package/dist/esm/data-structures/stack/stack.d.ts +124 -102
- package/dist/esm/data-structures/stack/stack.js +181 -125
- package/dist/esm/data-structures/stack/stack.js.map +1 -1
- package/dist/esm/data-structures/trie/trie.d.ts +164 -165
- package/dist/esm/data-structures/trie/trie.js +193 -172
- package/dist/esm/data-structures/trie/trie.js.map +1 -1
- package/dist/esm/interfaces/binary-tree.d.ts +56 -6
- package/dist/esm/interfaces/graph.d.ts +16 -0
- package/dist/esm/types/data-structures/base/base.d.ts +1 -1
- package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +4 -0
- package/dist/esm/types/utils/utils.d.ts +6 -6
- package/dist/esm/utils/utils.d.ts +110 -49
- package/dist/esm/utils/utils.js +139 -68
- package/dist/esm/utils/utils.js.map +1 -1
- package/dist/umd/data-structure-typed.js +4737 -6525
- package/dist/umd/data-structure-typed.min.js +8 -6
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +3 -4
- package/src/data-structures/base/iterable-element-base.ts +238 -115
- package/src/data-structures/base/iterable-entry-base.ts +96 -120
- package/src/data-structures/base/linear-base.ts +271 -277
- package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
- package/src/data-structures/binary-tree/avl-tree.ts +239 -206
- package/src/data-structures/binary-tree/binary-tree.ts +677 -901
- package/src/data-structures/binary-tree/bst.ts +568 -570
- package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
- package/src/data-structures/binary-tree/tree-counter.ts +199 -218
- package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
- package/src/data-structures/graph/abstract-graph.ts +339 -264
- package/src/data-structures/graph/directed-graph.ts +146 -236
- package/src/data-structures/graph/map-graph.ts +63 -60
- package/src/data-structures/graph/undirected-graph.ts +129 -152
- package/src/data-structures/hash/hash-map.ts +274 -496
- package/src/data-structures/heap/heap.ts +389 -402
- package/src/data-structures/heap/max-heap.ts +12 -76
- package/src/data-structures/heap/min-heap.ts +13 -76
- package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
- package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
- package/src/data-structures/priority-queue/priority-queue.ts +3 -92
- package/src/data-structures/queue/deque.ts +381 -357
- package/src/data-structures/queue/queue.ts +310 -264
- package/src/data-structures/stack/stack.ts +217 -131
- package/src/data-structures/trie/trie.ts +240 -175
- package/src/interfaces/binary-tree.ts +240 -6
- package/src/interfaces/graph.ts +37 -0
- package/src/types/data-structures/base/base.ts +5 -5
- package/src/types/data-structures/graph/abstract-graph.ts +5 -0
- package/src/types/utils/utils.ts +9 -5
- package/src/utils/utils.ts +152 -86
- package/test/integration/index.html +1 -1
- package/test/performance/benchmark-runner.ts +528 -0
- package/test/performance/reportor.mjs +43 -43
- package/test/performance/runner-config.json +39 -0
- package/test/performance/single-suite-runner.ts +69 -0
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
- package/test/unit/data-structures/binary-tree/bst.test.ts +84 -5
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -3
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
- package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/heap/heap.test.ts +14 -21
- package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
- package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
- package/test/unit/data-structures/queue/queue.test.ts +4 -5
- package/test/unit/utils/utils.test.ts +0 -1
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
1
8
|
import { uuidV4 } from '../../utils';
|
|
2
9
|
import { IterableEntryBase } from '../base';
|
|
3
10
|
import { Heap } from '../heap';
|
|
@@ -5,13 +12,6 @@ import { Queue } from '../queue';
|
|
|
5
12
|
export class AbstractVertex {
|
|
6
13
|
key;
|
|
7
14
|
value;
|
|
8
|
-
/**
|
|
9
|
-
* The function is a protected constructor that takes an key and an optional value as parameters.
|
|
10
|
-
* @param {VertexKey} key - The `key` parameter is of type `VertexKey` and represents the identifier of the vertex. It is
|
|
11
|
-
* used to uniquely identify the vertex object.
|
|
12
|
-
* @param {V} [value] - The parameter "value" is an optional parameter of type V. It is used to assign a value to the
|
|
13
|
-
* vertex. If no value is provided, it will be set to undefined.
|
|
14
|
-
*/
|
|
15
15
|
constructor(key, value) {
|
|
16
16
|
this.key = key;
|
|
17
17
|
this.value = value;
|
|
@@ -20,15 +20,6 @@ export class AbstractVertex {
|
|
|
20
20
|
export class AbstractEdge {
|
|
21
21
|
value;
|
|
22
22
|
weight;
|
|
23
|
-
/**
|
|
24
|
-
* The above function is a protected constructor that initializes the weight, value, and hash code properties of an
|
|
25
|
-
* object.
|
|
26
|
-
* @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the object. If
|
|
27
|
-
* a value is provided, it will be assigned to the `_weight` property. If no value is provided, the default value of 1
|
|
28
|
-
* will be assigned.
|
|
29
|
-
* @param {VO} [value] - The `value` parameter is of type `VO`, which means it can be any type. It is an optional parameter,
|
|
30
|
-
* meaning it can be omitted when creating an instance of the class.
|
|
31
|
-
*/
|
|
32
23
|
constructor(weight, value) {
|
|
33
24
|
this.weight = weight !== undefined ? weight : 1;
|
|
34
25
|
this.value = value;
|
|
@@ -39,9 +30,29 @@ export class AbstractEdge {
|
|
|
39
30
|
return this._hashCode;
|
|
40
31
|
}
|
|
41
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Abstract graph over vertices and edges.
|
|
35
|
+
* @template V - Vertex value type.
|
|
36
|
+
* @template E - Edge value type.
|
|
37
|
+
* @template VO - Concrete vertex subclass (extends AbstractVertex<V>).
|
|
38
|
+
* @template EO - Concrete edge subclass (extends AbstractEdge<E>).
|
|
39
|
+
* @remarks Time O(1), Space O(1)
|
|
40
|
+
* @example examples will be generated by unit test
|
|
41
|
+
*/
|
|
42
42
|
export class AbstractGraph extends IterableEntryBase {
|
|
43
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Construct a graph with runtime defaults.
|
|
45
|
+
* @param options - `GraphOptions<V>` in `options.graph` (e.g. `vertexValueInitializer`, `defaultEdgeWeight`).
|
|
46
|
+
* @remarks Time O(1), Space O(1)
|
|
47
|
+
*/
|
|
48
|
+
constructor(options) {
|
|
44
49
|
super();
|
|
50
|
+
const graph = options?.graph;
|
|
51
|
+
this._options = { defaultEdgeWeight: 1, ...(graph ?? {}) };
|
|
52
|
+
}
|
|
53
|
+
_options = { defaultEdgeWeight: 1 };
|
|
54
|
+
get options() {
|
|
55
|
+
return this._options;
|
|
45
56
|
}
|
|
46
57
|
_vertexMap = new Map();
|
|
47
58
|
get vertexMap() {
|
|
@@ -54,33 +65,29 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
54
65
|
return this._vertexMap.size;
|
|
55
66
|
}
|
|
56
67
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* @param {VertexKey} vertexKey - The `vertexKey` parameter is the identifier of the vertex that you want to retrieve from
|
|
62
|
-
* the `_vertexMap` map.
|
|
63
|
-
* @returns The method `getVertex` returns the vertex with the specified `vertexKey` if it exists in the `_vertexMap`
|
|
64
|
-
* map. If the vertex does not exist, it returns `undefined`.
|
|
68
|
+
* Get vertex instance by key.
|
|
69
|
+
* @param vertexKey - Vertex key.
|
|
70
|
+
* @returns Vertex instance or `undefined`.
|
|
71
|
+
* @remarks Time O(1), Space O(1)
|
|
65
72
|
*/
|
|
66
73
|
getVertex(vertexKey) {
|
|
67
74
|
return this._vertexMap.get(vertexKey) || undefined;
|
|
68
75
|
}
|
|
69
76
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @param {VO | VertexKey} vertexOrKey - The parameter `vertexOrKey` can be either a vertex object (`VO`) or a vertex ID
|
|
75
|
-
* (`VertexKey`).
|
|
76
|
-
* @returns a boolean value.
|
|
77
|
+
* Whether a vertex exists.
|
|
78
|
+
* @param vertexOrKey - Vertex or key.
|
|
79
|
+
* @returns `true` if present, otherwise `false`.
|
|
80
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
77
81
|
*/
|
|
78
82
|
hasVertex(vertexOrKey) {
|
|
79
83
|
return this._vertexMap.has(this._getVertexKey(vertexOrKey));
|
|
80
84
|
}
|
|
81
85
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
86
|
+
* Add a vertex by key/value or by pre-built vertex.
|
|
87
|
+
* @param keyOrVertex - Vertex key or existing vertex instance.
|
|
88
|
+
* @param value - Optional payload.
|
|
89
|
+
* @returns `true` if inserted; `false` when key already exists.
|
|
90
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
84
91
|
*/
|
|
85
92
|
addVertex(keyOrVertex, value) {
|
|
86
93
|
if (keyOrVertex instanceof AbstractVertex) {
|
|
@@ -91,19 +98,21 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
91
98
|
return this._addVertex(newVertex);
|
|
92
99
|
}
|
|
93
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Type guard: check if a value is a valid vertex key.
|
|
103
|
+
* @param potentialKey - Value to test.
|
|
104
|
+
* @returns `true` if string/number; else `false`.
|
|
105
|
+
* @remarks Time O(1), Space O(1)
|
|
106
|
+
*/
|
|
94
107
|
isVertexKey(potentialKey) {
|
|
95
108
|
const potentialKeyType = typeof potentialKey;
|
|
96
109
|
return potentialKeyType === 'string' || potentialKeyType === 'number';
|
|
97
110
|
}
|
|
98
111
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* @param {VO[] | VertexKey[]} vertexMap - The `vertexMap` parameter can be either an array of vertexMap (`VO[]`) or an array
|
|
104
|
-
* of vertex IDs (`VertexKey[]`).
|
|
105
|
-
* @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertexMap
|
|
106
|
-
* were removed.
|
|
112
|
+
* Delete multiple vertices.
|
|
113
|
+
* @param vertexMap - Array of vertices or keys.
|
|
114
|
+
* @returns `true` if any vertex was removed.
|
|
115
|
+
* @remarks Time O(sum(deg)), Space O(1)
|
|
107
116
|
*/
|
|
108
117
|
removeManyVertices(vertexMap) {
|
|
109
118
|
const removed = [];
|
|
@@ -113,23 +122,24 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
113
122
|
return removed.length > 0;
|
|
114
123
|
}
|
|
115
124
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* @
|
|
121
|
-
* identifier of a vertex in a graph, while VO represents the type of the vertex object itself.
|
|
122
|
-
* @param {VertexKey | VO} v2 - The parameter `v2` represents the second vertex in the edge. It can be either a
|
|
123
|
-
* `VertexKey` or a `VO` type, which represents the type of the vertex.
|
|
124
|
-
* @returns A boolean value is being returned.
|
|
125
|
+
* Whether an edge exists between two vertices.
|
|
126
|
+
* @param v1 - Endpoint A vertex or key.
|
|
127
|
+
* @param v2 - Endpoint B vertex or key.
|
|
128
|
+
* @returns `true` if present; otherwise `false`.
|
|
129
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
125
130
|
*/
|
|
126
131
|
hasEdge(v1, v2) {
|
|
127
132
|
const edge = this.getEdge(v1, v2);
|
|
128
133
|
return !!edge;
|
|
129
134
|
}
|
|
130
135
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
136
|
+
* Add an edge by instance or by `(src, dest, weight?, value?)`.
|
|
137
|
+
* @param srcOrEdge - Edge instance or source vertex/key.
|
|
138
|
+
* @param dest - Destination vertex/key (when adding by pair).
|
|
139
|
+
* @param weight - Edge weight.
|
|
140
|
+
* @param value - Edge payload.
|
|
141
|
+
* @returns `true` if inserted; otherwise `false`.
|
|
142
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
133
143
|
*/
|
|
134
144
|
addEdge(srcOrEdge, dest, weight, value) {
|
|
135
145
|
if (srcOrEdge instanceof AbstractEdge) {
|
|
@@ -152,18 +162,12 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
152
162
|
}
|
|
153
163
|
}
|
|
154
164
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* @
|
|
160
|
-
*
|
|
161
|
-
* @param {VertexKey | VO} destOrKey - The `destOrKey` parameter represents the destination vertex of the edge. It can be
|
|
162
|
-
* either a `VertexKey` or a vertex object `VO`.
|
|
163
|
-
* @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrKey)
|
|
164
|
-
* and the destination vertex (destOrKey).
|
|
165
|
-
* @returns a boolean value. If the edge exists between the source and destination vertexMap, the function will update
|
|
166
|
-
* the weight of the edge and return true. If the edge does not exist, the function will return false.
|
|
165
|
+
* Set the weight of an existing edge.
|
|
166
|
+
* @param srcOrKey - Source vertex or key.
|
|
167
|
+
* @param destOrKey - Destination vertex or key.
|
|
168
|
+
* @param weight - New weight.
|
|
169
|
+
* @returns `true` if updated; otherwise `false`.
|
|
170
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
167
171
|
*/
|
|
168
172
|
setEdgeWeight(srcOrKey, destOrKey, weight) {
|
|
169
173
|
const edge = this.getEdge(srcOrKey, destOrKey);
|
|
@@ -176,15 +180,12 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
176
180
|
}
|
|
177
181
|
}
|
|
178
182
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
* @
|
|
184
|
-
*
|
|
185
|
-
* @param {VO | VertexKey} v2 - The parameter `v2` represents either a vertex object (`VO`) or a vertex ID (`VertexKey`).
|
|
186
|
-
* @param limit - The count of limitation of result array.
|
|
187
|
-
* @returns The function `getAllPathsBetween` returns an array of arrays of vertexMap (`VO[][]`).
|
|
183
|
+
* Enumerate simple paths up to a limit.
|
|
184
|
+
* @param v1 - Source vertex or key.
|
|
185
|
+
* @param v2 - Destination vertex or key.
|
|
186
|
+
* @param limit - Maximum number of paths to collect.
|
|
187
|
+
* @returns Array of paths (each path is an array of vertices).
|
|
188
|
+
* @remarks Time O(paths) worst-case exponential, Space O(V + paths)
|
|
188
189
|
*/
|
|
189
190
|
getAllPathsBetween(v1, v2, limit = 1000) {
|
|
190
191
|
const paths = [];
|
|
@@ -213,12 +214,10 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
213
214
|
return paths;
|
|
214
215
|
}
|
|
215
216
|
/**
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* @param {VO[]} path - An array of vertexMap (VO) representing a path in a graph.
|
|
221
|
-
* @returns The function `getPathSumWeight` returns the sum of the weights of the edgeMap in the given path.
|
|
217
|
+
* Sum the weights along a vertex path.
|
|
218
|
+
* @param path - Sequence of vertices.
|
|
219
|
+
* @returns Path weight sum (0 if empty or edge missing).
|
|
220
|
+
* @remarks Time O(L), Space O(1) where L is path length
|
|
222
221
|
*/
|
|
223
222
|
getPathSumWeight(path) {
|
|
224
223
|
let sum = 0;
|
|
@@ -228,21 +227,12 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
228
227
|
return sum;
|
|
229
228
|
}
|
|
230
229
|
/**
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* @
|
|
237
|
-
* @param {VO | VertexKey} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex to which
|
|
238
|
-
* you want to find the minimum cost or weight from the source vertex `v1`.
|
|
239
|
-
* @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edgeMap have weights.
|
|
240
|
-
* If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
|
|
241
|
-
* the edgeMap. If isWeight is set to false or not provided, the function will calculate the
|
|
242
|
-
* @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertexMap (`v1`
|
|
243
|
-
* and `v2`). If the `isWeight` parameter is `true`, it calculates the minimum weight among all paths between the
|
|
244
|
-
* vertexMap. If `isWeight` is `false` or not provided, it uses a breadth-first search (BFS) algorithm to calculate the
|
|
245
|
-
* minimum number of
|
|
230
|
+
* Minimum hops/weight between two vertices.
|
|
231
|
+
* @param v1 - Source vertex or key.
|
|
232
|
+
* @param v2 - Destination vertex or key.
|
|
233
|
+
* @param isWeight - If `true`, compare by path weight; otherwise by hop count.
|
|
234
|
+
* @returns Minimum cost or `undefined` if missing/unreachable.
|
|
235
|
+
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
246
236
|
*/
|
|
247
237
|
getMinCostBetween(v1, v2, isWeight) {
|
|
248
238
|
if (isWeight === undefined)
|
|
@@ -256,7 +246,6 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
256
246
|
return min;
|
|
257
247
|
}
|
|
258
248
|
else {
|
|
259
|
-
// BFS
|
|
260
249
|
const vertex2 = this._getVertex(v2);
|
|
261
250
|
const vertex1 = this._getVertex(v1);
|
|
262
251
|
if (!(vertex1 && vertex2)) {
|
|
@@ -267,12 +256,11 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
267
256
|
visited.set(vertex1, true);
|
|
268
257
|
let cost = 0;
|
|
269
258
|
while (queue.length > 0) {
|
|
270
|
-
for (let i = 0
|
|
259
|
+
for (let i = 0, layerSize = queue.length; i < layerSize; i++) {
|
|
271
260
|
const cur = queue.shift();
|
|
272
261
|
if (cur === vertex2) {
|
|
273
262
|
return cost;
|
|
274
263
|
}
|
|
275
|
-
// TODO consider optimizing to AbstractGraph
|
|
276
264
|
if (cur !== undefined) {
|
|
277
265
|
const neighbors = this.getNeighbors(cur);
|
|
278
266
|
for (const neighbor of neighbors) {
|
|
@@ -289,23 +277,13 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
289
277
|
}
|
|
290
278
|
}
|
|
291
279
|
/**
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
* @
|
|
298
|
-
*
|
|
299
|
-
* @param {VO | VertexKey} v2 - VO | VertexKey - The second vertex or vertex ID between which we want to find the minimum
|
|
300
|
-
* path.
|
|
301
|
-
* @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edgeMap in finding the
|
|
302
|
-
* minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
|
|
303
|
-
* to false, the function will use breadth-first search (BFS) to find the minimum path.
|
|
304
|
-
* @param isDFS - If set to true, it enforces the use of getAllPathsBetween to first obtain all possible paths,
|
|
305
|
-
* followed by iterative computation of the shortest path. This approach may result in exponential time complexity,
|
|
306
|
-
* so the default method is to use the Dijkstra algorithm to obtain the shortest weighted path.
|
|
307
|
-
* @returns The function `getMinPathBetween` returns an array of vertexMap (`VO[]`) representing the minimum path between
|
|
308
|
-
* two vertexMap (`v1` and `v2`). If there is no path between the vertexMap, it returns `undefined`.
|
|
280
|
+
* Minimum path (as vertex sequence) between two vertices.
|
|
281
|
+
* @param v1 - Source vertex or key.
|
|
282
|
+
* @param v2 - Destination vertex or key.
|
|
283
|
+
* @param isWeight - If `true`, compare by path weight; otherwise by hop count.
|
|
284
|
+
* @param isDFS - For weighted mode only: if `true`, brute-force all paths; if `false`, use Dijkstra.
|
|
285
|
+
* @returns Vertex sequence, or `undefined`/empty when unreachable depending on branch.
|
|
286
|
+
* @remarks Time O((V + E) log V) weighted / O(V + E) unweighted, Space O(V + E)
|
|
309
287
|
*/
|
|
310
288
|
getMinPathBetween(v1, v2, isWeight, isDFS = false) {
|
|
311
289
|
if (isWeight === undefined)
|
|
@@ -327,11 +305,19 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
327
305
|
return allPaths[minIndex] || undefined;
|
|
328
306
|
}
|
|
329
307
|
else {
|
|
308
|
+
/**
|
|
309
|
+
* Dijkstra (binary-heap) shortest paths for non-negative weights.
|
|
310
|
+
* @param src - Source vertex or key.
|
|
311
|
+
* @param dest - Optional destination for early stop.
|
|
312
|
+
* @param getMinDist - If `true`, compute global minimum distance.
|
|
313
|
+
* @param genPaths - If `true`, also generate path arrays.
|
|
314
|
+
* @returns Result bag or `undefined` if source missing.
|
|
315
|
+
* @remarks Time O((V + E) log V), Space O(V + E)
|
|
316
|
+
*/
|
|
330
317
|
return this.dijkstra(v1, v2, true, true)?.minPath ?? [];
|
|
331
318
|
}
|
|
332
319
|
}
|
|
333
320
|
else {
|
|
334
|
-
// DFS
|
|
335
321
|
let minPath = [];
|
|
336
322
|
const vertex1 = this._getVertex(v1);
|
|
337
323
|
const vertex2 = this._getVertex(v2);
|
|
@@ -358,23 +344,13 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
358
344
|
}
|
|
359
345
|
}
|
|
360
346
|
/**
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
*
|
|
366
|
-
* @
|
|
367
|
-
*
|
|
368
|
-
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
|
|
369
|
-
* parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
|
|
370
|
-
* identifier. If no destination is provided, the value is set to `undefined`.
|
|
371
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
372
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
373
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
374
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
375
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
376
|
-
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
377
|
-
* @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<VO>`.
|
|
347
|
+
* Dijkstra without heap (array-based selection).
|
|
348
|
+
* @param src - Source vertex or key.
|
|
349
|
+
* @param dest - Optional destination for early stop.
|
|
350
|
+
* @param getMinDist - If `true`, compute global minimum distance.
|
|
351
|
+
* @param genPaths - If `true`, also generate path arrays.
|
|
352
|
+
* @returns Result bag or `undefined` if source missing.
|
|
353
|
+
* @remarks Time O(V^2 + E), Space O(V + E)
|
|
378
354
|
*/
|
|
379
355
|
dijkstraWithoutHeap(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
380
356
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
@@ -384,7 +360,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
384
360
|
const vertexMap = this._vertexMap;
|
|
385
361
|
const distMap = new Map();
|
|
386
362
|
const seen = new Set();
|
|
387
|
-
const preMap = new Map();
|
|
363
|
+
const preMap = new Map();
|
|
388
364
|
const srcVertex = this._getVertex(src);
|
|
389
365
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
390
366
|
if (!srcVertex) {
|
|
@@ -447,7 +423,6 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
447
423
|
if (edge) {
|
|
448
424
|
const curFromMap = distMap.get(cur);
|
|
449
425
|
const neighborFromMap = distMap.get(neighbor);
|
|
450
|
-
// TODO after no-non-undefined-assertion not ensure the logic
|
|
451
426
|
if (curFromMap !== undefined && neighborFromMap !== undefined) {
|
|
452
427
|
if (edge.weight + curFromMap < neighborFromMap) {
|
|
453
428
|
distMap.set(neighbor, edge.weight + curFromMap);
|
|
@@ -473,26 +448,6 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
473
448
|
getPaths(minDest);
|
|
474
449
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
475
450
|
}
|
|
476
|
-
/**
|
|
477
|
-
* Time Complexity: O((V + E) * log(V)) - Depends on the implementation (using a binary heap).
|
|
478
|
-
* Space Complexity: O(V + E) - Depends on the implementation (using a binary heap).
|
|
479
|
-
*
|
|
480
|
-
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
481
|
-
* The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
|
|
482
|
-
* optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
|
|
483
|
-
* @param {VO | VertexKey} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
|
|
484
|
-
* start. It can be either a vertex object or a vertex ID.
|
|
485
|
-
* @param {VO | VertexKey | undefined} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
|
|
486
|
-
* vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
|
|
487
|
-
* will calculate the shortest paths to all other vertexMap from the source vertex.
|
|
488
|
-
* @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
|
|
489
|
-
* distance from the source vertex to the destination vertex should be calculated and returned in the result. If
|
|
490
|
-
* `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
|
|
491
|
-
* @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
|
|
492
|
-
* paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
|
|
493
|
-
* shortest paths from the source vertex to all other vertexMap in the graph. If `genPaths
|
|
494
|
-
* @returns The function `dijkstra` returns an object of type `DijkstraResult<VO>`.
|
|
495
|
-
*/
|
|
496
451
|
dijkstra(src, dest = undefined, getMinDist = false, genPaths = false) {
|
|
497
452
|
let minDist = Number.MAX_SAFE_INTEGER;
|
|
498
453
|
let minDest = undefined;
|
|
@@ -501,7 +456,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
501
456
|
const vertexMap = this._vertexMap;
|
|
502
457
|
const distMap = new Map();
|
|
503
458
|
const seen = new Set();
|
|
504
|
-
const preMap = new Map();
|
|
459
|
+
const preMap = new Map();
|
|
505
460
|
const srcVertex = this._getVertex(src);
|
|
506
461
|
const destVertex = dest ? this._getVertex(dest) : undefined;
|
|
507
462
|
if (!srcVertex)
|
|
@@ -515,11 +470,6 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
515
470
|
heap.add({ key: 0, value: srcVertex });
|
|
516
471
|
distMap.set(srcVertex, 0);
|
|
517
472
|
preMap.set(srcVertex, undefined);
|
|
518
|
-
/**
|
|
519
|
-
* The function `getPaths` retrieves all paths from vertexMap to a specified minimum vertex.
|
|
520
|
-
* @param {VO | undefined} minV - The parameter `minV` is of type `VO | undefined`. It represents the minimum vertex value or
|
|
521
|
-
* undefined.
|
|
522
|
-
*/
|
|
523
473
|
const getPaths = (minV) => {
|
|
524
474
|
for (const vertex of vertexMap) {
|
|
525
475
|
const vertexOrKey = vertex[1];
|
|
@@ -559,7 +509,7 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
559
509
|
const weight = this.getEdge(cur, neighbor)?.weight;
|
|
560
510
|
if (typeof weight === 'number') {
|
|
561
511
|
const distSrcToNeighbor = distMap.get(neighbor);
|
|
562
|
-
if (distSrcToNeighbor) {
|
|
512
|
+
if (distSrcToNeighbor !== undefined) {
|
|
563
513
|
if (dist + weight < distSrcToNeighbor) {
|
|
564
514
|
heap.add({ key: dist + weight, value: neighbor });
|
|
565
515
|
preMap.set(neighbor, cur);
|
|
@@ -589,22 +539,13 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
589
539
|
return { distMap, preMap, seen, paths, minDist, minPath };
|
|
590
540
|
}
|
|
591
541
|
/**
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
*
|
|
597
|
-
*
|
|
598
|
-
*
|
|
599
|
-
* @param {VO | VertexKey} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
|
|
600
|
-
* start calculating the shortest paths. It can be either a vertex object or a vertex ID.
|
|
601
|
-
* @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
|
|
602
|
-
* @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
|
|
603
|
-
* calculate the minimum distance from the source vertex to all other vertexMap in the graph. If `getMin` is set to
|
|
604
|
-
* `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
|
|
605
|
-
* @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertexMap from the source
|
|
606
|
-
* vertex.
|
|
607
|
-
* @returns The function `bellmanFord` returns an object with the following properties:
|
|
542
|
+
* Bellman-Ford single-source shortest paths with option to scan negative cycles.
|
|
543
|
+
* @param src - Source vertex or key.
|
|
544
|
+
* @param scanNegativeCycle - If `true`, also detect negative cycles.
|
|
545
|
+
* @param getMin - If `true`, compute global minimum distance.
|
|
546
|
+
* @param genPath - If `true`, generate path arrays via predecessor map.
|
|
547
|
+
* @returns Result bag including distances, predecessors, and optional cycle flag.
|
|
548
|
+
* @remarks Time O(V * E), Space O(V + E)
|
|
608
549
|
*/
|
|
609
550
|
bellmanFord(src, scanNegativeCycle, getMin, genPath) {
|
|
610
551
|
if (getMin === undefined)
|
|
@@ -614,10 +555,9 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
614
555
|
const srcVertex = this._getVertex(src);
|
|
615
556
|
const paths = [];
|
|
616
557
|
const distMap = new Map();
|
|
617
|
-
const preMap = new Map();
|
|
558
|
+
const preMap = new Map();
|
|
618
559
|
let min = Number.MAX_SAFE_INTEGER;
|
|
619
560
|
let minPath = [];
|
|
620
|
-
// TODO
|
|
621
561
|
let hasNegativeCycle;
|
|
622
562
|
if (scanNegativeCycle)
|
|
623
563
|
hasNegativeCycle = false;
|
|
@@ -693,38 +633,15 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
693
633
|
return { hasNegativeCycle, distMap, preMap, paths, min, minPath };
|
|
694
634
|
}
|
|
695
635
|
/**
|
|
696
|
-
*
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
* Dijkstra algorithm time: O(logVE) space: O(VO + EO)
|
|
700
|
-
* Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
|
|
701
|
-
*/
|
|
702
|
-
/**
|
|
703
|
-
* BellmanFord time:O(VE) space:O(VO)
|
|
704
|
-
* one to rest pairs
|
|
705
|
-
* The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edgeMap for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edgeMap, the Bellman-Ford algorithm is more flexible in some scenarios.
|
|
706
|
-
* The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
|
|
707
|
-
*/
|
|
708
|
-
/**
|
|
709
|
-
* Time Complexity: O(V^3) - Cubic time (Floyd-Warshall algorithm).
|
|
710
|
-
* Space Complexity: O(V^2) - Quadratic space (Floyd-Warshall algorithm).
|
|
711
|
-
*
|
|
712
|
-
* Not support graph with negative weight cycle
|
|
713
|
-
* all pairs
|
|
714
|
-
* The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edgeMap, and it can simultaneously compute shortest paths between any two nodes.
|
|
715
|
-
* The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertexMap in a
|
|
716
|
-
* graph.
|
|
717
|
-
* @returns The function `floydWarshall()` returns an object with two properties: `costs` and `predecessor`. The `costs`
|
|
718
|
-
* property is a 2D array of numbers representing the shortest path costs between vertexMap in a graph. The
|
|
719
|
-
* `predecessor` property is a 2D array of vertexMap (or `undefined`) representing the predecessor vertexMap in the shortest
|
|
720
|
-
* path between vertexMap in the
|
|
636
|
+
* Floyd–Warshall all-pairs shortest paths.
|
|
637
|
+
* @returns `{ costs, predecessor }` matrices.
|
|
638
|
+
* @remarks Time O(V^3), Space O(V^2)
|
|
721
639
|
*/
|
|
722
640
|
floydWarshall() {
|
|
723
641
|
const idAndVertices = [...this._vertexMap];
|
|
724
642
|
const n = idAndVertices.length;
|
|
725
643
|
const costs = [];
|
|
726
644
|
const predecessor = [];
|
|
727
|
-
// successors
|
|
728
645
|
for (let i = 0; i < n; i++) {
|
|
729
646
|
costs[i] = [];
|
|
730
647
|
predecessor[i] = [];
|
|
@@ -750,8 +667,10 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
750
667
|
return { costs, predecessor };
|
|
751
668
|
}
|
|
752
669
|
/**
|
|
753
|
-
*
|
|
754
|
-
*
|
|
670
|
+
* Enumerate simple cycles (may be expensive).
|
|
671
|
+
* @param isInclude2Cycle - If `true`, include 2-cycles when graph semantics allow.
|
|
672
|
+
* @returns Array of cycles (each as array of vertex keys).
|
|
673
|
+
* @remarks Time exponential in worst-case, Space O(V + E)
|
|
755
674
|
*/
|
|
756
675
|
getCycles(isInclude2Cycle = false) {
|
|
757
676
|
const cycles = [];
|
|
@@ -776,7 +695,6 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
776
695
|
for (const vertex of this.vertexMap.values()) {
|
|
777
696
|
dfs(vertex, [], visited);
|
|
778
697
|
}
|
|
779
|
-
// Use a set to eliminate duplicate cycles
|
|
780
698
|
const uniqueCycles = new Map();
|
|
781
699
|
for (const cycle of cycles) {
|
|
782
700
|
const sorted = [...cycle].sort().toString();
|
|
@@ -786,24 +704,22 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
786
704
|
uniqueCycles.set(sorted, cycle);
|
|
787
705
|
}
|
|
788
706
|
}
|
|
789
|
-
|
|
707
|
+
/**
|
|
708
|
+
* Map entries to an array via callback.
|
|
709
|
+
* @param callback - `(key, value, index, self) => T`.
|
|
710
|
+
* @param thisArg - Optional `this` for callback.
|
|
711
|
+
* @returns Mapped results.
|
|
712
|
+
* @remarks Time O(V), Space O(V)
|
|
713
|
+
*/
|
|
790
714
|
return [...uniqueCycles].map(cycleString => cycleString[1]);
|
|
791
715
|
}
|
|
792
716
|
/**
|
|
793
|
-
*
|
|
794
|
-
*
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
*
|
|
798
|
-
* @
|
|
799
|
-
* `value`, `key`, `index`, and `this`. It is used to determine whether an element should be included
|
|
800
|
-
* in the filtered array. The callback function should return `true` if the element should be
|
|
801
|
-
* included, and `
|
|
802
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
803
|
-
* specify the value of `this` within the `predicate` function. It is used when you want to bind a
|
|
804
|
-
* specific object as the context for the `predicate` function. If `thisArg` is provided, it will be
|
|
805
|
-
* @returns The `filter` method returns an array of key-value pairs `[VertexKey, V | undefined][]`
|
|
806
|
-
* that satisfy the given predicate function.
|
|
717
|
+
* Induced-subgraph filter: keep vertices where `predicate(key, value)` is true,
|
|
718
|
+
* and only keep edges whose endpoints both survive.
|
|
719
|
+
* @param predicate - `(key, value, index, self) => boolean`.
|
|
720
|
+
* @param thisArg - Optional `this` for callback.
|
|
721
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
722
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
807
723
|
*/
|
|
808
724
|
filter(predicate, thisArg) {
|
|
809
725
|
const filtered = [];
|
|
@@ -814,21 +730,23 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
814
730
|
}
|
|
815
731
|
index++;
|
|
816
732
|
}
|
|
817
|
-
return filtered;
|
|
733
|
+
return this._createLike(filtered, this._snapshotOptions());
|
|
818
734
|
}
|
|
819
735
|
/**
|
|
820
|
-
*
|
|
821
|
-
*
|
|
822
|
-
*
|
|
823
|
-
* The `map` function iterates over the elements of a collection and applies a callback function to
|
|
824
|
-
* each element, returning an array of the results.
|
|
825
|
-
* @param callback - The callback parameter is a function that will be called for each element in the
|
|
826
|
-
* map. It takes four arguments:
|
|
827
|
-
* @param {any} [thisArg] - The `thisArg` parameter is an optional argument that allows you to
|
|
828
|
-
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
829
|
-
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
830
|
-
* @returns The `map` function is returning an array of type `T[]`.
|
|
736
|
+
* Preserve the old behavior: return filtered entries as an array.
|
|
737
|
+
* @remarks Time O(V), Space O(V)
|
|
831
738
|
*/
|
|
739
|
+
filterEntries(predicate, thisArg) {
|
|
740
|
+
const filtered = [];
|
|
741
|
+
let index = 0;
|
|
742
|
+
for (const [key, value] of this) {
|
|
743
|
+
if (predicate.call(thisArg, key, value, index, this)) {
|
|
744
|
+
filtered.push([key, value]);
|
|
745
|
+
}
|
|
746
|
+
index++;
|
|
747
|
+
}
|
|
748
|
+
return filtered;
|
|
749
|
+
}
|
|
832
750
|
map(callback, thisArg) {
|
|
833
751
|
const mapped = [];
|
|
834
752
|
let index = 0;
|
|
@@ -838,23 +756,135 @@ export class AbstractGraph extends IterableEntryBase {
|
|
|
838
756
|
}
|
|
839
757
|
return mapped;
|
|
840
758
|
}
|
|
759
|
+
/**
|
|
760
|
+
* Create a deep clone of the graph with the same species.
|
|
761
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
762
|
+
*/
|
|
763
|
+
/**
|
|
764
|
+
* Create a deep clone of the graph with the same species.
|
|
765
|
+
* @returns A new graph of the same concrete class (`this` type).
|
|
766
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
767
|
+
*/
|
|
768
|
+
clone() {
|
|
769
|
+
return this._createLike(undefined, this._snapshotOptions());
|
|
770
|
+
}
|
|
771
|
+
// ===== Same-species factory & cloning helpers =====
|
|
772
|
+
/**
|
|
773
|
+
* Internal iterator over `[key, value]` entries in insertion order.
|
|
774
|
+
* @returns Iterator of `[VertexKey, V | undefined]`.
|
|
775
|
+
* @remarks Time O(V), Space O(1)
|
|
776
|
+
*/
|
|
841
777
|
*_getIterator() {
|
|
842
778
|
for (const vertex of this._vertexMap.values()) {
|
|
843
779
|
yield [vertex.key, vertex.value];
|
|
844
780
|
}
|
|
845
781
|
}
|
|
782
|
+
/**
|
|
783
|
+
* Capture configuration needed to reproduce the current graph.
|
|
784
|
+
* Currently the graph has no runtime options, so we return an empty object.
|
|
785
|
+
*/
|
|
786
|
+
/**
|
|
787
|
+
* Capture configuration needed to reproduce the current graph.
|
|
788
|
+
* @returns Options bag (opaque to callers).
|
|
789
|
+
* @remarks Time O(1), Space O(1)
|
|
790
|
+
*/
|
|
791
|
+
_snapshotOptions() {
|
|
792
|
+
return { graph: { ...this._options } };
|
|
793
|
+
}
|
|
794
|
+
/**
|
|
795
|
+
* Create an empty graph instance of the same concrete species (Directed/Undirected/etc).
|
|
796
|
+
* @remarks Time O(1), Space O(1)
|
|
797
|
+
*/
|
|
798
|
+
/**
|
|
799
|
+
* Create an empty graph instance of the same concrete species.
|
|
800
|
+
* @param _options - Snapshot options from `_snapshotOptions()`.
|
|
801
|
+
* @returns A new empty graph instance of `this` type.
|
|
802
|
+
* @remarks Time O(1), Space O(1)
|
|
803
|
+
*/
|
|
804
|
+
_createInstance(_options) {
|
|
805
|
+
const Ctor = this.constructor;
|
|
806
|
+
const instance = new Ctor();
|
|
807
|
+
const graph = _options?.graph;
|
|
808
|
+
if (graph)
|
|
809
|
+
instance._options = { ...instance._options, ...graph };
|
|
810
|
+
else
|
|
811
|
+
instance._options = { ...instance._options, ...this._options };
|
|
812
|
+
return instance;
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Create a same-species graph populated with the given entries.
|
|
816
|
+
* Also preserves edges between kept vertices from the source graph.
|
|
817
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
818
|
+
*/
|
|
819
|
+
/**
|
|
820
|
+
* Create a same-species graph populated with entries; preserves edges among kept vertices.
|
|
821
|
+
* @param iter - Optional entries to seed the new graph.
|
|
822
|
+
* @param options - Snapshot options.
|
|
823
|
+
* @returns A new graph of `this` type.
|
|
824
|
+
* @remarks Time O(V + E), Space O(V + E)
|
|
825
|
+
*/
|
|
826
|
+
_createLike(iter, options) {
|
|
827
|
+
const g = this._createInstance(options);
|
|
828
|
+
// 1) Add vertices
|
|
829
|
+
if (iter) {
|
|
830
|
+
for (const [k, v] of iter) {
|
|
831
|
+
g.addVertex(k, v);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
else {
|
|
835
|
+
for (const [k, v] of this) {
|
|
836
|
+
g.addVertex(k, v);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
// 2) Add edges whose endpoints exist in the new graph
|
|
840
|
+
const edges = this.edgeSet();
|
|
841
|
+
for (const e of edges) {
|
|
842
|
+
const ends = this.getEndsOfEdge(e);
|
|
843
|
+
if (!ends)
|
|
844
|
+
continue;
|
|
845
|
+
const [va, vb] = ends;
|
|
846
|
+
const ka = va.key;
|
|
847
|
+
const kb = vb.key;
|
|
848
|
+
const hasA = g.hasVertex ? g.hasVertex(ka) : false;
|
|
849
|
+
const hasB = g.hasVertex ? g.hasVertex(kb) : false;
|
|
850
|
+
if (hasA && hasB) {
|
|
851
|
+
const w = e.weight;
|
|
852
|
+
const val = e.value;
|
|
853
|
+
const newEdge = g.createEdge(ka, kb, w, val);
|
|
854
|
+
g._addEdge(newEdge);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return g;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Insert a pre-built vertex into the graph.
|
|
861
|
+
* @param newVertex - Concrete vertex instance.
|
|
862
|
+
* @returns `true` if inserted; `false` if key already exists.
|
|
863
|
+
* @remarks Time O(1) avg, Space O(1)
|
|
864
|
+
*/
|
|
846
865
|
_addVertex(newVertex) {
|
|
847
866
|
if (this.hasVertex(newVertex)) {
|
|
848
867
|
return false;
|
|
849
|
-
// throw (new Error('Duplicated vertex key is not allowed'));
|
|
850
868
|
}
|
|
851
869
|
this._vertexMap.set(newVertex.key, newVertex);
|
|
852
870
|
return true;
|
|
853
871
|
}
|
|
872
|
+
/**
|
|
873
|
+
* Resolve a vertex key or instance to the concrete vertex instance.
|
|
874
|
+
* @param vertexOrKey - Vertex key or existing vertex.
|
|
875
|
+
* @returns Vertex instance or `undefined`.
|
|
876
|
+
* @remarks Time O(1), Space O(1)
|
|
877
|
+
*/
|
|
854
878
|
_getVertex(vertexOrKey) {
|
|
855
879
|
const vertexKey = this._getVertexKey(vertexOrKey);
|
|
856
880
|
return this._vertexMap.get(vertexKey) || undefined;
|
|
857
881
|
}
|
|
882
|
+
/**
|
|
883
|
+
* Resolve a vertex key from a key or vertex instance.
|
|
884
|
+
* @param vertexOrKey - Vertex key or existing vertex.
|
|
885
|
+
* @returns The vertex key.
|
|
886
|
+
* @remarks Time O(1), Space O(1)
|
|
887
|
+
*/
|
|
858
888
|
_getVertexKey(vertexOrKey) {
|
|
859
889
|
return vertexOrKey instanceof AbstractVertex ? vertexOrKey.key : vertexOrKey;
|
|
860
890
|
}
|