data-structure-typed 1.18.6 → 1.18.8

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 (262) hide show
  1. package/README.md +154 -366
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +72 -51
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +226 -158
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +7 -6
  5. package/dist/data-structures/binary-tree/avl-tree.js +5 -3
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +16 -6
  7. package/dist/data-structures/binary-tree/binary-tree.js +16 -7
  8. package/dist/data-structures/binary-tree/bst.d.ts +37 -28
  9. package/dist/data-structures/binary-tree/bst.js +44 -76
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +30 -1
  11. package/dist/data-structures/binary-tree/rb-tree.js +27 -14
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +18 -7
  13. package/dist/data-structures/binary-tree/tree-multiset.js +20 -15
  14. package/dist/data-structures/graph/abstract-graph.d.ts +28 -27
  15. package/dist/data-structures/graph/abstract-graph.js +58 -36
  16. package/dist/data-structures/graph/directed-graph.d.ts +27 -24
  17. package/dist/data-structures/graph/directed-graph.js +65 -60
  18. package/dist/data-structures/graph/undirected-graph.d.ts +12 -18
  19. package/dist/data-structures/graph/undirected-graph.js +52 -65
  20. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +100 -0
  21. package/dist/data-structures/interfaces/abstract-binary-tree.js +2 -0
  22. package/dist/data-structures/interfaces/abstract-graph.d.ts +3 -8
  23. package/dist/data-structures/interfaces/avl-tree.d.ts +16 -1
  24. package/dist/data-structures/interfaces/binary-tree.d.ts +4 -24
  25. package/dist/data-structures/interfaces/bst.d.ts +18 -1
  26. package/dist/data-structures/interfaces/directed-graph.d.ts +4 -1
  27. package/dist/data-structures/interfaces/index.d.ts +1 -1
  28. package/dist/data-structures/interfaces/index.js +1 -1
  29. package/dist/data-structures/interfaces/rb-tree.d.ts +9 -0
  30. package/dist/data-structures/interfaces/rb-tree.js +2 -0
  31. package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
  32. package/dist/data-structures/interfaces/undirected-graph.d.ts +4 -1
  33. package/dist/data-structures/tree/tree.d.ts +1 -4
  34. package/dist/data-structures/tree/tree.js +1 -12
  35. package/dist/data-structures/types/abstract-binary-tree.d.ts +11 -7
  36. package/dist/data-structures/types/abstract-binary-tree.js +9 -5
  37. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  38. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  39. package/dist/data-structures/types/bst.d.ts +1 -1
  40. package/dist/data-structures/types/bst.js +1 -1
  41. package/dist/data-structures/types/directed-graph.js +1 -1
  42. package/dist/data-structures/types/helpers.d.ts +1 -8
  43. package/dist/data-structures/types/rb-tree.d.ts +2 -0
  44. package/dist/data-structures/types/rb-tree.js +1 -1
  45. package/dist/data-structures/types/tree-multiset.d.ts +4 -4
  46. package/dist/utils/types/utils.d.ts +18 -0
  47. package/dist/utils/utils.d.ts +6 -1
  48. package/dist/utils/utils.js +83 -1
  49. package/package.json +2 -2
  50. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  51. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  52. package/backup/recursive-type/src/assets/logo.png +0 -0
  53. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  54. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
  55. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
  56. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
  57. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
  58. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
  59. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
  60. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  61. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  62. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  63. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
  64. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
  65. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
  66. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
  67. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
  68. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  69. package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
  70. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
  71. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  72. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  73. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  74. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  75. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  76. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  77. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  78. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  79. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  80. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  81. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  82. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  83. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  84. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  85. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
  86. package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
  87. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
  88. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
  89. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
  90. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
  91. package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
  92. package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
  93. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
  94. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
  95. package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
  96. package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
  97. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
  98. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
  99. package/backup/recursive-type/src/data-structures/index.ts +0 -15
  100. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
  101. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
  102. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
  103. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
  104. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
  105. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
  106. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
  107. package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
  108. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
  109. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
  110. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
  111. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
  112. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
  113. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
  114. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  115. package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
  116. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
  117. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
  118. package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
  119. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
  120. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
  121. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
  122. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
  123. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
  124. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
  125. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
  126. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
  127. package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
  128. package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
  129. package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
  130. package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
  131. package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
  132. package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
  133. package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
  134. package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
  135. package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
  136. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
  137. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
  138. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
  139. package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
  140. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
  141. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
  142. package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
  143. package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
  144. package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
  145. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
  146. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
  147. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
  148. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
  149. package/backup/recursive-type/src/index.ts +0 -1
  150. package/backup/recursive-type/src/utils/index.ts +0 -2
  151. package/backup/recursive-type/src/utils/types/index.ts +0 -1
  152. package/backup/recursive-type/src/utils/types/utils.ts +0 -6
  153. package/backup/recursive-type/src/utils/utils.ts +0 -78
  154. package/docs/.nojekyll +0 -1
  155. package/docs/assets/highlight.css +0 -92
  156. package/docs/assets/main.js +0 -58
  157. package/docs/assets/search.js +0 -1
  158. package/docs/assets/style.css +0 -1367
  159. package/docs/classes/AVLTree.html +0 -2339
  160. package/docs/classes/AVLTreeNode.html +0 -450
  161. package/docs/classes/AaTree.html +0 -166
  162. package/docs/classes/AbstractBinaryTree.html +0 -2023
  163. package/docs/classes/AbstractBinaryTreeNode.html +0 -491
  164. package/docs/classes/AbstractEdge.html +0 -289
  165. package/docs/classes/AbstractGraph.html +0 -1046
  166. package/docs/classes/AbstractVertex.html +0 -252
  167. package/docs/classes/ArrayDeque.html +0 -433
  168. package/docs/classes/BST.html +0 -2196
  169. package/docs/classes/BSTNode.html +0 -453
  170. package/docs/classes/BTree.html +0 -166
  171. package/docs/classes/BinaryIndexedTree.html +0 -335
  172. package/docs/classes/BinaryTree.html +0 -2037
  173. package/docs/classes/BinaryTreeNode.html +0 -452
  174. package/docs/classes/Character.html +0 -214
  175. package/docs/classes/CoordinateMap.html +0 -477
  176. package/docs/classes/CoordinateSet.html +0 -438
  177. package/docs/classes/Deque.html +0 -969
  178. package/docs/classes/DirectedEdge.html +0 -360
  179. package/docs/classes/DirectedGraph.html +0 -1408
  180. package/docs/classes/DirectedVertex.html +0 -248
  181. package/docs/classes/DoublyLinkedList.html +0 -962
  182. package/docs/classes/DoublyLinkedListNode.html +0 -291
  183. package/docs/classes/HashTable.html +0 -166
  184. package/docs/classes/Heap.html +0 -417
  185. package/docs/classes/HeapItem.html +0 -249
  186. package/docs/classes/Matrix2D.html +0 -496
  187. package/docs/classes/MatrixNTI2D.html +0 -234
  188. package/docs/classes/MaxHeap.html +0 -430
  189. package/docs/classes/MaxPriorityQueue.html +0 -830
  190. package/docs/classes/MinHeap.html +0 -431
  191. package/docs/classes/MinPriorityQueue.html +0 -832
  192. package/docs/classes/Navigator.html +0 -307
  193. package/docs/classes/ObjectDeque.html +0 -449
  194. package/docs/classes/Pair.html +0 -166
  195. package/docs/classes/PriorityQueue.html +0 -754
  196. package/docs/classes/Queue.html +0 -386
  197. package/docs/classes/SegmentTree.html +0 -428
  198. package/docs/classes/SegmentTreeNode.html +0 -351
  199. package/docs/classes/SinglyLinkedList.html +0 -782
  200. package/docs/classes/SinglyLinkedListNode.html +0 -264
  201. package/docs/classes/SkipLinkedList.html +0 -166
  202. package/docs/classes/SplayTree.html +0 -166
  203. package/docs/classes/Stack.html +0 -362
  204. package/docs/classes/TreeMap.html +0 -166
  205. package/docs/classes/TreeMultiSet.html +0 -2196
  206. package/docs/classes/TreeMultiSetNode.html +0 -450
  207. package/docs/classes/TreeNode.html +0 -337
  208. package/docs/classes/TreeSet.html +0 -166
  209. package/docs/classes/Trie.html +0 -366
  210. package/docs/classes/TrieNode.html +0 -274
  211. package/docs/classes/TwoThreeTree.html +0 -166
  212. package/docs/classes/UndirectedEdge.html +0 -331
  213. package/docs/classes/UndirectedGraph.html +0 -1196
  214. package/docs/classes/UndirectedVertex.html +0 -248
  215. package/docs/classes/Vector2D.html +0 -799
  216. package/docs/enums/CP.html +0 -175
  217. package/docs/enums/FamilyPosition.html +0 -175
  218. package/docs/enums/LoopType.html +0 -176
  219. package/docs/enums/RBColor.html +0 -168
  220. package/docs/enums/TopologicalProperty.html +0 -175
  221. package/docs/index.html +0 -639
  222. package/docs/interfaces/IBinaryTree.html +0 -189
  223. package/docs/interfaces/IBinaryTreeNode.html +0 -396
  224. package/docs/interfaces/IDirectedGraph.html +0 -259
  225. package/docs/interfaces/IGraph.html +0 -471
  226. package/docs/interfaces/IUNDirectedGraph.html +0 -151
  227. package/docs/modules.html +0 -250
  228. package/docs/types/AVLTreeOptions.html +0 -144
  229. package/docs/types/AbstractBinaryTreeOptions.html +0 -150
  230. package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -146
  231. package/docs/types/AbstractResultByProperty.html +0 -146
  232. package/docs/types/AbstractResultsByProperty.html +0 -146
  233. package/docs/types/BSTComparator.html +0 -156
  234. package/docs/types/BSTOptions.html +0 -146
  235. package/docs/types/BinaryTreeDeletedResult.html +0 -153
  236. package/docs/types/BinaryTreeNodeId.html +0 -141
  237. package/docs/types/BinaryTreeNodePropertyName.html +0 -141
  238. package/docs/types/BinaryTreeOptions.html +0 -144
  239. package/docs/types/DFSOrderPattern.html +0 -141
  240. package/docs/types/DijkstraResult.html +0 -161
  241. package/docs/types/Direction.html +0 -141
  242. package/docs/types/EdgeId.html +0 -141
  243. package/docs/types/HeapOptions.html +0 -162
  244. package/docs/types/IdObject.html +0 -151
  245. package/docs/types/KeyValObject.html +0 -146
  246. package/docs/types/NavigatorParams.html +0 -175
  247. package/docs/types/NodeOrPropertyName.html +0 -141
  248. package/docs/types/PriorityQueueComparator.html +0 -161
  249. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -141
  250. package/docs/types/PriorityQueueOptions.html +0 -155
  251. package/docs/types/RBTreeOptions.html +0 -144
  252. package/docs/types/RecursiveAVLTreeNode.html +0 -146
  253. package/docs/types/RecursiveBSTNode.html +0 -146
  254. package/docs/types/RecursiveBinaryTreeNode.html +0 -146
  255. package/docs/types/RecursiveTreeMultiSetNode.html +0 -146
  256. package/docs/types/SegmentTreeNodeVal.html +0 -141
  257. package/docs/types/TopologicalStatus.html +0 -141
  258. package/docs/types/TreeMultiSetOptions.html +0 -146
  259. package/docs/types/Turning.html +0 -141
  260. package/docs/types/VertexId.html +0 -141
  261. package/notes/bst.test.ts +0 -181
  262. package/notes/note.md +0 -34
