data-structure-typed 1.50.1 → 1.50.3

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 (196) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/README.md +30 -28
  3. package/README_zh-CN.md +1 -1
  4. package/SPECIFICATION.md +56 -0
  5. package/SPONSOR-zh-CN.md +90 -0
  6. package/SPONSOR.md +54 -0
  7. package/benchmark/report.html +13 -13
  8. package/benchmark/report.json +149 -161
  9. package/dist/cjs/data-structures/base/iterable-base.d.ts +120 -9
  10. package/dist/cjs/data-structures/base/iterable-base.js +143 -7
  11. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js +101 -72
  14. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  17. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  19. package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
  20. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  21. package/dist/cjs/data-structures/binary-tree/bst.d.ts +92 -79
  22. package/dist/cjs/data-structures/binary-tree/bst.js +68 -76
  23. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  25. package/dist/cjs/data-structures/binary-tree/rb-tree.js +152 -99
  26. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  27. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  28. package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
  29. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  30. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  31. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +102 -85
  32. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  33. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -78
  34. package/dist/cjs/data-structures/graph/abstract-graph.js +3 -189
  35. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  36. package/dist/cjs/data-structures/graph/directed-graph.d.ts +73 -0
  37. package/dist/cjs/data-structures/graph/directed-graph.js +131 -0
  38. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  39. package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
  40. package/dist/cjs/data-structures/graph/map-graph.js +14 -0
  41. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  42. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +76 -7
  43. package/dist/cjs/data-structures/graph/undirected-graph.js +151 -18
  44. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  45. package/dist/cjs/data-structures/hash/hash-map.d.ts +254 -28
  46. package/dist/cjs/data-structures/hash/hash-map.js +347 -78
  47. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  48. package/dist/cjs/data-structures/heap/heap.d.ts +95 -25
  49. package/dist/cjs/data-structures/heap/heap.js +95 -26
  50. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  51. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  52. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +141 -77
  53. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  54. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  55. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +164 -115
  56. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  57. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  58. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
  59. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  60. package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
  61. package/dist/cjs/data-structures/matrix/matrix.js +50 -11
  62. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  63. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  64. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  65. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  66. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  67. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  68. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  69. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  70. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
  71. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  72. package/dist/cjs/data-structures/queue/deque.d.ts +139 -35
  73. package/dist/cjs/data-structures/queue/deque.js +200 -62
  74. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  75. package/dist/cjs/data-structures/queue/queue.d.ts +103 -49
  76. package/dist/cjs/data-structures/queue/queue.js +111 -49
  77. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  78. package/dist/cjs/data-structures/stack/stack.d.ts +51 -21
  79. package/dist/cjs/data-structures/stack/stack.js +58 -22
  80. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  81. package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
  82. package/dist/cjs/data-structures/tree/tree.js +77 -11
  83. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  84. package/dist/cjs/data-structures/trie/trie.d.ts +135 -34
  85. package/dist/cjs/data-structures/trie/trie.js +153 -33
  86. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  87. package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  88. package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
  89. package/dist/cjs/types/utils/utils.d.ts +1 -0
  90. package/dist/mjs/data-structures/base/iterable-base.d.ts +120 -9
  91. package/dist/mjs/data-structures/base/iterable-base.js +143 -7
  92. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +72 -47
  93. package/dist/mjs/data-structures/binary-tree/avl-tree.js +102 -73
  94. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  95. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  96. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
  97. package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
  98. package/dist/mjs/data-structures/binary-tree/bst.d.ts +92 -79
  99. package/dist/mjs/data-structures/binary-tree/bst.js +68 -76
  100. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +127 -57
  101. package/dist/mjs/data-structures/binary-tree/rb-tree.js +153 -100
  102. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  103. package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
  104. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +72 -58
  105. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +102 -86
  106. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -78
  107. package/dist/mjs/data-structures/graph/abstract-graph.js +3 -188
  108. package/dist/mjs/data-structures/graph/directed-graph.d.ts +73 -0
  109. package/dist/mjs/data-structures/graph/directed-graph.js +131 -0
  110. package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
  111. package/dist/mjs/data-structures/graph/map-graph.js +14 -0
  112. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +76 -7
  113. package/dist/mjs/data-structures/graph/undirected-graph.js +152 -19
  114. package/dist/mjs/data-structures/hash/hash-map.d.ts +254 -28
  115. package/dist/mjs/data-structures/hash/hash-map.js +353 -84
  116. package/dist/mjs/data-structures/heap/heap.d.ts +95 -25
  117. package/dist/mjs/data-structures/heap/heap.js +95 -26
  118. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +126 -63
  119. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +144 -80
  120. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +154 -106
  121. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +166 -117
  122. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
  123. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
  124. package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
  125. package/dist/mjs/data-structures/matrix/matrix.js +50 -11
  126. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  127. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  128. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  129. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  130. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  131. package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
  132. package/dist/mjs/data-structures/queue/deque.d.ts +139 -35
  133. package/dist/mjs/data-structures/queue/deque.js +205 -67
  134. package/dist/mjs/data-structures/queue/queue.d.ts +103 -49
  135. package/dist/mjs/data-structures/queue/queue.js +111 -49
  136. package/dist/mjs/data-structures/stack/stack.d.ts +51 -21
  137. package/dist/mjs/data-structures/stack/stack.js +58 -22
  138. package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
  139. package/dist/mjs/data-structures/tree/tree.js +80 -14
  140. package/dist/mjs/data-structures/trie/trie.d.ts +135 -34
  141. package/dist/mjs/data-structures/trie/trie.js +156 -36
  142. package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
  143. package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
  144. package/dist/mjs/types/utils/utils.d.ts +1 -0
  145. package/dist/umd/data-structure-typed.js +3121 -1583
  146. package/dist/umd/data-structure-typed.min.js +3 -3
  147. package/dist/umd/data-structure-typed.min.js.map +1 -1
  148. package/package.json +61 -55
  149. package/src/data-structures/base/iterable-base.ts +184 -19
  150. package/src/data-structures/binary-tree/avl-tree.ts +134 -100
  151. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  152. package/src/data-structures/binary-tree/binary-tree.ts +674 -671
  153. package/src/data-structures/binary-tree/bst.ts +127 -136
  154. package/src/data-structures/binary-tree/rb-tree.ts +199 -166
  155. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  156. package/src/data-structures/binary-tree/tree-multimap.ts +138 -115
  157. package/src/data-structures/graph/abstract-graph.ts +4 -211
  158. package/src/data-structures/graph/directed-graph.ts +152 -0
  159. package/src/data-structures/graph/map-graph.ts +15 -0
  160. package/src/data-structures/graph/undirected-graph.ts +171 -19
  161. package/src/data-structures/hash/hash-map.ts +389 -96
  162. package/src/data-structures/heap/heap.ts +97 -26
  163. package/src/data-structures/linked-list/doubly-linked-list.ts +156 -83
  164. package/src/data-structures/linked-list/singly-linked-list.ts +174 -120
  165. package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
  166. package/src/data-structures/matrix/matrix.ts +52 -12
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  170. package/src/data-structures/queue/deque.ts +225 -70
  171. package/src/data-structures/queue/queue.ts +118 -49
  172. package/src/data-structures/stack/stack.ts +63 -23
  173. package/src/data-structures/tree/tree.ts +89 -15
  174. package/src/data-structures/trie/trie.ts +173 -38
  175. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
  176. package/src/types/data-structures/hash/hash-map.ts +4 -3
  177. package/src/types/utils/utils.ts +2 -0
  178. package/test/performance/data-structures/graph/directed-graph.test.ts +4 -4
  179. package/test/performance/data-structures/queue/deque.test.ts +26 -25
  180. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
  181. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
  182. package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
  183. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
  184. package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
  185. package/test/unit/data-structures/graph/abstract-graph.test.ts +11 -0
  186. package/test/unit/data-structures/graph/directed-graph.test.ts +190 -5
  187. package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
  188. package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
  189. package/test/unit/data-structures/heap/heap.test.ts +18 -1
  190. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
  191. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
  192. package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
  193. package/test/unit/data-structures/queue/deque.test.ts +65 -5
  194. package/test/unit/data-structures/queue/queue.test.ts +24 -5
  195. package/test/unit/data-structures/stack/stack.test.ts +17 -0
  196. package/test/unit/data-structures/trie/trie.test.ts +17 -0
