data-structure-typed 1.50.2 → 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 (162) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +17 -15
  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 -155
  9. package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -0
  10. package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
  12. package/dist/cjs/data-structures/binary-tree/avl-tree.js +33 -1
  13. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  15. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  16. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
  18. package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
  19. package/dist/cjs/data-structures/binary-tree/bst.d.ts +46 -13
  20. package/dist/cjs/data-structures/binary-tree/bst.js +46 -13
  21. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  22. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
  23. package/dist/cjs/data-structures/binary-tree/rb-tree.js +73 -15
  24. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  25. package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  26. package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
  27. package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
  28. package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +35 -2
  29. package/dist/cjs/data-structures/binary-tree/tree-multimap.js +38 -0
  30. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
  31. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -78
  32. package/dist/cjs/data-structures/graph/abstract-graph.js +0 -189
  33. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  34. package/dist/cjs/data-structures/graph/directed-graph.d.ts +59 -0
  35. package/dist/cjs/data-structures/graph/directed-graph.js +105 -0
  36. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  37. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +60 -7
  38. package/dist/cjs/data-structures/graph/undirected-graph.js +126 -18
  39. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  40. package/dist/cjs/data-structures/hash/hash-map.d.ts +143 -23
  41. package/dist/cjs/data-structures/hash/hash-map.js +196 -62
  42. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  43. package/dist/cjs/data-structures/heap/heap.d.ts +29 -19
  44. package/dist/cjs/data-structures/heap/heap.js +29 -20
  45. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  46. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
  47. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +83 -25
  48. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  49. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
  50. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +34 -3
  51. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  52. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  53. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
  54. package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
  55. package/dist/cjs/data-structures/matrix/matrix.js +1 -1
  56. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  57. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  58. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  59. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  60. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  61. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  62. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  63. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
  64. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  65. package/dist/cjs/data-structures/queue/deque.d.ts +95 -21
  66. package/dist/cjs/data-structures/queue/deque.js +100 -16
  67. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  68. package/dist/cjs/data-structures/queue/queue.d.ts +65 -45
  69. package/dist/cjs/data-structures/queue/queue.js +65 -45
  70. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  71. package/dist/cjs/data-structures/stack/stack.d.ts +36 -22
  72. package/dist/cjs/data-structures/stack/stack.js +36 -22
  73. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  74. package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
  75. package/dist/cjs/data-structures/tree/tree.js +77 -11
  76. package/dist/cjs/data-structures/tree/tree.js.map +1 -1
  77. package/dist/cjs/data-structures/trie/trie.d.ts +100 -36
  78. package/dist/cjs/data-structures/trie/trie.js +115 -36
  79. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  80. package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -0
  81. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
  82. package/dist/mjs/data-structures/binary-tree/avl-tree.js +34 -2
  83. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
  84. package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
  85. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
  86. package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
  87. package/dist/mjs/data-structures/binary-tree/bst.d.ts +46 -13
  88. package/dist/mjs/data-structures/binary-tree/bst.js +46 -13
  89. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
  90. package/dist/mjs/data-structures/binary-tree/rb-tree.js +74 -16
  91. package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
  92. package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
  93. package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +35 -2
  94. package/dist/mjs/data-structures/binary-tree/tree-multimap.js +38 -1
  95. package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -78
  96. package/dist/mjs/data-structures/graph/abstract-graph.js +0 -188
  97. package/dist/mjs/data-structures/graph/directed-graph.d.ts +59 -0
  98. package/dist/mjs/data-structures/graph/directed-graph.js +105 -0
  99. package/dist/mjs/data-structures/graph/undirected-graph.d.ts +60 -7
  100. package/dist/mjs/data-structures/graph/undirected-graph.js +127 -19
  101. package/dist/mjs/data-structures/hash/hash-map.d.ts +143 -23
  102. package/dist/mjs/data-structures/hash/hash-map.js +204 -70
  103. package/dist/mjs/data-structures/heap/heap.d.ts +29 -19
  104. package/dist/mjs/data-structures/heap/heap.js +29 -20
  105. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
  106. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +86 -28
  107. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
  108. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +36 -5
  109. package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
  110. package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
  111. package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
  112. package/dist/mjs/data-structures/matrix/matrix.js +1 -1
  113. package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
  114. package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
  115. package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
  116. package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
  117. package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
  118. package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
  119. package/dist/mjs/data-structures/queue/deque.d.ts +95 -21
  120. package/dist/mjs/data-structures/queue/deque.js +105 -21
  121. package/dist/mjs/data-structures/queue/queue.d.ts +65 -45
  122. package/dist/mjs/data-structures/queue/queue.js +65 -45
  123. package/dist/mjs/data-structures/stack/stack.d.ts +36 -22
  124. package/dist/mjs/data-structures/stack/stack.js +36 -22
  125. package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
  126. package/dist/mjs/data-structures/tree/tree.js +80 -14
  127. package/dist/mjs/data-structures/trie/trie.d.ts +100 -36
  128. package/dist/mjs/data-structures/trie/trie.js +118 -39
  129. package/dist/umd/data-structure-typed.js +1445 -595
  130. package/dist/umd/data-structure-typed.min.js +2 -2
  131. package/dist/umd/data-structure-typed.min.js.map +1 -1
  132. package/package.json +59 -58
  133. package/src/data-structures/base/iterable-base.ts +12 -0
  134. package/src/data-structures/binary-tree/avl-tree.ts +37 -3
  135. package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
  136. package/src/data-structures/binary-tree/binary-tree.ts +1 -1
  137. package/src/data-structures/binary-tree/bst.ts +46 -13
  138. package/src/data-structures/binary-tree/rb-tree.ts +79 -18
  139. package/src/data-structures/binary-tree/segment-tree.ts +145 -11
  140. package/src/data-structures/binary-tree/tree-multimap.ts +42 -3
  141. package/src/data-structures/graph/abstract-graph.ts +0 -211
  142. package/src/data-structures/graph/directed-graph.ts +122 -0
  143. package/src/data-structures/graph/undirected-graph.ts +143 -19
  144. package/src/data-structures/hash/hash-map.ts +228 -76
  145. package/src/data-structures/heap/heap.ts +31 -20
  146. package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
  147. package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
  148. package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
  149. package/src/data-structures/matrix/matrix.ts +1 -1
  150. package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
  151. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
  152. package/src/data-structures/priority-queue/priority-queue.ts +8 -0
  153. package/src/data-structures/queue/deque.ts +118 -22
  154. package/src/data-structures/queue/queue.ts +68 -45
  155. package/src/data-structures/stack/stack.ts +39 -23
  156. package/src/data-structures/tree/tree.ts +89 -15
  157. package/src/data-structures/trie/trie.ts +131 -40
  158. package/test/performance/data-structures/graph/directed-graph.test.ts +1 -1
  159. package/test/unit/data-structures/graph/abstract-graph.test.ts +3 -0
  160. package/test/unit/data-structures/graph/directed-graph.test.ts +189 -253
  161. package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
  162. package/test/unit/data-structures/queue/queue.test.ts +2 -0
