typescript-ds-lib 0.3.8 → 0.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +3 -5
- package/dist/index.d.ts +2 -0
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/base-collection.d.ts +4 -0
- package/dist/lib/base-collection.js.map +1 -1
- package/dist/lib/binary-search-tree.d.ts +5 -0
- package/dist/lib/binary-search-tree.js +22 -4
- package/dist/lib/binary-search-tree.js.map +1 -1
- package/dist/lib/deque.d.ts +4 -0
- package/dist/lib/deque.js +8 -0
- package/dist/lib/deque.js.map +1 -1
- package/dist/lib/graph/base.d.ts +167 -0
- package/dist/lib/graph/base.js +233 -0
- package/dist/lib/graph/base.js.map +1 -0
- package/dist/lib/graph/linked-list-graph.d.ts +18 -0
- package/dist/lib/graph/linked-list-graph.js +71 -0
- package/dist/lib/graph/linked-list-graph.js.map +1 -0
- package/dist/lib/graph/matrix-graph.d.ts +18 -0
- package/dist/lib/graph/matrix-graph.js +102 -0
- package/dist/lib/graph/matrix-graph.js.map +1 -0
- package/dist/lib/hash-table.d.ts +4 -0
- package/dist/lib/hash-table.js +25 -4
- package/dist/lib/hash-table.js.map +1 -1
- package/dist/lib/hash-utils.d.ts +0 -1
- package/dist/lib/hash-utils.js +4 -45
- package/dist/lib/hash-utils.js.map +1 -1
- package/dist/lib/heap.d.ts +4 -0
- package/dist/lib/heap.js +17 -4
- package/dist/lib/heap.js.map +1 -1
- package/dist/lib/linked-list.d.ts +4 -0
- package/dist/lib/linked-list.js +19 -0
- package/dist/lib/linked-list.js.map +1 -1
- package/dist/lib/map.d.ts +1 -0
- package/dist/lib/map.js +5 -0
- package/dist/lib/map.js.map +1 -1
- package/dist/lib/matrix.js +14 -6
- package/dist/lib/matrix.js.map +1 -1
- package/dist/lib/priority-queue.d.ts +4 -0
- package/dist/lib/priority-queue.js +8 -0
- package/dist/lib/priority-queue.js.map +1 -1
- package/dist/lib/queue.d.ts +4 -0
- package/dist/lib/queue.js +8 -0
- package/dist/lib/queue.js.map +1 -1
- package/dist/lib/red-black-tree.d.ts +6 -0
- package/dist/lib/red-black-tree.js +21 -0
- package/dist/lib/red-black-tree.js.map +1 -1
- package/dist/lib/set.d.ts +4 -0
- package/dist/lib/set.js +8 -0
- package/dist/lib/set.js.map +1 -1
- package/dist/lib/stack.d.ts +5 -1
- package/dist/lib/stack.js +8 -0
- package/dist/lib/stack.js.map +1 -1
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.js +47 -0
- package/dist/lib/utils.js.map +1 -0
- package/package.json +9 -5
- package/dist/lib/graph.d.ts +0 -14
- package/dist/lib/graph.js +0 -3
- package/dist/lib/graph.js.map +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ TypeScript data structure implementations without external dependencies. Why to
|
|
|
8
8
|
- 0 Dependencies
|
|
9
9
|
- Lightweight
|
|
10
10
|
- Comes with Comparator (for custom types)
|
|
11
|
-
- `
|
|
11
|
+
- `equals()` method provided for all data structures
|
|
12
12
|
- Well documented
|
|
13
13
|
|
|
14
14
|
|
|
@@ -42,6 +42,7 @@ See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master
|
|
|
42
42
|
## Data Structures
|
|
43
43
|
- Binary Search Tree
|
|
44
44
|
- Deque
|
|
45
|
+
- Graph (beta version and partially tested)
|
|
45
46
|
- Hash Table (unordered map)
|
|
46
47
|
- Heap
|
|
47
48
|
- Linked List
|
|
@@ -52,7 +53,6 @@ See the [documentation](https://github.com/baloian/typescript-ds-lib/blob/master
|
|
|
52
53
|
- Red-Black Tree
|
|
53
54
|
- Set
|
|
54
55
|
- Stack
|
|
55
|
-
- Graph (coming soon)
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
## `Map-Set` vs Native JavaScript `Map-Set`
|
|
@@ -69,9 +69,7 @@ You can consider the library's `Map` and `Set` as ordered map and set, and nativ
|
|
|
69
69
|
|
|
70
70
|
## Contributions
|
|
71
71
|
Contributions are welcome and can be made by submitting GitHub pull requests
|
|
72
|
-
to this repository.
|
|
73
|
-
[Airbnb JavaScript Style Guide](https://github.com/airbnb/javascript) and the
|
|
74
|
-
rules specified in `.eslintrc.json` file.
|
|
72
|
+
to this repository.
|
|
75
73
|
|
|
76
74
|
|
|
77
75
|
## License
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { BinarySearchTree } from './lib/binary-search-tree';
|
|
3
3
|
export { Deque } from './lib/deque';
|
|
4
|
+
export { MatrixGraph } from './lib/graph/matrix-graph';
|
|
5
|
+
export { LinkedListGraph } from './lib/graph/linked-list-graph';
|
|
4
6
|
export { HashTable } from './lib/hash-table';
|
|
5
7
|
export { Heap } from './lib/heap';
|
|
6
8
|
export { LinkedList } from './lib/linked-list';
|
package/dist/index.js
CHANGED
|
@@ -14,12 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.Stack = exports.Set = exports.RedBlackTree = exports.Queue = exports.PriorityQueue = exports.Matrix = exports.Map = exports.LinkedList = exports.Heap = exports.HashTable = exports.Deque = exports.BinarySearchTree = void 0;
|
|
17
|
+
exports.Stack = exports.Set = exports.RedBlackTree = exports.Queue = exports.PriorityQueue = exports.Matrix = exports.Map = exports.LinkedList = exports.Heap = exports.HashTable = exports.LinkedListGraph = exports.MatrixGraph = exports.Deque = exports.BinarySearchTree = void 0;
|
|
18
18
|
__exportStar(require("./types"), exports);
|
|
19
19
|
var binary_search_tree_1 = require("./lib/binary-search-tree");
|
|
20
20
|
Object.defineProperty(exports, "BinarySearchTree", { enumerable: true, get: function () { return binary_search_tree_1.BinarySearchTree; } });
|
|
21
21
|
var deque_1 = require("./lib/deque");
|
|
22
22
|
Object.defineProperty(exports, "Deque", { enumerable: true, get: function () { return deque_1.Deque; } });
|
|
23
|
+
var matrix_graph_1 = require("./lib/graph/matrix-graph");
|
|
24
|
+
Object.defineProperty(exports, "MatrixGraph", { enumerable: true, get: function () { return matrix_graph_1.MatrixGraph; } });
|
|
25
|
+
var linked_list_graph_1 = require("./lib/graph/linked-list-graph");
|
|
26
|
+
Object.defineProperty(exports, "LinkedListGraph", { enumerable: true, get: function () { return linked_list_graph_1.LinkedListGraph; } });
|
|
23
27
|
var hash_table_1 = require("./lib/hash-table");
|
|
24
28
|
Object.defineProperty(exports, "HashTable", { enumerable: true, get: function () { return hash_table_1.HashTable; } });
|
|
25
29
|
var heap_1 = require("./lib/heap");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+DAA4D;AAAnD,sHAAA,gBAAgB,OAAA;AACzB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,iDAA+C;AAAtC,yGAAA,UAAU,OAAA;AACnB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,uDAAoD;AAA3C,8GAAA,YAAY,OAAA;AACrB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,qCAAoC;AAA3B,8FAAA,KAAK,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+DAA4D;AAAnD,sHAAA,gBAAgB,OAAA;AACzB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,yDAAuD;AAA9C,2GAAA,WAAW,OAAA;AACpB,mEAAgE;AAAvD,oHAAA,eAAe,OAAA;AACxB,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,iDAA+C;AAAtC,yGAAA,UAAU,OAAA;AACnB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AACd,uDAAoD;AAA3C,8GAAA,YAAY,OAAA;AACrB,iCAAgC;AAAvB,0FAAA,GAAG,OAAA;AACZ,qCAAoC;AAA3B,8FAAA,KAAK,OAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base-collection.js","sourceRoot":"","sources":["../../lib/base-collection.ts"],"names":[],"mappings":";;;AAAA,MAAsB,cAAc;
|
|
1
|
+
{"version":3,"file":"base-collection.js","sourceRoot":"","sources":["../../lib/base-collection.ts"],"names":[],"mappings":";;;AAAA,MAAsB,cAAc;CAoBnC;AApBD,wCAoBC"}
|
|
@@ -31,4 +31,9 @@ export declare class BinarySearchTree<T> extends BaseCollection<T> implements Bi
|
|
|
31
31
|
* Returns the number of nodes in the BST.
|
|
32
32
|
*/
|
|
33
33
|
size(): number;
|
|
34
|
+
/**
|
|
35
|
+
* Checks if two BSTs are equal.
|
|
36
|
+
*/
|
|
37
|
+
equals(other: BinarySearchTree<T>): boolean;
|
|
38
|
+
private areTreesEqual;
|
|
34
39
|
}
|
|
@@ -34,10 +34,6 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
34
34
|
}
|
|
35
35
|
let current = this.root;
|
|
36
36
|
while (true) {
|
|
37
|
-
if (this.isEqual(value, current.value)) {
|
|
38
|
-
// Value already exists, don't insert
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
37
|
if (this.comparator(value, current.value)) {
|
|
42
38
|
if (current.left === null) {
|
|
43
39
|
current.left = newNode;
|
|
@@ -46,6 +42,9 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
46
42
|
}
|
|
47
43
|
current = current.left;
|
|
48
44
|
}
|
|
45
|
+
else if (this.isEqual(value, current.value)) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
49
48
|
else {
|
|
50
49
|
if (current.right === null) {
|
|
51
50
|
current.right = newNode;
|
|
@@ -204,6 +203,25 @@ class BinarySearchTree extends base_collection_1.BaseCollection {
|
|
|
204
203
|
size() {
|
|
205
204
|
return this.nodeCount;
|
|
206
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Checks if two BSTs are equal.
|
|
208
|
+
*/
|
|
209
|
+
equals(other) {
|
|
210
|
+
if (!other || !(other instanceof BinarySearchTree))
|
|
211
|
+
return false;
|
|
212
|
+
if (this.size() !== other.size())
|
|
213
|
+
return false;
|
|
214
|
+
return this.areTreesEqual(this.root, other.root);
|
|
215
|
+
}
|
|
216
|
+
areTreesEqual(node1, node2) {
|
|
217
|
+
if (node1 === null && node2 === null)
|
|
218
|
+
return true;
|
|
219
|
+
if (node1 === null || node2 === null)
|
|
220
|
+
return false;
|
|
221
|
+
return (this.isEqual(node1.value, node2.value) &&
|
|
222
|
+
this.areTreesEqual(node1.left, node2.left) &&
|
|
223
|
+
this.areTreesEqual(node1.right, node2.right));
|
|
224
|
+
}
|
|
207
225
|
}
|
|
208
226
|
exports.BinarySearchTree = BinarySearchTree;
|
|
209
227
|
//# sourceMappingURL=binary-search-tree.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binary-search-tree.js","sourceRoot":"","sources":["../../lib/binary-search-tree.ts"],"names":[],"mappings":";;;AACA,uDAAmD;
|
|
1
|
+
{"version":3,"file":"binary-search-tree.js","sourceRoot":"","s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CAAC;IACxB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAA0B;QAC/B,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,KAAK,YAAY,gBAAgB,CAAC;YAAE,OAAO,KAAK,CAAC;QACjE,IAAI,IAAI,CAAC,IAAI,EAAE,KAAK,KAAK,CAAC,IAAI,EAAE;YAAE,OAAO,KAAK,CAAC;QAC/C,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;IACnD,CAAC;IAEO,aAAa,CAAC,KAAyB,EAAE,KAAyB;QACxE,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,IAAI,CAAC;QAClD,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QACnD,OAAO,CACL,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC;YACtC,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC;YAC1C,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAC7C,CAAC;IACJ,CAAC;CACF;AAxND,4CAwNC"}
|
package/dist/lib/deque.d.ts
CHANGED
package/dist/lib/deque.js
CHANGED
|
@@ -63,6 +63,14 @@ class Deque extends base_collection_1.BaseCollection {
|
|
|
63
63
|
clear() {
|
|
64
64
|
this.items.clear();
|
|
65
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks if two deques are equal.
|
|
68
|
+
*/
|
|
69
|
+
equals(other) {
|
|
70
|
+
if (!other || !(other instanceof Deque))
|
|
71
|
+
return false;
|
|
72
|
+
return this.items.equals(other.items);
|
|
73
|
+
}
|
|
66
74
|
}
|
|
67
75
|
exports.Deque = Deque;
|
|
68
76
|
//# sourceMappingURL=deque.js.map
|
package/dist/lib/deque.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deque.js","sourceRoot":"","sources":["../../lib/deque.ts"],"names":[],"mappings":";;;AAAA,+CAA2C;AAC3C,uDAAmD;
|
|
1
|
+
{"version":3,"file":"deque.js","sourceRoot":"","sources":["../../lib/deque.ts"],"names":[],"mappings":";;;AAAA,+CAA2C;AAC3C,uDAAmD;AAWnD,MAAa,KAAS,SAAQ,gCAAiB;IACrC,KAAK,CAAgB;IAE7B;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,KAAK,GAAG,IAAI,wBAAU,EAAK,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,OAAU;QAClB,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACH,QAAQ,CAAC,OAAU;QACjB,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC/B,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,KAAK;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;IAC/C,CAAC;IAED;;OAEG;IACH,OAAO;QACL,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,IAAI;QACF,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAC3B,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAe;QACpB,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC;YAAE,OAAO,KAAK,CAAC;QACtD,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;CACF;AA9ED,sBA8EC"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { BaseCollection } from '../base-collection';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for Graph data structure implementations.
|
|
4
|
+
* Provides common functionality that can be extended by specific implementations
|
|
5
|
+
* like LinkedList-based or Matrix-based graphs.
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class BaseGraph<V, W = number> extends BaseCollection<V> {
|
|
8
|
+
protected vertices: Set<V>;
|
|
9
|
+
protected vertexToIndex: Map<V, number>;
|
|
10
|
+
protected indexToVertex: Map<number, V>;
|
|
11
|
+
protected nextVertexIndex: number;
|
|
12
|
+
constructor();
|
|
13
|
+
/**
|
|
14
|
+
* Adds a vertex to the graph.
|
|
15
|
+
* @param vertex - The vertex to add
|
|
16
|
+
*/
|
|
17
|
+
addVertex(vertex: V): void;
|
|
18
|
+
/**
|
|
19
|
+
* Removes a vertex from the graph and all its associated edges.
|
|
20
|
+
* @param vertex - The vertex to remove
|
|
21
|
+
*/
|
|
22
|
+
removeVertex(vertex: V): void;
|
|
23
|
+
/**
|
|
24
|
+
* Adds an edge between two vertices with an optional weight.
|
|
25
|
+
* @param from - The source vertex
|
|
26
|
+
* @param to - The destination vertex
|
|
27
|
+
* @param weight - The weight of the edge (default: 1)
|
|
28
|
+
*/
|
|
29
|
+
addEdge(from: V, to: V, weight?: W): void;
|
|
30
|
+
/**
|
|
31
|
+
* Removes an edge between two vertices.
|
|
32
|
+
* @param from - The source vertex
|
|
33
|
+
* @param to - The destination vertex
|
|
34
|
+
*/
|
|
35
|
+
removeEdge(from: V, to: V): void;
|
|
36
|
+
/**
|
|
37
|
+
* Gets all neighbors of a vertex.
|
|
38
|
+
* @param vertex - The vertex to get neighbors for
|
|
39
|
+
* @returns Array of neighboring vertices
|
|
40
|
+
*/
|
|
41
|
+
getNeighbors(vertex: V): V[];
|
|
42
|
+
/**
|
|
43
|
+
* Gets the weight of an edge between two vertices.
|
|
44
|
+
* @param from - The source vertex
|
|
45
|
+
* @param to - The destination vertex
|
|
46
|
+
* @returns The weight of the edge, or undefined if no edge exists
|
|
47
|
+
*/
|
|
48
|
+
getEdgeWeight(from: V, to: V): W | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Gets all vertices in the graph.
|
|
51
|
+
* @returns Array of all vertices
|
|
52
|
+
*/
|
|
53
|
+
getVertices(): V[];
|
|
54
|
+
/**
|
|
55
|
+
* Checks if a vertex exists in the graph.
|
|
56
|
+
* @param vertex - The vertex to check
|
|
57
|
+
* @returns True if the vertex exists, false otherwise
|
|
58
|
+
*/
|
|
59
|
+
hasVertex(vertex: V): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Checks if an edge exists between two vertices.
|
|
62
|
+
* @param from - The source vertex
|
|
63
|
+
* @param to - The destination vertex
|
|
64
|
+
* @returns True if the edge exists, false otherwise
|
|
65
|
+
*/
|
|
66
|
+
hasEdge(from: V, to: V): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Returns the number of vertices in the graph.
|
|
69
|
+
* @returns The number of vertices
|
|
70
|
+
*/
|
|
71
|
+
vertexCount(): number;
|
|
72
|
+
/**
|
|
73
|
+
* Returns the number of edges in the graph.
|
|
74
|
+
* @returns The number of edges
|
|
75
|
+
*/
|
|
76
|
+
edgeCount(): number;
|
|
77
|
+
/**
|
|
78
|
+
* Clears all vertices and edges from the graph.
|
|
79
|
+
*/
|
|
80
|
+
clear(): void;
|
|
81
|
+
/**
|
|
82
|
+
* Returns the number of elements (vertices) in the graph.
|
|
83
|
+
* @returns The number of vertices
|
|
84
|
+
*/
|
|
85
|
+
size(): number;
|
|
86
|
+
/**
|
|
87
|
+
* Checks if the graph is empty.
|
|
88
|
+
* @returns True if the graph has no vertices, false otherwise
|
|
89
|
+
*/
|
|
90
|
+
isEmpty(): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Checks if this graph equals another graph.
|
|
93
|
+
* @param other - The other graph to compare with
|
|
94
|
+
* @returns True if the graphs are equal, false otherwise
|
|
95
|
+
*/
|
|
96
|
+
equals(other: BaseGraph<V, W>): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Gets the index of a vertex.
|
|
99
|
+
* @param vertex - The vertex to get the index for
|
|
100
|
+
* @returns The index of the vertex, or -1 if not found
|
|
101
|
+
*/
|
|
102
|
+
protected getVertexIndex(vertex: V): number;
|
|
103
|
+
/**
|
|
104
|
+
* Gets the vertex at a given index.
|
|
105
|
+
* @param index - The index to get the vertex for
|
|
106
|
+
* @returns The vertex at the index, or undefined if not found
|
|
107
|
+
*/
|
|
108
|
+
protected getVertexAt(index: number): V | undefined;
|
|
109
|
+
/**
|
|
110
|
+
* Gets all vertex indices.
|
|
111
|
+
* @returns Array of vertex indices
|
|
112
|
+
*/
|
|
113
|
+
protected getAllVertexIndices(): number[];
|
|
114
|
+
/**
|
|
115
|
+
* Called when a vertex is added to the graph.
|
|
116
|
+
* @param vertex - The vertex that was added
|
|
117
|
+
*/
|
|
118
|
+
protected abstract onVertexAdded(vertex: V): void;
|
|
119
|
+
/**
|
|
120
|
+
* Called when a vertex is removed from the graph.
|
|
121
|
+
* @param vertex - The vertex that was removed
|
|
122
|
+
* @param vertexIndex - The index of the removed vertex
|
|
123
|
+
*/
|
|
124
|
+
protected abstract onVertexRemoved(vertex: V, vertexIndex: number): void;
|
|
125
|
+
/**
|
|
126
|
+
* Called when an edge is added to the graph.
|
|
127
|
+
* @param from - The source vertex
|
|
128
|
+
* @param to - The destination vertex
|
|
129
|
+
* @param weight - The weight of the edge
|
|
130
|
+
*/
|
|
131
|
+
protected abstract onEdgeAdded(from: V, to: V, weight: W): void;
|
|
132
|
+
/**
|
|
133
|
+
* Called when an edge is removed from the graph.
|
|
134
|
+
* @param from - The source vertex
|
|
135
|
+
* @param to - The destination vertex
|
|
136
|
+
*/
|
|
137
|
+
protected abstract onEdgeRemoved(from: V, to: V): void;
|
|
138
|
+
/**
|
|
139
|
+
* Gets the neighbors of a vertex.
|
|
140
|
+
* @param vertex - The vertex to get neighbors for
|
|
141
|
+
* @returns Array of neighboring vertices
|
|
142
|
+
*/
|
|
143
|
+
protected abstract onGetNeighbors(vertex: V): V[];
|
|
144
|
+
/**
|
|
145
|
+
* Gets the weight of an edge between two vertices.
|
|
146
|
+
* @param from - The source vertex
|
|
147
|
+
* @param to - The destination vertex
|
|
148
|
+
* @returns The weight of the edge, or undefined if no edge exists
|
|
149
|
+
*/
|
|
150
|
+
protected abstract onGetEdgeWeight(from: V, to: V): W | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* Checks if an edge exists between two vertices.
|
|
153
|
+
* @param from - The source vertex
|
|
154
|
+
* @param to - The destination vertex
|
|
155
|
+
* @returns True if the edge exists, false otherwise
|
|
156
|
+
*/
|
|
157
|
+
protected abstract onHasEdge(from: V, to: V): boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Gets the total number of edges in the graph.
|
|
160
|
+
* @returns The number of edges
|
|
161
|
+
*/
|
|
162
|
+
protected abstract onGetEdgeCount(): number;
|
|
163
|
+
/**
|
|
164
|
+
* Called when the graph is cleared.
|
|
165
|
+
*/
|
|
166
|
+
protected abstract onClear(): void;
|
|
167
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseGraph = void 0;
|
|
4
|
+
const base_collection_1 = require("../base-collection");
|
|
5
|
+
/**
|
|
6
|
+
* Base class for Graph data structure implementations.
|
|
7
|
+
* Provides common functionality that can be extended by specific implementations
|
|
8
|
+
* like LinkedList-based or Matrix-based graphs.
|
|
9
|
+
*/
|
|
10
|
+
class BaseGraph extends base_collection_1.BaseCollection {
|
|
11
|
+
vertices;
|
|
12
|
+
vertexToIndex;
|
|
13
|
+
indexToVertex;
|
|
14
|
+
nextVertexIndex;
|
|
15
|
+
constructor() {
|
|
16
|
+
super();
|
|
17
|
+
this.vertices = new Set();
|
|
18
|
+
this.vertexToIndex = new Map();
|
|
19
|
+
this.indexToVertex = new Map();
|
|
20
|
+
this.nextVertexIndex = 0;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Adds a vertex to the graph.
|
|
24
|
+
* @param vertex - The vertex to add
|
|
25
|
+
*/
|
|
26
|
+
addVertex(vertex) {
|
|
27
|
+
if (!this.vertices.has(vertex)) {
|
|
28
|
+
this.vertices.add(vertex);
|
|
29
|
+
this.vertexToIndex.set(vertex, this.nextVertexIndex);
|
|
30
|
+
this.indexToVertex.set(this.nextVertexIndex, vertex);
|
|
31
|
+
this.nextVertexIndex++;
|
|
32
|
+
this.onVertexAdded(vertex);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Removes a vertex from the graph and all its associated edges.
|
|
37
|
+
* @param vertex - The vertex to remove
|
|
38
|
+
*/
|
|
39
|
+
removeVertex(vertex) {
|
|
40
|
+
if (this.vertices.has(vertex)) {
|
|
41
|
+
const vertexIndex = this.vertexToIndex.get(vertex);
|
|
42
|
+
// Remove all edges connected to this vertex
|
|
43
|
+
this.getVertices().forEach((otherVertex) => {
|
|
44
|
+
if (otherVertex !== vertex) {
|
|
45
|
+
this.removeEdge(vertex, otherVertex);
|
|
46
|
+
this.removeEdge(otherVertex, vertex);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
// Remove from tracking collections
|
|
50
|
+
this.vertices.delete(vertex);
|
|
51
|
+
this.indexToVertex.delete(vertexIndex);
|
|
52
|
+
this.vertexToIndex.delete(vertex);
|
|
53
|
+
this.onVertexRemoved(vertex, vertexIndex);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Adds an edge between two vertices with an optional weight.
|
|
58
|
+
* @param from - The source vertex
|
|
59
|
+
* @param to - The destination vertex
|
|
60
|
+
* @param weight - The weight of the edge (default: 1)
|
|
61
|
+
*/
|
|
62
|
+
addEdge(from, to, weight = 1) {
|
|
63
|
+
if (!this.vertices.has(from)) {
|
|
64
|
+
this.addVertex(from);
|
|
65
|
+
}
|
|
66
|
+
if (!this.vertices.has(to)) {
|
|
67
|
+
this.addVertex(to);
|
|
68
|
+
}
|
|
69
|
+
if (from !== to) {
|
|
70
|
+
this.onEdgeAdded(from, to, weight);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Removes an edge between two vertices.
|
|
75
|
+
* @param from - The source vertex
|
|
76
|
+
* @param to - The destination vertex
|
|
77
|
+
*/
|
|
78
|
+
removeEdge(from, to) {
|
|
79
|
+
if (this.vertices.has(from) && this.vertices.has(to)) {
|
|
80
|
+
this.onEdgeRemoved(from, to);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Gets all neighbors of a vertex.
|
|
85
|
+
* @param vertex - The vertex to get neighbors for
|
|
86
|
+
* @returns Array of neighboring vertices
|
|
87
|
+
*/
|
|
88
|
+
getNeighbors(vertex) {
|
|
89
|
+
if (!this.vertices.has(vertex)) {
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
return this.onGetNeighbors(vertex);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Gets the weight of an edge between two vertices.
|
|
96
|
+
* @param from - The source vertex
|
|
97
|
+
* @param to - The destination vertex
|
|
98
|
+
* @returns The weight of the edge, or undefined if no edge exists
|
|
99
|
+
*/
|
|
100
|
+
getEdgeWeight(from, to) {
|
|
101
|
+
if (!this.vertices.has(from) || !this.vertices.has(to)) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
return this.onGetEdgeWeight(from, to);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Gets all vertices in the graph.
|
|
108
|
+
* @returns Array of all vertices
|
|
109
|
+
*/
|
|
110
|
+
getVertices() {
|
|
111
|
+
return Array.from(this.vertices);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Checks if a vertex exists in the graph.
|
|
115
|
+
* @param vertex - The vertex to check
|
|
116
|
+
* @returns True if the vertex exists, false otherwise
|
|
117
|
+
*/
|
|
118
|
+
hasVertex(vertex) {
|
|
119
|
+
return this.vertices.has(vertex);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Checks if an edge exists between two vertices.
|
|
123
|
+
* @param from - The source vertex
|
|
124
|
+
* @param to - The destination vertex
|
|
125
|
+
* @returns True if the edge exists, false otherwise
|
|
126
|
+
*/
|
|
127
|
+
hasEdge(from, to) {
|
|
128
|
+
if (!this.vertices.has(from) || !this.vertices.has(to)) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
return this.onHasEdge(from, to);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns the number of vertices in the graph.
|
|
135
|
+
* @returns The number of vertices
|
|
136
|
+
*/
|
|
137
|
+
vertexCount() {
|
|
138
|
+
return this.vertices.size;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Returns the number of edges in the graph.
|
|
142
|
+
* @returns The number of edges
|
|
143
|
+
*/
|
|
144
|
+
edgeCount() {
|
|
145
|
+
return this.onGetEdgeCount();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Clears all vertices and edges from the graph.
|
|
149
|
+
*/
|
|
150
|
+
clear() {
|
|
151
|
+
this.vertices.clear();
|
|
152
|
+
this.vertexToIndex.clear();
|
|
153
|
+
this.indexToVertex.clear();
|
|
154
|
+
this.nextVertexIndex = 0;
|
|
155
|
+
this.onClear();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Returns the number of elements (vertices) in the graph.
|
|
159
|
+
* @returns The number of vertices
|
|
160
|
+
*/
|
|
161
|
+
size() {
|
|
162
|
+
return this.vertexCount();
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Checks if the graph is empty.
|
|
166
|
+
* @returns True if the graph has no vertices, false otherwise
|
|
167
|
+
*/
|
|
168
|
+
isEmpty() {
|
|
169
|
+
return this.vertices.size === 0;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Checks if this graph equals another graph.
|
|
173
|
+
* @param other - The other graph to compare with
|
|
174
|
+
* @returns True if the graphs are equal, false otherwise
|
|
175
|
+
*/
|
|
176
|
+
equals(other) {
|
|
177
|
+
if (!(other instanceof BaseGraph)) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
if (this.vertexCount() !== other.vertexCount()) {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
const thisVertices = this.getVertices();
|
|
184
|
+
const otherVertices = other.getVertices();
|
|
185
|
+
// Check if all vertices exist in both graphs
|
|
186
|
+
for (const vertex of thisVertices) {
|
|
187
|
+
if (!other.hasVertex(vertex)) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Check if all edges exist in both graphs
|
|
192
|
+
for (const from of thisVertices) {
|
|
193
|
+
for (const to of thisVertices) {
|
|
194
|
+
if (this.hasEdge(from, to) !== other.hasEdge(from, to)) {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
if (this.hasEdge(from, to)) {
|
|
198
|
+
const thisWeight = this.getEdgeWeight(from, to);
|
|
199
|
+
const otherWeight = other.getEdgeWeight(from, to);
|
|
200
|
+
if (thisWeight !== otherWeight) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Gets the index of a vertex.
|
|
210
|
+
* @param vertex - The vertex to get the index for
|
|
211
|
+
* @returns The index of the vertex, or -1 if not found
|
|
212
|
+
*/
|
|
213
|
+
getVertexIndex(vertex) {
|
|
214
|
+
return this.vertexToIndex.get(vertex) ?? -1;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Gets the vertex at a given index.
|
|
218
|
+
* @param index - The index to get the vertex for
|
|
219
|
+
* @returns The vertex at the index, or undefined if not found
|
|
220
|
+
*/
|
|
221
|
+
getVertexAt(index) {
|
|
222
|
+
return this.indexToVertex.get(index);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Gets all vertex indices.
|
|
226
|
+
* @returns Array of vertex indices
|
|
227
|
+
*/
|
|
228
|
+
getAllVertexIndices() {
|
|
229
|
+
return Array.from(this.vertexToIndex.values());
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
exports.BaseGraph = BaseGraph;
|
|
233
|
+
//# sourceMappingURL=base.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../lib/graph/base.ts"],"names":[],"mappings":";;;AAAA,wDAAoD;AAEpD;;;;GAIG;AACH,MAAsB,SAAyB,SAAQ,gCAAiB;IAC5D,QAAQ,CAAS;IACjB,aAAa,CAAiB;IAC9B,aAAa,CAAiB;IAC9B,eAAe,CAAS;IAElC;QACE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,IAAI,GAAG,EAAK,CAAC;QAC7B,IAAI,CAAC,aAAa,GAAG,IAAI,GAAG,EAAa,CAAC;QAC1C,IAAI,CAAC,aAAa,GAAG,IAAI,GAAG,EAAa,CAAC;QAC1C,IAAI,CAAC,eAAe,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACH,SAAS,CAAC,MAAS;QACjB,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,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AC,CAAC;IACvC,CAAC;IAED;;;OAGG;IACO,mBAAmB;QAC3B,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,EAAE,CAAC,CAAC;IACjD,CAAC;CAiEF;AAvTD,8BAuTC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseGraph } from './base';
|
|
2
|
+
/**
|
|
3
|
+
* LinkedList-based Graph implementation.
|
|
4
|
+
* Uses adjacency lists to represent the graph.
|
|
5
|
+
*/
|
|
6
|
+
export declare class LinkedListGraph<V, W = number> extends BaseGraph<V, W> {
|
|
7
|
+
private adjacencyLists;
|
|
8
|
+
constructor();
|
|
9
|
+
protected onVertexAdded(vertex: V): void;
|
|
10
|
+
protected onVertexRemoved(vertex: V, vertexIndex: number): void;
|
|
11
|
+
protected onEdgeAdded(from: V, to: V, weight: W): void;
|
|
12
|
+
protected onEdgeRemoved(from: V, to: V): void;
|
|
13
|
+
protected onGetNeighbors(vertex: V): V[];
|
|
14
|
+
protected onGetEdgeWeight(from: V, to: V): W | undefined;
|
|
15
|
+
protected onHasEdge(from: V, to: V): boolean;
|
|
16
|
+
protected onGetEdgeCount(): number;
|
|
17
|
+
protected onClear(): void;
|
|
18
|
+
}
|