data-structure-typed 0.9.16 → 1.3.1
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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +134 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -1,390 +1,422 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
-
if (!m) return o;
|
|
20
|
-
var i = m.call(o), r, ar = [], e;
|
|
21
|
-
try {
|
|
22
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
-
}
|
|
24
|
-
catch (error) { e = { error: error }; }
|
|
25
|
-
finally {
|
|
26
|
-
try {
|
|
27
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
-
}
|
|
29
|
-
finally { if (e) throw e.error; }
|
|
30
|
-
}
|
|
31
|
-
return ar;
|
|
32
|
-
};
|
|
33
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
34
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
35
|
-
if (ar || !(i in from)) {
|
|
36
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
37
|
-
ar[i] = from[i];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
41
|
-
};
|
|
42
|
-
var __values = (this && this.__values) || function(o) {
|
|
43
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
44
|
-
if (m) return m.call(o);
|
|
45
|
-
if (o && typeof o.length === "number") return {
|
|
46
|
-
next: function () {
|
|
47
|
-
if (o && i >= o.length) o = void 0;
|
|
48
|
-
return { value: o && o[i++], done: !o };
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
52
|
-
};
|
|
53
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
3
|
exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
4
|
+
/**
|
|
5
|
+
* data-structure-typed
|
|
6
|
+
*
|
|
7
|
+
* @author Tyler Zeng
|
|
8
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
9
|
+
* @license MIT License
|
|
10
|
+
*/
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const abstract_graph_1 = require("./abstract-graph");
|
|
13
|
+
class DirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a vertex with an optional value.
|
|
16
|
+
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
17
|
+
* used to uniquely identify the vertex within a graph or data structure.
|
|
18
|
+
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
|
|
19
|
+
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
20
|
+
*/
|
|
21
|
+
constructor(id, val) {
|
|
22
|
+
super(id, val);
|
|
61
23
|
}
|
|
62
|
-
|
|
63
|
-
}(abstract_graph_1.AbstractVertex));
|
|
24
|
+
}
|
|
64
25
|
exports.DirectedVertex = DirectedVertex;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
26
|
+
class DirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function initializes the source and destination vertices of an edge, along with an optional weight
|
|
29
|
+
* and value.
|
|
30
|
+
* @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
|
|
31
|
+
* a graph.
|
|
32
|
+
* @param {VertexId} dest - The `dest` parameter represents the destination vertex of an edge. It is of type
|
|
33
|
+
* `VertexId`, which is likely a unique identifier for a vertex in a graph.
|
|
34
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
|
|
35
|
+
* @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value associated with
|
|
36
|
+
* the edge.
|
|
37
|
+
*/
|
|
38
|
+
constructor(src, dest, weight, val) {
|
|
39
|
+
super(weight, val);
|
|
40
|
+
this._src = src;
|
|
41
|
+
this._dest = dest;
|
|
42
|
+
}
|
|
43
|
+
get src() {
|
|
44
|
+
return this._src;
|
|
45
|
+
}
|
|
46
|
+
set src(v) {
|
|
47
|
+
this._src = v;
|
|
48
|
+
}
|
|
49
|
+
get dest() {
|
|
50
|
+
return this._dest;
|
|
51
|
+
}
|
|
52
|
+
set dest(v) {
|
|
53
|
+
this._dest = v;
|
|
72
54
|
}
|
|
73
|
-
|
|
74
|
-
get: function () {
|
|
75
|
-
return this._src;
|
|
76
|
-
},
|
|
77
|
-
set: function (v) {
|
|
78
|
-
this._src = v;
|
|
79
|
-
},
|
|
80
|
-
enumerable: false,
|
|
81
|
-
configurable: true
|
|
82
|
-
});
|
|
83
|
-
Object.defineProperty(DirectedEdge.prototype, "dest", {
|
|
84
|
-
get: function () {
|
|
85
|
-
return this._dest;
|
|
86
|
-
},
|
|
87
|
-
set: function (v) {
|
|
88
|
-
this._dest = v;
|
|
89
|
-
},
|
|
90
|
-
enumerable: false,
|
|
91
|
-
configurable: true
|
|
92
|
-
});
|
|
93
|
-
return DirectedEdge;
|
|
94
|
-
}(abstract_graph_1.AbstractEdge));
|
|
55
|
+
}
|
|
95
56
|
exports.DirectedEdge = DirectedEdge;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
57
|
+
class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
58
|
+
/**
|
|
59
|
+
* The constructor function initializes an instance of a class.
|
|
60
|
+
*/
|
|
61
|
+
constructor() {
|
|
62
|
+
super();
|
|
63
|
+
this._outEdgeMap = new Map();
|
|
64
|
+
this._inEdgeMap = new Map();
|
|
65
|
+
}
|
|
66
|
+
get outEdgeMap() {
|
|
67
|
+
return this._outEdgeMap;
|
|
68
|
+
}
|
|
69
|
+
get inEdgeMap() {
|
|
70
|
+
return this._inEdgeMap;
|
|
104
71
|
}
|
|
105
|
-
|
|
106
|
-
|
|
72
|
+
/**
|
|
73
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
74
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* The function creates a new vertex with an optional value and returns it.
|
|
78
|
+
* @param {VertexId} id - The `id` parameter is the unique identifier for the vertex. It is of type `VertexId`, which
|
|
79
|
+
* could be a number or a string depending on how you want to identify your vertices.
|
|
80
|
+
* @param [val] - The 'val' parameter is an optional value that can be assigned to the vertex. If a value is provided,
|
|
81
|
+
* it will be assigned to the 'val' property of the vertex. If no value is provided, the 'val' property will be
|
|
82
|
+
* assigned the same value as the 'id' parameter
|
|
83
|
+
* @returns a new instance of a DirectedVertex object, casted as type V.
|
|
84
|
+
*/
|
|
85
|
+
createVertex(id, val) {
|
|
86
|
+
return new DirectedVertex(id, val !== null && val !== void 0 ? val : id);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
|
|
90
|
+
* This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
|
|
91
|
+
*/
|
|
92
|
+
/**
|
|
93
|
+
* The function creates a directed edge between two vertices with an optional weight and value.
|
|
94
|
+
* @param {VertexId} src - The source vertex ID of the edge. It represents the starting point of the edge.
|
|
95
|
+
* @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex for the edge.
|
|
96
|
+
* @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge. If no
|
|
97
|
+
* weight is provided, it defaults to 1.
|
|
98
|
+
* @param [val] - The 'val' parameter is an optional value that can be assigned to the edge. It can be of any type and
|
|
99
|
+
* is used to store additional information or data associated with the edge.
|
|
100
|
+
* @returns a new instance of a DirectedEdge object, casted as type E.
|
|
101
|
+
*/
|
|
102
|
+
createEdge(src, dest, weight, val) {
|
|
103
|
+
return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, val);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
107
|
+
* @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
108
|
+
* @param {V | null | VertexId} destOrId - The `destOrId` parameter in the `getEdge` function represents the
|
|
109
|
+
* destination vertex of the edge. It can be either a vertex object (`V`), a vertex ID (`VertexId`), or `null` if the
|
|
110
|
+
* destination is not specified.
|
|
111
|
+
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
112
|
+
*/
|
|
113
|
+
getEdge(srcOrId, destOrId) {
|
|
114
|
+
let edges = [];
|
|
107
115
|
if (srcOrId !== null && destOrId !== null) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
if (src &&
|
|
111
|
-
|
|
116
|
+
const src = this._getVertex(srcOrId);
|
|
117
|
+
const dest = this._getVertex(destOrId);
|
|
118
|
+
if (src && dest) {
|
|
119
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
112
120
|
if (srcOutEdges) {
|
|
113
|
-
edges = srcOutEdges.filter(
|
|
121
|
+
edges = srcOutEdges.filter(edge => edge.dest === dest.id);
|
|
114
122
|
}
|
|
115
123
|
}
|
|
116
124
|
}
|
|
117
125
|
return edges[0] || null;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
srcOutEdges.push(edge);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
this._outEdgeMap.set(srcVertex, [edge]);
|
|
133
|
-
}
|
|
134
|
-
var destInEdges = this._inEdgeMap.get(destVertex);
|
|
135
|
-
if (destInEdges) {
|
|
136
|
-
destInEdges.push(edge);
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
this._inEdgeMap.set(destVertex, [edge]);
|
|
140
|
-
}
|
|
141
|
-
return true;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
DirectedGraph.prototype.removeEdgeBetween = function (srcOrId, destOrId) {
|
|
148
|
-
var src = this.getVertex(srcOrId);
|
|
149
|
-
var dest = this.getVertex(destOrId);
|
|
150
|
-
var removed = null;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
129
|
+
* @param {V | VertexId} srcOrId - The source vertex or its ID.
|
|
130
|
+
* @param {V | VertexId} destOrId - The `destOrId` parameter represents the destination vertex or its ID.
|
|
131
|
+
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
132
|
+
*/
|
|
133
|
+
removeEdgeSrcToDest(srcOrId, destOrId) {
|
|
134
|
+
const src = this._getVertex(srcOrId);
|
|
135
|
+
const dest = this._getVertex(destOrId);
|
|
136
|
+
let removed = null;
|
|
151
137
|
if (!src || !dest) {
|
|
152
138
|
return null;
|
|
153
139
|
}
|
|
154
|
-
|
|
140
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
155
141
|
if (srcOutEdges) {
|
|
156
|
-
(0, utils_1.arrayRemove)(srcOutEdges,
|
|
142
|
+
(0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.dest === dest.id);
|
|
157
143
|
}
|
|
158
|
-
|
|
144
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
159
145
|
if (destInEdges) {
|
|
160
|
-
removed = (0, utils_1.arrayRemove)(destInEdges,
|
|
146
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.src === src.id)[0] || null;
|
|
161
147
|
}
|
|
162
148
|
return removed;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
152
|
+
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
153
|
+
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
154
|
+
* @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
155
|
+
*/
|
|
156
|
+
removeEdge(edge) {
|
|
157
|
+
let removed = null;
|
|
158
|
+
const src = this._getVertex(edge.src);
|
|
159
|
+
const dest = this._getVertex(edge.dest);
|
|
168
160
|
if (src && dest) {
|
|
169
|
-
|
|
161
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
170
162
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
171
|
-
(0, utils_1.arrayRemove)(srcOutEdges,
|
|
163
|
+
(0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.src === src.id);
|
|
172
164
|
}
|
|
173
|
-
|
|
165
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
174
166
|
if (destInEdges && destInEdges.length > 0) {
|
|
175
|
-
removed = (0, utils_1.arrayRemove)(destInEdges,
|
|
167
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.dest === dest.id)[0];
|
|
176
168
|
}
|
|
177
169
|
}
|
|
178
170
|
return removed;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The function removes edges between two vertices and returns the removed edges.
|
|
174
|
+
* @param {VertexId | V} v1 - The parameter `v1` can be either a `VertexId` or a `V`. A `VertexId` represents the
|
|
175
|
+
* unique identifier of a vertex in a graph, while `V` represents the actual vertex object.
|
|
176
|
+
* @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V` object. It is used to specify
|
|
177
|
+
* the second vertex in the edge that needs to be removed.
|
|
178
|
+
* @returns an array of removed edges (E[]).
|
|
179
|
+
*/
|
|
180
|
+
removeEdgesBetween(v1, v2) {
|
|
181
|
+
const removed = [];
|
|
182
|
+
if (v1 && v2) {
|
|
183
|
+
const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
|
|
184
|
+
const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
|
|
185
|
+
v1ToV2 && removed.push(v1ToV2);
|
|
186
|
+
v2ToV1 && removed.push(v2ToV1);
|
|
187
|
+
}
|
|
188
|
+
return removed;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
192
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
193
|
+
* (`VertexId`).
|
|
194
|
+
* @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
|
|
195
|
+
*/
|
|
196
|
+
incomingEdgesOf(vertexOrId) {
|
|
197
|
+
const target = this._getVertex(vertexOrId);
|
|
185
198
|
if (target) {
|
|
186
|
-
return this.
|
|
199
|
+
return this.inEdgeMap.get(target) || [];
|
|
187
200
|
}
|
|
188
201
|
return [];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
205
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
206
|
+
* (`VertexId`).
|
|
207
|
+
* @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
|
|
208
|
+
*/
|
|
209
|
+
outgoingEdgesOf(vertexOrId) {
|
|
210
|
+
const target = this._getVertex(vertexOrId);
|
|
192
211
|
if (target) {
|
|
193
212
|
return this._outEdgeMap.get(target) || [];
|
|
194
213
|
}
|
|
195
214
|
return [];
|
|
196
|
-
}
|
|
197
|
-
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
218
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
219
|
+
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
220
|
+
*/
|
|
221
|
+
degreeOf(vertexOrId) {
|
|
198
222
|
return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
|
|
199
|
-
}
|
|
200
|
-
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
226
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
227
|
+
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
228
|
+
*/
|
|
229
|
+
inDegreeOf(vertexOrId) {
|
|
201
230
|
return this.incomingEdgesOf(vertexOrId).length;
|
|
202
|
-
}
|
|
203
|
-
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
234
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
235
|
+
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
236
|
+
*/
|
|
237
|
+
outDegreeOf(vertexOrId) {
|
|
204
238
|
return this.outgoingEdgesOf(vertexOrId).length;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
242
|
+
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
243
|
+
* @returns The function `edgesOf` returns an array of edges.
|
|
244
|
+
*/
|
|
245
|
+
edgesOf(vertexOrId) {
|
|
246
|
+
return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
|
|
250
|
+
* @param {E} e - The parameter "e" is of type E, which represents an edge in a graph.
|
|
251
|
+
* @returns either a vertex object (V) or null.
|
|
252
|
+
*/
|
|
253
|
+
getEdgeSrc(e) {
|
|
254
|
+
return this._getVertex(e.src);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
258
|
+
* @param {E} e - The parameter "e" is of type "E", which represents an edge in a graph.
|
|
259
|
+
* @returns either a vertex object of type V or null.
|
|
260
|
+
*/
|
|
261
|
+
getEdgeDest(e) {
|
|
262
|
+
return this._getVertex(e.dest);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
266
|
+
* @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
267
|
+
* find the destinations. It can be either a `V` object, a `VertexId` value, or `null`.
|
|
268
|
+
* @returns an array of vertices (V[]).
|
|
269
|
+
*/
|
|
270
|
+
getDestinations(vertex) {
|
|
217
271
|
if (vertex === null) {
|
|
218
272
|
return [];
|
|
219
273
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (child) {
|
|
227
|
-
destinations.push(child);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
232
|
-
finally {
|
|
233
|
-
try {
|
|
234
|
-
if (outgoingEdges_1_1 && !outgoingEdges_1_1.done && (_a = outgoingEdges_1.return)) _a.call(outgoingEdges_1);
|
|
274
|
+
const destinations = [];
|
|
275
|
+
const outgoingEdges = this.outgoingEdgesOf(vertex);
|
|
276
|
+
for (const outEdge of outgoingEdges) {
|
|
277
|
+
const child = this.getEdgeDest(outEdge);
|
|
278
|
+
if (child) {
|
|
279
|
+
destinations.push(child);
|
|
235
280
|
}
|
|
236
|
-
finally { if (e_1) throw e_1.error; }
|
|
237
281
|
}
|
|
238
282
|
return destinations;
|
|
239
|
-
}
|
|
240
|
-
/**--- start find cycles --- */
|
|
283
|
+
}
|
|
241
284
|
/**
|
|
242
|
-
*
|
|
243
|
-
*
|
|
285
|
+
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
286
|
+
* in the sorted order, or null if the graph contains a cycle.
|
|
287
|
+
* @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
|
|
288
|
+
* property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
|
|
289
|
+
* specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
|
|
290
|
+
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
|
|
244
291
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
var _this = this;
|
|
248
|
-
// vector<vector<int>> g;
|
|
249
|
-
// vector<int> color;
|
|
250
|
-
// int last;
|
|
251
|
-
// bool hasCycle;
|
|
252
|
-
//
|
|
253
|
-
// bool topo_sort() {
|
|
254
|
-
// int n = g.size();
|
|
255
|
-
// vector<int> degree(n, 0);
|
|
256
|
-
// queue<int> q;
|
|
257
|
-
// for (int i = 0; i < n; i++) {
|
|
258
|
-
// degree[i] = g[i].size();
|
|
259
|
-
// if (degree[i] <= 1) {
|
|
260
|
-
// q.push(i);
|
|
261
|
-
// }
|
|
262
|
-
// }
|
|
263
|
-
// int cnt = 0;
|
|
264
|
-
// while (!q.empty()) {
|
|
265
|
-
// cnt++;
|
|
266
|
-
// int root = q.front();
|
|
267
|
-
// q.pop();
|
|
268
|
-
// for (auto child : g[root]) {
|
|
269
|
-
// degree[child]--;
|
|
270
|
-
// if (degree[child] == 1) {
|
|
271
|
-
// q.push(child);
|
|
272
|
-
// }
|
|
273
|
-
// }
|
|
274
|
-
// }
|
|
275
|
-
// return (cnt != n);
|
|
276
|
-
// }
|
|
292
|
+
topologicalSort(propertyName) {
|
|
293
|
+
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
277
294
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
278
295
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
var entry = _d.value;
|
|
283
|
-
statusMap.set(entry[1], 0);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
287
|
-
finally {
|
|
288
|
-
try {
|
|
289
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
290
|
-
}
|
|
291
|
-
finally { if (e_2) throw e_2.error; }
|
|
296
|
+
const statusMap = new Map();
|
|
297
|
+
for (const entry of this.vertices) {
|
|
298
|
+
statusMap.set(entry[1], 0);
|
|
292
299
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
var e_4, _a;
|
|
300
|
+
let sorted = [];
|
|
301
|
+
let hasCycle = false;
|
|
302
|
+
const dfs = (cur) => {
|
|
297
303
|
statusMap.set(cur, 1);
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (childStatus === 0) {
|
|
304
|
-
dfs(child);
|
|
305
|
-
}
|
|
306
|
-
else if (childStatus === 1) {
|
|
307
|
-
hasCycle = true;
|
|
308
|
-
}
|
|
304
|
+
const children = this.getDestinations(cur);
|
|
305
|
+
for (const child of children) {
|
|
306
|
+
const childStatus = statusMap.get(child);
|
|
307
|
+
if (childStatus === 0) {
|
|
308
|
+
dfs(child);
|
|
309
309
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
finally {
|
|
313
|
-
try {
|
|
314
|
-
if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
|
|
310
|
+
else if (childStatus === 1) {
|
|
311
|
+
hasCycle = true;
|
|
315
312
|
}
|
|
316
|
-
finally { if (e_4) throw e_4.error; }
|
|
317
313
|
}
|
|
318
314
|
statusMap.set(cur, 2);
|
|
319
315
|
sorted.push(cur);
|
|
320
316
|
};
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (statusMap.get(entry[1]) === 0) {
|
|
325
|
-
dfs(entry[1]);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
330
|
-
finally {
|
|
331
|
-
try {
|
|
332
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
317
|
+
for (const entry of this.vertices) {
|
|
318
|
+
if (statusMap.get(entry[1]) === 0) {
|
|
319
|
+
dfs(entry[1]);
|
|
333
320
|
}
|
|
334
|
-
finally { if (e_3) throw e_3.error; }
|
|
335
321
|
}
|
|
336
|
-
if (hasCycle)
|
|
322
|
+
if (hasCycle)
|
|
337
323
|
return null;
|
|
338
|
-
|
|
324
|
+
if (propertyName === 'id')
|
|
325
|
+
sorted = sorted.map(vertex => vertex instanceof DirectedVertex ? vertex.id : vertex);
|
|
339
326
|
return sorted.reverse();
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* The `edgeSet` function returns an array of all the edges in the graph.
|
|
330
|
+
* @returns The `edgeSet()` method returns an array of edges (`E[]`).
|
|
331
|
+
*/
|
|
332
|
+
edgeSet() {
|
|
333
|
+
let edges = [];
|
|
334
|
+
this._outEdgeMap.forEach(outEdges => {
|
|
335
|
+
edges = [...edges, ...outEdges];
|
|
346
336
|
});
|
|
347
337
|
return edges;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
|
|
341
|
+
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
342
|
+
* (`VertexId`).
|
|
343
|
+
* @returns an array of vertices (V[]).
|
|
344
|
+
*/
|
|
345
|
+
getNeighbors(vertexOrId) {
|
|
346
|
+
const neighbors = [];
|
|
347
|
+
const vertex = this._getVertex(vertexOrId);
|
|
353
348
|
if (vertex) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
if (neighbor) {
|
|
361
|
-
neighbors.push(neighbor);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
366
|
-
finally {
|
|
367
|
-
try {
|
|
368
|
-
if (outEdges_1_1 && !outEdges_1_1.done && (_a = outEdges_1.return)) _a.call(outEdges_1);
|
|
349
|
+
const outEdges = this.outgoingEdgesOf(vertex);
|
|
350
|
+
for (const outEdge of outEdges) {
|
|
351
|
+
const neighbor = this._getVertex(outEdge.dest);
|
|
352
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
353
|
+
if (neighbor) {
|
|
354
|
+
neighbors.push(neighbor);
|
|
369
355
|
}
|
|
370
|
-
finally { if (e_5) throw e_5.error; }
|
|
371
356
|
}
|
|
372
357
|
}
|
|
373
358
|
return neighbors;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
362
|
+
* otherwise it returns null.
|
|
363
|
+
* @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in a graph.
|
|
364
|
+
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
|
|
365
|
+
* graph. If the edge does not exist, it returns `null`.
|
|
366
|
+
*/
|
|
367
|
+
getEndsOfEdge(edge) {
|
|
368
|
+
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
377
369
|
return null;
|
|
378
370
|
}
|
|
379
|
-
|
|
380
|
-
|
|
371
|
+
const v1 = this._getVertex(edge.src);
|
|
372
|
+
const v2 = this._getVertex(edge.dest);
|
|
381
373
|
if (v1 && v2) {
|
|
382
374
|
return [v1, v2];
|
|
383
375
|
}
|
|
384
376
|
else {
|
|
385
377
|
return null;
|
|
386
378
|
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
|
|
382
|
+
* @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in a graph. It is the edge that
|
|
383
|
+
* needs to be added to the graph.
|
|
384
|
+
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
385
|
+
* source or destination vertex does not exist in the graph.
|
|
386
|
+
*/
|
|
387
|
+
_addEdgeOnly(edge) {
|
|
388
|
+
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
389
|
+
return false;
|
|
390
|
+
}
|
|
391
|
+
const srcVertex = this._getVertex(edge.src);
|
|
392
|
+
const destVertex = this._getVertex(edge.dest);
|
|
393
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
394
|
+
if (srcVertex && destVertex) {
|
|
395
|
+
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
396
|
+
if (srcOutEdges) {
|
|
397
|
+
srcOutEdges.push(edge);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
this._outEdgeMap.set(srcVertex, [edge]);
|
|
401
|
+
}
|
|
402
|
+
const destInEdges = this._inEdgeMap.get(destVertex);
|
|
403
|
+
if (destInEdges) {
|
|
404
|
+
destInEdges.push(edge);
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
this._inEdgeMap.set(destVertex, [edge]);
|
|
408
|
+
}
|
|
409
|
+
return true;
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
_setOutEdgeMap(value) {
|
|
416
|
+
this._outEdgeMap = value;
|
|
417
|
+
}
|
|
418
|
+
_setInEdgeMap(value) {
|
|
419
|
+
this._inEdgeMap = value;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
390
422
|
exports.DirectedGraph = DirectedGraph;
|