@@ -40,6 +40,7 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
40
40
  constructor();
41
41
  protected _vertexMap: Map<VertexKey, VO>;
42
42
  get vertexMap(): Map<VertexKey, VO>;
43
+ set vertexMap(v: Map<VertexKey, VO>);
43
44
  /**
44
45
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
45
46
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -361,84 +362,6 @@ export declare abstract class AbstractGraph<V = any, E = any, VO extends Abstrac
361
362
  costs: number[][];
362
363
  predecessor: (VO | undefined)[][];
363
364
  };
364
- /**
365
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
366
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
367
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
368
- * Tarjan can find cycles in directed or undirected graph
369
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
370
- * Tarjan solve the bi-connected components of undirected graphs;
371
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
372
- * /
373
-
374
- /**
375
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
376
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
377
- *
378
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
379
- * Tarjan can find cycles in directed or undirected graph
380
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
381
- * Tarjan solve the bi-connected components of undirected graphs;
382
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
383
- * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
384
- * strongly connected components (SCCs), and cycles in a graph.
385
- * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
386
- * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
387
- * number of connected components in the graph.
388
- * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
389
- * (edgeMap whose removal would increase the number of connected components in the graph).
390
- * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
391
- * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
392
- * SCCs will not be calculated or returned.
393
- * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
394
- * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
395
- * are arrays of vertexMap that form cycles within the SCCs.
396
- * @returns The function `tarjan` returns an object with the following properties:
397
- */
398
- tarjan(needCutVertexes?: boolean, needBridges?: boolean, needSCCs?: boolean, needCycles?: boolean): {
399
- dfnMap: Map<VO, number>;
400
- lowMap: Map<VO, number>;
401
- bridges: EO[];
402
- cutVertexes: VO[];
403
- SCCs: Map<number, VO[]>;
404
- cycles: Map<number, VO[]>;
405
- };
406
- /**
407
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
408
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
409
- */
410
- /**
411
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
412
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
413
- *
414
- * The function returns a map that associates each vertex object with its corresponding depth-first
415
- * number.
416
- * @returns A Map object with keys of type VO and values of type number.
417
- */
418
- getDFNMap(): Map<VO, number>;
419
- /**
420
- * The function returns a Map object that contains the low values of each vertex in a Tarjan
421
- * algorithm.
422
- * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
423
- * type `number`.
424
- */
425
- getLowMap(): Map<VO, number>;
426
- /**
427
- * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
428
- * @returns an array of VO objects, specifically the cut vertexes.
429
- */
430
- getCutVertexes(): VO[];
431
- /**
432
- * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
433
- * algorithm.
434
- * @returns a map where the keys are numbers and the values are arrays of VO objects.
435
- */
436
- getSCCs(): Map<number, VO[]>;
437
- /**
438
- * The function "getBridges" returns an array of bridges using the Tarjan algorithm.
439
- * @returns the bridges found using the Tarjan algorithm.
440
- */
441
- getBridges(): EO[];
442
365
  /**
443
366
  * O(V+E+C)
444
367
  * O(V+C)
@@ -47,6 +47,9 @@ export class AbstractGraph extends IterableEntryBase {
47
47
  get vertexMap() {
48
48
  return this._vertexMap;
49
49
  }
50
+ set vertexMap(v) {
51
+ this._vertexMap = v;
52
+ }
50
53
  /**
51
54
  * Time Complexity: O(1) - Constant time for Map lookup.
52
55
  * Space Complexity: O(1) - Constant space, as it creates only a few variables.
@@ -813,194 +816,6 @@ export class AbstractGraph extends IterableEntryBase {
813
816
  }
814
817
  return { costs, predecessor };
815
818
  }
816
- /**
817
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
818
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
819
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
820
- * Tarjan can find cycles in directed or undirected graph
821
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
822
- * Tarjan solve the bi-connected components of undirected graphs;
823
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
824
- * /
825
-
826
- /**
827
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
828
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
829
- *
830
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
831
- * Tarjan can find cycles in directed or undirected graph
832
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
833
- * Tarjan solve the bi-connected components of undirected graphs;
834
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
835
- * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
836
- * strongly connected components (SCCs), and cycles in a graph.
837
- * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
838
- * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
839
- * number of connected components in the graph.
840
- * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
841
- * (edgeMap whose removal would increase the number of connected components in the graph).
842
- * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
843
- * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
844
- * SCCs will not be calculated or returned.
845
- * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
846
- * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
847
- * are arrays of vertexMap that form cycles within the SCCs.
848
- * @returns The function `tarjan` returns an object with the following properties:
849
- */
850
- tarjan(needCutVertexes = false, needBridges = false, needSCCs = true, needCycles = false) {
851
- // !! in undirected graph we will not let child visit parent when dfs
852
- // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
853
- // !! bridge: low(child) > dfn(cur)
854
- const defaultConfig = false;
855
- if (needCutVertexes === undefined)
856
- needCutVertexes = defaultConfig;
857
- if (needBridges === undefined)
858
- needBridges = defaultConfig;
859
- if (needSCCs === undefined)
860
- needSCCs = defaultConfig;
861
- if (needCycles === undefined)
862
- needCycles = defaultConfig;
863
- const dfnMap = new Map();
864
- const lowMap = new Map();
865
- const vertexMap = this._vertexMap;
866
- vertexMap.forEach(v => {
867
- dfnMap.set(v, -1);
868
- lowMap.set(v, Infinity);
869
- });
870
- const [root] = vertexMap.values();
871
- const cutVertexes = [];
872
- const bridges = [];
873
- let dfn = 0;
874
- const dfs = (cur, parent) => {
875
- dfn++;
876
- dfnMap.set(cur, dfn);
877
- lowMap.set(cur, dfn);
878
- const neighbors = this.getNeighbors(cur);
879
- let childCount = 0; // child in dfs tree not child in graph
880
- for (const neighbor of neighbors) {
881
- if (neighbor !== parent) {
882
- if (dfnMap.get(neighbor) === -1) {
883
- childCount++;
884
- dfs(neighbor, cur);
885
- }
886
- const childLow = lowMap.get(neighbor);
887
- const curLow = lowMap.get(cur);
888
- // TODO after no-non-undefined-assertion not ensure the logic
889
- if (curLow !== undefined && childLow !== undefined) {
890
- lowMap.set(cur, Math.min(curLow, childLow));
891
- }
892
- const curFromMap = dfnMap.get(cur);
893
- if (childLow !== undefined && curFromMap !== undefined) {
894
- if (needCutVertexes) {
895
- if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
896
- // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
897
- cutVertexes.push(cur);
898
- }
899
- }
900
- if (needBridges) {
901
- if (childLow > curFromMap) {
902
- const edgeCurToNeighbor = this.getEdge(cur, neighbor);
903
- if (edgeCurToNeighbor) {
904
- bridges.push(edgeCurToNeighbor);
905
- }
906
- }
907
- }
908
- }
909
- }
910
- }
911
- };
912
- dfs(root, undefined);
913
- let SCCs = new Map();
914
- const getSCCs = () => {
915
- const SCCs = new Map();
916
- lowMap.forEach((low, vertex) => {
917
- if (!SCCs.has(low)) {
918
- SCCs.set(low, [vertex]);
919
- }
920
- else {
921
- SCCs.get(low)?.push(vertex);
922
- }
923
- });
924
- return SCCs;
925
- };
926
- if (needSCCs) {
927
- SCCs = getSCCs();
928
- }
929
- const cycles = new Map();
930
- if (needCycles) {
931
- const visitedMap = new Map();
932
- const stack = [];
933
- const findCyclesDFS = (cur, parent) => {
934
- visitedMap.set(cur, true);
935
- stack.push(cur);
936
- const neighbors = this.getNeighbors(cur);
937
- for (const neighbor of neighbors) {
938
- if (!visitedMap.get(neighbor)) {
939
- findCyclesDFS(neighbor, cur);
940
- }
941
- else if (stack.includes(neighbor) && neighbor !== parent) {
942
- const cycleStartIndex = stack.indexOf(neighbor);
943
- const cycle = stack.slice(cycleStartIndex);
944
- const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
945
- cycles.set(cycleLow, cycle);
946
- }
947
- }
948
- stack.pop();
949
- };
950
- vertexMap.forEach(v => {
951
- if (!visitedMap.get(v)) {
952
- findCyclesDFS(v, undefined);
953
- }
954
- });
955
- }
956
- return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
957
- }
958
- /**
959
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
960
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
961
- */
962
- /**
963
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
964
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
965
- *
966
- * The function returns a map that associates each vertex object with its corresponding depth-first
967
- * number.
968
- * @returns A Map object with keys of type VO and values of type number.
969
- */
970
- getDFNMap() {
971
- return this.tarjan(false, false, false, false).dfnMap;
972
- }
973
- /**
974
- * The function returns a Map object that contains the low values of each vertex in a Tarjan
975
- * algorithm.
976
- * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
977
- * type `number`.
978
- */
979
- getLowMap() {
980
- return this.tarjan(false, false, false, false).lowMap;
981
- }
982
- /**
983
- * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
984
- * @returns an array of VO objects, specifically the cut vertexes.
985
- */
986
- getCutVertexes() {
987
- return this.tarjan(true, false, false, false).cutVertexes;
988
- }
989
- /**
990
- * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
991
- * algorithm.
992
- * @returns a map where the keys are numbers and the values are arrays of VO objects.
993
- */
994
- getSCCs() {
995
- return this.tarjan(false, false, true, false).SCCs;
996
- }
997
- /**
998
- * The function "getBridges" returns an array of bridges using the Tarjan algorithm.
999
- * @returns the bridges found using the Tarjan algorithm.
1000
- */
1001
- getBridges() {
1002
- return this.tarjan(false, true, false, false).bridges;
1003
- }
1004
819
  /**
1005
820
  * O(V+E+C)
1006
821
  * O(V+C)
@@ -41,8 +41,10 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
41
41
  constructor();
42
42
  protected _outEdgeMap: Map<VO, EO[]>;
43
43
  get outEdgeMap(): Map<VO, EO[]>;
44
+ set outEdgeMap(v: Map<VO, EO[]>);
44
45
  protected _inEdgeMap: Map<VO, EO[]>;
45
46
  get inEdgeMap(): Map<VO, EO[]>;
47
+ set inEdgeMap(v: Map<VO, EO[]>);
46
48
  /**
47
49
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
48
50
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -327,6 +329,77 @@ export declare class DirectedGraph<V = any, E = any, VO extends DirectedVertex<V
327
329
  * graph. If the edge does not exist, it returns `undefined`.
328
330
  */
