data-structure-typed 0.8.6
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/.idea/data-structure-typed.iml +12 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +2 -0
- package/dist/data-structures/binary-tree/aa-tree.js +6 -0
- package/dist/data-structures/binary-tree/avl-tree.js +231 -0
- package/dist/data-structures/binary-tree/b-tree.js +6 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +31 -0
- package/dist/data-structures/binary-tree/binary-tree.js +992 -0
- package/dist/data-structures/binary-tree/bst.js +431 -0
- package/dist/data-structures/binary-tree/index.js +20 -0
- package/dist/data-structures/binary-tree/rb-tree.js +6 -0
- package/dist/data-structures/binary-tree/segment-tree.js +151 -0
- package/dist/data-structures/binary-tree/splay-tree.js +6 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +16 -0
- package/dist/data-structures/binary-tree/two-three-tree.js +6 -0
- package/dist/data-structures/graph/abstract-graph.js +648 -0
- package/dist/data-structures/graph/directed-graph.js +268 -0
- package/dist/data-structures/graph/index.js +19 -0
- package/dist/data-structures/graph/undirected-graph.js +142 -0
- package/dist/data-structures/hash/coordinate-map.js +24 -0
- package/dist/data-structures/hash/coordinate-set.js +21 -0
- package/dist/data-structures/hash/hash-table.js +2 -0
- package/dist/data-structures/hash/index.js +17 -0
- package/dist/data-structures/hash/pair.js +2 -0
- package/dist/data-structures/hash/tree-map.js +2 -0
- package/dist/data-structures/hash/tree-set.js +2 -0
- package/dist/data-structures/heap/heap.js +114 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.js +22 -0
- package/dist/data-structures/heap/min-heap.js +22 -0
- package/dist/data-structures/index.js +25 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +259 -0
- package/dist/data-structures/linked-list/index.js +18 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +660 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/data-structures/matrix/index.js +19 -0
- package/dist/data-structures/matrix/matrix.js +14 -0
- package/dist/data-structures/matrix/matrix2d.js +119 -0
- package/dist/data-structures/matrix/navigator.js +78 -0
- package/dist/data-structures/matrix/vector2d.js +161 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +15 -0
- package/dist/data-structures/priority-queue/priority-queue.js +174 -0
- package/dist/data-structures/queue/deque.js +132 -0
- package/dist/data-structures/queue/index.js +17 -0
- package/dist/data-structures/queue/queue.js +113 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.js +97 -0
- package/dist/data-structures/trampoline.js +52 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.js +141 -0
- package/dist/index.js +17 -0
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +21 -0
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +140 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +32 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +33 -0
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +11 -0
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +126 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +51 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +24 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +8 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +7 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/pair.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.d.ts +72 -0
- package/dist/types/data-structures/heap/index.d.ts +3 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +14 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +14 -0
- package/dist/types/data-structures/index.d.ts +9 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +59 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +358 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.d.ts +3 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +25 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +31 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +74 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +4 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +36 -0
- package/dist/types/data-structures/queue/deque.d.ts +37 -0
- package/dist/types/data-structures/queue/index.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +76 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +69 -0
- package/dist/types/data-structures/trampoline.d.ts +25 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +28 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +17 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/utils.d.ts +46 -0
- package/dist/types/utils.d.ts +122 -0
- package/dist/types/utils.js +53 -0
- package/dist/utils.js +569 -0
- package/package.json +75 -0
- package/src/data-structures/binary-tree/aa-tree.ts +3 -0
- package/src/data-structures/binary-tree/avl-tree.ts +232 -0
- package/src/data-structures/binary-tree/b-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +33 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1088 -0
- package/src/data-structures/binary-tree/bst.ts +404 -0
- package/src/data-structures/binary-tree/index.ts +4 -0
- package/src/data-structures/binary-tree/rb-tree.ts +3 -0
- package/src/data-structures/binary-tree/segment-tree.ts +164 -0
- package/src/data-structures/binary-tree/splay-tree.ts +3 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +21 -0
- package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
- package/src/data-structures/graph/abstract-graph.ts +789 -0
- package/src/data-structures/graph/directed-graph.ts +322 -0
- package/src/data-structures/graph/index.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +154 -0
- package/src/data-structures/hash/coordinate-map.ts +24 -0
- package/src/data-structures/hash/coordinate-set.ts +20 -0
- package/src/data-structures/hash/hash-table.ts +1 -0
- package/src/data-structures/hash/index.ts +1 -0
- package/src/data-structures/hash/pair.ts +1 -0
- package/src/data-structures/hash/tree-map.ts +1 -0
- package/src/data-structures/hash/tree-set.ts +1 -0
- package/src/data-structures/heap/heap.ts +136 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +22 -0
- package/src/data-structures/heap/min-heap.ts +24 -0
- package/src/data-structures/index.ts +10 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +258 -0
- package/src/data-structures/linked-list/index.ts +2 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +750 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/data-structures/matrix/index.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +13 -0
- package/src/data-structures/matrix/matrix2d.ts +125 -0
- package/src/data-structures/matrix/navigator.ts +99 -0
- package/src/data-structures/matrix/vector2d.ts +189 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -0
- package/src/data-structures/priority-queue/priority-queue.ts +208 -0
- package/src/data-structures/queue/deque.ts +139 -0
- package/src/data-structures/queue/index.ts +1 -0
- package/src/data-structures/queue/queue.ts +123 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +104 -0
- package/src/data-structures/trampoline.ts +91 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +153 -0
- package/src/index.ts +1 -0
- package/src/types/index.ts +1 -0
- package/src/types/patches/index.d.ts +0 -0
- package/src/types/utils.ts +158 -0
- package/src/utils.ts +605 -0
- package/tsconfig.json +52 -0
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
|
|
4
|
+
const utils_1 = require("../../utils");
|
|
5
|
+
const abstract_graph_1 = require("./abstract-graph");
|
|
6
|
+
class DirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
7
|
+
constructor(id) {
|
|
8
|
+
super(id);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.DirectedVertex = DirectedVertex;
|
|
12
|
+
class DirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
13
|
+
constructor(src, dest, weight) {
|
|
14
|
+
super(weight);
|
|
15
|
+
this._src = src;
|
|
16
|
+
this._dest = dest;
|
|
17
|
+
}
|
|
18
|
+
get src() {
|
|
19
|
+
return this._src;
|
|
20
|
+
}
|
|
21
|
+
set src(v) {
|
|
22
|
+
this._src = v;
|
|
23
|
+
}
|
|
24
|
+
get dest() {
|
|
25
|
+
return this._dest;
|
|
26
|
+
}
|
|
27
|
+
set dest(v) {
|
|
28
|
+
this._dest = v;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.DirectedEdge = DirectedEdge;
|
|
32
|
+
// Strongly connected, One direction connected, Weakly connected
|
|
33
|
+
class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
34
|
+
constructor() {
|
|
35
|
+
super();
|
|
36
|
+
this._outEdgeMap = new Map();
|
|
37
|
+
this._inEdgeMap = new Map();
|
|
38
|
+
}
|
|
39
|
+
getEdge(srcOrId, destOrId) {
|
|
40
|
+
let edges = [];
|
|
41
|
+
if (srcOrId !== null && destOrId !== null) {
|
|
42
|
+
const src = this.getVertex(srcOrId);
|
|
43
|
+
const dest = this.getVertex(destOrId);
|
|
44
|
+
if (src && dest) {
|
|
45
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
46
|
+
if (srcOutEdges) {
|
|
47
|
+
edges = srcOutEdges.filter(edge => edge.dest === dest.id);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return edges[0] || null;
|
|
52
|
+
}
|
|
53
|
+
addEdge(edge) {
|
|
54
|
+
if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
const srcVertex = this.getVertex(edge.src);
|
|
58
|
+
const destVertex = this.getVertex(edge.dest);
|
|
59
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
60
|
+
if (srcVertex && destVertex) {
|
|
61
|
+
const srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
62
|
+
if (srcOutEdges) {
|
|
63
|
+
srcOutEdges.push(edge);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
this._outEdgeMap.set(srcVertex, [edge]);
|
|
67
|
+
}
|
|
68
|
+
const destInEdges = this._inEdgeMap.get(destVertex);
|
|
69
|
+
if (destInEdges) {
|
|
70
|
+
destInEdges.push(edge);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
this._inEdgeMap.set(destVertex, [edge]);
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
removeEdgeBetween(srcOrId, destOrId) {
|
|
82
|
+
const src = this.getVertex(srcOrId);
|
|
83
|
+
const dest = this.getVertex(destOrId);
|
|
84
|
+
let removed = null;
|
|
85
|
+
if (!src || !dest) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
89
|
+
if (srcOutEdges) {
|
|
90
|
+
(0, utils_1.arrayRemove)(srcOutEdges, edge => edge.dest === dest.id);
|
|
91
|
+
}
|
|
92
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
93
|
+
if (destInEdges) {
|
|
94
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, edge => edge.src === src.id)[0] || null;
|
|
95
|
+
}
|
|
96
|
+
return removed;
|
|
97
|
+
}
|
|
98
|
+
removeEdge(edge) {
|
|
99
|
+
let removed = null;
|
|
100
|
+
const src = this.getVertex(edge.src);
|
|
101
|
+
const dest = this.getVertex(edge.dest);
|
|
102
|
+
if (src && dest) {
|
|
103
|
+
const srcOutEdges = this._outEdgeMap.get(src);
|
|
104
|
+
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
105
|
+
(0, utils_1.arrayRemove)(srcOutEdges, edge => edge.src === src.id);
|
|
106
|
+
}
|
|
107
|
+
const destInEdges = this._inEdgeMap.get(dest);
|
|
108
|
+
if (destInEdges && destInEdges.length > 0) {
|
|
109
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, edge => edge.dest === dest.id)[0];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return removed;
|
|
113
|
+
}
|
|
114
|
+
removeAllEdges(src, dest) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
incomingEdgesOf(vertexOrId) {
|
|
118
|
+
const target = this.getVertex(vertexOrId);
|
|
119
|
+
if (target) {
|
|
120
|
+
return this._inEdgeMap.get(target) || [];
|
|
121
|
+
}
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
outgoingEdgesOf(vertexOrId) {
|
|
125
|
+
const target = this.getVertex(vertexOrId);
|
|
126
|
+
if (target) {
|
|
127
|
+
return this._outEdgeMap.get(target) || [];
|
|
128
|
+
}
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
degreeOf(vertexOrId) {
|
|
132
|
+
return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
|
|
133
|
+
}
|
|
134
|
+
inDegreeOf(vertexOrId) {
|
|
135
|
+
return this.incomingEdgesOf(vertexOrId).length;
|
|
136
|
+
}
|
|
137
|
+
outDegreeOf(vertexOrId) {
|
|
138
|
+
return this.outgoingEdgesOf(vertexOrId).length;
|
|
139
|
+
}
|
|
140
|
+
edgesOf(vertexOrId) {
|
|
141
|
+
return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
|
|
142
|
+
}
|
|
143
|
+
getEdgeSrc(e) {
|
|
144
|
+
return this.getVertex(e.src);
|
|
145
|
+
}
|
|
146
|
+
getEdgeDest(e) {
|
|
147
|
+
return this.getVertex(e.dest);
|
|
148
|
+
}
|
|
149
|
+
getDestinations(vertex) {
|
|
150
|
+
if (vertex === null) {
|
|
151
|
+
return [];
|
|
152
|
+
}
|
|
153
|
+
const destinations = [];
|
|
154
|
+
const outgoingEdges = this.outgoingEdgesOf(vertex);
|
|
155
|
+
for (const outEdge of outgoingEdges) {
|
|
156
|
+
const child = this.getEdgeDest(outEdge);
|
|
157
|
+
if (child) {
|
|
158
|
+
destinations.push(child);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return destinations;
|
|
162
|
+
}
|
|
163
|
+
/**--- start find cycles --- */
|
|
164
|
+
/**
|
|
165
|
+
* when stored with adjacency list time: O(V+E)
|
|
166
|
+
* when stored with adjacency matrix time: O(V^2)
|
|
167
|
+
*/
|
|
168
|
+
topologicalSort() {
|
|
169
|
+
// vector<vector<int>> g;
|
|
170
|
+
// vector<int> color;
|
|
171
|
+
// int last;
|
|
172
|
+
// bool hasCycle;
|
|
173
|
+
//
|
|
174
|
+
// bool topo_sort() {
|
|
175
|
+
// int n = g.size();
|
|
176
|
+
// vector<int> degree(n, 0);
|
|
177
|
+
// queue<int> q;
|
|
178
|
+
// for (int i = 0; i < n; i++) {
|
|
179
|
+
// degree[i] = g[i].size();
|
|
180
|
+
// if (degree[i] <= 1) {
|
|
181
|
+
// q.push(i);
|
|
182
|
+
// }
|
|
183
|
+
// }
|
|
184
|
+
// int cnt = 0;
|
|
185
|
+
// while (!q.empty()) {
|
|
186
|
+
// cnt++;
|
|
187
|
+
// int root = q.front();
|
|
188
|
+
// q.pop();
|
|
189
|
+
// for (auto child : g[root]) {
|
|
190
|
+
// degree[child]--;
|
|
191
|
+
// if (degree[child] == 1) {
|
|
192
|
+
// q.push(child);
|
|
193
|
+
// }
|
|
194
|
+
// }
|
|
195
|
+
// }
|
|
196
|
+
// return (cnt != n);
|
|
197
|
+
// }
|
|
198
|
+
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
199
|
+
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
200
|
+
const statusMap = new Map();
|
|
201
|
+
for (const entry of this._vertices) {
|
|
202
|
+
statusMap.set(entry[1], 0);
|
|
203
|
+
}
|
|
204
|
+
const sorted = [];
|
|
205
|
+
let hasCycle = false;
|
|
206
|
+
const dfs = (cur) => {
|
|
207
|
+
statusMap.set(cur, 1);
|
|
208
|
+
const children = this.getDestinations(cur);
|
|
209
|
+
for (const child of children) {
|
|
210
|
+
const childStatus = statusMap.get(child);
|
|
211
|
+
if (childStatus === 0) {
|
|
212
|
+
dfs(child);
|
|
213
|
+
}
|
|
214
|
+
else if (childStatus === 1) {
|
|
215
|
+
hasCycle = true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
statusMap.set(cur, 2);
|
|
219
|
+
sorted.push(cur);
|
|
220
|
+
};
|
|
221
|
+
for (const entry of this._vertices) {
|
|
222
|
+
if (statusMap.get(entry[1]) === 0) {
|
|
223
|
+
dfs(entry[1]);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (hasCycle) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
return sorted.reverse();
|
|
230
|
+
}
|
|
231
|
+
/**--- end find cycles --- */
|
|
232
|
+
edgeSet() {
|
|
233
|
+
let edges = [];
|
|
234
|
+
this._outEdgeMap.forEach(outEdges => {
|
|
235
|
+
edges = [...edges, ...outEdges];
|
|
236
|
+
});
|
|
237
|
+
return edges;
|
|
238
|
+
}
|
|
239
|
+
getNeighbors(vertexOrId) {
|
|
240
|
+
const neighbors = [];
|
|
241
|
+
const vertex = this.getVertex(vertexOrId);
|
|
242
|
+
if (vertex) {
|
|
243
|
+
const outEdges = this.outgoingEdgesOf(vertex);
|
|
244
|
+
for (const outEdge of outEdges) {
|
|
245
|
+
const neighbor = this.getVertex(outEdge.dest);
|
|
246
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
247
|
+
if (neighbor) {
|
|
248
|
+
neighbors.push(neighbor);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return neighbors;
|
|
253
|
+
}
|
|
254
|
+
getEndsOfEdge(edge) {
|
|
255
|
+
if (!this.containsEdge(edge.src, edge.dest)) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
const v1 = this.getVertex(edge.src);
|
|
259
|
+
const v2 = this.getVertex(edge.dest);
|
|
260
|
+
if (v1 && v2) {
|
|
261
|
+
return [v1, v2];
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
exports.DirectedGraph = DirectedGraph;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstract-graph"), exports);
|
|
18
|
+
__exportStar(require("./directed-graph"), exports);
|
|
19
|
+
__exportStar(require("./undirected-graph"), exports);
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = void 0;
|
|
4
|
+
const utils_1 = require("../../utils");
|
|
5
|
+
const abstract_graph_1 = require("./abstract-graph");
|
|
6
|
+
class UndirectedVertex extends abstract_graph_1.AbstractVertex {
|
|
7
|
+
constructor(id) {
|
|
8
|
+
super(id);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.UndirectedVertex = UndirectedVertex;
|
|
12
|
+
class UndirectedEdge extends abstract_graph_1.AbstractEdge {
|
|
13
|
+
get vertices() {
|
|
14
|
+
return this._vertices;
|
|
15
|
+
}
|
|
16
|
+
set vertices(v) {
|
|
17
|
+
this._vertices = v;
|
|
18
|
+
}
|
|
19
|
+
constructor(v1, v2, weight) {
|
|
20
|
+
super(weight);
|
|
21
|
+
this._vertices = [v1, v2];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.UndirectedEdge = UndirectedEdge;
|
|
25
|
+
class UndirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
26
|
+
constructor() {
|
|
27
|
+
super();
|
|
28
|
+
this._edges = new Map();
|
|
29
|
+
}
|
|
30
|
+
getEdge(v1, v2) {
|
|
31
|
+
var _a;
|
|
32
|
+
let edges = [];
|
|
33
|
+
if (v1 !== null && v2 !== null) {
|
|
34
|
+
const vertex1 = this.getVertex(v1);
|
|
35
|
+
const vertex2 = this.getVertex(v2);
|
|
36
|
+
if (vertex1 && vertex2) {
|
|
37
|
+
edges = (_a = this._edges.get(vertex1)) === null || _a === void 0 ? void 0 : _a.filter(e => e.vertices.includes(vertex2.id));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return edges ? edges[0] || null : null;
|
|
41
|
+
}
|
|
42
|
+
addEdge(edge) {
|
|
43
|
+
for (const end of edge.vertices) {
|
|
44
|
+
const endVertex = this.getVertex(end);
|
|
45
|
+
if (endVertex === null)
|
|
46
|
+
return false;
|
|
47
|
+
if (endVertex) {
|
|
48
|
+
const edges = this._edges.get(endVertex);
|
|
49
|
+
if (edges) {
|
|
50
|
+
edges.push(edge);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this._edges.set(endVertex, [edge]);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
removeEdgeBetween(v1, v2) {
|
|
60
|
+
const vertex1 = this.getVertex(v1);
|
|
61
|
+
const vertex2 = this.getVertex(v2);
|
|
62
|
+
if (!vertex1 || !vertex2) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const v1Edges = this._edges.get(vertex1);
|
|
66
|
+
let removed = null;
|
|
67
|
+
if (v1Edges) {
|
|
68
|
+
removed = (0, utils_1.arrayRemove)(v1Edges, e => e.vertices.includes(vertex2.id))[0] || null;
|
|
69
|
+
}
|
|
70
|
+
const v2Edges = this._edges.get(vertex2);
|
|
71
|
+
if (v2Edges) {
|
|
72
|
+
(0, utils_1.arrayRemove)(v2Edges, e => e.vertices.includes(vertex1.id));
|
|
73
|
+
}
|
|
74
|
+
return removed;
|
|
75
|
+
}
|
|
76
|
+
removeEdge(edge) {
|
|
77
|
+
return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
|
|
78
|
+
}
|
|
79
|
+
degreeOf(vertexOrId) {
|
|
80
|
+
var _a;
|
|
81
|
+
const vertex = this.getVertex(vertexOrId);
|
|
82
|
+
if (vertex) {
|
|
83
|
+
return ((_a = this._edges.get(vertex)) === null || _a === void 0 ? void 0 : _a.length) || 0;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
edgesOf(vertexOrId) {
|
|
90
|
+
const vertex = this.getVertex(vertexOrId);
|
|
91
|
+
if (vertex) {
|
|
92
|
+
return this._edges.get(vertex) || [];
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
edgeSet() {
|
|
99
|
+
const edgeSet = new Set();
|
|
100
|
+
this._edges.forEach(edges => {
|
|
101
|
+
edges.forEach(edge => {
|
|
102
|
+
edgeSet.add(edge);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
return [...edgeSet];
|
|
106
|
+
}
|
|
107
|
+
getEdgesOf(vertexOrId) {
|
|
108
|
+
const vertex = this.getVertex(vertexOrId);
|
|
109
|
+
if (!vertex) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
return this._edges.get(vertex) || [];
|
|
113
|
+
}
|
|
114
|
+
getNeighbors(vertexOrId) {
|
|
115
|
+
const neighbors = [];
|
|
116
|
+
const vertex = this.getVertex(vertexOrId);
|
|
117
|
+
if (vertex) {
|
|
118
|
+
const neighborEdges = this.getEdgesOf(vertex);
|
|
119
|
+
for (const edge of neighborEdges) {
|
|
120
|
+
const neighbor = this.getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
|
|
121
|
+
if (neighbor) {
|
|
122
|
+
neighbors.push(neighbor);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return neighbors;
|
|
127
|
+
}
|
|
128
|
+
getEndsOfEdge(edge) {
|
|
129
|
+
if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const v1 = this.getVertex(edge.vertices[0]);
|
|
133
|
+
const v2 = this.getVertex(edge.vertices[1]);
|
|
134
|
+
if (v1 && v2) {
|
|
135
|
+
return [v1, v2];
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.UndirectedGraph = UndirectedGraph;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CoordinateSet = void 0;
|
|
4
|
+
class CoordinateSet extends Map {
|
|
5
|
+
constructor(joint) {
|
|
6
|
+
super();
|
|
7
|
+
this._joint = '_';
|
|
8
|
+
if (joint !== undefined)
|
|
9
|
+
this._joint = joint;
|
|
10
|
+
}
|
|
11
|
+
has(key) {
|
|
12
|
+
return super.has(key.join(this._joint));
|
|
13
|
+
}
|
|
14
|
+
set(key, value) {
|
|
15
|
+
return super.set(key.join(this._joint), value);
|
|
16
|
+
}
|
|
17
|
+
get(key) {
|
|
18
|
+
return super.get(key.join(this._joint));
|
|
19
|
+
}
|
|
20
|
+
delete(key) {
|
|
21
|
+
return super.delete(key.join(this._joint));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.CoordinateSet = CoordinateSet;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CoordinateSet = void 0;
|
|
4
|
+
class CoordinateSet extends Set {
|
|
5
|
+
constructor(joint) {
|
|
6
|
+
super();
|
|
7
|
+
this._joint = '_';
|
|
8
|
+
if (joint !== undefined)
|
|
9
|
+
this._joint = joint;
|
|
10
|
+
}
|
|
11
|
+
has(value) {
|
|
12
|
+
return super.has(value.join(this._joint));
|
|
13
|
+
}
|
|
14
|
+
add(value) {
|
|
15
|
+
return super.add(value.join(this._joint));
|
|
16
|
+
}
|
|
17
|
+
delete(value) {
|
|
18
|
+
return super.delete(value.join(this._joint));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.CoordinateSet = CoordinateSet;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./hash-table"), exports);
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Heap = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @copyright 2021 Pablo Rios <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT
|
|
7
|
+
*
|
|
8
|
+
* @abstract
|
|
9
|
+
* @class Heap
|
|
10
|
+
*/
|
|
11
|
+
class Heap {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a priority queue
|
|
14
|
+
* @public
|
|
15
|
+
* @params {object} [options]
|
|
16
|
+
*/
|
|
17
|
+
constructor(options) {
|
|
18
|
+
if (options) {
|
|
19
|
+
const { priority } = options;
|
|
20
|
+
if (priority !== undefined && typeof priority !== 'function') {
|
|
21
|
+
throw new Error('.constructor expects a valid priority function');
|
|
22
|
+
}
|
|
23
|
+
this._priorityCb = priority || ((el) => +el);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
this._priorityCb = (el) => +el;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* @public
|
|
31
|
+
* @returns {number}
|
|
32
|
+
*/
|
|
33
|
+
get size() {
|
|
34
|
+
return this._pq.size;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
* @returns {boolean}
|
|
39
|
+
*/
|
|
40
|
+
isEmpty() {
|
|
41
|
+
return this._pq.size < 1;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns an element with highest priority in the queue
|
|
45
|
+
* @public
|
|
46
|
+
* @returns {object}
|
|
47
|
+
*/
|
|
48
|
+
peek() {
|
|
49
|
+
return this._pq.peek();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns an element with lowest priority in the queue
|
|
53
|
+
* @public
|
|
54
|
+
* @returns {object}
|
|
55
|
+
*/
|
|
56
|
+
peekLast() {
|
|
57
|
+
return this._pq.leaf();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Adds an element to the queue
|
|
61
|
+
* @public
|
|
62
|
+
* @param {any} element
|
|
63
|
+
* @param priority
|
|
64
|
+
* @throws {Error} if priority is not a valid number
|
|
65
|
+
*/
|
|
66
|
+
offer(element, priority) {
|
|
67
|
+
if (typeof element === 'number') {
|
|
68
|
+
priority = element;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
if (priority === undefined) {
|
|
72
|
+
throw new Error('.offer expects a numeric priority');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (priority && Number.isNaN(+priority)) {
|
|
76
|
+
throw new Error('.offer expects a numeric priority');
|
|
77
|
+
}
|
|
78
|
+
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
|
|
79
|
+
throw new Error('.offer expects a numeric priority '
|
|
80
|
+
+ 'or a constructor callback that returns a number');
|
|
81
|
+
}
|
|
82
|
+
const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
|
|
83
|
+
this._pq.offer({ priority: _priority, element });
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Removes and returns an element with highest priority in the queue
|
|
88
|
+
* @public
|
|
89
|
+
* @returns {object}
|
|
90
|
+
*/
|
|
91
|
+
poll() {
|
|
92
|
+
const top = this._pq.poll();
|
|
93
|
+
if (!top) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return top;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Returns a sorted list of elements
|
|
100
|
+
* @public
|
|
101
|
+
* @returns {array}
|
|
102
|
+
*/
|
|
103
|
+
toArray() {
|
|
104
|
+
return this._pq.toArray();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Clears the queue
|
|
108
|
+
* @public
|
|
109
|
+
*/
|
|
110
|
+
clear() {
|
|
111
|
+
this._pq.clear();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.Heap = Heap;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./max-heap"), exports);
|
|
18
|
+
__exportStar(require("./min-heap"), exports);
|
|
19
|
+
__exportStar(require("./heap"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @copyright 2020 Pablo Rios <zrwusa@gmail.com>
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.MaxHeap = void 0;
|
|
8
|
+
const heap_1 = require("./heap");
|
|
9
|
+
const priority_queue_1 = require("../priority-queue");
|
|
10
|
+
/**
|
|
11
|
+
* @class MaxHeap
|
|
12
|
+
* @extends Heap
|
|
13
|
+
*/
|
|
14
|
+
class MaxHeap extends heap_1.Heap {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super(options);
|
|
17
|
+
this._pq = new priority_queue_1.PriorityQueue({
|
|
18
|
+
comparator: (a, b) => b.priority - a.priority
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.MaxHeap = MaxHeap;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @copyright 2020 Pablo Rios <zrwusa@gmail.com>
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.MinHeap = void 0;
|
|
8
|
+
const heap_1 = require("./heap");
|
|
9
|
+
const priority_queue_1 = require("../priority-queue");
|
|
10
|
+
/**
|
|
11
|
+
* @class MinHeap
|
|
12
|
+
* @extends Heap
|
|
13
|
+
*/
|
|
14
|
+
class MinHeap extends heap_1.Heap {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super(options);
|
|
17
|
+
this._pq = new priority_queue_1.PriorityQueue({
|
|
18
|
+
comparator: (a, b) => a.priority - b.priority
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.MinHeap = MinHeap;
|