@@ -1,5 +1,5 @@
1
1
  import type { DijkstraResult, VertexId } from '../types';
2
- import { IGraph } from '../interfaces';
2
+ import { IAbstractGraph } from '../interfaces';
3
3
  export declare abstract class AbstractVertex<T = number> {
4
4
  protected constructor(id: VertexId, val?: T);
5
5
  private _id;
@@ -21,7 +21,7 @@ export declare abstract class AbstractEdge<T = number> {
21
21
  get hashCode(): string;
22
22
  protected _setHashCode(v: string): void;
23
23
  }
24
- export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E extends AbstractEdge<any>> implements IGraph<V, E> {
24
+ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E extends AbstractEdge<any>> implements IAbstractGraph<V, E> {
25
25
  private _vertices;
26
26
  get vertices(): Map<VertexId, V>;
27
27
  /**
@@ -30,7 +30,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
30
30
  * @param id
31
31
  * @param val
32
32
  */
33
- abstract _createVertex(id: VertexId, val?: V): V;
33
+ abstract createVertex(id: VertexId, val?: V): V;
34
34
  /**
35
35
  * 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.
36
36
  * 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.
@@ -39,12 +39,9 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
39
39
  * @param weight
40
40
  * @param val
41
41
  */
42
- abstract _createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
43
- abstract removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
42
+ abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
44
43
  abstract removeEdge(edge: E): E | null;
45
- _getVertex(vertexOrId: VertexId | V): V | null;
46
44
  getVertex(vertexId: VertexId): V | null;
47
- _getVertexId(vertexOrId: V | VertexId): VertexId;
48
45
  /**
49
46
  * The function checks if a vertex exists in a graph.
50
47
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
@@ -53,8 +50,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
53
50
  */
54
51
  hasVertex(vertexOrId: V | VertexId): boolean;
55
52
  abstract getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
56
- createAddVertex(id: VertexId, val?: V['val']): boolean;
57
- addVertex(newVertex: V): boolean;
53
+ addVertex(vertex: V): boolean;
54
+ addVertex(id: VertexId, val?: V['val']): boolean;
58
55
  /**
59
56
  * The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
60
57
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
@@ -83,8 +80,8 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
83
80
  * vertices `v1` and `v2`, and `false` otherwise.
84
81
  */
85
82
  hasEdge(v1: VertexId | V, v2: VertexId | V): boolean;
86
- createAddEdge(src: V | VertexId, dest: V | VertexId, weight: number, val: E['val']): boolean;
87
- abstract addEdge(edge: E): boolean;
83
+ addEdge(edge: E): boolean;
84
+ addEdge(src: V | VertexId, dest: V | VertexId, weight?: number, val?: E['val']): boolean;
88
85
  /**
89
86
  * The function sets the weight of an edge between two vertices in a graph.
90
87
  * @param {VertexId | V} srcOrId - The `srcOrId` parameter can be either a `VertexId` or a `V` object. It represents
@@ -179,11 +176,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
179
176
  * @returns The function `dijkstra` returns an object of type `DijkstraResult<V>`.
180
177
  */
181
178
  dijkstra(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult<V>;
182
- /**
183
- * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
184
- * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
185
- * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
186
- */
187
179
  /**
188
180
  * Dijkstra algorithm time: O(logVE) space: O(V + E)
189
181
  * Dijkstra's algorithm is used to find the shortest paths from a source node to all other nodes in a graph. Its basic idea is to repeatedly choose the node closest to the source node and update the distances of other nodes using this node as an intermediary. Dijkstra's algorithm requires that the edge weights in the graph are non-negative.
@@ -213,12 +205,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
213
205
  min: number;
214
206
  minPath: V[];
215
207
  };
216
- /**
217
- * BellmanFord time:O(VE) space:O(V)
218
- * one to rest pairs
219
- * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
220
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
221
- */
222
208
  /**
223
209
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
224
210
  * all pairs
@@ -234,11 +220,6 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
234
220
  costs: number[][];
235
221
  predecessor: (V | null)[][];
236
222
  };
237
- /**
238
- * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
239
- * all pairs
240
- * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
241
- */
242
223
  /**
243
224
  * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
244
225
  * Tarjan can find cycles in directed or undirected graph
@@ -268,6 +249,26 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any>, E ext
268
249
  SCCs: Map<number, V[]>;
269
250
  cycles: Map<number, V[]>;
270
251
  };
252
+ /**
253
+ * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
254
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
255
+ * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
256
+ */
257
+ protected _addVertexOnly(newVertex: V): boolean;
258
+ protected abstract _addEdgeOnly(edge: E): boolean;
259
+ /**
260
+ * BellmanFord time:O(VE) space:O(V)
261
+ * one to rest pairs
262
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
263
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
264
+ */
265
+ protected _getVertex(vertexOrId: VertexId | V): V | null;
266
+ /**
267
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
268
+ * all pairs
269
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
270
+ */
271
+ protected _getVertexId(vertexOrId: V | VertexId): VertexId;
271
272
  /**--- start find cycles --- */
272
273
  protected _setVertices(value: Map<VertexId, V>): void;
273
274
  }
@@ -115,7 +115,7 @@ var AbstractEdge = /** @class */ (function () {
115
115
  // * @param weight
116
116
  // * @param val
117
117
  // */
118
- // abstract _createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
118
+ // abstract createEdge(srcOrV1: VertexId | string, destOrV2: VertexId | string, weight?: number, val?: E): E;
119
119
  AbstractEdge.prototype._setHashCode = function (v) {
120
120
  this._hashCode = v;
121
121
  };
@@ -137,16 +137,9 @@ var AbstractGraph = /** @class */ (function () {
137
137
  enumerable: false,
138
138
  configurable: true
139
139
  });
140
- AbstractGraph.prototype._getVertex = function (vertexOrId) {
141
- var vertexId = this._getVertexId(vertexOrId);
142
- return this._vertices.get(vertexId) || null;
143
- };
144
140
  AbstractGraph.prototype.getVertex = function (vertexId) {
145
141
  return this._vertices.get(vertexId) || null;
146
142
  };
147
- AbstractGraph.prototype._getVertexId = function (vertexOrId) {
148
- return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
149
- };
150
143
  /**
151
144
  * The function checks if a vertex exists in a graph.
152
145
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
@@ -156,17 +149,14 @@ var AbstractGraph = /** @class */ (function () {
156
149
  AbstractGraph.prototype.hasVertex = function (vertexOrId) {
157
150
  return this._vertices.has(this._getVertexId(vertexOrId));
158
151
  };
159
- AbstractGraph.prototype.createAddVertex = function (id, val) {
160
- var newVertex = this._createVertex(id, val);
161
- return this.addVertex(newVertex);
162
- };
163
- AbstractGraph.prototype.addVertex = function (newVertex) {
164
- if (this.hasVertex(newVertex)) {
165
- return false;
166
- // throw (new Error('Duplicated vertex id is not allowed'));
152
+ AbstractGraph.prototype.addVertex = function (idOrVertex, val) {
153
+ if (idOrVertex instanceof AbstractVertex) {
154
+ return this._addVertexOnly(idOrVertex);
155
+ }
156
+ else {
157
+ var newVertex = this.createVertex(idOrVertex, val);
158
+ return this._addVertexOnly(newVertex);
167
159
  }
168
- this._vertices.set(newVertex.id, newVertex);
169
- return true;
170
160
  };
171
161
  /**
172
162
  * The `removeVertex` function removes a vertex from a graph by its ID or by the vertex object itself.
@@ -216,13 +206,25 @@ var AbstractGraph = /** @class */ (function () {
216
206
  var edge = this.getEdge(v1, v2);
217
207
  return !!edge;
218
208
  };
219
- AbstractGraph.prototype.createAddEdge = function (src, dest, weight, val) {
220
- if (src instanceof AbstractVertex)
221
- src = src.id;
222
- if (dest instanceof AbstractVertex)
223
- dest = dest.id;
224
- var newEdge = this._createEdge(src, dest, weight, val);
225
- return this.addEdge(newEdge);
209
+ AbstractGraph.prototype.addEdge = function (srcOrEdge, dest, weight, val) {
210
+ if (srcOrEdge instanceof AbstractEdge) {
211
+ return this._addEdgeOnly(srcOrEdge);
212
+ }
213
+ else {
214
+ if (dest instanceof AbstractVertex || typeof dest === 'string' || typeof dest === 'number') {
215
+ if (!(this.hasVertex(srcOrEdge) && this.hasVertex(dest)))
216
+ return false;
217
+ if (srcOrEdge instanceof AbstractVertex)
218
+ srcOrEdge = srcOrEdge.id;
219
+ if (dest instanceof AbstractVertex)
220
+ dest = dest.id;
221
+ var newEdge = this.createEdge(srcOrEdge, dest, weight, val);
222
+ return this._addEdgeOnly(newEdge);
223
+ }
224
+ else {
225
+ throw new Error('dest must be a Vertex or vertex id while srcOrEdge is an Edge');
226
+ }
227
+ }
226
228
  };
227
229
  /**
228
230
  * The function sets the weight of an edge between two vertices in a graph.
@@ -891,12 +893,6 @@ var AbstractGraph = /** @class */ (function () {
891
893
  }
892
894
  return { hasNegativeCycle: hasNegativeCycle, distMap: distMap, preMap: preMap, paths: paths, min: min, minPath: minPath };
893
895
  };
894
- /**
895
- * BellmanFord time:O(VE) space:O(V)
896
- * one to rest pairs
897
- * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
898
- * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
899
- */
900
896
  /**
901
897
  * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
902
898
  * all pairs
@@ -939,11 +935,6 @@ var AbstractGraph = /** @class */ (function () {
939
935
  }
940
936
  return { costs: costs, predecessor: predecessor };
941
937
  };
942
- /**
943
- * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
944
- * all pairs
945
- * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
946
- */
947
938
  /**
948
939
  * Tarjan is an algorithm based on DFS,which is used to solve the connectivity problem of graphs.
949
940
  * Tarjan can find cycles in directed or undirected graph
@@ -1071,6 +1062,37 @@ var AbstractGraph = /** @class */ (function () {
1071
1062
  }
1072
1063
  return { dfnMap: dfnMap, lowMap: lowMap, bridges: bridges, articulationPoints: articulationPoints, SCCs: SCCs, cycles: cycles };
1073
1064
  };
1065
+ /**
1066
+ * Dijkstra's algorithm only solves the single-source shortest path problem, while the Bellman-Ford algorithm and Floyd-Warshall algorithm can address shortest paths between all pairs of nodes.
1067
+ * Dijkstra's algorithm is suitable for graphs with non-negative edge weights, whereas the Bellman-Ford algorithm and Floyd-Warshall algorithm can handle negative-weight edges.
1068
+ * The time complexity of Dijkstra's algorithm and the Bellman-Ford algorithm depends on the size of the graph, while the time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of nodes. For dense graphs, Floyd-Warshall might become slower.
1069
+ */
1070
+ AbstractGraph.prototype._addVertexOnly = function (newVertex) {
1071
+ if (this.hasVertex(newVertex)) {
1072
+ return false;
1073
+ // throw (new Error('Duplicated vertex id is not allowed'));
1074
+ }
1075
+ this._vertices.set(newVertex.id, newVertex);
1076
+ return true;
1077
+ };
1078
+ /**
1079
+ * BellmanFord time:O(VE) space:O(V)
1080
+ * one to rest pairs
1081
+ * The Bellman-Ford algorithm is also used to find the shortest paths from a source node to all other nodes in a graph. Unlike Dijkstra's algorithm, it can handle edge weights that are negative. Its basic idea involves iterative relaxation of all edges for several rounds to gradually approximate the shortest paths. Due to its ability to handle negative-weight edges, the Bellman-Ford algorithm is more flexible in some scenarios.
1082
+ * The `bellmanFord` function implements the Bellman-Ford algorithm to find the shortest path from a source vertex to
1083
+ */
1084
+ AbstractGraph.prototype._getVertex = function (vertexOrId) {
1085
+ var vertexId = this._getVertexId(vertexOrId);
1086
+ return this._vertices.get(vertexId) || null;
1087
+ };
1088
+ /**
1089
+ * Floyd algorithm time: O(V^3) space: O(V^2), not support graph with negative weight cycle
1090
+ * all pairs
1091
+ * The Floyd-Warshall algorithm is used to find the shortest paths between all pairs of nodes in a graph. It employs dynamic programming to compute the shortest paths from any node to any other node. The Floyd-Warshall algorithm's advantage lies in its ability to handle graphs with negative-weight edges, and it can simultaneously compute shortest paths between any two nodes.
1092
+ */
1093
+ AbstractGraph.prototype._getVertexId = function (vertexOrId) {
1094
+ return vertexOrId instanceof AbstractVertex ? vertexOrId.id : vertexOrId;
1095
+ };
1074
1096
  /**--- start find cycles --- */
1075
1097
  AbstractGraph.prototype._setVertices = function (value) {
1076
1098
  this._vertices = value;
@@ -44,7 +44,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
44
44
  * @param id
45
45
  * @param val
46
46
  */
47
- _createVertex(id: VertexId, val?: V['val']): V;
47
+ createVertex(id: VertexId, val?: V['val']): V;
48
48
  /**
49
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.
50
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.
@@ -53,7 +53,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
53
53
  * @param weight
54
54
  * @param val
55
55
  */
56
- _createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E;
56
+ createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E;
57
57
  /**
58
58
  * The function `getEdge` returns the directed edge between two vertices, given their source and destination.
59
59
  * @param {V | null | VertexId} srcOrId - The source vertex or its ID. It can be either a
@@ -63,14 +63,6 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
63
63
  * @returns a E object or null.
64
64
  */
65
65
  getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null;
66
- /**
67
- * The `addEdge` function adds a directed edge to a graph if the source and destination vertices exist.
68
- * @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
69
- * contains two properties:
70
- * @returns The method `addEdge` returns a boolean value. It returns `true` if the edge was successfully added to the
71
- * graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
72
- */
73
- addEdge(edge: E): boolean;
74
66
  /**
75
67
  * The `removeEdgeBetween` function removes an edge between two vertices in a directed graph and returns the removed
76
68
  * edge, or null if the edge was not found.
@@ -81,7 +73,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
81
73
  * @returns The function `removeEdgeBetween` returns the removed edge (`E`) if it exists, or `null` if
82
74
  * the edge does not exist.
83
75
  */
84
- removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
76
+ removeEdgeSrcToDest(srcOrId: V | VertexId, destOrId: V | VertexId): E | null;
85
77
  /**
86
78
  * The `removeEdge` function removes a directed edge from a graph and returns the removed edge, or null if the edge was
87
79
  * not found.
@@ -92,13 +84,14 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
92
84
  */
93
85
  removeEdge(edge: E): E | null;
94
86
  /**
95
- * The function removeAllEdges removes all edges between two vertices.
96
- * @param {VertexId | V} src - The `src` parameter can be either a `VertexId` or a `V`.
97
- * @param {VertexId | V} dest - The `dest` parameter represents the destination vertex of an edge. It
98
- * can be either a `VertexId` or a `V`.
99
- * @returns An empty array of DirectedEdge objects is being returned.
87
+ * The function removes all edges between two vertices and returns the removed edges.
88
+ * @param {VertexId | V} v1 - The parameter `v1` represents either a `VertexId` or a `V` object. It is used to identify
89
+ * the first vertex in the graph.
90
+ * @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V`. It is used to identify the
91
+ * second vertex involved in the edges that need to be removed.
92
+ * @returns The function `removeEdgesBetween` returns an array of removed edges (`E[]`).
100
93
  */
101
- removeAllEdges(src: VertexId | V, dest: VertexId | V): E[];
94
+ removeEdgesBetween(v1: VertexId | V, v2: VertexId | V): E[];
102
95
  /**
103
96
  * The function returns an array of incoming edges of a given vertex or vertex ID.
104
97
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
@@ -163,18 +156,19 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
163
156
  */
164
157
  getDestinations(vertex: V | VertexId | null): V[];
165
158
  /**
166
- * The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
167
- * reverse order, or null if the graph contains a cycle.
168
- * @returns The function `topologicalSort()` returns an array of `V` or `VertexId` objects in
169
- * topological order, or `null` if there is a cycle in the graph.
159
+ * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
160
+ * in the sorted order, or null if the graph contains a cycle.
161
+ * @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
162
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
163
+ * specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
164
+ * @returns an array of vertices or vertex IDs in topological order, or null if there is a cycle in the graph.
170
165
  */
171
- topologicalSort(): Array<V | VertexId> | null;
166
+ topologicalSort(propertyName?: 'vertex' | 'id'): Array<V | VertexId> | null;
172
167
  /**
173
168
  * The `edgeSet` function returns an array of all directed edges in the graph.
174
169
  * @returns The `edgeSet()` method returns an array of `E` objects.
175
170
  */
176
171
  edgeSet(): E[];
177
- /**--- start find cycles --- */
178
172
  /**
179
173
  * The function `getNeighbors` returns an array of neighboring vertices of a given vertex in a directed graph.
180
174
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
@@ -182,7 +176,7 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
182
176
  * @returns an array of DirectedVertex objects.
183
177
  */
184
178
  getNeighbors(vertexOrId: V | VertexId): V[];
185
- /**--- end find cycles --- */
179
+ /**--- start find cycles --- */
186
180
  /**
187
181
  * The function "getEndsOfEdge" returns the source and destination vertices of a directed edge if it exists in the
188
182
  * graph, otherwise it returns null.
@@ -191,6 +185,15 @@ export declare class DirectedGraph<V extends DirectedVertex<any> = DirectedVerte
191
185
  * not exist in the graph.
192
186
  */
193
187
  getEndsOfEdge(edge: E): [V, V] | null;
188
+ /**--- end find cycles --- */
189
+ /**
190
+ * The `_addEdgeOnly` function adds a directed edge to a graph if the source and destination vertices exist.
191
+ * @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
192
+ * contains two properties:
193
+ * @returns The method `_addEdgeOnly` returns a boolean value. It returns `true` if the edge was successfully added to the
194
+ * graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
195
+ */
196
+ protected _addEdgeOnly(edge: E): boolean;
194
197
  protected _setOutEdgeMap(value: Map<V, E[]>): void;
195
198
  protected _setInEdgeMap(value: Map<V, E[]>): void;
196
199
  }
@@ -148,7 +148,7 @@ var DirectedGraph = /** @class */ (function (_super) {
148
148
  * @param id
149
149
  * @param val
150
150
  */
151
- DirectedGraph.prototype._createVertex = function (id, val) {
151
+ DirectedGraph.prototype.createVertex = function (id, val) {
152
152
  return new DirectedVertex(id, val !== null && val !== void 0 ? val : id);
153
153
  };
154
154
  /**
@@ -159,7 +159,7 @@ var DirectedGraph = /** @class */ (function (_super) {
159
159
  * @param weight
160
160
  * @param val
161
161
  */
162
- DirectedGraph.prototype._createEdge = function (src, dest, weight, val) {
162
+ DirectedGraph.prototype.createEdge = function (src, dest, weight, val) {
163
163
  return new DirectedEdge(src, dest, weight !== null && weight !== void 0 ? weight : 1, val);
164
164
  };
165
165
  /**
@@ -184,41 +184,6 @@ var DirectedGraph = /** @class */ (function (_super) {
184
184
  }
185
185
  return edges[0] || null;
186
186
  };
187
- /**
188
- * The `addEdge` function adds a directed edge to a graph if the source and destination vertices exist.
189
- * @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
190
- * contains two properties:
191
- * @returns The method `addEdge` returns a boolean value. It returns `true` if the edge was successfully added to the
192
- * graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
193
- */
194
- DirectedGraph.prototype.addEdge = function (edge) {
195
- if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
196
- return false;
197
- }
198
- var srcVertex = this._getVertex(edge.src);
199
- var destVertex = this._getVertex(edge.dest);
200
- // TODO after no-non-null-assertion not ensure the logic
201
- if (srcVertex && destVertex) {
202
- var srcOutEdges = this._outEdgeMap.get(srcVertex);
203
- if (srcOutEdges) {
204
- srcOutEdges.push(edge);
205
- }
206
- else {
207
- this._outEdgeMap.set(srcVertex, [edge]);
208
- }
209
- var destInEdges = this._inEdgeMap.get(destVertex);
210
- if (destInEdges) {
211
- destInEdges.push(edge);
212
- }
213
- else {
214
- this._inEdgeMap.set(destVertex, [edge]);
215
- }
216
- return true;
217
- }
218
- else {
219
- return false;
220
- }
221
- };
222
187
  /**
223
188
  * The `removeEdgeBetween` function removes an edge between two vertices in a directed graph and returns the removed
224
189
  * edge, or null if the edge was not found.
@@ -229,7 +194,7 @@ var DirectedGraph = /** @class */ (function (_super) {
229
194
  * @returns The function `removeEdgeBetween` returns the removed edge (`E`) if it exists, or `null` if
230
195
  * the edge does not exist.
231
196
  */
232
- DirectedGraph.prototype.removeEdgeBetween = function (srcOrId, destOrId) {
197
+ DirectedGraph.prototype.removeEdgeSrcToDest = function (srcOrId, destOrId) {
233
198
  var src = this._getVertex(srcOrId);
234
199
  var dest = this._getVertex(destOrId);
235
200
  var removed = null;
@@ -238,14 +203,6 @@ var DirectedGraph = /** @class */ (function (_super) {
238
203
  }
239
204
  var srcOutEdges = this._outEdgeMap.get(src);
240
205
  if (srcOutEdges) {
241
- /**
242
- * The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
243
- * found.
244
- * @param {E} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
245
- * object that has `src` and `dest` properties, which represent the source and destination vertices of the edge,
246
- * respectively.
247
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
248
- */
249
206
  (0, utils_1.arrayRemove)(srcOutEdges, function (edge) { return edge.dest === dest.id; });
250
207
  }
251
208
  var destInEdges = this._inEdgeMap.get(dest);
@@ -279,14 +236,22 @@ var DirectedGraph = /** @class */ (function (_super) {
279
236
  return removed;
280
237
  };
281
238
  /**
282
- * The function removeAllEdges removes all edges between two vertices.
283
- * @param {VertexId | V} src - The `src` parameter can be either a `VertexId` or a `V`.
284
- * @param {VertexId | V} dest - The `dest` parameter represents the destination vertex of an edge. It
285
- * can be either a `VertexId` or a `V`.
286
- * @returns An empty array of DirectedEdge objects is being returned.
239
+ * The function removes all edges between two vertices and returns the removed edges.
240
+ * @param {VertexId | V} v1 - The parameter `v1` represents either a `VertexId` or a `V` object. It is used to identify
241
+ * the first vertex in the graph.
242
+ * @param {VertexId | V} v2 - The parameter `v2` represents either a `VertexId` or a `V`. It is used to identify the
243
+ * second vertex involved in the edges that need to be removed.
244
+ * @returns The function `removeEdgesBetween` returns an array of removed edges (`E[]`).
287
245
  */
288
- DirectedGraph.prototype.removeAllEdges = function (src, dest) {
289
- return [];
246
+ DirectedGraph.prototype.removeEdgesBetween = function (v1, v2) {
247
+ var removed = [];
248
+ if (v1 && v2) {
249
+ var v1ToV2 = this.removeEdgeSrcToDest(v1, v2);
250
+ var v2ToV1 = this.removeEdgeSrcToDest(v2, v1);
251
+ v1ToV2 && removed.push(v1ToV2);
252
+ v2ToV1 && removed.push(v2ToV1);
253
+ }
254
+ return removed;
290
255
  };
291
256
  /**
292
257
  * The function returns an array of incoming edges of a given vertex or vertex ID.
@@ -400,14 +365,17 @@ var DirectedGraph = /** @class */ (function (_super) {
400
365
  return destinations;
401
366
  };
402
367
  /**
403
- * The `topologicalSort` function performs a topological sort on a directed graph and returns the sorted vertices in
404
- * reverse order, or null if the graph contains a cycle.
405
- * @returns The function `topologicalSort()` returns an array of `V` or `VertexId` objects in
406
- * topological order, or `null` if there is a cycle in the graph.
368
+ * The `topologicalSort` function performs a topological sort on a graph and returns an array of vertices or vertex IDs
369
+ * in the sorted order, or null if the graph contains a cycle.
370
+ * @param {'vertex' | 'id'} [propertyName] - The `propertyName` parameter is an optional parameter that specifies the
371
+ * property to use for sorting the vertices. It can have two possible values: 'vertex' or 'id'. If 'vertex' is
372
+ * specified, the vertices themselves will be used for sorting. If 'id' is specified, the ids of
373
+ * @returns an array of vertices or vertex IDs in topological order, or null if there is a cycle in the graph.
407
374
  */
408
- DirectedGraph.prototype.topologicalSort = function () {
375
+ DirectedGraph.prototype.topologicalSort = function (propertyName) {
409
376
  var e_2, _a, e_3, _b;
410
377
  var _this = this;
378
+ propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
411
379
  // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
412
380
  // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
413
381
  var statusMap = new Map();
@@ -469,6 +437,8 @@ var DirectedGraph = /** @class */ (function (_super) {
469
437
  }
470
438
  if (hasCycle)
471
439
  return null;
440
+ if (propertyName === 'id')
441
+ sorted = sorted.map(function (vertex) { return vertex instanceof DirectedVertex ? vertex.id : vertex; });
472
442
  return sorted.reverse();
473
443
  };
474
444
  /**
@@ -482,7 +452,6 @@ var DirectedGraph = /** @class */ (function (_super) {
482
452
  });
483
453
  return edges;
484
454
  };
485
- /**--- start find cycles --- */
486
455
  /**
487
456
  * The function `getNeighbors` returns an array of neighboring vertices of a given vertex in a directed graph.
488
457
  * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a `V`
@@ -515,7 +484,7 @@ var DirectedGraph = /** @class */ (function (_super) {
515
484
  }
516
485
  return neighbors;
517
486
  };
518
- /**--- end find cycles --- */
487
+ /**--- start find cycles --- */
519
488
  /**
520
489
  * The function "getEndsOfEdge" returns the source and destination vertices of a directed edge if it exists in the
521
490
  * graph, otherwise it returns null.
@@ -536,6 +505,42 @@ var DirectedGraph = /** @class */ (function (_super) {
536
505
  return null;
537
506
  }
538
507
  };
508
+ /**--- end find cycles --- */
509
+ /**
510
+ * The `_addEdgeOnly` function adds a directed edge to a graph if the source and destination vertices exist.
511
+ * @param edge - The parameter `edge` is of type `E`, which represents a directed edge in a graph. It
512
+ * contains two properties:
513
+ * @returns The method `_addEdgeOnly` returns a boolean value. It returns `true` if the edge was successfully added to the
514
+ * graph, and `false` if either the source or destination vertex of the edge is not present in the graph.
515
+ */
516
+ DirectedGraph.prototype._addEdgeOnly = function (edge) {
517
+ if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
518
+ return false;
519
+ }
520
+ var srcVertex = this._getVertex(edge.src);
521
+ var destVertex = this._getVertex(edge.dest);
522
+ // TODO after no-non-null-assertion not ensure the logic
523
+ if (srcVertex && destVertex) {
524
+ var srcOutEdges = this._outEdgeMap.get(srcVertex);
525
+ if (srcOutEdges) {
526
+ srcOutEdges.push(edge);
527
+ }
528
+ else {
529
+ this._outEdgeMap.set(srcVertex, [edge]);
530
+ }
531
+ var destInEdges = this._inEdgeMap.get(destVertex);
532
+ if (destInEdges) {
533
+ destInEdges.push(edge);
534
+ }
535
+ else {
536
+ this._inEdgeMap.set(destVertex, [edge]);
537
+ }
538
+ return true;
539
+ }
540
+ else {
541
+ return false;
542
+ }
543
+ };
539
544
  DirectedGraph.prototype._setOutEdgeMap = function (value) {
540
545
  this._outEdgeMap = value;
541
546
  };