data-structure-typed 1.12.11 → 1.15.0

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.
Files changed (81) hide show
  1. package/README.md +278 -179
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +14 -5
  3. package/dist/data-structures/binary-tree/avl-tree.js +15 -6
  4. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +11 -2
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +11 -2
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +72 -18
  7. package/dist/data-structures/binary-tree/binary-tree.js +139 -62
  8. package/dist/data-structures/binary-tree/bst.d.ts +92 -5
  9. package/dist/data-structures/binary-tree/bst.js +89 -5
  10. package/dist/data-structures/binary-tree/segment-tree.d.ts +70 -2
  11. package/dist/data-structures/binary-tree/segment-tree.js +86 -2
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +34 -3
  13. package/dist/data-structures/binary-tree/tree-multiset.js +35 -4
  14. package/dist/data-structures/graph/abstract-graph.d.ts +45 -5
  15. package/dist/data-structures/graph/abstract-graph.js +59 -11
  16. package/dist/data-structures/graph/directed-graph.d.ts +26 -4
  17. package/dist/data-structures/graph/directed-graph.js +38 -39
  18. package/dist/data-structures/graph/undirected-graph.d.ts +23 -0
  19. package/dist/data-structures/graph/undirected-graph.js +41 -3
  20. package/dist/data-structures/hash/coordinate-map.d.ts +12 -3
  21. package/dist/data-structures/hash/coordinate-map.js +21 -2
  22. package/dist/data-structures/hash/coordinate-set.d.ts +12 -3
  23. package/dist/data-structures/hash/coordinate-set.js +21 -2
  24. package/dist/data-structures/heap/heap.d.ts +25 -6
  25. package/dist/data-structures/heap/heap.js +46 -8
  26. package/dist/data-structures/heap/max-heap.d.ts +5 -2
  27. package/dist/data-structures/heap/max-heap.js +5 -2
  28. package/dist/data-structures/heap/min-heap.d.ts +5 -2
  29. package/dist/data-structures/heap/min-heap.js +5 -2
  30. package/dist/data-structures/index.d.ts +1 -0
  31. package/dist/data-structures/index.js +1 -0
  32. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +32 -9
  33. package/dist/data-structures/linked-list/doubly-linked-list.js +75 -8
  34. package/dist/data-structures/linked-list/singly-linked-list.d.ts +267 -330
  35. package/dist/data-structures/linked-list/singly-linked-list.js +263 -275
  36. package/dist/data-structures/matrix/matrix.d.ts +5 -2
  37. package/dist/data-structures/matrix/matrix.js +5 -2
  38. package/dist/data-structures/matrix/matrix2d.d.ts +5 -2
  39. package/dist/data-structures/matrix/matrix2d.js +5 -2
  40. package/dist/data-structures/matrix/navigator.d.ts +5 -2
  41. package/dist/data-structures/matrix/vector2d.d.ts +5 -2
  42. package/dist/data-structures/matrix/vector2d.js +5 -2
  43. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +5 -2
  44. package/dist/data-structures/priority-queue/max-priority-queue.js +5 -2
  45. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +5 -2
  46. package/dist/data-structures/priority-queue/min-priority-queue.js +5 -2
  47. package/dist/data-structures/priority-queue/priority-queue.d.ts +14 -5
  48. package/dist/data-structures/priority-queue/priority-queue.js +20 -4
  49. package/dist/data-structures/queue/deque.d.ts +30 -16
  50. package/dist/data-structures/queue/deque.js +62 -12
  51. package/dist/data-structures/queue/queue.d.ts +4 -4
  52. package/dist/data-structures/queue/queue.js +4 -4
  53. package/dist/data-structures/stack/stack.d.ts +1 -1
  54. package/dist/data-structures/stack/stack.js +1 -1
  55. package/dist/data-structures/trie/trie.d.ts +6 -3
  56. package/dist/data-structures/trie/trie.js +7 -4
  57. package/dist/data-structures/types/abstract-graph.d.ts +2 -2
  58. package/dist/utils/index.d.ts +1 -0
  59. package/dist/utils/index.js +1 -0
  60. package/dist/utils/types/utils.d.ts +8 -10
  61. package/dist/utils/types/utils.js +0 -1
  62. package/dist/utils/utils.d.ts +18 -8
  63. package/dist/utils/utils.js +93 -47
  64. package/package.json +2 -2
  65. package/src/data-structures/binary-tree/aa-tree.ts +1 -1
  66. package/src/data-structures/binary-tree/binary-tree.ts +84 -14
  67. package/src/data-structures/binary-tree/segment-tree.ts +45 -13
  68. package/src/data-structures/graph/abstract-graph.ts +58 -15
  69. package/src/data-structures/graph/directed-graph.ts +14 -5
  70. package/src/data-structures/graph/undirected-graph.ts +23 -6
  71. package/src/data-structures/hash/coordinate-map.ts +13 -1
  72. package/src/data-structures/hash/coordinate-set.ts +13 -1
  73. package/src/data-structures/heap/heap.ts +31 -0
  74. package/src/data-structures/linked-list/doubly-linked-list.ts +68 -11
  75. package/src/data-structures/linked-list/singly-linked-list.ts +312 -334
  76. package/src/data-structures/priority-queue/priority-queue.ts +15 -2
  77. package/src/data-structures/queue/deque.ts +38 -8
  78. package/src/data-structures/types/abstract-graph.ts +3 -3
  79. package/tests/unit/data-structures/graph/directed-graph.test.ts +431 -8
  80. package/dist/utils/trampoline.d.ts +0 -14
  81. package/dist/utils/trampoline.js +0 -130