329
331
  getEndsOfEdge(edge: EO): [VO, VO] | undefined;
332
+ /**
333
+ * The isEmpty function checks if the graph is empty.
334
+ *
335
+ * @return A boolean value
336
+ */
337
+ isEmpty(): boolean;
338
+ /**
339
+ * Time Complexity: O(1)
340
+ * Space Complexity: O(1)
341
+ */
342
+ /**
343
+ * Time Complexity: O(1)
344
+ * Space Complexity: O(1)
345
+ *
346
+ * The clear function resets the vertex map, in-edge map, and out-edge map.
347
+ */
348
+ clear(): void;
349
+ /**
350
+ * The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
351
+ *
352
+ * @return A new instance of the directedgraph class
353
+ */
354
+ clone(): DirectedGraph<V, E, VO, EO>;
355
+ /**
356
+ * Time Complexity: O(V + E)
357
+ * Space Complexity: O(V)
358
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
359
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
360
+ */
361
+ /**
362
+ * Time Complexity: O(V + E)
363
+ * Space Complexity: O(V)
364
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
365
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
366
+ *
367
+ * The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
368
+ * graph.
369
+ * @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
370
+ * `SCCs`.
371
+ */
372
+ tarjan(): {
373
+ dfnMap: Map<VO, number>;
374
+ lowMap: Map<VO, number>;
375
+ SCCs: Map<number, VO[]>;
376
+ };
377
+ /**
378
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
379
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
380
+ */
381
+ /**
382
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
383
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
384
+ *
385
+ * The function returns a map that associates each vertex object with its corresponding depth-first
386
+ * number.
387
+ * @returns A Map object with keys of type VO and values of type number.
388
+ */
389
+ getDFNMap(): Map<VO, number>;
390
+ /**
391
+ * The function returns a Map object that contains the low values of each vertex in a Tarjan
392
+ * algorithm.
393
+ * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
394
+ * type `number`.
395
+ */
396
+ getLowMap(): Map<VO, number>;
397
+ /**
398
+ * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
399
+ * algorithm.
400
+ * @returns a map where the keys are numbers and the values are arrays of VO objects.
401
+ */
402
+ getSCCs(): Map<number, VO[]>;
330
403
  /**
331
404
  * Time Complexity: O(1)
332
405
  * Space Complexity: O(1)
@@ -43,10 +43,16 @@ export class DirectedGraph extends AbstractGraph {
43
43
  get outEdgeMap() {
44
44
  return this._outEdgeMap;
45
45
  }
46
+ set outEdgeMap(v) {
47
+ this._outEdgeMap = v;
48
+ }
46
49
  _inEdgeMap = new Map();
47
50
  get inEdgeMap() {
48
51
  return this._inEdgeMap;
49
52
  }
53
+ set inEdgeMap(v) {
54
+ this._inEdgeMap = v;
55
+ }
50
56
  /**
51
57
  * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
52
58
  * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
@@ -524,6 +530,131 @@ export class DirectedGraph extends AbstractGraph {
524
530
  return undefined;
525
531
  }
526
532
  }
533
+ /**
534
+ * The isEmpty function checks if the graph is empty.
535
+ *
536
+ * @return A boolean value
537
+ */
538
+ isEmpty() {
539
+ return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
540
+ }
541
+ /**
542
+ * Time Complexity: O(1)
543
+ * Space Complexity: O(1)
544
+ */
545
+ /**
546
+ * Time Complexity: O(1)
547
+ * Space Complexity: O(1)
548
+ *
549
+ * The clear function resets the vertex map, in-edge map, and out-edge map.
550
+ */
551
+ clear() {
552
+ this._vertexMap = new Map();
553
+ this._inEdgeMap = new Map();
554
+ this._outEdgeMap = new Map();
555
+ }
556
+ /**
557
+ * The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
558
+ *
559
+ * @return A new instance of the directedgraph class
560
+ */
561
+ clone() {
562
+ const cloned = new DirectedGraph();
563
+ cloned.vertexMap = new Map(this.vertexMap);
564
+ cloned.inEdgeMap = new Map(this.inEdgeMap);
565
+ cloned.outEdgeMap = new Map(this.outEdgeMap);
566
+ return cloned;
567
+ }
568
+ /**
569
+ * Time Complexity: O(V + E)
570
+ * Space Complexity: O(V)
571
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
572
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
573
+ */
574
+ /**
575
+ * Time Complexity: O(V + E)
576
+ * Space Complexity: O(V)
577
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
578
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
579
+ *
580
+ * The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
581
+ * graph.
582
+ * @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
583
+ * `SCCs`.
584
+ */
585
+ tarjan() {
586
+ const dfnMap = new Map();
587
+ const lowMap = new Map();
588
+ const SCCs = new Map();
589
+ let time = 0;
590
+ const stack = [];
591
+ const inStack = new Set();
592
+ const dfs = (vertex) => {
593
+ dfnMap.set(vertex, time);
594
+ lowMap.set(vertex, time);
595
+ time++;
596
+ stack.push(vertex);
597
+ inStack.add(vertex);
598
+ const neighbors = this.getNeighbors(vertex);
599
+ for (const neighbor of neighbors) {
600
+ if (!dfnMap.has(neighbor)) {
601
+ dfs(neighbor);
602
+ lowMap.set(vertex, Math.min(lowMap.get(vertex), lowMap.get(neighbor)));
603
+ }
604
+ else if (inStack.has(neighbor)) {
605
+ lowMap.set(vertex, Math.min(lowMap.get(vertex), dfnMap.get(neighbor)));
606
+ }
607
+ }
608
+ if (dfnMap.get(vertex) === lowMap.get(vertex)) {
609
+ const SCC = [];
610
+ let poppedVertex;
611
+ do {
612
+ poppedVertex = stack.pop();
613
+ inStack.delete(poppedVertex);
614
+ SCC.push(poppedVertex);
615
+ } while (poppedVertex !== vertex);
616
+ SCCs.set(SCCs.size, SCC);
617
+ }
618
+ };
619
+ for (const vertex of this.vertexMap.values()) {
620
+ if (!dfnMap.has(vertex)) {
621
+ dfs(vertex);
622
+ }
623
+ }
624
+ return { dfnMap, lowMap, SCCs };
625
+ }
626
+ /**
627
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
628
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
629
+ */
630
+ /**
631
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
632
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
633
+ *
634
+ * The function returns a map that associates each vertex object with its corresponding depth-first
635
+ * number.
636
+ * @returns A Map object with keys of type VO and values of type number.
637
+ */
638
+ getDFNMap() {
639
+ return this.tarjan().dfnMap;
640
+ }
641
+ /**
642
+ * The function returns a Map object that contains the low values of each vertex in a Tarjan
643
+ * algorithm.
644
+ * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
645
+ * type `number`.
646
+ */
647
+ getLowMap() {
648
+ return this.tarjan().lowMap;
649
+ }
650
+ /**
651
+ * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
652
+ * algorithm.
653
+ * @returns a map where the keys are numbers and the values are arrays of VO objects.
654
+ */
655
+ getSCCs() {
656
+ return this.tarjan().SCCs;
657
+ }
527
658
  /**
528
659
  * Time Complexity: O(1)
529
660
  * Space Complexity: O(1)
@@ -70,4 +70,12 @@ export declare class MapGraph<V = any, E = any, VO extends MapVertex<V> = MapVer
70
70
  * @returns a new instance of the `MapEdge` class, cast as type `EO`.
71
71
  */