@@ -9,18 +9,136 @@
9
9
  import type { SegmentTreeNodeVal } from '../../types';
10
10
 
11
11
  export class SegmentTreeNode {
12
- start = 0;
13
- end = 0;
14
- value: SegmentTreeNodeVal | undefined = undefined;
15
- sum = 0;
16
- left: SegmentTreeNode | undefined = undefined;
17
- right: SegmentTreeNode | undefined = undefined;
18
-
12
+ /**
13
+ * The constructor initializes the properties of a SegmentTreeNode object.
14
+ * @param {number} start - The `start` parameter represents the starting index of the segment covered
15
+ * by this node in a segment tree.
16
+ * @param {number} end - The `end` parameter represents the end index of the segment covered by this
17
+ * node in a segment tree.
18
+ * @param {number} sum - The `sum` parameter represents the sum of the values in the range covered by
19
+ * the segment tree node.
20
+ * @param {SegmentTreeNodeVal | undefined} [value] - The `value` parameter is an optional parameter
21
+ * of type `SegmentTreeNodeVal`. It represents the value associated with the segment tree node.
22
+ */
19
23
  constructor(start: number, end: number, sum: number, value?: SegmentTreeNodeVal | undefined) {
20
- this.start = start;
21
- this.end = end;
22
- this.sum = sum;
23
- this.value = value || undefined;
24
+ this._start = start;
25
+ this._end = end;
26
+ this._sum = sum;
27
+ this._value = value || undefined;
28
+ }
29
+
30
+ protected _start = 0;
31
+
32
+ /**
33
+ * The function returns the value of the protected variable _start.
34
+ * @returns The start value, which is of type number.
35
+ */
36
+ get start(): number {
37
+ return this._start;
38
+ }
39
+
40
+ /**
41
+ * The above function sets the value of the "start" property.
42
+ * @param {number} value - The value parameter is of type number.
43
+ */
44
+ set start(value: number) {
45
+ this._start = value;
46
+ }
47
+
48
+ protected _end = 0;
49
+
50
+ /**
51
+ * The function returns the value of the protected variable `_end`.
52
+ * @returns The value of the protected property `_end`.
53
+ */
54
+ get end(): number {
55
+ return this._end;
56
+ }
57
+
58
+ /**
59
+ * The above function sets the value of the "end" property.
60
+ * @param {number} value - The value parameter is a number that represents the new value for the end
61
+ * property.
62
+ */
63
+ set end(value: number) {
64
+ this._end = value;
65
+ }
66
+
67
+ protected _value: SegmentTreeNodeVal | undefined = undefined;
68
+
69
+ /**
70
+ * The function returns the value of a segment tree node.
71
+ * @returns The value being returned is either a `SegmentTreeNodeVal` object or `undefined`.
72
+ */
73
+ get value(): SegmentTreeNodeVal | undefined {
74
+ return this._value;
75
+ }
76
+
77
+ /**
78
+ * The function sets the value of a segment tree node.
79
+ * @param {SegmentTreeNodeVal | undefined} value - The `value` parameter is of type
80
+ * `SegmentTreeNodeVal` or `undefined`.
81
+ */
82
+ set value(value: SegmentTreeNodeVal | undefined) {
83
+ this._value = value;
84
+ }
85
+
86
+ protected _sum = 0;
87
+
88
+ /**
89
+ * The function returns the value of the sum property.
90
+ * @returns The method is returning the value of the variable `_sum`.
91
+ */
92
+ get sum(): number {
93
+ return this._sum;
94
+ }
95
+
96
+ /**
97
+ * The above function sets the value of the sum property.
98
+ * @param {number} value - The parameter "value" is of type "number".
99
+ */
100
+ set sum(value: number) {
101
+ this._sum = value;
102
+ }
103
+
104
+ protected _left: SegmentTreeNode | undefined = undefined;
105
+
106
+ /**
107
+ * The function returns the left child of a segment tree node.
108
+ * @returns The `left` property of the `SegmentTreeNode` object is being returned. It is of type
109
+ * `SegmentTreeNode` or `undefined`.
110
+ */
111
+ get left(): SegmentTreeNode | undefined {
112
+ return this._left;
113
+ }
114
+
115
+ /**
116
+ * The function sets the value of the left property of a SegmentTreeNode object.
117
+ * @param {SegmentTreeNode | undefined} value - The value parameter is of type SegmentTreeNode or
118
+ * undefined.
119
+ */
120
+ set left(value: SegmentTreeNode | undefined) {
121
+ this._left = value;
122
+ }
123
+
124
+ protected _right: SegmentTreeNode | undefined = undefined;
125
+
126
+ /**
127
+ * The function returns the right child of a segment tree node.
128
+ * @returns The `getRight()` method is returning a value of type `SegmentTreeNode` or `undefined`.
129
+ */
130
+ get right(): SegmentTreeNode | undefined {
131
+ return this._right;
132
+ }
133
+
134
+ /**
135
+ * The function sets the right child of a segment tree node.
136
+ * @param {SegmentTreeNode | undefined} value - The `value` parameter is of type `SegmentTreeNode |
137
+ * undefined`. This means that it can accept either a `SegmentTreeNode` object or `undefined` as its
138
+ * value.
139
+ */
140
+ set right(value: SegmentTreeNode | undefined) {
141
+ this._right = value;
24
142
  }
25
143
  }
