data-structure-typed 0.9.16 → 1.12.10

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 (199) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/README.md +102 -1
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
  5. package/dist/data-structures/binary-tree/avl-tree.js +57 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
  8. package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
  9. package/dist/data-structures/binary-tree/binary-tree.js +283 -15
  10. package/dist/data-structures/binary-tree/bst.d.ts +16 -0
  11. package/dist/data-structures/binary-tree/bst.js +12 -0
  12. package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +4 -0
  14. package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
  16. package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
  17. package/dist/data-structures/graph/abstract-graph.js +166 -12
  18. package/dist/data-structures/graph/directed-graph.d.ts +111 -1
  19. package/dist/data-structures/graph/directed-graph.js +122 -0
  20. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  21. package/dist/data-structures/graph/undirected-graph.js +67 -0
  22. package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
  23. package/dist/data-structures/hash/coordinate-map.js +32 -0
  24. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  25. package/dist/data-structures/hash/coordinate-set.js +25 -0
  26. package/dist/data-structures/heap/heap.d.ts +26 -31
  27. package/dist/data-structures/heap/heap.js +23 -32
  28. package/dist/data-structures/heap/max-heap.d.ts +6 -1
  29. package/dist/data-structures/heap/max-heap.js +6 -1
  30. package/dist/data-structures/heap/min-heap.d.ts +7 -1
  31. package/dist/data-structures/heap/min-heap.js +7 -1
  32. package/dist/data-structures/index.d.ts +0 -1
  33. package/dist/data-structures/index.js +0 -1
  34. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
  35. package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
  36. package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
  37. package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
  38. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  39. package/dist/data-structures/matrix/matrix.js +11 -0
  40. package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
  41. package/dist/data-structures/matrix/matrix2d.js +82 -2
  42. package/dist/data-structures/matrix/navigator.d.ts +32 -0
  43. package/dist/data-structures/matrix/navigator.js +28 -0
  44. package/dist/data-structures/matrix/vector2d.d.ts +139 -15
  45. package/dist/data-structures/matrix/vector2d.js +139 -15
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
  51. package/dist/data-structures/priority-queue/priority-queue.js +126 -0
  52. package/dist/data-structures/queue/deque.d.ts +69 -0
  53. package/dist/data-structures/queue/deque.js +69 -0
  54. package/dist/data-structures/queue/queue.d.ts +30 -32
  55. package/dist/data-structures/queue/queue.js +30 -32
  56. package/dist/data-structures/stack/stack.d.ts +26 -31
  57. package/dist/data-structures/stack/stack.js +26 -31
  58. package/dist/data-structures/trie/trie.d.ts +26 -6
  59. package/dist/data-structures/trie/trie.js +26 -7
  60. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  61. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  62. package/dist/data-structures/types/index.d.ts +1 -1
  63. package/dist/data-structures/types/index.js +1 -1
  64. package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
  65. package/dist/data-structures/types/tree-multiset.d.ts +1 -1
  66. package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
  67. package/dist/utils/types/index.d.ts +1 -0
  68. package/dist/utils/types/index.js +17 -0
  69. package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
  70. package/dist/{data-structures → utils}/types/utils.js +1 -0
  71. package/dist/utils/utils.d.ts +1 -1
  72. package/docs/.nojekyll +1 -0
  73. package/docs/assets/highlight.css +85 -0
  74. package/docs/assets/main.js +58 -0
  75. package/docs/assets/search.js +1 -0
  76. package/docs/assets/style.css +1367 -0
  77. package/docs/classes/AVLTree.html +2046 -0
  78. package/docs/classes/AVLTreeNode.html +423 -0
  79. package/docs/classes/AaTree.html +117 -0
  80. package/docs/classes/AbstractEdge.html +198 -0
  81. package/docs/classes/AbstractGraph.html +891 -0
  82. package/docs/classes/AbstractVertex.html +164 -0
  83. package/docs/classes/ArrayDeque.html +384 -0
  84. package/docs/classes/BST.html +1893 -0
  85. package/docs/classes/BSTNode.html +425 -0
  86. package/docs/classes/BTree.html +117 -0
  87. package/docs/classes/BinaryIndexedTree.html +244 -0
  88. package/docs/classes/BinaryTree.html +1754 -0
  89. package/docs/classes/BinaryTreeNode.html +396 -0
  90. package/docs/classes/Character.html +165 -0
  91. package/docs/classes/CoordinateMap.html +394 -0
  92. package/docs/classes/CoordinateSet.html +355 -0
  93. package/docs/classes/Deque.html +617 -0
  94. package/docs/classes/DirectedEdge.html +247 -0
  95. package/docs/classes/DirectedGraph.html +1207 -0
  96. package/docs/classes/DirectedVertex.html +154 -0
  97. package/docs/classes/DoublyLinkedList.html +619 -0
  98. package/docs/classes/DoublyLinkedListNode.html +160 -0
  99. package/docs/classes/Heap.html +315 -0
  100. package/docs/classes/Matrix2D.html +447 -0
  101. package/docs/classes/MatrixNTI2D.html +181 -0
  102. package/docs/classes/MaxHeap.html +325 -0
  103. package/docs/classes/MaxPriorityQueue.html +668 -0
  104. package/docs/classes/MinHeap.html +326 -0
  105. package/docs/classes/MinPriorityQueue.html +668 -0
  106. package/docs/classes/Navigator.html +285 -0
  107. package/docs/classes/ObjectDeque.html +289 -0
  108. package/docs/classes/PriorityQueue.html +643 -0
  109. package/docs/classes/Queue.html +337 -0
  110. package/docs/classes/RBTree.html +117 -0
  111. package/docs/classes/SegmentTree.html +234 -0
  112. package/docs/classes/SegmentTreeNode.html +302 -0
  113. package/docs/classes/SinglyLinkedList.html +1035 -0
  114. package/docs/classes/SinglyLinkedListNode.html +304 -0
  115. package/docs/classes/SplayTree.html +117 -0
  116. package/docs/classes/Stack.html +313 -0
  117. package/docs/classes/TreeMultiSet.html +1897 -0
  118. package/docs/classes/Trie.html +317 -0
  119. package/docs/classes/TrieNode.html +221 -0
  120. package/docs/classes/TwoThreeTree.html +117 -0
  121. package/docs/classes/UndirectedEdge.html +220 -0
  122. package/docs/classes/UndirectedGraph.html +1006 -0
  123. package/docs/classes/UndirectedVertex.html +154 -0
  124. package/docs/classes/Vector2D.html +746 -0
  125. package/docs/enums/CP.html +126 -0
  126. package/docs/enums/FamilyPosition.html +126 -0
  127. package/docs/enums/LoopType.html +119 -0
  128. package/docs/index.html +288 -0
  129. package/docs/modules.html +146 -0
  130. package/jest.config.js +5 -0
  131. package/package.json +19 -11
  132. package/rename_clear_files.sh +29 -0
  133. package/src/assets/complexities-diff.jpg +0 -0
  134. package/src/assets/data-structure-complexities.jpg +0 -0
  135. package/src/data-structures/binary-tree/avl-tree.ts +57 -0
  136. package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
  137. package/src/data-structures/binary-tree/binary-tree.ts +288 -18
  138. package/src/data-structures/binary-tree/bst.ts +16 -1
  139. package/src/data-structures/binary-tree/segment-tree.ts +5 -0
  140. package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
  141. package/src/data-structures/diagrams/README.md +5 -7
  142. package/src/data-structures/graph/abstract-graph.ts +167 -15
  143. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  148. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  149. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  150. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  151. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  152. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  153. package/src/data-structures/graph/directed-graph.ts +127 -5
  154. package/src/data-structures/graph/undirected-graph.ts +67 -1
  155. package/src/data-structures/hash/coordinate-map.ts +32 -0
  156. package/src/data-structures/hash/coordinate-set.ts +25 -0
  157. package/src/data-structures/heap/heap.ts +27 -32
  158. package/src/data-structures/heap/max-heap.ts +6 -1
  159. package/src/data-structures/heap/min-heap.ts +7 -1
  160. package/src/data-structures/index.ts +0 -1
  161. package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
  162. package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
  163. package/src/data-structures/matrix/matrix.ts +11 -0
  164. package/src/data-structures/matrix/matrix2d.ts +82 -2
  165. package/src/data-structures/matrix/navigator.ts +32 -0
  166. package/src/data-structures/matrix/vector2d.ts +139 -15
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +130 -0
  170. package/src/data-structures/queue/deque.ts +69 -0
  171. package/src/data-structures/queue/queue.ts +30 -32
  172. package/src/data-structures/stack/stack.ts +26 -31
  173. package/src/data-structures/trie/trie.ts +26 -7
  174. package/src/data-structures/types/avl-tree.ts +1 -1
  175. package/src/data-structures/types/binary-tree.ts +1 -1
  176. package/src/data-structures/types/bst.ts +1 -1
  177. package/src/data-structures/types/index.ts +1 -1
  178. package/src/data-structures/types/navigator.ts +1 -0
  179. package/src/data-structures/types/singly-linked-list.ts +1 -15
  180. package/src/data-structures/types/tree-multiset.ts +1 -1
  181. package/src/{data-structures → utils}/trampoline.ts +6 -6
  182. package/src/utils/types/index.ts +1 -0
  183. package/src/{data-structures → utils}/types/utils.ts +7 -0
  184. package/src/utils/utils.ts +12 -2
  185. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  186. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  187. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  188. package/tests/unit/data-structures/graph/index.ts +3 -0
  189. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  191. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  192. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  194. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  195. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  196. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  197. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  198. package/src/data-structures/graph/diagrams/mst.png +0 -0
  199. /package/dist/{data-structures → utils}/trampoline.js +0 -0
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  import {arrayRemove, uuidV4} from '../../utils';
2
6
  import {PriorityQueue} from '../priority-queue';