@@ -17,8 +17,11 @@ var __extends = (this && this.__extends) || (function () {
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
18
  exports.TreeMultiSet = void 0;
19
19
  /**
20
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
21
- * @license MIT
20
+ * data-structure-typed
21
+ *
22
+ * @author Tyler Zeng
23
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
24
+ * @license MIT License
22
25
  */
23
26
  var bst_1 = require("./bst");
24
27
  var TreeMultiSet = /** @class */ (function (_super) {
@@ -26,12 +29,40 @@ var TreeMultiSet = /** @class */ (function (_super) {
26
29
  function TreeMultiSet() {
27
30
  return _super !== null && _super.apply(this, arguments) || this;
28
31
  }
32
+ /**
33
+ * The function creates a new BSTNode with the given id, value, and count.
34
+ * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
35
+ * distinguish one node from another in the tree.
36
+ * @param {T} val - The `val` parameter represents the value that will be stored in the binary search tree node.
37
+ * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
38
+ * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
39
+ * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
40
+ */
29
41
  TreeMultiSet.prototype.createNode = function (id, val, count) {
30
42
  return new bst_1.BSTNode(id, val, count);
31
43
  };
32
- TreeMultiSet.prototype.put = function (id, val, count) {
33
- return _super.prototype.put.call(this, id, val, count);
44
+ /**
45
+ * The function overrides the add method of the BinarySearchTree class in TypeScript.
46
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
47
+ * @param {T | null} val - The `val` parameter represents the value that you want to add to the binary search tree. It
48
+ * can be of type `T` (the generic type) or `null`.
49
+ * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
50
+ * of times the value should be added to the binary search tree. If not provided, the default value is `undefined`.
51
+ * @returns The `add` method is returning a `BSTNode<T>` object or `null`.
52
+ */
53
+ TreeMultiSet.prototype.add = function (id, val, count) {
54
+ return _super.prototype.add.call(this, id, val, count);
34
55
  };
56
+ /**
57
+ * The function overrides the remove method of the superclass and returns the result of calling the superclass's remove
58
+ * method.
59
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
60
+ * removed from the tree.
61
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean value that
62
+ * determines whether to update the left sum of all nodes in the tree after removing a node. If `isUpdateAllLeftSum` is
63
+ * set to `true`, the left sum of all nodes will be recalculated. If it
64
+ * @returns The method is returning an array of TreeMultiSetDeletedResult objects.
65
+ */
35
66
  TreeMultiSet.prototype.remove = function (id, isUpdateAllLeftSum) {
36
67
  return _super.prototype.remove.call(this, id, isUpdateAllLeftSum);
37
68
  };
@@ -1,18 +1,35 @@
1
1
  import type { DijkstraResult, IGraph, VertexId } from '../types';
2
2
  export declare class AbstractVertex {
3
- constructor(id: VertexId);
4
3
  protected _id: VertexId;
5
4
  get id(): VertexId;
5
+ /**
6
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
7
+ */
8
+ getId(): VertexId;
6
9
  set id(v: VertexId);
10
+ constructor(id: VertexId);
7
11
  }
8
12
  export declare abstract class AbstractEdge {
9
13
  static DEFAULT_EDGE_WEIGHT: number;
14
+ /**
15
+ * The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
16
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
17
+ * no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
18
+ */
10
19
  protected constructor(weight?: number);
11
20
  private _weight;
12
21
  get weight(): number;
22
+ /**
23
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
24
+ */
25
+ getWeight(): number;
13
26
  set weight(v: number);
14
27
  private _hashCode;
15
28
  get hashCode(): string;
29
+ /**
30
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
31
+ */
32
+ getHashCode(): string;
16
33
  set hashCode(v: string);
17
34
  }
18
35
  export declare abstract class AbstractGraph<V extends AbstractVertex, E extends AbstractEdge> implements IGraph<V, E> {
@@ -39,9 +56,9 @@ export declare abstract class AbstractGraph<V extends AbstractVertex, E extends
39
56
  * The function checks if a vertex exists in a graph.
40
57
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
41
58
  * (`VertexId`).
42
- * @returns The method `containsVertex` returns a boolean value.
59
+ * @returns The method `hasVertex` returns a boolean value.
43
60
  */
44
- containsVertex(vertexOrId: V | VertexId): boolean;
61
+ hasVertex(vertexOrId: V | VertexId): boolean;
45
62
  /**
46
63
  * The function `vertexSet()` returns a map of vertices.
47
64
  * @returns The method `vertexSet()` returns a map of vertex IDs to vertex objects.
@@ -79,10 +96,10 @@ export declare abstract class AbstractGraph<V extends AbstractVertex, E extends
79
96
  * a vertex in a graph, while V represents the type of the vertex itself.
80
97
  * @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
81
98
  * or a `V` type.
82
- * @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
99
+ * @returns The function `hasEdge` returns a boolean value. It returns `true` if there is an edge between the
83
100
  * vertices `v1` and `v2`, and `false` otherwise.
84
101
  */
85
- containsEdge(v1: VertexId | V, v2: VertexId | V): boolean;
102
+ hasEdge(v1: VertexId | V, v2: VertexId | V): boolean;
86
103
  abstract addEdge(edge: E): boolean;
87
104
  /**
88
105
  * The function sets the weight of an edge between two vertices in a graph.
@@ -159,8 +176,18 @@ export declare abstract class AbstractGraph<V extends AbstractVertex, E extends
159
176
  * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
160
177
  */
161
178
  dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
179
+ /**
180
+ * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
181
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
182
+ * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
183
+ */
162
184
  /**
163
185
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
186
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
187
+ */
188
+ /**
189
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
190
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
164
191
  * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
165
192
  * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
166
193
  * @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
@@ -181,6 +208,13 @@ export declare abstract class AbstractGraph<V extends AbstractVertex, E extends
181
208
  /**
182
209
  * BellmanFord time:O(VE) space:O(V)
183
210
  * one to rest pairs
211
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
212
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
213
+ */
214
+ /**
215
+ * BellmanFord time:O(VE) space:O(V)
216
+ * one to rest pairs
217
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
184
218
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
185
219
  * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
186
220
  * @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
@@ -204,6 +238,12 @@ export declare abstract class AbstractGraph<V extends AbstractVertex, E extends
204
238
  /**
205
239
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
206
240
  * all pairs
241
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
242
+ */
243
+ /**
244
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
245
+ * all pairs
246
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
207
247
  * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
208
248
  * graph.
209
249
  * @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
@@ -38,8 +38,11 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.AbstractGraph = exports.AbstractEdge = exports.AbstractVertex = void 0;
40
40
  /**
41
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
42
- * @license MIT
41
+ * data-structure-typed
42
+ *
43
+ * @author Tyler Zeng
44
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
45
+ * @license MIT License
43
46
  */
44
47
  var utils_1 = require("../../utils");
45
48
  var priority_queue_1 = require("../priority-queue");
@@ -57,10 +60,21 @@ var AbstractVertex = /** @class */ (function () {
57
60
  enumerable: false,
58
61
  configurable: true
59
62
  });
63
+ /**
64
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
65
+ */
66
+ AbstractVertex.prototype.getId = function () {
67
+ return this._id;
68
+ };
60
69
  return AbstractVertex;
61
70
  }());
62
71
  exports.AbstractVertex = AbstractVertex;
63
72
  var AbstractEdge = /** @class */ (function () {
73
+ /**
74
+ * The function is a protected constructor that initializes the weight and generates a unique hash code for an edge.
75
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge. If
76
+ * no weight is provided, it will default to the value of `AbstractEdge.DEFAULT_EDGE_WEIGHT`.
77
+ */
64
78
  function AbstractEdge(weight) {
65
79
  if (weight === undefined)
66
80
  weight = AbstractEdge.DEFAULT_EDGE_WEIGHT;
@@ -77,6 +91,12 @@ var AbstractEdge = /** @class */ (function () {
77
91
  enumerable: false,
78
92
  configurable: true
79
93
  });
94
+ /**
95
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
96
+ */
97
+ AbstractEdge.prototype.getWeight = function () {
98
+ return this._weight;
99
+ };
80
100
  Object.defineProperty(AbstractEdge.prototype, "hashCode", {
81
101
  get: function () {
82
102
  return this._hashCode;
@@ -87,6 +107,12 @@ var AbstractEdge = /** @class */ (function () {
87
107
  enumerable: false,
88
108
  configurable: true
89
109
  });
110
+ /**
111
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
112
+ */
113
+ AbstractEdge.prototype.getHashCode = function () {
114
+ return this._hashCode;
115
+ };
90
116
  AbstractEdge.DEFAULT_EDGE_WEIGHT = 1;
91
117
  return AbstractEdge;
92
118
  }());
@@ -95,8 +121,7 @@ exports.AbstractEdge = AbstractEdge;
95
121
  var AbstractGraph = /** @class */ (function () {
96
122
  function AbstractGraph() {
97
123
  this._vertices = new Map();
98
- // unionFind() {
99
- // }
124
+ // unionFind() {}
100
125
  /**--- end find cycles --- */
101
126
  // Minimum Spanning Tree
102
127
  }
@@ -125,9 +150,9 @@ var AbstractGraph = /** @class */ (function () {
125
150
  * The function checks if a vertex exists in a graph.
126
151
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
127
152
  * (`VertexId`).
128
- * @returns The method `containsVertex` returns a boolean value.
153
+ * @returns The method `hasVertex` returns a boolean value.
129
154
  */
130
- AbstractGraph.prototype.containsVertex = function (vertexOrId) {
155
+ AbstractGraph.prototype.hasVertex = function (vertexOrId) {
131
156
  return this._vertices.has(this.getVertexId(vertexOrId));
132
157
  };
133
158
  /**
@@ -144,7 +169,7 @@ var AbstractGraph = /** @class */ (function () {
144
169
  * false. Otherwise, it will add the newVertex to the graph and return true.
145
170
  */
146
171
  AbstractGraph.prototype.addVertex = function (newVertex) {
147
- if (this.containsVertex(newVertex)) {
172
+ if (this.hasVertex(newVertex)) {
148
173
  return false;
149
174
  }
150
175
  this._vertices.set(newVertex.id, newVertex);
@@ -191,10 +216,10 @@ var AbstractGraph = /** @class */ (function () {
191
216
  * a vertex in a graph, while V represents the type of the vertex itself.
192
217
  * @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
193
218
  * or a `V` type.
194
- * @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
219
+ * @returns The function `hasEdge` returns a boolean value. It returns `true` if there is an edge between the
195
220
  * vertices `v1` and `v2`, and `false` otherwise.
196
221
  */
197
- AbstractGraph.prototype.containsEdge = function (v1, v2) {
222
+ AbstractGraph.prototype.hasEdge = function (v1, v2) {
198
223
  var edge = this.getEdge(v1, v2);
199
224
  return !!edge;
200
225
  };
@@ -605,8 +630,18 @@ var AbstractGraph = /** @class */ (function () {
605
630
  genPaths && getPaths(minDest);
606
631
  return { distMap: distMap, preMap: preMap, seen: seen, paths: paths, minDist: minDist, minPath: minPath };
607
632
  };
633
+ /**
634
+ * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
635
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
636
+ * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
637
+ */
608
638
  /**
609
639
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
640
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
641
+ */
642
+ /**
643
+ * Dijkstra algorithm time: O(logVE) space: O(V + E)
644
+ * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
610
645
  * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
611
646
  * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
612
647
  * @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
@@ -660,7 +695,7 @@ var AbstractGraph = /** @class */ (function () {
660
695
  finally { if (e_11) throw e_11.error; }
661
696
  }
662
697
  var heap = new priority_queue_1.PriorityQueue({ comparator: function (a, b) { return a.id - b.id; } });
663
- heap.offer({ id: 0, val: srcVertex });
698
+ heap.add({ id: 0, val: srcVertex });
664
699
  distMap.set(srcVertex, 0);
665
700
  preMap.set(srcVertex, null);
666
701
  var getPaths = function (minV) {
@@ -717,7 +752,7 @@ var AbstractGraph = /** @class */ (function () {
717
752
  var distSrcToNeighbor = distMap.get(neighbor);
718
753
  if (distSrcToNeighbor) {
719
754
  if (dist + weight < distSrcToNeighbor) {
720
- heap.offer({ id: dist + weight, val: neighbor });
755
+ heap.add({ id: dist + weight, val: neighbor });
721
756
  preMap.set(neighbor, cur);
722
757
  distMap.set(neighbor, dist + weight);
723
758
  }
@@ -755,6 +790,13 @@ var AbstractGraph = /** @class */ (function () {
755
790
  /**
756
791
  * BellmanFord time:O(VE) space:O(V)
757
792
  * one to rest pairs
793
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
794
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
795
+ */
796
+ /**
797
+ * BellmanFord time:O(VE) space:O(V)
798
+ * one to rest pairs
799
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
758
800
  * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
759
801
  * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
760
802
  * @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
@@ -866,6 +908,12 @@ var AbstractGraph = /** @class */ (function () {
866
908
  /**
867
909
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
868
910
  * all pairs
911
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
912
+ */
913
+ /**
914
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
915
+ * all pairs
916
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
869
917
  * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
870
918
  * graph.
871
919
  * @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
@@ -1,15 +1,37 @@
1
1
  import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
2
2
  import type { IDirectedGraph, VertexId } from '../types';
3
3
  export declare class DirectedVertex extends AbstractVertex {
4
+ /**
5
+ * The constructor function initializes an object with a given id.
6
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
7
+ * vertex within a graph or network.
8
+ */
4
9
  constructor(id: VertexId);
5
10
  }
6
11
  export declare class DirectedEdge extends AbstractEdge {
12
+ /**
13
+ * The constructor function initializes the source and destination vertices of an edge, with an optional weight.
14
+ * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
15
+ * a graph.
16
+ * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
17
+ * to which an edge is directed.
18
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
19
+ * between two vertices.
20
+ */
7
21
  constructor(src: VertexId, dest: VertexId, weight?: number);
8
22
  private _src;
9
23
  get src(): VertexId;
24
+ /**
25
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
26
+ */
27
+ getSrc(): VertexId;
10
28
  set src(v: VertexId);
11
29
  private _dest;
12
30
  get dest(): VertexId;
31
+ /**
32
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
33
+ */
34
+ getDest(): VertexId;
13
35
  set dest(v: VertexId);
14
36
  }
15
37
  export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
@@ -123,10 +145,10 @@ export declare class DirectedGraph<V extends DirectedVertex, E extends DirectedE
123
145
  /**
124
146
  * when stored with adjacency list time: O(V+E)
125
147
  * when stored with adjacency matrix time: O(V^2)
126
- * The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
127
- * reverse order, or null if the graph contains a cycle.
128
- * @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
129
- * the graph. If there is a cycle in the graph, it returns `null`.
148
+ * The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
149
+ * order, or null if the graph contains a cycle.
150
+ * @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
151
+ * cycle in the graph. If there is a cycle, it returns `null`.
130
152
  */
131
153
  topologicalSort(): V[] | null;
132
154
  /**--- end find cycles --- */
@@ -53,13 +53,21 @@ var __values = (this && this.__values) || function(o) {
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
54
  exports.DirectedGraph = exports.DirectedEdge = exports.DirectedVertex = void 0;
55
55
  /**
56
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
57
- * @license MIT
56
+ * data-structure-typed
57
+ *
58
+ * @author Tyler Zeng
59
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
60
+ * @license MIT License
58
61
  */
59
62
  var utils_1 = require("../../utils");
60
63
  var abstract_graph_1 = require("./abstract-graph");
61
64
  var DirectedVertex = /** @class */ (function (_super) {
62
65
  __extends(DirectedVertex, _super);
66
+ /**
67
+ * The constructor function initializes an object with a given id.
68
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
69
+ * vertex within a graph or network.
70
+ */
63
71
  function DirectedVertex(id) {
64
72
  return _super.call(this, id) || this;
65
73
  }
@@ -68,6 +76,15 @@ var DirectedVertex = /** @class */ (function (_super) {
68
76
  exports.DirectedVertex = DirectedVertex;
69
77
  var DirectedEdge = /** @class */ (function (_super) {
70
78
  __extends(DirectedEdge, _super);
79
+ /**
80
+ * The constructor function initializes the source and destination vertices of an edge, with an optional weight.
81
+ * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
82
+ * a graph.
83
+ * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
84
+ * to which an edge is directed.
85
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
86
+ * between two vertices.
87
+ */
71
88
  function DirectedEdge(src, dest, weight) {
72
89
  var _this = _super.call(this, weight) || this;
73
90
  _this._src = src;
@@ -84,6 +101,12 @@ var DirectedEdge = /** @class */ (function (_super) {
84
101
  enumerable: false,
85
102
  configurable: true
86
103
  });
104
+ /**
105
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
106
+ */
107
+ DirectedEdge.prototype.getSrc = function () {
108
+ return this._src;
109
+ };
87
110
  Object.defineProperty(DirectedEdge.prototype, "dest", {
88
111
  get: function () {
89
112
  return this._dest;
@@ -94,6 +117,12 @@ var DirectedEdge = /** @class */ (function (_super) {
94
117
  enumerable: false,
95
118
  configurable: true
96
119
  });
120
+ /**
121
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
122
+ */
123
+ DirectedEdge.prototype.getDest = function () {
124
+ return this._dest;
125
+ };
97
126
  return DirectedEdge;
98
127
  }(abstract_graph_1.AbstractEdge));
99
128
  exports.DirectedEdge = DirectedEdge;
@@ -136,7 +165,7 @@ var DirectedGraph = /** @class */ (function (_super) {
136
165
  * graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
137
166
  */
138
167
  DirectedGraph.prototype.addEdge = function (edge) {
139
- if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
168
+ if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
140
169
  return false;
141
170
  }
142
171
  var srcVertex = this.getVertex(edge.src);
@@ -342,43 +371,14 @@ var DirectedGraph = /** @class */ (function (_super) {
342
371
  /**
343
372
  * when stored with adjacency list time: O(V+E)
344
373
  * when stored with adjacency matrix time: O(V^2)
345
- * The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
346
- * reverse order, or null if the graph contains a cycle.
347
- * @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
348
- * the graph. If there is a cycle in the graph, it returns `null`.
374
+ * The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
375
+ * order, or null if the graph contains a cycle.
376
+ * @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
377
+ * cycle in the graph. If there is a cycle, it returns `null`.
349
378
  */
350
379
  DirectedGraph.prototype.topologicalSort = function () {
351
380
  var e_2, _a, e_3, _b;
352
381
  var _this = this;
353
- // vector<vector<int>> g;
354
- // vector<int> color;
355
- // int last;
356
- // bool hasCycle;
357
- //
358
- // bool topo_sort() {
359
- // int n = g.size();
360
- // vector<int> degree(n, 0);
361
- // queue<int> q;
362
- // for (int i = 0; i < n; i++) {
363
- // degree[i] = g[i].size();
364
- // if (degree[i] <= 1) {
365
- // q.push(i);
366
- // }
367
- // }
368
- // int cnt = 0;
369
- // while (!q.empty()) {
370
- // cnt++;
371
- // int root = q.front();
372
- // q.pop();
373
- // for (auto child : g[root]) {
374
- // degree[child]--;
375
- // if (degree[child] == 1) {
376
- // q.push(child);
377
- // }
378
- // }
379
- // }
380
- // return (cnt != n);
381
- // }
382
382
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
383
383
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
384
384
  var statusMap = new Map();
@@ -438,9 +438,8 @@ var DirectedGraph = /** @class */ (function (_super) {
438
438
  }
439
439
  finally { if (e_3) throw e_3.error; }
440
440
  }
441
- if (hasCycle) {
441
+ if (hasCycle)
442
442
  return null;
443
- }
444
443
  return sorted.reverse();
445
444
  };
446
445
  /**--- end find cycles --- */
@@ -495,7 +494,7 @@ var DirectedGraph = /** @class */ (function (_super) {
495
494
  * returns null.
496
495
  */
497
496
  DirectedGraph.prototype.getEndsOfEdge = function (edge) {
498
- if (!this.containsEdge(edge.src, edge.dest)) {
497
+ if (!this.hasEdge(edge.src, edge.dest)) {
499
498
  return null;
500
499
  }
501
500
  var v1 = this.getVertex(edge.src);
@@ -1,16 +1,39 @@
1
1
  import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
2
2
  import type { VertexId } from '../types';
3
3
  export declare class UndirectedVertex extends AbstractVertex {
4
+ /**
5
+ * The constructor function initializes an object with a given id.
6
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
7
+ * vertex within a graph or network.
8
+ */
4
9
  constructor(id: VertexId);
5
10
  }
6
11
  export declare class UndirectedEdge extends AbstractEdge {
12
+ /**
13
+ * The constructor function initializes an instance of a class with two vertex IDs and an optional weight.
14
+ * @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
15
+ * @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
16
+ * graph.
17
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
18
+ * between two vertices.
19
+ */
7
20
  constructor(v1: VertexId, v2: VertexId, weight?: number);
8
21
  private _vertices;
9
22
  get vertices(): [VertexId, VertexId];
23
+ /**
24
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
25
+ */
26
+ getVertices(): [VertexId, VertexId];
10
27
  set vertices(v: [VertexId, VertexId]);
11
28
  }
12
29
  export declare class UndirectedGraph<V extends UndirectedVertex, E extends UndirectedEdge> extends AbstractGraph<V, E> {
13
30
  protected _edges: Map<V, E[]>;
31
+ get edges(): Map<V, E[]>;
32
+ /**
33
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
34
+ */
35
+ getEdges(): Map<V, E[]>;
36
+ protected set edges(v: Map<V, E[]>);
14
37
  constructor();
15
38
  /**
16
39
  * The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
@@ -53,13 +53,21 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
54
  exports.UndirectedGraph = exports.UndirectedEdge = exports.UndirectedVertex = void 0;
55
55
  /**
56
- * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
57
- * @license MIT
56
+ * data-structure-typed
57
+ *
58
+ * @author Tyler Zeng
59
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
60
+ * @license MIT License
58
61
  */
59
62
  var utils_1 = require("../../utils");
60
63
  var abstract_graph_1 = require("./abstract-graph");
61
64
  var UndirectedVertex = /** @class */ (function (_super) {
62
65
  __extends(UndirectedVertex, _super);
66
+ /**
67
+ * The constructor function initializes an object with a given id.
68
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
69
+ * vertex within a graph or network.
70
+ */
63
71
  function UndirectedVertex(id) {
64
72
  return _super.call(this, id) || this;
65
73
  }
@@ -68,6 +76,14 @@ var UndirectedVertex = /** @class */ (function (_super) {
68
76
  exports.UndirectedVertex = UndirectedVertex;
69
77
  var UndirectedEdge = /** @class */ (function (_super) {
70
78
  __extends(UndirectedEdge, _super);
79
+ /**
80
+ * The constructor function initializes an instance of a class with two vertex IDs and an optional weight.
81
+ * @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
82
+ * @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
83
+ * graph.
84
+ * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
85
+ * between two vertices.
86
+ */
71
87
  function UndirectedEdge(v1, v2, weight) {
72
88
  var _this = _super.call(this, weight) || this;
73
89
  _this._vertices = [v1, v2];
@@ -83,6 +99,12 @@ var UndirectedEdge = /** @class */ (function (_super) {
83
99
  enumerable: false,
84
100
  configurable: true
85
101
  });
102
+ /**
103
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
104
+ */
105
+ UndirectedEdge.prototype.getVertices = function () {
106
+ return this._vertices;
107
+ };
86
108
  return UndirectedEdge;
87
109
  }(abstract_graph_1.AbstractEdge));
88
110
  exports.UndirectedEdge = UndirectedEdge;
@@ -93,6 +115,22 @@ var UndirectedGraph = /** @class */ (function (_super) {
93
115
  _this._edges = new Map();
94
116
  return _this;
95
117
  }
118
+ Object.defineProperty(UndirectedGraph.prototype, "edges", {
119
+ get: function () {
120
+ return this._edges;
121
+ },
122
+ set: function (v) {
123
+ this._edges = v;
124
+ },
125
+ enumerable: false,
126
+ configurable: true
127
+ });
128
+ /**
129
+ * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
130
+ */
131
+ UndirectedGraph.prototype.getEdges = function () {
132
+ return this._edges;
133
+ };
96
134
  /**
97
135
  * The function `getEdge` returns the first edge that connects two vertices, or null if no such edge exists.
98
136
  * @param {V | null | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID
@@ -277,7 +315,7 @@ var UndirectedGraph = /** @class */ (function (_super) {
277
315
  * `null`.
278
316
  */
279
317
  UndirectedGraph.prototype.getEndsOfEdge = function (edge) {
280
- if (!this.containsEdge(edge.vertices[0], edge.vertices[1])) {
318
+ if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
281
319
  return null;
282
320
  }
283
321
  var v1 = this.getVertex(edge.vertices[0]);