72
72
  createEdge(src: VertexKey, dest: VertexKey, weight?: number, value?: E): EO;
73
+ /**
74
+ * The override function is used to override the default behavior of a function.
75
+ * In this case, we are overriding the clone() function from Graph&lt;V, E&gt;.
76
+ * The clone() function returns a new graph that is an exact copy of the original graph.
77
+ *
78
+ * @return A mapgraph&lt;v, e, vo, eo&gt;
79
+ */
80
+ clone(): MapGraph<V, E, VO, EO>;
73
81
  }
@@ -87,4 +87,18 @@ export class MapGraph extends DirectedGraph {
87
87
  createEdge(src, dest, weight, value) {
88
88
  return new MapEdge(src, dest, weight, value);
89
89
  }
90
+ /**
91
+ * The override function is used to override the default behavior of a function.
92
+ * In this case, we are overriding the clone() function from Graph&lt;V, E&gt;.
93
+ * The clone() function returns a new graph that is an exact copy of the original graph.
94
+ *
95
+ * @return A mapgraph&lt;v, e, vo, eo&gt;
96
+ */
97
+ clone() {
98
+ const cloned = new MapGraph(this.originCoord, this.bottomRight);
99
+ cloned.vertexMap = new Map(this.vertexMap);
100
+ cloned.inEdgeMap = new Map(this.inEdgeMap);
101
+ cloned.outEdgeMap = new Map(this.outEdgeMap);
102
+ return cloned;
103
+ }
90
104
  }