3
7
  import type {DijkstraResult, IGraph, VertexId} from '../types';
@@ -7,7 +11,7 @@ export class AbstractVertex {
7
11
  this._id = id;
8
12
  }
9
13
 
10
- private _id: VertexId;
14
+ protected _id: VertexId;
11
15
 
12
16
  public get id(): VertexId {
13
17
  return this._id;
@@ -58,25 +62,55 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
58
62
 
59
63
  abstract removeEdge(edge: E): E | null;
60
64
 
65
+ /**
66
+ * The function `getVertex` returns the vertex object associated with a given vertex ID or vertex object, or null if it
67
+ * does not exist.
68
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
69
+ * @returns The function `getVertex` returns the vertex object (`V`) corresponding to the given `vertexOrId` parameter.
70
+ * If the vertex is found in the `_vertices` map, it is returned. Otherwise, `null` is returned.
71
+ */
61
72
  getVertex(vertexOrId: VertexId | V): V | null {
62
73
  const vertexId = this.getVertexId(vertexOrId);
63
74
  return this._vertices.get(vertexId) || null;
64
75
  }
65
76
 
77
+ /**
78
+ * The function `getVertexId` returns the id of a vertex, whether it is passed as an instance of `AbstractVertex` or as
79
+ * a `VertexId`.
80
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
81
+ * (`VertexId`).
82
+ * @returns the id of the vertex.
83
+ */
66
84
  getVertexId(vertexOrId: V | VertexId): VertexId {
67
85
  return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
68
86
  }
69
87
 
88
+ /**
89
+ * The function checks if a vertex exists in a graph.
90
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
91
+ * (`VertexId`).
92
+ * @returns The method `containsVertex` returns a boolean value.
93
+ */
70
94
  containsVertex(vertexOrId: V | VertexId): boolean {
71
95
  return this._vertices.has(this.getVertexId(vertexOrId));
72
96
  }
73
97
 
98
+ /**
99
+ * The function `vertexSet()` returns a map of vertices.
100
+ * @returns The method `vertexSet()` returns a map of vertex IDs to vertex objects.
101
+ */
74
102
  vertexSet(): Map<VertexId, V> {
75
103
  return this._vertices;
76
104
  }
77
105
 
78
106
  abstract getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
79
107
 
108
+ /**
109
+ * The addVertex function adds a new vertex to a graph if it does not already exist.
110
+ * @param {V} newVertex - The parameter "newVertex" is of type V, which represents a vertex in a graph.
111
+ * @returns The method is returning a boolean value. If the newVertex is already contained in the graph, it will return
112
+ * false. Otherwise, it will add the newVertex to the graph and return true.
113
+ */
80
114
  addVertex(newVertex: V): boolean {
81
115
  if (this.containsVertex(newVertex)) {
82
116
  return false;
@@ -85,11 +119,24 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
85
119
  return true;
86
120
  }
87
121
 
122
+ /**
123
+ * The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
124
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
125
+ * (`VertexId`).
126
+ * @returns The method `removeVertex` returns a boolean value.
127
+ */
88
128
  removeVertex(vertexOrId: V | VertexId): boolean {
89
129
  const vertexId = this.getVertexId(vertexOrId);
90
130
  return this._vertices.delete(vertexId);
91
131
  }
92
132
 
133
+ /**
134
+ * The function removes all vertices from a graph and returns a boolean indicating if any vertices were removed.
135
+ * @param {V[] | VertexId[]} vertices - The `vertices` parameter can be either an array of vertices (`V[]`) or an array
136
+ * of vertex IDs (`VertexId[]`).
137
+ * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
138
+ * were removed.
139
+ */
93
140
  removeAllVertices(vertices: V[] | VertexId[]): boolean {
94
141
  const removed: boolean[] = [];
95
142
  for (const v of vertices) {
@@ -104,6 +151,15 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
104
151
 
105
152
  abstract edgesOf(vertexOrId: V | VertexId): E[];
106
153
 
154
+ /**
155
+ * The function checks if there is an edge between two vertices in a graph.
156
+ * @param {VertexId | V} v1 - The parameter v1 can be either a VertexId or a V. A VertexId represents the identifier of
157
+ * a vertex in a graph, while V represents the type of the vertex itself.
158
+ * @param {VertexId | V} v2 - The parameter `v2` represents the second vertex in an edge. It can be either a `VertexId`
159
+ * or a `V` type.
160
+ * @returns The function `containsEdge` returns a boolean value. It returns `true` if there is an edge between the
161
+ * vertices `v1` and `v2`, and `false` otherwise.
162
+ */
107
163
  containsEdge(v1: VertexId | V, v2: VertexId | V): boolean {
108
164
  const edge = this.getEdge(v1, v2);
109
165
  return !!edge;
@@ -111,6 +167,17 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
111
167
 
112
168
  abstract addEdge(edge: E): boolean;
113
169
 
170
+ /**
171
+ * The function sets the weight of an edge between two vertices in a graph.
172
+ * @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
173
+ * the source vertex of the edge.
174
+ * @param {VertexId | V} destOrId - The `destOrId` parameter represents the destination vertex of the edge. It can be
175
+ * either a `VertexId` or a vertex object `V`.
176
+ * @param {number} weight - The weight parameter represents the weight of the edge between the source vertex (srcOrId)
177
+ * and the destination vertex (destOrId).
178
+ * @returns a boolean value. If the edge exists between the source and destination vertices, the function will update
179
+ * the weight of the edge and return true. If the edge does not exist, the function will return false.
180
+ */
114
181
  setEdgeWeight(srcOrId: VertexId | V, destOrId: VertexId | V, weight: number): boolean {
115
182
  const edge = this.getEdge(srcOrId, destOrId);
116
183
  if (edge) {
@@ -123,6 +190,15 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
123
190
 
124
191
  abstract getNeighbors(vertexOrId: V | VertexId): V[];
125
192
 
193
+ /**
194
+ * The function `getAllPathsBetween` finds all paths between two vertices in a graph using depth-first search.
195
+ * @param {V | VertexId} v1 - The parameter `v1` represents either a vertex object (`V`) or a vertex ID (`VertexId`).
196
+ * It is the starting vertex for finding paths.
197
+ * @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
198
+ * want to find paths to from the starting vertex `v1`.
199
+ * @returns an array of arrays of vertices (V[][]). Each inner array represents a path between the given vertices (v1
200
+ * and v2).
201
+ */
126
202
  getAllPathsBetween(v1: V | VertexId, v2: V | VertexId): V[][] {
127
203
  const paths: V[][] = [];
128
204
  const vertex1 = this.getVertex(v1);
@@ -154,7 +230,11 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
154
230
  return paths;
155
231
  }
156
232
 
157
-
233
+ /**
234
+ * The function calculates the sum of weights along a given path.
235
+ * @param {V[]} path - An array of vertices (V) representing a path in a graph.
236
+ * @returns The function `getPathSumWeight` returns the sum of the weights of the edges in the given path.
237
+ */
158
238
  getPathSumWeight(path: V[]): number {
159
239
  let sum = 0;
160
240
  for (let i = 0; i < path.length; i++) {
@@ -163,6 +243,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
163
243
  return sum;
164
244
  }
165
245
 
246
+ /**
247
+ * The function `getMinCostBetween` calculates the minimum cost between two vertices in a graph, either based on edge
248
+ * weights or using a breadth-first search algorithm.
249
+ * @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or vertex ID of the graph.
250
+ * @param {V | VertexId} v2 - The parameter `v2` represents the second vertex in the graph. It can be either a vertex
251
+ * object or a vertex ID.
252
+ * @param {boolean} [isWeight] - isWeight is an optional parameter that indicates whether the graph edges have weights.
253
+ * If isWeight is set to true, the function will calculate the minimum cost between v1 and v2 based on the weights of
254
+ * the edges. If isWeight is set to false or not provided, the function will calculate the
255
+ * @returns The function `getMinCostBetween` returns a number representing the minimum cost between two vertices (`v1`
256
+ * and `v2`) in a graph. If the `isWeight` parameter is `true`, it calculates the minimum weight between the vertices.
257
+ * If `isWeight` is `false` or not provided, it calculates the minimum number of edges between the vertices. If the
258
+ * vertices are not
259
+ */
166
260
  getMinCostBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): number | null {
167
261
  if (isWeight === undefined) isWeight = false;
168
262
 
@@ -208,6 +302,18 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
208
302
  }
209
303
  }
210
304
 
305
+ /**
306
+ * The function `getMinPathBetween` returns the minimum path between two vertices in a graph, either based on weight or
307
+ * using a breadth-first search algorithm.
308
+ * @param {V | VertexId} v1 - The parameter `v1` represents the starting vertex or its ID.
309
+ * @param {V | VertexId} v2 - The parameter `v2` represents the destination vertex or its ID. It is the vertex that we
310
+ * want to find the minimum path to from the source vertex `v1`.
311
+ * @param {boolean} [isWeight] - A boolean flag indicating whether to consider the weight of edges in finding the
312
+ * minimum path. If set to true, the function will use Dijkstra's algorithm to find the minimum weighted path. If set
313
+ * to false, the function will use breadth-first search (BFS) to find the minimum path. If
314
+ * @returns The function `getMinPathBetween` returns an array of vertices (`V[]`) representing the minimum path between
315
+ * two vertices (`v1` and `v2`). If no path is found, it returns `null`.
316
+ */
211
317
  getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null {
212
318
  if (isWeight === undefined) isWeight = false;
213
319
 
@@ -261,10 +367,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
261
367
 
262
368
  /**
263
369
  * Dijkstra algorithm time: O(VE) space: O(V + E)
264
- * @param src
265
- * @param dest
266
- * @param getMinDist
267
- * @param genPaths
370
+ * The function `dijkstraWithoutHeap` implements Dijkstra's algorithm to find the shortest path between two vertices in
371
+ * a graph without using a heap data structure.
372
+ * @param {V | VertexId} src - The source vertex from which to start the Dijkstra's algorithm. It can be either a
373
+ * vertex object or a vertex ID.
374
+ * @param {V | VertexId | null} [dest] - The `dest` parameter in the `dijkstraWithoutHeap` function is an optional
375
+ * parameter that specifies the destination vertex for the Dijkstra algorithm. It can be either a vertex object or its
376
+ * identifier. If no destination is provided, the value is set to `null`.
377
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
378
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
379
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
380
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
381
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
382
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
383
+ * @returns The function `dijkstraWithoutHeap` returns an object of type `DijkstraResult<V>`.
268
384
  */
269
385
  dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V> {
270
386
  if (getMinDist === undefined) getMinDist = false;
@@ -375,13 +491,22 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
375
491
  return {distMap, preMap, seen, paths, minDist, minPath};
376
492
  }
377
493
 
378
-
379
494
  /**
380
495
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
381
- * @param src
382
- * @param dest
383
- * @param getMinDist
384
- * @param genPaths
496
+ * The `dijkstra` function implements Dijkstra's algorithm to find the shortest path between a source vertex and an
497
+ * optional destination vertex, and optionally returns the minimum distance, the paths, and other information.
498
+ * @param {V | VertexId} src - The `src` parameter represents the source vertex from which the Dijkstra algorithm will
499
+ * start. It can be either a vertex object or a vertex ID.
500
+ * @param {V | VertexId | null} [dest] - The `dest` parameter is the destination vertex or vertex ID. It specifies the
501
+ * vertex to which the shortest path is calculated from the source vertex. If no destination is provided, the algorithm
502
+ * will calculate the shortest paths to all other vertices from the source vertex.
503
+ * @param {boolean} [getMinDist] - The `getMinDist` parameter is a boolean flag that determines whether the minimum
504
+ * distance from the source vertex to the destination vertex should be calculated and returned in the result. If
505
+ * `getMinDist` is set to `true`, the `minDist` property in the result will contain the minimum distance
506
+ * @param {boolean} [genPaths] - The `genPaths` parameter is a boolean flag that determines whether or not to generate
507
+ * paths in the Dijkstra algorithm. If `genPaths` is set to `true`, the algorithm will calculate and return the
508
+ * shortest paths from the source vertex to all other vertices in the graph. If `genPaths
509
+ * @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
385
510
  */
386
511
  dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V> {
387
512
  if (getMinDist === undefined) getMinDist = false;
@@ -496,10 +621,17 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
496
621
  /**
497
622
  * BellmanFord time:O(VE) space:O(V)
498
623
  * one to rest pairs
499
- * @param src
500
- * @param scanNegativeCycle
501
- * @param getMin
502
- * @param genPath
624
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
625
+ * all other vertices in a graph, and optionally detects negative cycles and generates the minimum path.
626
+ * @param {V | VertexId} src - The `src` parameter is the source vertex from which the Bellman-Ford algorithm will
627
+ * start calculating the shortest paths. It can be either a vertex object or a vertex ID.
628
+ * @param {boolean} [scanNegativeCycle] - A boolean flag indicating whether to scan for negative cycles in the graph.
629
+ * @param {boolean} [getMin] - The `getMin` parameter is a boolean flag that determines whether the algorithm should
630
+ * calculate the minimum distance from the source vertex to all other vertices in the graph. If `getMin` is set to
631
+ * `true`, the algorithm will find the minimum distance and update the `min` variable with the minimum
632
+ * @param {boolean} [genPath] - A boolean flag indicating whether to generate paths for all vertices from the source
633
+ * vertex.
634
+ * @returns The function `bellmanFord` returns an object with the following properties:
503
635
  */
504
636
  bellmanFord(src: V | VertexId, scanNegativeCycle?: boolean, getMin?: boolean, genPath?: boolean) {
505
637
  if (getMin === undefined) getMin = false;
@@ -592,6 +724,12 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
592
724
  /**
593
725
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
594
726
  * all pairs
727
+ * The function implements the Floyd-Warshall algorithm to find the shortest path between all pairs of vertices in a
728
+ * graph.
729
+ * @returns The function `floyd()` returns an object with two properties: `costs` and `predecessor`. The `costs`
730
+ * property is a 2D array of numbers representing the shortest path costs between vertices in a graph. The
731
+ * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
732
+ * path between vertices in the
595
733
  */
596
734
  floyd(): { costs: number[][], predecessor: (V | null)[][] } {
597
735
  const idAndVertices = [...this._vertices];
@@ -638,6 +776,20 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
638
776
  * Tarjan can find the articulation points and bridges(critical edges) of undirected graphs in linear time,
639
777
  * Tarjan solve the bi-connected components of undirected graphs;
640
778
  * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
779
+ * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
780
+ * strongly connected components (SCCs), and cycles in a graph.
781
+ * @param {boolean} [needArticulationPoints] - A boolean value indicating whether or not to calculate and return the
782
+ * articulation points in the graph. Articulation points are the vertices in a graph whose removal would increase the
783
+ * number of connected components in the graph.
784
+ * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
785
+ * (edges whose removal would increase the number of connected components in the graph).
786
+ * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
787
+ * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
788
+ * SCCs will not be calculated or returned.
789
+ * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
790
+ * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
791
+ * are arrays of vertices that form cycles within the SCCs.
792
+ * @returns The function `tarjan` returns an object with the following properties:
641
793
  */
642
794
  tarjan(needArticulationPoints?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean) {
643
795
  // !! in undirected graph we will not let child visit parent when DFS
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  import {arrayRemove} from '../../utils';
2
6
  import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
3
7
  import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types';
@@ -46,6 +50,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
46
50
  super();
47
51
  }
48
52
 
53
+ /**
54
+ * The function `getEdge` returns the first edge between two vertices, given their source and destination.
55
+ * @param {V | null | VertexId} srcOrId - The `srcOrId` parameter can be either a vertex object (`V`), a vertex ID
56
+ * (`VertexId`), or `null`. It represents the source vertex of the edge.
57
+ * @param {V | null | VertexId} destOrId - The `destOrId` parameter is either a vertex object (`V`), a vertex ID
58
+ * (`VertexId`), or `null`. It represents the destination vertex of the edge.
59
+ * @returns an edge (E) or null.
60
+ */
49
61
  getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
50
62
  let edges: E[] = [];
51
63
 
@@ -64,6 +76,13 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
64
76
  return edges[0] || null;
65
77
  }
66
78
 
79
+ /**
80
+ * The `addEdge` function adds an edge to a graph if the source and destination vertices exist.
81
+ * @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in the graph. It contains
82
+ * information about the source vertex (`src`) and the destination vertex (`dest`) of the edge.
83
+ * @returns The `addEdge` function returns a boolean value. It returns `true` if the edge was successfully added to the
84
+ * graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
85
+ */
67
86
  addEdge(edge: E): boolean {
68
87
  if (!(this.containsVertex(edge.src) && this.containsVertex(edge.dest))) {
69
88
  return false;
@@ -93,6 +112,16 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
93
112
  }
94
113
  }
95
114
 
115
+ /**
116
+ * The function removes an edge between two vertices in a directed graph and returns the removed edge.
117
+ * @param {V | VertexId} srcOrId - The source vertex or its ID.
118
+ * @param {V | VertexId} destOrId - The `destOrId` parameter in the `removeEdgeBetween` function represents the
119
+ * destination vertex of the edge that needs to be removed. It can be either a vertex object (`V`) or a vertex ID
120
+ * (`VertexId`).
121
+ * @returns The function `removeEdgeBetween` returns the removed edge (`E`) if the edge between the source and
122
+ * destination vertices is successfully removed. If either the source or destination vertex is not found, or if the
123
+ * edge does not exist, it returns `null`.
124
+ */
96
125
  removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
97
126
 
98
127
  const src: V | null = this.getVertex(srcOrId);
@@ -104,6 +133,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
104
133
 
105
134
  const srcOutEdges = this._outEdgeMap.get(src);
106
135
  if (srcOutEdges) {
136
+ /**
137
+ * The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
138
+ * found.
139
+ * @param {E} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
140
+ * object that has `src` and `dest` properties, which represent the source and destination vertices of the edge,
141
+ * respectively.
142
+ * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
143
+ */
107
144
  arrayRemove<E>(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
108
145
  }
109
146
 
@@ -114,7 +151,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
114
151
  return removed;
115
152
  }
116
153
 
117
- removeEdge(edge: E): E | null {
154
+ /**
155
+ * The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
156
+ * found.
157
+ * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
158
+ * and `dest`, which represent the source and destination vertices of the edge, respectively.
159
+ * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
160
+ */
161
+ removeEdge(edge: E ): E | null {
118
162
  let removed: E | null = null;
119
163
  const src = this.getVertex(edge.src);
120
164
  const dest = this.getVertex(edge.dest);
@@ -134,10 +178,24 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
134
178
  return removed;
135
179
  }
136
180
 
181
+ /**
182
+ * The function removeAllEdges removes all edges between two vertices.
183
+ * @param {VertexId | V} src - The `src` parameter represents the source vertex from which the edges will be removed.
184
+ * It can be either a `VertexId` or a `V` type, which represents the identifier or object of the vertex.
185
+ * @param {VertexId | V} dest - The `dest` parameter represents the destination vertex of an edge. It can be either a
186
+ * `VertexId` or a vertex object `V`.
187
+ * @returns An empty array is being returned.
188
+ */
137
189
  removeAllEdges(src: VertexId | V, dest: VertexId | V): E[] {
138
190
  return [];
139
191
  }
140
192
 
193
+ /**
194
+ * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
195
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
196
+ * (`VertexId`).
197
+ * @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
198
+ */
141
199
  incomingEdgesOf(vertexOrId: V | VertexId): E[] {
142
200
  const target = this.getVertex(vertexOrId);
143
201
  if (target) {
@@ -146,6 +204,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
146
204
  return [];
147
205
  }
148
206
 
207
+ /**
208
+ * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
209
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
210
+ * (`VertexId`).
211
+ * @returns The method `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
212
+ */
149
213
  outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
150
214
  const target = this.getVertex(vertexOrId);
151
215
  if (target) {
@@ -154,30 +218,67 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
154
218
  return [];
155
219
  }
156
220
 
221
+ /**
222
+ * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
223
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
224
+ * @returns the sum of the out-degree and in-degree of the specified vertex or vertex ID.
225
+ */
157
226
  degreeOf(vertexOrId: VertexId | V): number {
158
227
  return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
159
228
  }
160
229
 
230
+ /**
231
+ * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
232
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
233
+ * @returns The number of incoming edges of the specified vertex or vertex ID.
234
+ */
161
235
  inDegreeOf(vertexOrId: VertexId | V): number {
162
236
  return this.incomingEdgesOf(vertexOrId).length;
163
237
  }
164
238
 
239
+ /**
240
+ * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
241
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
242
+ * @returns The number of outgoing edges from the specified vertex or vertex ID.
243
+ */
165
244
  outDegreeOf(vertexOrId: VertexId | V): number {
166
245
  return this.outgoingEdgesOf(vertexOrId).length;
167
246
  }
168
247
 
248
+ /**
249
+ * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
250
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
251
+ * @returns The function `edgesOf` returns an array of edges.
252
+ */
169
253
  edgesOf(vertexOrId: VertexId | V): E[] {
170
254
  return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
171
255
  }
172
256
 
257
+ /**
258
+ * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
259
+ * @param {E} e - E - an edge object
260
+ * @returns the source vertex of the given edge, or null if the edge does not exist.
261
+ */
173
262
  getEdgeSrc(e: E): V | null {
174
263
  return this.getVertex(e.src);
175
264
  }
176
265
 
266
+ /**
267
+ * The function "getEdgeDest" returns the vertex associated with the destination of an edge.
268
+ * @param {E} e - The parameter `e` is of type `E`, which represents an edge in a graph.
269
+ * @returns either a vertex object (of type V) or null.
270
+ */
177
271
  getEdgeDest(e: E): V | null {
178
272
  return this.getVertex(e.dest);
179
273
  }
180
274
 
275
+ /**
276
+ * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
277
+ * @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
278
+ * find the destinations. It can be either a `V` object, a `VertexId` (which is a unique identifier for a vertex), or
279
+ * `null` if we want to find destinations from all vertices.
280
+ * @returns an array of vertices (V[]).
281
+ */
181
282
  getDestinations(vertex: V | VertexId | null): V[] {
182
283
  if (vertex === null) {
183
284
  return [];
@@ -198,8 +299,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
198
299
  /**
199
300
  * when stored with adjacency list time: O(V+E)
200
301
  * when stored with adjacency matrix time: O(V^2)
302
+ * The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
303
+ * reverse order, or null if the graph contains a cycle.
304
+ * @returns The function `topologicalSort()` returns an array of vertices in topological order if there is no cycle in
305
+ * the graph. If there is a cycle in the graph, it returns `null`.
201
306
  */
202
- topologicalSort(): (V | VertexId)[] | null {
307
+ topologicalSort(): V[] | null {
203
308
  // vector<vector<int>> g;
204
309
  // vector<int> color;
205
310
  // int last;
@@ -231,14 +336,14 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
231
336
  // }
232
337
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
233
338
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
234
- const statusMap: Map<V | VertexId, TopologicalStatus> = new Map<V, TopologicalStatus>();
339
+ const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
235
340
  for (const entry of this._vertices) {
236
341
  statusMap.set(entry[1], 0);
237
342
  }
238
343
 
239
- const sorted: (V | VertexId)[] = [];
344
+ const sorted: (V)[] = [];
240
345
  let hasCycle = false;
241
- const dfs = (cur: V | VertexId) => {
346
+ const dfs = (cur: V) => {
242
347
  statusMap.set(cur, 1);
243
348
  const children = this.getDestinations(cur);
244
349
  for (const child of children) {
@@ -267,6 +372,10 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
267
372
 
268
373
  /**--- end find cycles --- */
269
374
 
375
+ /**
376
+ * The `edgeSet` function returns an array of all the edges in the graph.
377
+ * @returns The `edgeSet()` method returns an array of edges (`E[]`).
378
+ */
270
379
  edgeSet(): E[] {
271
380
  let edges: E[] = [];
272
381
  this._outEdgeMap.forEach(outEdges => {
@@ -275,6 +384,12 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
275
384
  return edges;
276
385
  }
277
386
 
387
+ /**
388
+ * The function `getNeighbors` returns an array of neighboring vertices for a given vertex or vertex ID.
389
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
390
+ * (`VertexId`).
391
+ * @returns an array of vertices (V[]).
392
+ */
278
393
  getNeighbors(vertexOrId: V | VertexId): V[] {
279
394
  const neighbors: V[] = [];
280
395
  const vertex = this.getVertex(vertexOrId);
@@ -291,6 +406,13 @@ export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> ext
291
406
  return neighbors;
292
407
  }
293
408
 
409
+ /**
410
+ * The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
411
+ * otherwise it returns null.
412
+ * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
413
+ * @returns an array containing two vertices [V, V] if the edge is found in the graph. If the edge is not found, it
414
+ * returns null.
415
+ */
294
416
  getEndsOfEdge(edge: E): [V, V] | null {
295
417
  if (!this.containsEdge(edge.src, edge.dest)) {
296
418
  return null;