26
144
 
@@ -51,24 +169,40 @@ export class SegmentTree {
51
169
 
52
170
  protected _values: number[] = [];
53
171
 
172
+ /**
173
+ * The function returns an array of numbers.
174
+ * @returns An array of numbers is being returned.
175
+ */
54
176
  get values(): number[] {
55
177
  return this._values;
56
178
  }
57
179
 
58
180
  protected _start = 0;
59
181
 
182
+ /**
183
+ * The function returns the value of the protected variable _start.
184
+ * @returns The start value, which is of type number.
185
+ */
60
186
  get start(): number {
61
187
  return this._start;
62
188
  }
63
189
 
64
190
  protected _end: number;
65
191
 
192
+ /**
193
+ * The function returns the value of the protected variable `_end`.
194
+ * @returns The value of the protected property `_end`.
195
+ */
66
196
  get end(): number {
67
197
  return this._end;
68
198
  }
69
199
 
70
200
  protected _root: SegmentTreeNode | undefined;
71
201
 
202
+ /**
203
+ * The function returns the root node of a segment tree.
204
+ * @returns The `root` property of the class `SegmentTreeNode` or `undefined` if it is not defined.
205
+ */
72
206
  get root(): SegmentTreeNode | undefined {
73
207
  return this._root;
74
208
  }
@@ -23,8 +23,6 @@ export class TreeMultimapNode<
23
23
  V = any,
24
24
  NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>
25
25
  > extends AVLTreeNode<K, V, NODE> {
26
- count: number;
27
-
28
26
  /**
29
27
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
30
28
  * @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
@@ -39,6 +37,25 @@ export class TreeMultimapNode<
39
37
  super(key, value);
40
38
  this.count = count;
41
39
  }
40
+
41
+ protected _count: number = 1;
42
+
43
+ /**
44
+ * The function returns the value of the protected variable _count.
45
+ * @returns The count property of the object, which is of type number.
46
+ */
47
+ get count(): number {
48
+ return this._count;
49
+ }
50
+
51
+ /**
52
+ * The above function sets the value of the count property.
53
+ * @param {number} value - The value parameter is of type number, which means it can accept any
54
+ * numeric value.
55
+ */
56
+ set count(value: number) {
57
+ this._count = value;
58
+ }
42
59
  }
43
60
 
44
61
  /**
@@ -57,9 +74,14 @@ export class TreeMultimap<
57
74
  if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
58
75
  }
59
76
 
60
- private _count = 0;
77
+ protected _count = 0;
61
78
 
62
79
  // TODO the _count is not accurate after nodes count modified
80
+ /**
81
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
82
+ * search.
83
+ * @returns the sum of the count property of all nodes in the tree.
84
+ */
63
85
  get count(): number {
64
86
  let sum = 0;
65
87
  this.dfs(node => (sum += node.count));
@@ -79,6 +101,15 @@ export class TreeMultimap<
79
101
  return new TreeMultimapNode(key, value, count) as NODE;
80
102
  }
81
103
 
104
+ /**
105
+ * The function creates a new TreeMultimap object with the specified options and returns it.
106
+ * @param [options] - The `options` parameter is an optional object that contains additional
107
+ * configuration options for creating the `TreeMultimap` object. It can include properties such as
108
+ * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
109
+ * the tree, respectively. These properties can be
110
+ * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
111
+ * default options. The returned value is casted as `TREE`.
112
+ */
82
113
  override createTree(options?: TreeMultimapOptions<K>): TREE {
83
114
  return new TreeMultimap<K, V, NODE, TREE>([], {
84
115
  iterationType: this.iterationType,
@@ -375,6 +406,14 @@ export class TreeMultimap<
375
406
  return undefined;
376
407
  }
377
408
 
409
+ /**
410
+ * The function replaces an old node with a new node and updates the count property of the new node.
411
+ * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
412
+ * needs to be replaced in a data structure.
413
+ * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
414
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
415
+ * superclass, after updating the `count` property of the `newNode` object.
416
+ */
378
417
  protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
379
418
  newNode.count = oldNode.count + newNode.count;
380
419
  return super._replaceNode(oldNode, newNode);
@@ -954,217 +954,6 @@ export abstract class AbstractGraph<
954
954
  return { costs, predecessor };
955
955
  }
956
956
 
957
- /**
958
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
959
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
960
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
961
- * Tarjan can find cycles in directed or undirected graph
962
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
963
- * Tarjan solve the bi-connected components of undirected graphs;
964
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
965
- * /
966
-
967
- /**
968
- * Time Complexity: O(V + E) - Linear time (Tarjan's algorithm).
969
- * Space Complexity: O(V) - Linear space (Tarjan's algorithm).
970
- *
971
- * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
972
- * Tarjan can find cycles in directed or undirected graph
973
- * Tarjan can find the articulation points and bridges(critical edgeMap) of undirected graphs in linear time,
974
- * Tarjan solve the bi-connected components of undirected graphs;
975
- * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
976
- * The `tarjan` function is used to perform various graph analysis tasks such as finding articulation points, bridges,
977
- * strongly connected components (SCCs), and cycles in a graph.
978
- * @param {boolean} [needCutVertexes] - A boolean value indicating whether or not to calculate and return the
979
- * articulation points in the graph. Articulation points are the vertexMap in a graph whose removal would increase the
980
- * number of connected components in the graph.
981
- * @param {boolean} [needBridges] - A boolean flag indicating whether the algorithm should find and return the bridges
982
- * (edgeMap whose removal would increase the number of connected components in the graph).
983
- * @param {boolean} [needSCCs] - A boolean value indicating whether the Strongly Connected Components (SCCs) of the
984
- * graph are needed. If set to true, the function will calculate and return the SCCs of the graph. If set to false, the
985
- * SCCs will not be calculated or returned.
986
- * @param {boolean} [needCycles] - A boolean flag indicating whether the algorithm should find cycles in the graph. If
987
- * set to true, the algorithm will return a map of cycles, where the keys are the low values of the SCCs and the values
988
- * are arrays of vertexMap that form cycles within the SCCs.
989
- * @returns The function `tarjan` returns an object with the following properties:
990
- */
991
- tarjan(
992
- needCutVertexes: boolean = false,
993
- needBridges: boolean = false,
994
- needSCCs: boolean = true,
995
- needCycles: boolean = false
996
- ) {
997
- // !! in undirected graph we will not let child visit parent when dfs
998
- // !! articulation point(in dfs search tree not in graph): (cur !== root && cur.has(child)) && (low(child) >= dfn(cur)) || (cur === root && cur.children() >= 2)
999
- // !! bridge: low(child) > dfn(cur)
1000
-
1001
- const defaultConfig = false;
1002
- if (needCutVertexes === undefined) needCutVertexes = defaultConfig;
1003
- if (needBridges === undefined) needBridges = defaultConfig;
1004
- if (needSCCs === undefined) needSCCs = defaultConfig;
1005
- if (needCycles === undefined) needCycles = defaultConfig;
1006
-
1007
- const dfnMap: Map<VO, number> = new Map();
1008
- const lowMap: Map<VO, number> = new Map();
1009
- const vertexMap = this._vertexMap;
1010
- vertexMap.forEach(v => {
1011
- dfnMap.set(v, -1);
1012
- lowMap.set(v, Infinity);
1013
- });
1014
-
1015
- const [root] = vertexMap.values();
1016
-
1017
- const cutVertexes: VO[] = [];
1018
- const bridges: EO[] = [];
1019
- let dfn = 0;
1020
- const dfs = (cur: VO, parent: VO | undefined) => {
1021
- dfn++;
1022
- dfnMap.set(cur, dfn);
1023
- lowMap.set(cur, dfn);
1024
-
1025
- const neighbors = this.getNeighbors(cur);
1026
- let childCount = 0; // child in dfs tree not child in graph
1027
- for (const neighbor of neighbors) {
1028
- if (neighbor !== parent) {
1029
- if (dfnMap.get(neighbor) === -1) {
1030
- childCount++;
1031
- dfs(neighbor, cur);
1032
- }
1033
- const childLow = lowMap.get(neighbor);
1034
- const curLow = lowMap.get(cur);
1035
- // TODO after no-non-undefined-assertion not ensure the logic
1036
- if (curLow !== undefined && childLow !== undefined) {
1037
- lowMap.set(cur, Math.min(curLow, childLow));
1038
- }
1039
- const curFromMap = dfnMap.get(cur);
1040
- if (childLow !== undefined && curFromMap !== undefined) {
1041
- if (needCutVertexes) {
1042
- if ((cur === root && childCount >= 2) || (cur !== root && childLow >= curFromMap)) {
1043
- // todo not ensure the logic if (cur === root && childCount >= 2 || ((cur !== root) && (childLow >= curFromMap))) {
1044
- cutVertexes.push(cur);
1045
- }
1046
- }
1047
-
1048
- if (needBridges) {
1049
- if (childLow > curFromMap) {
1050
- const edgeCurToNeighbor = this.getEdge(cur, neighbor);
1051
- if (edgeCurToNeighbor) {
1052
- bridges.push(edgeCurToNeighbor);
1053
- }
1054
- }
1055
- }
1056
- }
1057
- }
1058
- }
1059
- };
1060
-
1061
- dfs(root, undefined);
1062
-
1063
- let SCCs: Map<number, VO[]> = new Map();
1064
-
1065
- const getSCCs = () => {
1066
- const SCCs: Map<number, VO[]> = new Map();
1067
- lowMap.forEach((low, vertex) => {
1068
- if (!SCCs.has(low)) {
1069
- SCCs.set(low, [vertex]);
1070
- } else {
1071
- SCCs.get(low)?.push(vertex);
1072
- }
1073
- });
1074
- return SCCs;
1075
- };
1076
-
1077
- if (needSCCs) {
1078
- SCCs = getSCCs();
1079
- }
1080
-
1081
- const cycles: Map<number, VO[]> = new Map();
1082
-
1083
- if (needCycles) {
1084
- const visitedMap: Map<VO, boolean> = new Map();
1085
- const stack: VO[] = [];
1086
- const findCyclesDFS = (cur: VO, parent: VO | undefined) => {
1087
- visitedMap.set(cur, true);
1088
- stack.push(cur);
1089
-
1090
- const neighbors = this.getNeighbors(cur);
1091
-
1092
- for (const neighbor of neighbors) {
1093
- if (!visitedMap.get(neighbor)) {
1094
- findCyclesDFS(neighbor, cur);
1095
- } else if (stack.includes(neighbor) && neighbor !== parent) {
1096
- const cycleStartIndex = stack.indexOf(neighbor);
1097
- const cycle = stack.slice(cycleStartIndex);
1098
- const cycleLow = Math.min(...cycle.map(v => dfnMap.get(v) || Infinity));
1099
- cycles.set(cycleLow, cycle);
1100
- }
1101
- }
1102
-
1103
- stack.pop();
1104
- };
1105
-
1106
- vertexMap.forEach(v => {
1107
- if (!visitedMap.get(v)) {
1108
- findCyclesDFS(v, undefined);
1109
- }
1110
- });
1111
- }
1112
-
1113
- return { dfnMap, lowMap, bridges, cutVertexes, SCCs, cycles };
1114
- }
1115
-
1116
- /**
1117
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
1118
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
1119
- */
1120
-
1121
- /**
1122
- * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
1123
- * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
1124
- *
1125
- * The function returns a map that associates each vertex object with its corresponding depth-first
1126
- * number.
1127
- * @returns A Map object with keys of type VO and values of type number.
1128
- */
1129
- getDFNMap(): Map<VO, number> {
1130
- return this.tarjan(false, false, false, false).dfnMap;
1131
- }
1132
-
1133
- /**
1134
- * The function returns a Map object that contains the low values of each vertex in a Tarjan
1135
- * algorithm.
1136
- * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
1137
- * type `number`.
1138
- */
1139
- getLowMap(): Map<VO, number> {
1140
- return this.tarjan(false, false, false, false).lowMap;
1141
- }
1142
-
1143
- /**
1144
- * The function "getCutVertexes" returns an array of cut vertexes using the Tarjan algorithm.
1145
- * @returns an array of VO objects, specifically the cut vertexes.
1146
- */
1147
- getCutVertexes(): VO[] {
1148
- return this.tarjan(true, false, false, false).cutVertexes;
1149
- }
1150
-
1151
- /**
1152
- * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
1153
- * algorithm.
1154
- * @returns a map where the keys are numbers and the values are arrays of VO objects.
1155
- */
1156
- getSCCs(): Map<number, VO[]> {
1157
- return this.tarjan(false, false, true, false).SCCs;
1158
- }
1159
-
1160
- /**
1161
- * The function "getBridges" returns an array of bridges using the Tarjan algorithm.
1162
- * @returns the bridges found using the Tarjan algorithm.
1163
- */
1164
- getBridges() {
1165
- return this.tarjan(false, true, false, false).bridges;
1166
- }
1167
-
1168
957
  /**
1169
958
  * O(V+E+C)
1170
959
  * O(V+C)
@@ -616,6 +616,23 @@ export class DirectedGraph<
616
616
  return this.vertexMap.size === 0 && this.inEdgeMap.size === 0 && this.outEdgeMap.size === 0;
617
617
  }
618
618
 
619
+ /**
620
+ * Time Complexity: O(1)
621
+ * Space Complexity: O(1)
622
+ */
623
+
624
+ /**
625
+ * Time Complexity: O(1)
626
+ * Space Complexity: O(1)
627
+ *
628
+ * The clear function resets the vertex map, in-edge map, and out-edge map.
629
+ */
630
+ clear() {
631
+ this._vertexMap = new Map<VertexKey, VO>();
632
+ this._inEdgeMap = new Map<VO, EO[]>();
633
+ this._outEdgeMap = new Map<VO, EO[]>();
634
+ }
635
+
619
636
  /**
620
637
  * The clone function creates a new DirectedGraph object with the same vertices and edges as the original.
621
638
  *
@@ -629,6 +646,111 @@ export class DirectedGraph<
629
646
  return cloned;
630
647
  }
631
648
 
649
+ /**
650
+ * Time Complexity: O(V + E)
651
+ * Space Complexity: O(V)
652
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
653
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
654
+ */
655
+
656
+ /**
657
+ * Time Complexity: O(V + E)
658
+ * Space Complexity: O(V)
659
+ * Tarjan is an algorithm based on dfs,which is used to solve the connectivity problem of graphs.
660
+ * Tarjan can find the SSC(strongly connected components), articulation points, and bridges of directed graphs.
661
+ *
662
+ * The function `tarjan` implements the Tarjan's algorithm to find strongly connected components in a
663
+ * graph.
664
+ * @returns The function `tarjan()` returns an object with three properties: `dfnMap`, `lowMap`, and
665
+ * `SCCs`.
666
+ */
667
+ tarjan(): { dfnMap: Map<VO, number>; lowMap: Map<VO, number>; SCCs: Map<number, VO[]> } {
668
+ const dfnMap = new Map<VO, number>();
669
+ const lowMap = new Map<VO, number>();
670
+ const SCCs = new Map<number, VO[]>();
671
+
672
+ let time = 0;
673
+
674
+ const stack: VO[] = [];
675
+ const inStack: Set<VO> = new Set();
676
+
677
+ const dfs = (vertex: VO) => {
678
+ dfnMap.set(vertex, time);
679
+ lowMap.set(vertex, time);
680
+ time++;
681
+
682
+ stack.push(vertex);
683
+ inStack.add(vertex);
684
+
685
+ const neighbors = this.getNeighbors(vertex);
686
+ for (const neighbor of neighbors) {
687
+ if (!dfnMap.has(neighbor)) {
688
+ dfs(neighbor);
689
+ lowMap.set(vertex, Math.min(lowMap.get(vertex)!, lowMap.get(neighbor)!));
690
+ } else if (inStack.has(neighbor)) {
691
+ lowMap.set(vertex, Math.min(lowMap.get(vertex)!, dfnMap.get(neighbor)!));
692
+ }
693
+ }
694
+
695
+ if (dfnMap.get(vertex) === lowMap.get(vertex)) {
696
+ const SCC: VO[] = [];
697
+ let poppedVertex: VO | undefined;
698
+
699
+ do {
700
+ poppedVertex = stack.pop();
701
+ inStack.delete(poppedVertex!);
702
+ SCC.push(poppedVertex!);
703
+ } while (poppedVertex !== vertex);
704
+
705
+ SCCs.set(SCCs.size, SCC);
706
+ }
707
+ };
708
+
709
+ for (const vertex of this.vertexMap.values()) {
710
+ if (!dfnMap.has(vertex)) {
711
+ dfs(vertex);
712
+ }
713
+ }
714
+
715
+ return { dfnMap, lowMap, SCCs };
716
+ }
717
+
718
+ /**
719
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
720
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
721
+ */
722
+
723
+ /**
724
+ * Time Complexity: O(V + E) - Depends on the implementation (Tarjan's algorithm).
725
+ * Space Complexity: O(V) - Depends on the implementation (Tarjan's algorithm).
726
+ *
727
+ * The function returns a map that associates each vertex object with its corresponding depth-first
728
+ * number.
729
+ * @returns A Map object with keys of type VO and values of type number.
730
+ */
731
+ getDFNMap(): Map<VO, number> {
732
+ return this.tarjan().dfnMap;
733
+ }
734
+
735
+ /**
736
+ * The function returns a Map object that contains the low values of each vertex in a Tarjan
737
+ * algorithm.
738
+ * @returns The method `getLowMap()` is returning a `Map` object with keys of type `VO` and values of
739
+ * type `number`.
740
+ */
741
+ getLowMap(): Map<VO, number> {
742
+ return this.tarjan().lowMap;
743
+ }
744
+
745
+ /**
746
+ * The function "getSCCs" returns a map of strongly connected components (SCCs) using the Tarjan
747
+ * algorithm.
748
+ * @returns a map where the keys are numbers and the values are arrays of VO objects.
749
+ */
750
+ getSCCs(): Map<number, VO[]> {
751
+ return this.tarjan().SCCs;
752
+ }
753
+
632
754
  /**
633
755
  * Time Complexity: O(1)
634
756
  * Space Complexity: O(1)