data-structure-typed 1.19.3 → 1.19.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
- package/dist/data-structures/binary-tree/avl-tree.js +46 -90
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
- package/dist/data-structures/binary-tree/binary-tree.js +9 -31
- package/dist/data-structures/binary-tree/bst.js +96 -139
- package/dist/data-structures/binary-tree/rb-tree.js +32 -56
- package/dist/data-structures/binary-tree/segment-tree.js +78 -120
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.js +340 -574
- package/dist/data-structures/graph/directed-graph.js +146 -276
- package/dist/data-structures/graph/undirected-graph.js +87 -176
- package/dist/data-structures/hash/coordinate-map.js +23 -45
- package/dist/data-structures/hash/coordinate-set.js +20 -42
- package/dist/data-structures/hash/hash-table.js +2 -5
- package/dist/data-structures/hash/pair.js +2 -5
- package/dist/data-structures/hash/tree-map.js +2 -5
- package/dist/data-structures/hash/tree-set.js +2 -5
- package/dist/data-structures/heap/heap.js +53 -77
- package/dist/data-structures/heap/max-heap.js +8 -26
- package/dist/data-structures/heap/min-heap.js +8 -26
- package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
- package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
- package/dist/data-structures/matrix/matrix.js +7 -8
- package/dist/data-structures/matrix/matrix2d.js +76 -93
- package/dist/data-structures/matrix/navigator.js +18 -37
- package/dist/data-structures/matrix/vector2d.js +80 -101
- package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
- package/dist/data-structures/priority-queue/priority-queue.js +93 -139
- package/dist/data-structures/queue/deque.js +82 -128
- package/dist/data-structures/queue/queue.js +24 -25
- package/dist/data-structures/stack/stack.js +21 -22
- package/dist/data-structures/tree/tree.js +32 -45
- package/dist/data-structures/trie/trie.js +93 -200
- package/dist/utils/utils.js +22 -107
- package/dist/utils/validate-type.js +2 -2
- package/package.json +1 -1
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/assets/logo.png +0 -0
- package/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
- package/src/data-structures/binary-tree/avl-tree.ts +297 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
- package/src/data-structures/binary-tree/binary-tree.ts +40 -0
- package/src/data-structures/binary-tree/bst.ts +435 -0
- 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 +12 -0
- package/src/data-structures/binary-tree/rb-tree.ts +102 -0
- package/src/data-structures/binary-tree/segment-tree.ts +243 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +1032 -0
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +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/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +472 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +270 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-table.ts +3 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/pair.ts +3 -0
- package/src/data-structures/hash/tree-map.ts +3 -0
- package/src/data-structures/hash/tree-set.ts +3 -0
- package/src/data-structures/heap/heap.ts +183 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +31 -0
- package/src/data-structures/heap/min-heap.ts +34 -0
- package/src/data-structures/index.ts +15 -0
- package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
- package/src/data-structures/interfaces/abstract-graph.ts +40 -0
- package/src/data-structures/interfaces/avl-tree.ts +28 -0
- package/src/data-structures/interfaces/binary-tree.ts +8 -0
- package/src/data-structures/interfaces/bst.ts +32 -0
- package/src/data-structures/interfaces/directed-graph.ts +20 -0
- package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/heap.ts +1 -0
- package/src/data-structures/interfaces/index.ts +15 -0
- package/src/data-structures/interfaces/navigator.ts +1 -0
- package/src/data-structures/interfaces/priority-queue.ts +1 -0
- package/src/data-structures/interfaces/rb-tree.ts +11 -0
- package/src/data-structures/interfaces/segment-tree.ts +1 -0
- package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
- package/src/data-structures/interfaces/tree-multiset.ts +12 -0
- package/src/data-structures/interfaces/undirected-graph.ts +6 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +208 -0
- package/src/data-structures/matrix/navigator.ts +122 -0
- package/src/data-structures/matrix/vector2d.ts +316 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
- package/src/data-structures/priority-queue/priority-queue.ts +354 -0
- package/src/data-structures/queue/deque.ts +251 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +120 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +69 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +227 -0
- package/src/data-structures/types/abstract-binary-tree.ts +42 -0
- package/src/data-structures/types/abstract-graph.ts +5 -0
- package/src/data-structures/types/avl-tree.ts +5 -0
- package/src/data-structures/types/binary-tree.ts +9 -0
- package/src/data-structures/types/bst.ts +12 -0
- package/src/data-structures/types/directed-graph.ts +8 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +5 -0
- package/src/data-structures/types/helpers.ts +1 -0
- package/src/data-structures/types/index.ts +15 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/rb-tree.ts +8 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +8 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/types/index.ts +2 -0
- package/src/utils/types/utils.ts +6 -0
- package/src/utils/types/validate-type.ts +25 -0
- package/src/utils/utils.ts +78 -0
- package/src/utils/validate-type.ts +69 -0
- package/tsconfig.json +1 -1
|
@@ -1,55 +1,4 @@
|
|
|
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
4
|
/**
|
|
@@ -59,10 +8,9 @@ exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
|
|
|
59
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
60
9
|
* @license MIT License
|
|
61
10
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
__extends(DirectedVertex, _super);
|
|
11
|
+
const utils_1 = require("../../utils");
|
|
12
|
+
const abstract_graph_1 = require("./abstract-graph");
|
|
13
|
+
class DirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
66
14
|
/**
|
|
67
15
|
* The constructor function initializes a vertex with an optional value.
|
|
68
16
|
* @param {VertexId} id - The `id` parameter is of type `VertexId` and represents the identifier of the vertex. It is
|
|
@@ -70,14 +18,12 @@ var DirectedVertex = /** @class */ (function (_super) {
|
|
|
70
18
|
* @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
|
|
71
19
|
* vertex. If no value is provided, the vertex will be initialized with a default value.
|
|
72
20
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
21
|
+
constructor(id, val) {
|
|
22
|
+
super(id, val);
|
|
75
23
|
}
|
|
76
|
-
|
|
77
|
-
}(abstract_graph_1.AbstractVertex));
|
|
24
|
+
}
|
|
78
25
|
exports.DirectedVertex = DirectedVertex;
|
|
79
|
-
|
|
80
|
-
__extends(DirectedEdge, _super);
|
|
26
|
+
class DirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
81
27
|
/**
|
|
82
28
|
* The constructor function initializes the source and destination vertices of an edge, along with an optional weight
|
|
83
29
|
* and value.
|
|
@@ -89,60 +35,40 @@ var DirectedEdge = /** @class */ (function (_super) {
|
|
|
89
35
|
* @param {T} [val] - The `val` parameter is an optional parameter of type `T`. It represents the value associated with
|
|
90
36
|
* the edge.
|
|
91
37
|
*/
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return _this;
|
|
38
|
+
constructor(src, dest, weight, val) {
|
|
39
|
+
super(weight, val);
|
|
40
|
+
this._src = src;
|
|
41
|
+
this._dest = dest;
|
|
97
42
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
},
|
|
112
|
-
set: function (v) {
|
|
113
|
-
this._dest = v;
|
|
114
|
-
},
|
|
115
|
-
enumerable: false,
|
|
116
|
-
configurable: true
|
|
117
|
-
});
|
|
118
|
-
return DirectedEdge;
|
|
119
|
-
}(abstract_graph_1.AbstractEdge));
|
|
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;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
120
56
|
exports.DirectedEdge = DirectedEdge;
|
|
121
|
-
|
|
122
|
-
__extends(DirectedGraph, _super);
|
|
57
|
+
class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
123
58
|
/**
|
|
124
59
|
* The constructor function initializes an instance of a class.
|
|
125
60
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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;
|
|
131
71
|
}
|
|
132
|
-
Object.defineProperty(DirectedGraph.prototype, "outEdgeMap", {
|
|
133
|
-
get: function () {
|
|
134
|
-
return this._outEdgeMap;
|
|
135
|
-
},
|
|
136
|
-
enumerable: false,
|
|
137
|
-
configurable: true
|
|
138
|
-
});
|
|
139
|
-
Object.defineProperty(DirectedGraph.prototype, "inEdgeMap", {
|
|
140
|
-
get: function () {
|
|
141
|
-
return this._inEdgeMap;
|
|
142
|
-
},
|
|
143
|
-
enumerable: false,
|
|
144
|
-
configurable: true
|
|
145
|
-
});
|
|
146
72
|
/**
|
|
147
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.
|
|
148
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.
|
|
@@ -156,9 +82,9 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
156
82
|
* assigned the same value as the 'id' parameter
|
|
157
83
|
* @returns a new instance of a DirectedVertex object, casted as type V.
|
|
158
84
|
*/
|
|
159
|
-
|
|
85
|
+
createVertex(id, val) {
|
|
160
86
|
return new DirectedVertex(id, val !== null && val !== void 0 ? val : id);
|
|
161
|
-
}
|
|
87
|
+
}
|
|
162
88
|
/**
|
|
163
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.
|
|
164
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.
|
|
@@ -173,9 +99,9 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
173
99
|
* is used to store additional information or data associated with the edge.
|
|
174
100
|
* @returns a new instance of a DirectedEdge object, casted as type E.
|
|
175
101
|
*/
|
|
176
|
-
|
|
102
|
+
createEdge(src, dest, weight, val) {
|
|
177
103
|
return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, val);
|
|
178
|
-
}
|
|
104
|
+
}
|
|
179
105
|
/**
|
|
180
106
|
* The `getEdge` function retrieves an edge between two vertices based on their source and destination IDs.
|
|
181
107
|
* @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a vertex object or a vertex ID.
|
|
@@ -184,65 +110,65 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
184
110
|
* destination is not specified.
|
|
185
111
|
* @returns the first edge found between the source and destination vertices, or null if no such edge is found.
|
|
186
112
|
*/
|
|
187
|
-
|
|
188
|
-
|
|
113
|
+
getEdge(srcOrId, destOrId) {
|
|
114
|
+
let edges = [];
|
|
189
115
|
if (srcOrId !== null && destOrId !== null) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if (src &&
|
|
193
|
-
|
|
116
|
+
const src = this._getVertex(srcOrId);
|
|
117
|
+
const dest = this._getVertex(destOrId);
|
|
118
|
+
if (src && dest) {
|
|
119
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
194
120
|
if (srcOutEdges) {
|
|
195
|
-
edges = srcOutEdges.filter(
|
|
121
|
+
edges = srcOutEdges.filter(edge => edge.dest === dest.id);
|
|
196
122
|
}
|
|
197
123
|
}
|
|
198
124
|
}
|
|
199
125
|
return edges[0] || null;
|
|
200
|
-
}
|
|
126
|
+
}
|
|
201
127
|
/**
|
|
202
128
|
* The function removes an edge between two vertices in a graph and returns the removed edge.
|
|
203
129
|
* @param {V | VertexId} srcOrId - The source vertex or its ID.
|
|
204
130
|
* @param {V | VertexId} destOrId - The `destOrId` parameter represents the destination vertex or its ID.
|
|
205
131
|
* @returns the removed edge (E) if it exists, or null if either the source or destination vertex does not exist.
|
|
206
132
|
*/
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
133
|
+
removeEdgeSrcToDest(srcOrId, destOrId) {
|
|
134
|
+
const src = this._getVertex(srcOrId);
|
|
135
|
+
const dest = this._getVertex(destOrId);
|
|
136
|
+
let removed = null;
|
|
211
137
|
if (!src || !dest) {
|
|
212
138
|
return null;
|
|
213
139
|
}
|
|
214
|
-
|
|
140
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
215
141
|
if (srcOutEdges) {
|
|
216
|
-
(0, utils_1.arrayRemove)(srcOutEdges,
|
|
142
|
+
(0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.dest === dest.id);
|
|
217
143
|
}
|
|
218
|
-
|
|
144
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
219
145
|
if (destInEdges) {
|
|
220
|
-
removed = (0, utils_1.arrayRemove)(destInEdges,
|
|
146
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.src === src.id)[0] || null;
|
|
221
147
|
}
|
|
222
148
|
return removed;
|
|
223
|
-
}
|
|
149
|
+
}
|
|
224
150
|
/**
|
|
225
151
|
* The function removes an edge from a graph and returns the removed edge, or null if the edge was not found.
|
|
226
152
|
* @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
|
|
227
153
|
* and `dest`, which represent the source and destination vertices of the edge, respectively.
|
|
228
154
|
* @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
|
|
229
155
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
156
|
+
removeEdge(edge) {
|
|
157
|
+
let removed = null;
|
|
158
|
+
const src = this._getVertex(edge.src);
|
|
159
|
+
const dest = this._getVertex(edge.dest);
|
|
234
160
|
if (src && dest) {
|
|
235
|
-
|
|
161
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
236
162
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
237
|
-
(0, utils_1.arrayRemove)(srcOutEdges,
|
|
163
|
+
(0, utils_1.arrayRemove)(srcOutEdges, (edge) => edge.src === src.id);
|
|
238
164
|
}
|
|
239
|
-
|
|
165
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
240
166
|
if (destInEdges && destInEdges.length > 0) {
|
|
241
|
-
removed = (0, utils_1.arrayRemove)(destInEdges,
|
|
167
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, (edge) => edge.dest === dest.id)[0];
|
|
242
168
|
}
|
|
243
169
|
}
|
|
244
170
|
return removed;
|
|
245
|
-
}
|
|
171
|
+
}
|
|
246
172
|
/**
|
|
247
173
|
* The function removes edges between two vertices and returns the removed edges.
|
|
248
174
|
* @param {VertexId | V} v1 - The parameter `v1` can be either a `VertexId` or a `V`. A `VertexId` represents the
|
|
@@ -251,121 +177,110 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
251
177
|
* the second vertex in the edge that needs to be removed.
|
|
252
178
|
* @returns an array of removed edges (E[]).
|
|
253
179
|
*/
|
|
254
|
-
|
|
255
|
-
|
|
180
|
+
removeEdgesBetween(v1, v2) {
|
|
181
|
+
const removed = [];
|
|
256
182
|
if (v1 && v2) {
|
|
257
|
-
|
|
258
|
-
|
|
183
|
+
const v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
|
|
184
|
+
const v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
|
|
259
185
|
v1ToV2 && removed.push(v1ToV2);
|
|
260
186
|
v2ToV1 && removed.push(v2ToV1);
|
|
261
187
|
}
|
|
262
188
|
return removed;
|
|
263
|
-
}
|
|
189
|
+
}
|
|
264
190
|
/**
|
|
265
191
|
* The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
|
|
266
192
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
267
193
|
* (`VertexId`).
|
|
268
194
|
* @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
|
|
269
195
|
*/
|
|
270
|
-
|
|
271
|
-
|
|
196
|
+
incomingEdgesOf(vertexOrId) {
|
|
197
|
+
const target = this._getVertex(vertexOrId);
|
|
272
198
|
if (target) {
|
|
273
199
|
return this.inEdgeMap.get(target) || [];
|
|
274
200
|
}
|
|
275
201
|
return [];
|
|
276
|
-
}
|
|
202
|
+
}
|
|
277
203
|
/**
|
|
278
204
|
* The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
|
|
279
205
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
|
|
280
206
|
* (`VertexId`).
|
|
281
207
|
* @returns The method `outgoingEdgesOf` returns an array of edges (`E[]`).
|
|
282
208
|
*/
|
|
283
|
-
|
|
284
|
-
|
|
209
|
+
outgoingEdgesOf(vertexOrId) {
|
|
210
|
+
const target = this._getVertex(vertexOrId);
|
|
285
211
|
if (target) {
|
|
286
212
|
return this._outEdgeMap.get(target) || [];
|
|
287
213
|
}
|
|
288
214
|
return [];
|
|
289
|
-
}
|
|
215
|
+
}
|
|
290
216
|
/**
|
|
291
217
|
* The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
|
|
292
218
|
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
293
219
|
* @returns The sum of the out-degree and in-degree of the specified vertex or vertex ID.
|
|
294
220
|
*/
|
|
295
|
-
|
|
221
|
+
degreeOf(vertexOrId) {
|
|
296
222
|
return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
|
|
297
|
-
}
|
|
223
|
+
}
|
|
298
224
|
/**
|
|
299
225
|
* The function "inDegreeOf" returns the number of incoming edges for a given vertex.
|
|
300
226
|
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
301
227
|
* @returns The number of incoming edges of the specified vertex or vertex ID.
|
|
302
228
|
*/
|
|
303
|
-
|
|
229
|
+
inDegreeOf(vertexOrId) {
|
|
304
230
|
return this.incomingEdgesOf(vertexOrId).length;
|
|
305
|
-
}
|
|
231
|
+
}
|
|
306
232
|
/**
|
|
307
233
|
* The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
|
|
308
234
|
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
309
235
|
* @returns The number of outgoing edges from the specified vertex or vertex ID.
|
|
310
236
|
*/
|
|
311
|
-
|
|
237
|
+
outDegreeOf(vertexOrId) {
|
|
312
238
|
return this.outgoingEdgesOf(vertexOrId).length;
|
|
313
|
-
}
|
|
239
|
+
}
|
|
314
240
|
/**
|
|
315
241
|
* The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
|
|
316
242
|
* @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
|
|
317
243
|
* @returns The function `edgesOf` returns an array of edges.
|
|
318
244
|
*/
|
|
319
|
-
|
|
320
|
-
return
|
|
321
|
-
}
|
|
245
|
+
edgesOf(vertexOrId) {
|
|
246
|
+
return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
|
|
247
|
+
}
|
|
322
248
|
/**
|
|
323
249
|
* The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
|
|
324
250
|
* @param {E} e - The parameter "e" is of type E, which represents an edge in a graph.
|
|
325
251
|
* @returns either a vertex object (V) or null.
|
|
326
252
|
*/
|
|
327
|
-
|
|
253
|
+
getEdgeSrc(e) {
|
|
328
254
|
return this._getVertex(e.src);
|
|
329
|
-
}
|
|
255
|
+
}
|
|
330
256
|
/**
|
|
331
257
|
* The function "getEdgeDest" returns the destination vertex of an edge.
|
|
332
258
|
* @param {E} e - The parameter "e" is of type "E", which represents an edge in a graph.
|
|
333
259
|
* @returns either a vertex object of type V or null.
|
|
334
260
|
*/
|
|
335
|
-
|
|
261
|
+
getEdgeDest(e) {
|
|
336
262
|
return this._getVertex(e.dest);
|
|
337
|
-
}
|
|
263
|
+
}
|
|
338
264
|
/**
|
|
339
265
|
* The function `getDestinations` returns an array of destination vertices connected to a given vertex.
|
|
340
266
|
* @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
|
|
341
267
|
* find the destinations. It can be either a `V` object, a `VertexId` value, or `null`.
|
|
342
268
|
* @returns an array of vertices (V[]).
|
|
343
269
|
*/
|
|
344
|
-
|
|
345
|
-
var e_1, _a;
|
|
270
|
+
getDestinations(vertex) {
|
|
346
271
|
if (vertex === null) {
|
|
347
272
|
return [];
|
|
348
273
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
if (child) {
|
|
356
|
-
destinations.push(child);
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
361
|
-
finally {
|
|
362
|
-
try {
|
|
363
|
-
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);
|
|
364
280
|
}
|
|
365
|
-
finally { if (e_1) throw e_1.error; }
|
|
366
281
|
}
|
|
367
282
|
return destinations;
|
|
368
|
-
}
|
|
283
|
+
}
|
|
369
284
|
/**
|
|
370
285
|
* The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
|
|
371
286
|
* in the sorted order, or null if the graph contains a cycle.
|
|
@@ -374,118 +289,74 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
374
289
|
* specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
|
|
375
290
|
* @returns an array of vertices or vertex IDs in topological order. If there is a cycle in the graph, it returns null.
|
|
376
291
|
*/
|
|
377
|
-
|
|
378
|
-
var e_2, _a, e_3, _b;
|
|
379
|
-
var _this = this;
|
|
292
|
+
topologicalSort(propertyName) {
|
|
380
293
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
381
294
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
382
295
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
var entry = _d.value;
|
|
387
|
-
statusMap.set(entry[1], 0);
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
391
|
-
finally {
|
|
392
|
-
try {
|
|
393
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
394
|
-
}
|
|
395
|
-
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);
|
|
396
299
|
}
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
var e_4, _a;
|
|
300
|
+
let sorted = [];
|
|
301
|
+
let hasCycle = false;
|
|
302
|
+
const dfs = (cur) => {
|
|
401
303
|
statusMap.set(cur, 1);
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
if (childStatus === 0) {
|
|
408
|
-
dfs(child);
|
|
409
|
-
}
|
|
410
|
-
else if (childStatus === 1) {
|
|
411
|
-
hasCycle = true;
|
|
412
|
-
}
|
|
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);
|
|
413
309
|
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
finally {
|
|
417
|
-
try {
|
|
418
|
-
if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
|
|
310
|
+
else if (childStatus === 1) {
|
|
311
|
+
hasCycle = true;
|
|
419
312
|
}
|
|
420
|
-
finally { if (e_4) throw e_4.error; }
|
|
421
313
|
}
|
|
422
314
|
statusMap.set(cur, 2);
|
|
423
315
|
sorted.push(cur);
|
|
424
316
|
};
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
if (statusMap.get(entry[1]) === 0) {
|
|
429
|
-
dfs(entry[1]);
|
|
430
|
-
}
|
|
317
|
+
for (const entry of this.vertices) {
|
|
318
|
+
if (statusMap.get(entry[1]) === 0) {
|
|
319
|
+
dfs(entry[1]);
|
|
431
320
|
}
|
|
432
321
|
}
|
|
433
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
434
|
-
finally {
|
|
435
|
-
try {
|
|
436
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
437
|
-
}
|
|
438
|
-
finally { if (e_3) throw e_3.error; }
|
|
439
|
-
}
|
|
440
322
|
if (hasCycle)
|
|
441
323
|
return null;
|
|
442
324
|
if (propertyName === 'id')
|
|
443
|
-
sorted = sorted.map(
|
|
325
|
+
sorted = sorted.map(vertex => vertex instanceof DirectedVertex ? vertex.id : vertex);
|
|
444
326
|
return sorted.reverse();
|
|
445
|
-
}
|
|
327
|
+
}
|
|
446
328
|
/**
|
|
447
329
|
* The `edgeSet` function returns an array of all the edges in the graph.
|
|
448
330
|
* @returns The `edgeSet()` method returns an array of edges (`E[]`).
|
|
449
331
|
*/
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
this._outEdgeMap.forEach(
|
|
453
|
-
edges =
|
|
332
|
+
edgeSet() {
|
|
333
|
+
let edges = [];
|
|
334
|
+
this._outEdgeMap.forEach(outEdges => {
|
|
335
|
+
edges = [...edges, ...outEdges];
|
|
454
336
|
});
|
|
455
337
|
return edges;
|
|
456
|
-
}
|
|
338
|
+
}
|
|
457
339
|
/**
|
|
458
340
|
* The function `getNeighbors` returns an array of neighboring vertices of a given vertex or vertex ID in a graph.
|
|
459
341
|
* @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
|
|
460
342
|
* (`VertexId`).
|
|
461
343
|
* @returns an array of vertices (V[]).
|
|
462
344
|
*/
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
var vertex = this._getVertex(vertexOrId);
|
|
345
|
+
getNeighbors(vertexOrId) {
|
|
346
|
+
const neighbors = [];
|
|
347
|
+
const vertex = this._getVertex(vertexOrId);
|
|
467
348
|
if (vertex) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
if (neighbor) {
|
|
475
|
-
neighbors.push(neighbor);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
catch (e_5_1) { e_5 = { error: e_5_1 }; }
|
|
480
|
-
finally {
|
|
481
|
-
try {
|
|
482
|
-
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);
|
|
483
355
|
}
|
|
484
|
-
finally { if (e_5) throw e_5.error; }
|
|
485
356
|
}
|
|
486
357
|
}
|
|
487
358
|
return neighbors;
|
|
488
|
-
}
|
|
359
|
+
}
|
|
489
360
|
/**
|
|
490
361
|
* The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
|
|
491
362
|
* otherwise it returns null.
|
|
@@ -493,19 +364,19 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
493
364
|
* @returns The function `getEndsOfEdge` returns an array containing two vertices `[V, V]` if the edge exists in the
|
|
494
365
|
* graph. If the edge does not exist, it returns `null`.
|
|
495
366
|
*/
|
|
496
|
-
|
|
367
|
+
getEndsOfEdge(edge) {
|
|
497
368
|
if (!this.hasEdge(edge.src, edge.dest)) {
|
|
498
369
|
return null;
|
|
499
370
|
}
|
|
500
|
-
|
|
501
|
-
|
|
371
|
+
const v1 = this._getVertex(edge.src);
|
|
372
|
+
const v2 = this._getVertex(edge.dest);
|
|
502
373
|
if (v1 && v2) {
|
|
503
374
|
return [v1, v2];
|
|
504
375
|
}
|
|
505
376
|
else {
|
|
506
377
|
return null;
|
|
507
378
|
}
|
|
508
|
-
}
|
|
379
|
+
}
|
|
509
380
|
/**
|
|
510
381
|
* The function `_addEdgeOnly` adds an edge to a graph if the source and destination vertices exist.
|
|
511
382
|
* @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in a graph. It is the edge that
|
|
@@ -513,22 +384,22 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
513
384
|
* @returns a boolean value. It returns true if the edge was successfully added to the graph, and false if either the
|
|
514
385
|
* source or destination vertex does not exist in the graph.
|
|
515
386
|
*/
|
|
516
|
-
|
|
387
|
+
_addEdgeOnly(edge) {
|
|
517
388
|
if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
|
|
518
389
|
return false;
|
|
519
390
|
}
|
|
520
|
-
|
|
521
|
-
|
|
391
|
+
const srcVertex = this._getVertex(edge.src);
|
|
392
|
+
const destVertex = this._getVertex(edge.dest);
|
|
522
393
|
// TODO after no-non-null-assertion not ensure the logic
|
|
523
394
|
if (srcVertex && destVertex) {
|
|
524
|
-
|
|
395
|
+
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
525
396
|
if (srcOutEdges) {
|
|
526
397
|
srcOutEdges.push(edge);
|
|
527
398
|
}
|
|
528
399
|
else {
|
|
529
400
|
this._outEdgeMap.set(srcVertex, [edge]);
|
|
530
401
|
}
|
|
531
|
-
|
|
402
|
+
const destInEdges = this._inEdgeMap.get(destVertex);
|
|
532
403
|
if (destInEdges) {
|
|
533
404
|
destInEdges.push(edge);
|
|
534
405
|
}
|
|
@@ -540,13 +411,12 @@ var DirectedGraph = /** @class */ (function (_super) {
|
|
|
540
411
|
else {
|
|
541
412
|
return false;
|
|
542
413
|
}
|
|
543
|
-
}
|
|
544
|
-
|
|
414
|
+
}
|
|
415
|
+
_setOutEdgeMap(value) {
|
|
545
416
|
this._outEdgeMap = value;
|
|
546
|
-
}
|
|
547
|
-
|
|
417
|
+
}
|
|
418
|
+
_setInEdgeMap(value) {
|
|
548
419
|
this._inEdgeMap = value;
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
}(abstract_graph_1.AbstractGraph));
|
|
420
|
+
}
|
|
421
|
+
}
|
|
552
422
|
exports.DirectedGraph = DirectedGraph;
|