data-structure-typed 0.8.18 → 0.9.16
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/modules.xml +1 -1
- package/README.md +197 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -5
- package/dist/data-structures/binary-tree/avl-tree.js +93 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +14 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +20 -32
- package/dist/data-structures/binary-tree/binary-tree.js +480 -370
- package/dist/data-structures/binary-tree/bst.d.ts +4 -8
- package/dist/data-structures/binary-tree/bst.js +152 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/segment-tree.js +91 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +31 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -33
- package/dist/data-structures/graph/abstract-graph.js +546 -311
- package/dist/data-structures/graph/directed-graph.d.ts +5 -13
- package/dist/data-structures/graph/directed-graph.js +250 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +4 -3
- package/dist/data-structures/graph/undirected-graph.js +166 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +1 -1
- package/dist/data-structures/hash/coordinate-map.js +38 -20
- package/dist/data-structures/hash/coordinate-set.js +33 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +2 -8
- package/dist/data-structures/heap/heap.js +36 -31
- package/dist/data-structures/heap/max-heap.d.ts +3 -2
- package/dist/data-structures/heap/max-heap.js +27 -9
- package/dist/data-structures/heap/min-heap.d.ts +3 -2
- package/dist/data-structures/heap/min-heap.js +27 -9
- package/dist/data-structures/index.d.ts +1 -0
- package/dist/data-structures/index.js +1 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +56 -53
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +8 -12
- package/dist/data-structures/linked-list/singly-linked-list.js +308 -174
- package/dist/data-structures/matrix/matrix.js +8 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +5 -5
- package/dist/data-structures/matrix/matrix2d.js +80 -63
- package/dist/data-structures/matrix/navigator.d.ts +2 -16
- package/dist/data-structures/matrix/navigator.js +37 -18
- package/dist/data-structures/matrix/vector2d.d.ts +18 -18
- package/dist/data-structures/matrix/vector2d.js +117 -94
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +25 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +15 -21
- package/dist/data-structures/priority-queue/priority-queue.js +159 -116
- package/dist/data-structures/queue/deque.js +82 -56
- package/dist/data-structures/queue/queue.d.ts +9 -10
- package/dist/data-structures/queue/queue.js +34 -34
- package/dist/data-structures/stack/stack.d.ts +9 -10
- package/dist/data-structures/stack/stack.js +31 -31
- package/dist/data-structures/trampoline.d.ts +14 -23
- package/dist/data-structures/trampoline.js +103 -25
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +234 -80
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.d.ts +5 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/{types → data-structures/types}/utils.d.ts +7 -1
- package/dist/{types → data-structures/types}/utils.js +20 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +6 -23
- package/dist/utils/utils.js +651 -0
- package/package.json +20 -42
- package/src/data-structures/binary-tree/avl-tree.ts +1 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +4 -4
- package/src/data-structures/binary-tree/binary-tree.ts +184 -139
- package/src/data-structures/binary-tree/bst.ts +15 -24
- 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/segment-tree.ts +20 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +1 -4
- package/src/data-structures/diagrams/README.md +7 -0
- package/src/data-structures/graph/abstract-graph.ts +58 -94
- 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 +12 -28
- package/src/data-structures/graph/undirected-graph.ts +11 -10
- package/src/data-structures/hash/coordinate-map.ts +1 -1
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +2 -11
- package/src/data-structures/heap/max-heap.ts +3 -2
- package/src/data-structures/heap/min-heap.ts +3 -2
- package/src/data-structures/index.ts +1 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +18 -32
- package/src/data-structures/matrix/matrix2d.ts +11 -11
- package/src/data-structures/matrix/navigator.ts +2 -14
- package/src/data-structures/matrix/vector2d.ts +52 -52
- package/src/data-structures/priority-queue/max-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +4 -3
- package/src/data-structures/priority-queue/priority-queue.ts +70 -78
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +12 -13
- package/src/data-structures/stack/stack.ts +12 -13
- package/src/data-structures/trampoline.ts +31 -71
- package/src/data-structures/trie/trie.ts +61 -11
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +12 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +15 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/{types → data-structures/types}/utils.ts +20 -5
- package/src/utils/index.ts +1 -0
- package/src/{utils.ts → utils/utils.ts} +32 -132
- package/tsconfig.json +9 -6
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/index.d.ts +0 -3
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/types/index.d.ts +0 -1
- package/dist/types/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/types/index.ts +0 -1
- package/src/types/patches/index.d.ts +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AbstractEdge, AbstractGraph, AbstractVertex
|
|
1
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
|
+
import type { IDirectedGraph, VertexId } from '../types';
|
|
2
3
|
export declare class DirectedVertex extends AbstractVertex {
|
|
3
4
|
constructor(id: VertexId);
|
|
4
5
|
}
|
|
@@ -11,16 +12,7 @@ export declare class DirectedEdge extends AbstractEdge {
|
|
|
11
12
|
get dest(): VertexId;
|
|
12
13
|
set dest(v: VertexId);
|
|
13
14
|
}
|
|
14
|
-
export
|
|
15
|
-
incomingEdgesOf(vertex: V): E[];
|
|
16
|
-
outgoingEdgesOf(vertex: V): E[];
|
|
17
|
-
inDegreeOf(vertexOrId: V | VertexId): number;
|
|
18
|
-
outDegreeOf(vertexOrId: V | VertexId): number;
|
|
19
|
-
getEdgeSrc(e: E): V | null;
|
|
20
|
-
getEdgeDest(e: E): V | null;
|
|
21
|
-
}
|
|
22
|
-
export type TopologicalStatus = 0 | 1 | 2;
|
|
23
|
-
export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements I_DirectedGraph<V, E> {
|
|
15
|
+
export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
|
|
24
16
|
protected _outEdgeMap: Map<V, E[]>;
|
|
25
17
|
protected _inEdgeMap: Map<V, E[]>;
|
|
26
18
|
constructor();
|
|
@@ -37,13 +29,13 @@ export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedE
|
|
|
37
29
|
edgesOf(vertexOrId: VertexId | V): E[];
|
|
38
30
|
getEdgeSrc(e: E): V | null;
|
|
39
31
|
getEdgeDest(e: E): V | null;
|
|
40
|
-
getDestinations(vertex: V | null): V[];
|
|
32
|
+
getDestinations(vertex: V | VertexId | null): V[];
|
|
41
33
|
/**--- start find cycles --- */
|
|
42
34
|
/**
|
|
43
35
|
* when stored with adjacency list time: O(V+E)
|
|
44
36
|
* when stored with adjacency matrix time: O(V^2)
|
|
45
37
|
*/
|
|
46
|
-
topologicalSort(): V[] | null;
|
|
38
|
+
topologicalSort(): (V | VertexId)[] | null;
|
|
47
39
|
/**--- end find cycles --- */
|
|
48
40
|
edgeSet(): E[];
|
|
49
41
|
getNeighbors(vertexOrId: V | VertexId): V[];
|
|
@@ -1,71 +1,137 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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);
|
|
9
23
|
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this._dest = dest;
|
|
17
|
-
}
|
|
18
|
-
get src() {
|
|
19
|
-
return this._src;
|
|
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; }
|
|
20
30
|
}
|
|
21
|
-
|
|
22
|
-
|
|
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
|
+
}
|
|
23
39
|
}
|
|
24
|
-
|
|
25
|
-
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
|
|
55
|
+
var utils_1 = require("../../utils");
|
|
56
|
+
var abstract_graph_1 = require("./abstract-graph");
|
|
57
|
+
var DirectedVertex = /** @class */ (function (_super) {
|
|
58
|
+
__extends(DirectedVertex, _super);
|
|
59
|
+
function DirectedVertex(id) {
|
|
60
|
+
return _super.call(this, id) || this;
|
|
26
61
|
}
|
|
27
|
-
|
|
28
|
-
|
|
62
|
+
return DirectedVertex;
|
|
63
|
+
}(abstract_graph_1.AbstractVertex));
|
|
64
|
+
exports.DirectedVertex = DirectedVertex;
|
|
65
|
+
var DirectedEdge = /** @class */ (function (_super) {
|
|
66
|
+
__extends(DirectedEdge, _super);
|
|
67
|
+
function DirectedEdge(src, dest, weight) {
|
|
68
|
+
var _this = _super.call(this, weight) || this;
|
|
69
|
+
_this._src = src;
|
|
70
|
+
_this._dest = dest;
|
|
71
|
+
return _this;
|
|
29
72
|
}
|
|
30
|
-
|
|
73
|
+
Object.defineProperty(DirectedEdge.prototype, "src", {
|
|
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));
|
|
31
95
|
exports.DirectedEdge = DirectedEdge;
|
|
32
96
|
// Strongly connected, One direction connected, Weakly connected
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
97
|
+
var DirectedGraph = /** @class */ (function (_super) {
|
|
98
|
+
__extends(DirectedGraph, _super);
|
|
99
|
+
function DirectedGraph() {
|
|
100
|
+
var _this = _super.call(this) || this;
|
|
101
|
+
_this._outEdgeMap = new Map();
|
|
102
|
+
_this._inEdgeMap = new Map();
|
|
103
|
+
return _this;
|
|
38
104
|
}
|
|
39
|
-
getEdge(srcOrId, destOrId) {
|
|
40
|
-
|
|
105
|
+
DirectedGraph.prototype.getEdge = function (srcOrId, destOrId) {
|
|
106
|
+
var edges = [];
|
|
41
107
|
if (srcOrId !== null && destOrId !== null) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (src &&
|
|
45
|
-
|
|
108
|
+
var src = this.getVertex(srcOrId);
|
|
109
|
+
var dest_1 = this.getVertex(destOrId);
|
|
110
|
+
if (src && dest_1) {
|
|
111
|
+
var srcOutEdges = this._outEdgeMap.get(src);
|
|
46
112
|
if (srcOutEdges) {
|
|
47
|
-
edges = srcOutEdges.filter(edge
|
|
113
|
+
edges = srcOutEdges.filter(function (edge) { return edge.dest === dest_1.id; });
|
|
48
114
|
}
|
|
49
115
|
}
|
|
50
116
|
}
|
|
51
117
|
return edges[0] || null;
|
|
52
|
-
}
|
|
53
|
-
addEdge(edge) {
|
|
118
|
+
};
|
|
119
|
+
DirectedGraph.prototype.addEdge = function (edge) {
|
|
54
120
|
if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
|
|
55
121
|
return false;
|
|
56
122
|
}
|
|
57
|
-
|
|
58
|
-
|
|
123
|
+
var srcVertex = this.getVertex(edge.src);
|
|
124
|
+
var destVertex = this.getVertex(edge.dest);
|
|
59
125
|
// TODO after no-non-null-assertion not ensure the logic
|
|
60
126
|
if (srcVertex && destVertex) {
|
|
61
|
-
|
|
127
|
+
var srcOutEdges = this._outEdgeMap.get(srcVertex);
|
|
62
128
|
if (srcOutEdges) {
|
|
63
129
|
srcOutEdges.push(edge);
|
|
64
130
|
}
|
|
65
131
|
else {
|
|
66
132
|
this._outEdgeMap.set(srcVertex, [edge]);
|
|
67
133
|
}
|
|
68
|
-
|
|
134
|
+
var destInEdges = this._inEdgeMap.get(destVertex);
|
|
69
135
|
if (destInEdges) {
|
|
70
136
|
destInEdges.push(edge);
|
|
71
137
|
}
|
|
@@ -77,95 +143,108 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
77
143
|
else {
|
|
78
144
|
return false;
|
|
79
145
|
}
|
|
80
|
-
}
|
|
81
|
-
removeEdgeBetween(srcOrId, destOrId) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
146
|
+
};
|
|
147
|
+
DirectedGraph.prototype.removeEdgeBetween = function (srcOrId, destOrId) {
|
|
148
|
+
var src = this.getVertex(srcOrId);
|
|
149
|
+
var dest = this.getVertex(destOrId);
|
|
150
|
+
var removed = null;
|
|
85
151
|
if (!src || !dest) {
|
|
86
152
|
return null;
|
|
87
153
|
}
|
|
88
|
-
|
|
154
|
+
var srcOutEdges = this._outEdgeMap.get(src);
|
|
89
155
|
if (srcOutEdges) {
|
|
90
|
-
(0, utils_1.arrayRemove)(srcOutEdges, edge
|
|
156
|
+
(0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.dest === dest.id; });
|
|
91
157
|
}
|
|
92
|
-
|
|
158
|
+
var destInEdges = this._inEdgeMap.get(dest);
|
|
93
159
|
if (destInEdges) {
|
|
94
|
-
removed = (0, utils_1.arrayRemove)(destInEdges, edge
|
|
160
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, function (edge) { return edge.src === src.id; })[0] || null;
|
|
95
161
|
}
|
|
96
162
|
return removed;
|
|
97
|
-
}
|
|
98
|
-
removeEdge(edge) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
163
|
+
};
|
|
164
|
+
DirectedGraph.prototype.removeEdge = function (edge) {
|
|
165
|
+
var removed = null;
|
|
166
|
+
var src = this.getVertex(edge.src);
|
|
167
|
+
var dest = this.getVertex(edge.dest);
|
|
102
168
|
if (src && dest) {
|
|
103
|
-
|
|
169
|
+
var srcOutEdges = this._outEdgeMap.get(src);
|
|
104
170
|
if (srcOutEdges && srcOutEdges.length > 0) {
|
|
105
|
-
(0, utils_1.arrayRemove)(srcOutEdges, edge
|
|
171
|
+
(0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.src === src.id; });
|
|
106
172
|
}
|
|
107
|
-
|
|
173
|
+
var destInEdges = this._inEdgeMap.get(dest);
|
|
108
174
|
if (destInEdges && destInEdges.length > 0) {
|
|
109
|
-
removed = (0, utils_1.arrayRemove)(destInEdges, edge
|
|
175
|
+
removed = (0, utils_1.arrayRemove)(destInEdges, function (edge) { return edge.dest === dest.id; })[0];
|
|
110
176
|
}
|
|
111
177
|
}
|
|
112
178
|
return removed;
|
|
113
|
-
}
|
|
114
|
-
removeAllEdges(src, dest) {
|
|
179
|
+
};
|
|
180
|
+
DirectedGraph.prototype.removeAllEdges = function (src, dest) {
|
|
115
181
|
return [];
|
|
116
|
-
}
|
|
117
|
-
incomingEdgesOf(vertexOrId) {
|
|
118
|
-
|
|
182
|
+
};
|
|
183
|
+
DirectedGraph.prototype.incomingEdgesOf = function (vertexOrId) {
|
|
184
|
+
var target = this.getVertex(vertexOrId);
|
|
119
185
|
if (target) {
|
|
120
186
|
return this._inEdgeMap.get(target) || [];
|
|
121
187
|
}
|
|
122
188
|
return [];
|
|
123
|
-
}
|
|
124
|
-
outgoingEdgesOf(vertexOrId) {
|
|
125
|
-
|
|
189
|
+
};
|
|
190
|
+
DirectedGraph.prototype.outgoingEdgesOf = function (vertexOrId) {
|
|
191
|
+
var target = this.getVertex(vertexOrId);
|
|
126
192
|
if (target) {
|
|
127
193
|
return this._outEdgeMap.get(target) || [];
|
|
128
194
|
}
|
|
129
195
|
return [];
|
|
130
|
-
}
|
|
131
|
-
degreeOf(vertexOrId) {
|
|
196
|
+
};
|
|
197
|
+
DirectedGraph.prototype.degreeOf = function (vertexOrId) {
|
|
132
198
|
return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
|
|
133
|
-
}
|
|
134
|
-
inDegreeOf(vertexOrId) {
|
|
199
|
+
};
|
|
200
|
+
DirectedGraph.prototype.inDegreeOf = function (vertexOrId) {
|
|
135
201
|
return this.incomingEdgesOf(vertexOrId).length;
|
|
136
|
-
}
|
|
137
|
-
outDegreeOf(vertexOrId) {
|
|
202
|
+
};
|
|
203
|
+
DirectedGraph.prototype.outDegreeOf = function (vertexOrId) {
|
|
138
204
|
return this.outgoingEdgesOf(vertexOrId).length;
|
|
139
|
-
}
|
|
140
|
-
edgesOf(vertexOrId) {
|
|
141
|
-
return [
|
|
142
|
-
}
|
|
143
|
-
getEdgeSrc(e) {
|
|
205
|
+
};
|
|
206
|
+
DirectedGraph.prototype.edgesOf = function (vertexOrId) {
|
|
207
|
+
return __spreadArray(__spreadArray([], __read(this.outgoingEdgesOf(vertexOrId)), false), __read(this.incomingEdgesOf(vertexOrId)), false);
|
|
208
|
+
};
|
|
209
|
+
DirectedGraph.prototype.getEdgeSrc = function (e) {
|
|
144
210
|
return this.getVertex(e.src);
|
|
145
|
-
}
|
|
146
|
-
getEdgeDest(e) {
|
|
211
|
+
};
|
|
212
|
+
DirectedGraph.prototype.getEdgeDest = function (e) {
|
|
147
213
|
return this.getVertex(e.dest);
|
|
148
|
-
}
|
|
149
|
-
getDestinations(vertex) {
|
|
214
|
+
};
|
|
215
|
+
DirectedGraph.prototype.getDestinations = function (vertex) {
|
|
216
|
+
var e_1, _a;
|
|
150
217
|
if (vertex === null) {
|
|
151
218
|
return [];
|
|
152
219
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
220
|
+
var destinations = [];
|
|
221
|
+
var outgoingEdges = this.outgoingEdgesOf(vertex);
|
|
222
|
+
try {
|
|
223
|
+
for (var outgoingEdges_1 = __values(outgoingEdges), outgoingEdges_1_1 = outgoingEdges_1.next(); !outgoingEdges_1_1.done; outgoingEdges_1_1 = outgoingEdges_1.next()) {
|
|
224
|
+
var outEdge = outgoingEdges_1_1.value;
|
|
225
|
+
var child = this.getEdgeDest(outEdge);
|
|
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);
|
|
159
235
|
}
|
|
236
|
+
finally { if (e_1) throw e_1.error; }
|
|
160
237
|
}
|
|
161
238
|
return destinations;
|
|
162
|
-
}
|
|
239
|
+
};
|
|
163
240
|
/**--- start find cycles --- */
|
|
164
241
|
/**
|
|
165
242
|
* when stored with adjacency list time: O(V+E)
|
|
166
243
|
* when stored with adjacency matrix time: O(V^2)
|
|
167
244
|
*/
|
|
168
|
-
topologicalSort() {
|
|
245
|
+
DirectedGraph.prototype.topologicalSort = function () {
|
|
246
|
+
var e_2, _a, e_3, _b;
|
|
247
|
+
var _this = this;
|
|
169
248
|
// vector<vector<int>> g;
|
|
170
249
|
// vector<int> color;
|
|
171
250
|
// int last;
|
|
@@ -197,72 +276,115 @@ class DirectedGraph extends abstract_graph_1.AbstractGraph {
|
|
|
197
276
|
// }
|
|
198
277
|
// When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
|
|
199
278
|
// When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
279
|
+
var statusMap = new Map();
|
|
280
|
+
try {
|
|
281
|
+
for (var _c = __values(this._vertices), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
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; }
|
|
203
292
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
293
|
+
var sorted = [];
|
|
294
|
+
var hasCycle = false;
|
|
295
|
+
var dfs = function (cur) {
|
|
296
|
+
var e_4, _a;
|
|
207
297
|
statusMap.set(cur, 1);
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
298
|
+
var children = _this.getDestinations(cur);
|
|
299
|
+
try {
|
|
300
|
+
for (var children_1 = __values(children), children_1_1 = children_1.next(); !children_1_1.done; children_1_1 = children_1.next()) {
|
|
301
|
+
var child = children_1_1.value;
|
|
302
|
+
var childStatus = statusMap.get(child);
|
|
303
|
+
if (childStatus === 0) {
|
|
304
|
+
dfs(child);
|
|
305
|
+
}
|
|
306
|
+
else if (childStatus === 1) {
|
|
307
|
+
hasCycle = true;
|
|
308
|
+
}
|
|
213
309
|
}
|
|
214
|
-
|
|
215
|
-
|
|
310
|
+
}
|
|
311
|
+
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
312
|
+
finally {
|
|
313
|
+
try {
|
|
314
|
+
if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
|
|
216
315
|
}
|
|
316
|
+
finally { if (e_4) throw e_4.error; }
|
|
217
317
|
}
|
|
218
318
|
statusMap.set(cur, 2);
|
|
219
319
|
sorted.push(cur);
|
|
220
320
|
};
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
321
|
+
try {
|
|
322
|
+
for (var _e = __values(this._vertices), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
323
|
+
var entry = _f.value;
|
|
324
|
+
if (statusMap.get(entry[1]) === 0) {
|
|
325
|
+
dfs(entry[1]);
|
|
326
|
+
}
|
|
224
327
|
}
|
|
225
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);
|
|
333
|
+
}
|
|
334
|
+
finally { if (e_3) throw e_3.error; }
|
|
335
|
+
}
|
|
226
336
|
if (hasCycle) {
|
|
227
337
|
return null;
|
|
228
338
|
}
|
|
229
339
|
return sorted.reverse();
|
|
230
|
-
}
|
|
340
|
+
};
|
|
231
341
|
/**--- end find cycles --- */
|
|
232
|
-
edgeSet() {
|
|
233
|
-
|
|
234
|
-
this._outEdgeMap.forEach(outEdges
|
|
235
|
-
edges = [
|
|
342
|
+
DirectedGraph.prototype.edgeSet = function () {
|
|
343
|
+
var edges = [];
|
|
344
|
+
this._outEdgeMap.forEach(function (outEdges) {
|
|
345
|
+
edges = __spreadArray(__spreadArray([], __read(edges), false), __read(outEdges), false);
|
|
236
346
|
});
|
|
237
347
|
return edges;
|
|
238
|
-
}
|
|
239
|
-
getNeighbors(vertexOrId) {
|
|
240
|
-
|
|
241
|
-
|
|
348
|
+
};
|
|
349
|
+
DirectedGraph.prototype.getNeighbors = function (vertexOrId) {
|
|
350
|
+
var e_5, _a;
|
|
351
|
+
var neighbors = [];
|
|
352
|
+
var vertex = this.getVertex(vertexOrId);
|
|
242
353
|
if (vertex) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
354
|
+
var outEdges = this.outgoingEdgesOf(vertex);
|
|
355
|
+
try {
|
|
356
|
+
for (var outEdges_1 = __values(outEdges), outEdges_1_1 = outEdges_1.next(); !outEdges_1_1.done; outEdges_1_1 = outEdges_1.next()) {
|
|
357
|
+
var outEdge = outEdges_1_1.value;
|
|
358
|
+
var neighbor = this.getVertex(outEdge.dest);
|
|
359
|
+
// TODO after no-non-null-assertion not ensure the logic
|
|
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);
|
|
249
369
|
}
|
|
370
|
+
finally { if (e_5) throw e_5.error; }
|
|
250
371
|
}
|
|
251
372
|
}
|
|
252
373
|
return neighbors;
|
|
253
|
-
}
|
|
254
|
-
getEndsOfEdge(edge) {
|
|
374
|
+
};
|
|
375
|
+
DirectedGraph.prototype.getEndsOfEdge = function (edge) {
|
|
255
376
|
if (!this.containsEdge(edge.src, edge.dest)) {
|
|
256
377
|
return null;
|
|
257
378
|
}
|
|
258
|
-
|
|
259
|
-
|
|
379
|
+
var v1 = this.getVertex(edge.src);
|
|
380
|
+
var v2 = this.getVertex(edge.dest);
|
|
260
381
|
if (v1 && v2) {
|
|
261
382
|
return [v1, v2];
|
|
262
383
|
}
|
|
263
384
|
else {
|
|
264
385
|
return null;
|
|
265
386
|
}
|
|
266
|
-
}
|
|
267
|
-
|
|
387
|
+
};
|
|
388
|
+
return DirectedGraph;
|
|
389
|
+
}(abstract_graph_1.AbstractGraph));
|
|
268
390
|
exports.DirectedGraph = DirectedGraph;
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import { AbstractEdge, AbstractGraph, AbstractVertex
|
|
1
|
+
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
|
2
|
+
import type { VertexId } from '../types';
|
|
2
3
|
export declare class UndirectedVertex extends AbstractVertex {
|
|
3
4
|
constructor(id: VertexId);
|
|
4
5
|
}
|
|
5
6
|
export declare class UndirectedEdge extends AbstractEdge {
|
|
7
|
+
constructor(v1: VertexId, v2: VertexId, weight?: number);
|
|
6
8
|
private _vertices;
|
|
7
9
|
get vertices(): [VertexId, VertexId];
|
|
8
10
|
set vertices(v: [VertexId, VertexId]);
|
|
9
|
-
constructor(v1: VertexId, v2: VertexId, weight?: number);
|
|
10
11
|
}
|
|
11
12
|
export declare class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
|
|
12
|
-
constructor();
|
|
13
13
|
protected _edges: Map<V, E[]>;
|
|
14
|
+
constructor();
|
|
14
15
|
getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null;
|
|
15
16
|
addEdge(edge: E): boolean;
|
|
16
17
|
removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null;
|