data-structure-typed 1.15.2 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/README.md +24 -24
  2. package/dist/data-structures/heap/heap.d.ts +30 -15
  3. package/dist/data-structures/heap/heap.js +60 -15
  4. package/dist/data-structures/heap/max-heap.d.ts +3 -3
  5. package/dist/data-structures/heap/min-heap.d.ts +3 -3
  6. package/dist/data-structures/index.d.ts +1 -0
  7. package/dist/data-structures/index.js +1 -0
  8. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +195 -68
  9. package/dist/data-structures/linked-list/doubly-linked-list.js +456 -244
  10. package/dist/data-structures/linked-list/singly-linked-list.d.ts +126 -241
  11. package/dist/data-structures/linked-list/singly-linked-list.js +331 -628
  12. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +4 -5
  13. package/dist/data-structures/priority-queue/max-priority-queue.js +30 -6
  14. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +4 -5
  15. package/dist/data-structures/priority-queue/min-priority-queue.js +31 -6
  16. package/dist/data-structures/priority-queue/priority-queue.d.ts +13 -2
  17. package/dist/data-structures/priority-queue/priority-queue.js +28 -22
  18. package/dist/data-structures/tree/index.d.ts +1 -0
  19. package/dist/data-structures/tree/index.js +17 -0
  20. package/dist/data-structures/tree/tree.d.ts +9 -0
  21. package/dist/data-structures/tree/tree.js +52 -0
  22. package/dist/data-structures/types/binary-tree.d.ts +0 -5
  23. package/dist/data-structures/types/heap.d.ts +0 -4
  24. package/dist/utils/types/utils.d.ts +1 -0
  25. package/docs/.nojekyll +1 -0
  26. package/docs/assets/highlight.css +92 -0
  27. package/docs/assets/main.js +58 -0
  28. package/docs/assets/search.js +1 -0
  29. package/docs/assets/style.css +1367 -0
  30. package/docs/classes/AVLTree.html +2191 -0
  31. package/docs/classes/AVLTreeNode.html +573 -0
  32. package/docs/classes/AaTree.html +149 -0
  33. package/docs/classes/AbstractEdge.html +268 -0
  34. package/docs/classes/AbstractGraph.html +926 -0
  35. package/docs/classes/AbstractVertex.html +214 -0
  36. package/docs/classes/ArrayDeque.html +416 -0
  37. package/docs/classes/BST.html +2037 -0
  38. package/docs/classes/BSTNode.html +569 -0
  39. package/docs/classes/BTree.html +149 -0
  40. package/docs/classes/BinaryIndexedTree.html +288 -0
  41. package/docs/classes/BinaryTree.html +1826 -0
  42. package/docs/classes/BinaryTreeNode.html +532 -0
  43. package/docs/classes/Character.html +197 -0
  44. package/docs/classes/CoordinateMap.html +468 -0
  45. package/docs/classes/CoordinateSet.html +429 -0
  46. package/docs/classes/Deque.html +897 -0
  47. package/docs/classes/DirectedEdge.html +353 -0
  48. package/docs/classes/DirectedGraph.html +1242 -0
  49. package/docs/classes/DirectedVertex.html +225 -0
  50. package/docs/classes/DoublyLinkedList.html +891 -0
  51. package/docs/classes/DoublyLinkedListNode.html +274 -0
  52. package/docs/classes/Heap.html +481 -0
  53. package/docs/classes/HeapItem.html +232 -0
  54. package/docs/classes/Matrix2D.html +479 -0
  55. package/docs/classes/MatrixNTI2D.html +217 -0
  56. package/docs/classes/MaxHeap.html +499 -0
  57. package/docs/classes/MaxPriorityQueue.html +808 -0
  58. package/docs/classes/MinHeap.html +500 -0
  59. package/docs/classes/MinPriorityQueue.html +810 -0
  60. package/docs/classes/Navigator.html +290 -0
  61. package/docs/classes/ObjectDeque.html +422 -0
  62. package/docs/classes/PriorityQueue.html +732 -0
  63. package/docs/classes/Queue.html +369 -0
  64. package/docs/classes/RBTree.html +149 -0
  65. package/docs/classes/SegmentTree.html +344 -0
  66. package/docs/classes/SegmentTreeNode.html +417 -0
  67. package/docs/classes/SinglyLinkedList.html +718 -0
  68. package/docs/classes/SinglyLinkedListNode.html +247 -0
  69. package/docs/classes/SplayTree.html +149 -0
  70. package/docs/classes/Stack.html +345 -0
  71. package/docs/classes/TreeMultiSet.html +2035 -0
  72. package/docs/classes/TreeNode.html +235 -0
  73. package/docs/classes/Trie.html +349 -0
  74. package/docs/classes/TrieNode.html +257 -0
  75. package/docs/classes/TwoThreeTree.html +149 -0
  76. package/docs/classes/UndirectedEdge.html +312 -0
  77. package/docs/classes/UndirectedGraph.html +1079 -0
  78. package/docs/classes/UndirectedVertex.html +225 -0
  79. package/docs/classes/Vector2D.html +782 -0
  80. package/docs/enums/CP.html +158 -0
  81. package/docs/enums/FamilyPosition.html +158 -0
  82. package/docs/enums/LoopType.html +159 -0
  83. package/docs/index.html +493 -0
  84. package/docs/interfaces/AVLTreeDeleted.html +160 -0
  85. package/docs/interfaces/HeapOptions.html +166 -0
  86. package/docs/interfaces/IDirectedGraph.html +242 -0
  87. package/docs/interfaces/IGraph.html +426 -0
  88. package/docs/interfaces/NavigatorParams.html +196 -0
  89. package/docs/interfaces/PriorityQueueOptions.html +167 -0
  90. package/docs/modules.html +216 -0
  91. package/docs/types/BSTComparator.html +139 -0
  92. package/docs/types/BSTDeletedResult.html +136 -0
  93. package/docs/types/BinaryTreeDeleted.html +136 -0
  94. package/docs/types/BinaryTreeNodeId.html +124 -0
  95. package/docs/types/BinaryTreeNodePropertyName.html +124 -0
  96. package/docs/types/DFSOrderPattern.html +124 -0
  97. package/docs/types/DijkstraResult.html +144 -0
  98. package/docs/types/Direction.html +124 -0
  99. package/docs/types/DoublyLinkedListGetBy.html +124 -0
  100. package/docs/types/NodeOrPropertyName.html +124 -0
  101. package/docs/types/PriorityQueueComparator.html +144 -0
  102. package/docs/types/PriorityQueueDFSOrderPattern.html +124 -0
  103. package/docs/types/ResultByProperty.html +129 -0
  104. package/docs/types/ResultsByProperty.html +129 -0
  105. package/docs/types/SegmentTreeNodeVal.html +124 -0
  106. package/docs/types/SpecifyOptional.html +131 -0
  107. package/docs/types/Thunk.html +132 -0
  108. package/docs/types/ToThunkFn.html +132 -0
  109. package/docs/types/TopologicalStatus.html +124 -0
  110. package/docs/types/TreeMultiSetDeletedResult.html +136 -0
  111. package/docs/types/TrlAsyncFn.html +137 -0
  112. package/docs/types/TrlFn.html +137 -0
  113. package/docs/types/Turning.html +124 -0
  114. package/docs/types/VertexId.html +124 -0
  115. package/{tests/unit/data-structures/binary-tree → notes}/bst.test.ts +37 -50
  116. package/notes/note.md +23 -0
  117. package/package.json +1 -3
  118. package/.idea/data-structure-typed.iml +0 -14
  119. package/.idea/modules.xml +0 -8
  120. package/.idea/vcs.xml +0 -6
  121. package/src/assets/complexities-diff.jpg +0 -0
  122. package/src/assets/data-structure-complexities.jpg +0 -0
  123. package/src/assets/logo.png +0 -0
  124. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  125. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  126. package/src/data-structures/binary-tree/avl-tree.ts +0 -293
  127. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  128. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -69
  129. package/src/data-structures/binary-tree/binary-tree.ts +0 -1492
  130. package/src/data-structures/binary-tree/bst.ts +0 -497
  131. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  132. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  133. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  134. package/src/data-structures/binary-tree/index.ts +0 -11
  135. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  136. package/src/data-structures/binary-tree/segment-tree.ts +0 -267
  137. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  138. package/src/data-structures/binary-tree/tree-multiset.ts +0 -53
  139. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  140. package/src/data-structures/diagrams/README.md +0 -5
  141. package/src/data-structures/graph/abstract-graph.ts +0 -958
  142. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  143. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  148. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  149. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  150. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  151. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  152. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  153. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  154. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  155. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  156. package/src/data-structures/graph/directed-graph.ts +0 -429
  157. package/src/data-structures/graph/index.ts +0 -3
  158. package/src/data-structures/graph/undirected-graph.ts +0 -259
  159. package/src/data-structures/hash/coordinate-map.ts +0 -74
  160. package/src/data-structures/hash/coordinate-set.ts +0 -63
  161. package/src/data-structures/hash/hash-table.ts +0 -1
  162. package/src/data-structures/hash/index.ts +0 -6
  163. package/src/data-structures/hash/pair.ts +0 -1
  164. package/src/data-structures/hash/tree-map.ts +0 -1
  165. package/src/data-structures/hash/tree-set.ts +0 -1
  166. package/src/data-structures/heap/heap.ts +0 -162
  167. package/src/data-structures/heap/index.ts +0 -3
  168. package/src/data-structures/heap/max-heap.ts +0 -31
  169. package/src/data-structures/heap/min-heap.ts +0 -34
  170. package/src/data-structures/index.ts +0 -13
  171. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -365
  172. package/src/data-structures/linked-list/index.ts +0 -2
  173. package/src/data-structures/linked-list/singly-linked-list.ts +0 -757
  174. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  175. package/src/data-structures/matrix/index.ts +0 -4
  176. package/src/data-structures/matrix/matrix.ts +0 -27
  177. package/src/data-structures/matrix/matrix2d.ts +0 -208
  178. package/src/data-structures/matrix/navigator.ts +0 -122
  179. package/src/data-structures/matrix/vector2d.ts +0 -316
  180. package/src/data-structures/priority-queue/index.ts +0 -3
  181. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -24
  182. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -24
  183. package/src/data-structures/priority-queue/priority-queue.ts +0 -349
  184. package/src/data-structures/queue/deque.ts +0 -251
  185. package/src/data-structures/queue/index.ts +0 -2
  186. package/src/data-structures/queue/queue.ts +0 -120
  187. package/src/data-structures/stack/index.ts +0 -1
  188. package/src/data-structures/stack/stack.ts +0 -98
  189. package/src/data-structures/trie/index.ts +0 -1
  190. package/src/data-structures/trie/trie.ts +0 -225
  191. package/src/data-structures/types/abstract-graph.ts +0 -51
  192. package/src/data-structures/types/avl-tree.ts +0 -6
  193. package/src/data-structures/types/binary-tree.ts +0 -15
  194. package/src/data-structures/types/bst.ts +0 -5
  195. package/src/data-structures/types/directed-graph.ts +0 -18
  196. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  197. package/src/data-structures/types/heap.ts +0 -8
  198. package/src/data-structures/types/index.ts +0 -13
  199. package/src/data-structures/types/navigator.ts +0 -13
  200. package/src/data-structures/types/priority-queue.ts +0 -9
  201. package/src/data-structures/types/segment-tree.ts +0 -1
  202. package/src/data-structures/types/singly-linked-list.ts +0 -1
  203. package/src/data-structures/types/tree-multiset.ts +0 -3
  204. package/src/index.ts +0 -1
  205. package/src/utils/index.ts +0 -2
  206. package/src/utils/types/index.ts +0 -1
  207. package/src/utils/types/utils.ts +0 -4
  208. package/src/utils/utils.ts +0 -78
  209. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  210. package/tests/unit/data-structures/graph/directed-graph.test.ts +0 -492
  211. package/tests/unit/data-structures/graph/index.ts +0 -3
  212. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
@@ -1,429 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import {arrayRemove} from '../../utils';
9
- import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
10
- import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types';
11
-
12
- export class DirectedVertex extends AbstractVertex {
13
- /**
14
- * The constructor function initializes an object with a given id.
15
- * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is used to uniquely identify the
16
- * vertex within a graph or network.
17
- */
18
- constructor(id: VertexId) {
19
- super(id);
20
- }
21
- }
22
-
23
- export class DirectedEdge extends AbstractEdge {
24
- /**
25
- * The constructor function initializes the source and destination vertices of an edge, with an optional weight.
26
- * @param {VertexId} src - The `src` parameter is the source vertex ID. It represents the starting point of an edge in
27
- * a graph.
28
- * @param {VertexId} dest - The `dest` parameter is the identifier of the destination vertex. It represents the vertex
29
- * to which an edge is directed.
30
- * @param {number} [weight] - The `weight` parameter is an optional number that represents the weight of the edge
31
- * between two vertices.
32
- */
33
- constructor(src: VertexId, dest: VertexId, weight?: number) {
34
- super(weight);
35
- this._src = src;
36
- this._dest = dest;
37
- }
38
-
39
- private _src: VertexId;
40
- get src(): VertexId {
41
- return this._src;
42
- }
43
-
44
- set src(v: VertexId) {
45
- this._src = v;
46
- }
47
-
48
- private _dest: VertexId;
49
-
50
- get dest(): VertexId {
51
- return this._dest;
52
- }
53
-
54
- set dest(v: VertexId) {
55
- this._dest = v;
56
- }
57
-
58
- /**
59
- * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
60
- */
61
- getSrc(): VertexId {
62
- return this._src;
63
- }
64
-
65
- /**
66
- * Starting from TypeScript version 5.0 and onwards, the use of distinct access modifiers for Getters and Setters is not permitted. As an alternative, to ensure compatibility, it is necessary to adopt a Java-style approach for Setters (using the same name as the property) while utilizing separate method names for Getters.
67
- */
68
- getDest(): VertexId {
69
- return this._dest;
70
- }
71
- }
72
-
73
- // Strongly connected, One direction connected, Weakly connected
74
- export class DirectedGraph<V extends DirectedVertex, E extends DirectedEdge> extends AbstractGraph<V, E> implements IDirectedGraph<V, E> {
75
-
76
- protected _outEdgeMap: Map<V, E[]> = new Map<V, E[]>();
77
-
78
- protected _inEdgeMap: Map<V, E[]> = new Map<V, E[]>();
79
-
80
- constructor() {
81
- super();
82
- }
83
-
84
- /**
85
- * The function `getEdge` returns the first edge between two vertices, given their source and destination.
86
- * @param {V | null | VertexId} srcOrId - The `srcOrId` parameter can be either a vertex object (`V`), a vertex ID
87
- * (`VertexId`), or `null`. It represents the source vertex of the edge.
88
- * @param {V | null | VertexId} destOrId - The `destOrId` parameter is either a vertex object (`V`), a vertex ID
89
- * (`VertexId`), or `null`. It represents the destination vertex of the edge.
90
- * @returns an edge (E) or null.
91
- */
92
- getEdge(srcOrId: V | null | VertexId, destOrId: V | null | VertexId): E | null {
93
- let edges: E[] = [];
94
-
95
- if (srcOrId !== null && destOrId !== null) {
96
- const src: V | null = this.getVertex(srcOrId);
97
- const dest: V | null = this.getVertex(destOrId);
98
-
99
- if (src && dest) {
100
- const srcOutEdges = this._outEdgeMap.get(src);
101
- if (srcOutEdges) {
102
- edges = srcOutEdges.filter(edge => edge.dest === dest.id);
103
- }
104
- }
105
- }
106
-
107
- return edges[0] || null;
108
- }
109
-
110
- /**
111
- * The `addEdge` function adds an edge to a graph if the source and destination vertices exist.
112
- * @param {E} edge - The parameter `edge` is of type `E`, which represents an edge in the graph. It contains
113
- * information about the source vertex (`src`) and the destination vertex (`dest`) of the edge.
114
- * @returns The `addEdge` function returns a boolean value. It returns `true` if the edge was successfully added to the
115
- * graph, and `false` if either the source or destination vertices of the edge are not present in the graph.
116
- */
117
- addEdge(edge: E): boolean {
118
- if (!(this.hasVertex(edge.src) && this.hasVertex(edge.dest))) {
119
- return false;
120
- }
121
-
122
- const srcVertex = this.getVertex(edge.src);
123
- const destVertex = this.getVertex(edge.dest);
124
-
125
- // TODO after no-non-null-assertion not ensure the logic
126
- if (srcVertex && destVertex) {
127
- const srcOutEdges = this._outEdgeMap.get(srcVertex);
128
- if (srcOutEdges) {
129
- srcOutEdges.push(edge);
130
- } else {
131
- this._outEdgeMap.set(srcVertex, [edge]);
132
- }
133
-
134
- const destInEdges = this._inEdgeMap.get(destVertex);
135
- if (destInEdges) {
136
- destInEdges.push(edge);
137
- } else {
138
- this._inEdgeMap.set(destVertex, [edge]);
139
- }
140
- return true;
141
- } else {
142
- return false;
143
- }
144
- }
145
-
146
- /**
147
- * The function removes an edge between two vertices in a directed graph and returns the removed edge.
148
- * @param {V | VertexId} srcOrId - The source vertex or its ID.
149
- * @param {V | VertexId} destOrId - The `destOrId` parameter in the `removeEdgeBetween` function represents the
150
- * destination vertex of the edge that needs to be removed. It can be either a vertex object (`V`) or a vertex ID
151
- * (`VertexId`).
152
- * @returns The function `removeEdgeBetween` returns the removed edge (`E`) if the edge between the source and
153
- * destination vertices is successfully removed. If either the source or destination vertex is not found, or if the
154
- * edge does not exist, it returns `null`.
155
- */
156
- removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null {
157
-
158
- const src: V | null = this.getVertex(srcOrId);
159
- const dest: V | null = this.getVertex(destOrId);
160
- let removed: E | null = null;
161
- if (!src || !dest) {
162
- return null;
163
- }
164
-
165
- const srcOutEdges = this._outEdgeMap.get(src);
166
- if (srcOutEdges) {
167
- /**
168
- * The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
169
- * found.
170
- * @param {E} edge - The `edge` parameter represents the edge that you want to remove from the graph. It should be an
171
- * object that has `src` and `dest` properties, which represent the source and destination vertices of the edge,
172
- * respectively.
173
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
174
- */
175
- arrayRemove<E>(srcOutEdges, (edge: DirectedEdge) => edge.dest === dest.id);
176
- }
177
-
178
- const destInEdges = this._inEdgeMap.get(dest);
179
- if (destInEdges) {
180
- removed = arrayRemove<E>(destInEdges, (edge: DirectedEdge) => edge.src === src.id)[0] || null;
181
- }
182
- return removed;
183
- }
184
-
185
- /**
186
- * The removeEdge function removes an edge from a graph and returns the removed edge, or null if the edge was not
187
- * found.
188
- * @param {E} edge - The `edge` parameter is an object that represents an edge in a graph. It has two properties: `src`
189
- * and `dest`, which represent the source and destination vertices of the edge, respectively.
190
- * @returns The method `removeEdge` returns the removed edge (`E`) if it exists, or `null` if the edge does not exist.
191
- */
192
- removeEdge(edge: E): E | null {
193
- let removed: E | null = null;
194
- const src = this.getVertex(edge.src);
195
- const dest = this.getVertex(edge.dest);
196
- if (src && dest) {
197
- const srcOutEdges = this._outEdgeMap.get(src);
198
- if (srcOutEdges && srcOutEdges.length > 0) {
199
- arrayRemove(srcOutEdges, (edge: DirectedEdge) => edge.src === src.id);
200
- }
201
-
202
- const destInEdges = this._inEdgeMap.get(dest);
203
- if (destInEdges && destInEdges.length > 0) {
204
- removed = arrayRemove(destInEdges, (edge: E) => edge.dest === dest.id)[0];
205
- }
206
-
207
- }
208
-
209
- return removed;
210
- }
211
-
212
- /**
213
- * The function removeAllEdges removes all edges between two vertices.
214
- * @param {VertexId | V} src - The `src` parameter represents the source vertex from which the edges will be removed.
215
- * It can be either a `VertexId` or a `V` type, which represents the identifier or object of the vertex.
216
- * @param {VertexId | V} dest - The `dest` parameter represents the destination vertex of an edge. It can be either a
217
- * `VertexId` or a vertex object `V`.
218
- * @returns An empty array is being returned.
219
- */
220
- removeAllEdges(src: VertexId | V, dest: VertexId | V): E[] {
221
- return [];
222
- }
223
-
224
- /**
225
- * The function `incomingEdgesOf` returns an array of incoming edges for a given vertex or vertex ID.
226
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
227
- * (`VertexId`).
228
- * @returns The method `incomingEdgesOf` returns an array of edges (`E[]`).
229
- */
230
- incomingEdgesOf(vertexOrId: V | VertexId): E[] {
231
- const target = this.getVertex(vertexOrId);
232
- if (target) {
233
- return this._inEdgeMap.get(target) || [];
234
- }
235
- return [];
236
- }
237
-
238
- /**
239
- * The function `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
240
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can accept either a vertex object (`V`) or a vertex ID
241
- * (`VertexId`).
242
- * @returns The method `outgoingEdgesOf` returns an array of outgoing edges from a given vertex or vertex ID.
243
- */
244
- outgoingEdgesOf(vertexOrId: V | VertexId): E[] {
245
- const target = this.getVertex(vertexOrId);
246
- if (target) {
247
- return this._outEdgeMap.get(target) || [];
248
- }
249
- return [];
250
- }
251
-
252
- /**
253
- * The function "degreeOf" returns the total degree of a vertex, which is the sum of its out-degree and in-degree.
254
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
255
- * @returns the sum of the out-degree and in-degree of the specified vertex or vertex ID.
256
- */
257
- degreeOf(vertexOrId: VertexId | V): number {
258
- return this.outDegreeOf(vertexOrId) + this.inDegreeOf(vertexOrId);
259
- }
260
-
261
- /**
262
- * The function "inDegreeOf" returns the number of incoming edges for a given vertex.
263
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
264
- * @returns The number of incoming edges of the specified vertex or vertex ID.
265
- */
266
- inDegreeOf(vertexOrId: VertexId | V): number {
267
- return this.incomingEdgesOf(vertexOrId).length;
268
- }
269
-
270
- /**
271
- * The function `outDegreeOf` returns the number of outgoing edges from a given vertex.
272
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
273
- * @returns The number of outgoing edges from the specified vertex or vertex ID.
274
- */
275
- outDegreeOf(vertexOrId: VertexId | V): number {
276
- return this.outgoingEdgesOf(vertexOrId).length;
277
- }
278
-
279
- /**
280
- * The function "edgesOf" returns an array of both outgoing and incoming edges of a given vertex or vertex ID.
281
- * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or a `V`.
282
- * @returns The function `edgesOf` returns an array of edges.
283
- */
284
- edgesOf(vertexOrId: VertexId | V): E[] {
285
- return [...this.outgoingEdgesOf(vertexOrId), ...this.incomingEdgesOf(vertexOrId)];
286
- }
287
-
288
- /**
289
- * The function "getEdgeSrc" returns the source vertex of an edge, or null if the edge does not exist.
290
- * @param {E} e - E - an edge object
291
- * @returns the source vertex of the given edge, or null if the edge does not exist.
292
- */
293
- getEdgeSrc(e: E): V | null {
294
- return this.getVertex(e.src);
295
- }
296
-
297
- /**
298
- * The function "getEdgeDest" returns the vertex associated with the destination of an edge.
299
- * @param {E} e - The parameter `e` is of type `E`, which represents an edge in a graph.
300
- * @returns either a vertex object (of type V) or null.
301
- */
302
- getEdgeDest(e: E): V | null {
303
- return this.getVertex(e.dest);
304
- }
305
-
306
- /**
307
- * The function `getDestinations` returns an array of destination vertices connected to a given vertex.
308
- * @param {V | VertexId | null} vertex - The `vertex` parameter represents the starting vertex from which we want to
309
- * find the destinations. It can be either a `V` object, a `VertexId` (which is a unique identifier for a vertex), or
310
- * `null` if we want to find destinations from all vertices.
311
- * @returns an array of vertices (V[]).
312
- */
313
- getDestinations(vertex: V | VertexId | null): V[] {
314
- if (vertex === null) {
315
- return [];
316
- }
317
- const destinations: V[] = [];
318
- const outgoingEdges = this.outgoingEdgesOf(vertex);
319
- for (const outEdge of outgoingEdges) {
320
- const child = this.getEdgeDest(outEdge);
321
- if (child) {
322
- destinations.push(child);
323
- }
324
- }
325
- return destinations;
326
- }
327
-
328
- /**--- start find cycles --- */
329
-
330
- /**
331
- * when stored with adjacency list time: O(V+E)
332
- * when stored with adjacency matrix time: O(V^2)
333
- * The `topologicalSort` function performs a topological sort on a graph and returns the sorted vertices in reverse
334
- * order, or null if the graph contains a cycle.
335
- * @returns The `topologicalSort()` function returns an array of vertices (`V[]`) in topological order if there is no
336
- * cycle in the graph. If there is a cycle, it returns `null`.
337
- */
338
- topologicalSort(): V[] | null {
339
- // When judging whether there is a cycle in the undirected graph, all nodes with degree of **<= 1** are enqueued
340
- // When judging whether there is a cycle in the directed graph, all nodes with **in degree = 0** are enqueued
341
- const statusMap: Map<V, TopologicalStatus> = new Map<V, TopologicalStatus>();
342
- for (const entry of this._vertices) {
343
- statusMap.set(entry[1], 0);
344
- }
345
-
346
- const sorted: (V)[] = [];
347
- let hasCycle = false;
348
- const dfs = (cur: V) => {
349
- statusMap.set(cur, 1);
350
- const children = this.getDestinations(cur);
351
- for (const child of children) {
352
- const childStatus = statusMap.get(child);
353
- if (childStatus === 0) {
354
- dfs(child);
355
- } else if (childStatus === 1) {
356
- hasCycle = true;
357
- }
358
- }
359
- statusMap.set(cur, 2);
360
- sorted.push(cur);
361
- };
362
-
363
- for (const entry of this._vertices) {
364
- if (statusMap.get(entry[1]) === 0) {
365
- dfs(entry[1]);
366
- }
367
- }
368
-
369
- if (hasCycle) return null;
370
-
371
- return sorted.reverse();
372
- }
373
-
374
- /**--- end find cycles --- */
375
-
376
- /**
377
- * The `edgeSet` function returns an array of all the edges in the graph.
378
- * @returns The `edgeSet()` method returns an array of edges (`E[]`).
379
- */
380
- edgeSet(): E[] {
381
- let edges: E[] = [];
382
- this._outEdgeMap.forEach(outEdges => {
383
- edges = [...edges, ...outEdges];
384
- });
385
- return edges;
386
- }
387
-
388
- /**
389
- * The function `getNeighbors` returns an array of neighboring vertices for a given vertex or vertex ID.
390
- * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either a vertex object (`V`) or a vertex ID
391
- * (`VertexId`).
392
- * @returns an array of vertices (V[]).
393
- */
394
- getNeighbors(vertexOrId: V | VertexId): V[] {
395
- const neighbors: V[] = [];
396
- const vertex = this.getVertex(vertexOrId);
397
- if (vertex) {
398
- const outEdges = this.outgoingEdgesOf(vertex);
399
- for (const outEdge of outEdges) {
400
- const neighbor = this.getVertex(outEdge.dest);
401
- // TODO after no-non-null-assertion not ensure the logic
402
- if (neighbor) {
403
- neighbors.push(neighbor);
404
- }
405
- }
406
- }
407
- return neighbors;
408
- }
409
-
410
- /**
411
- * The function "getEndsOfEdge" returns the source and destination vertices of an edge if it exists in the graph,
412
- * otherwise it returns null.
413
- * @param {E} edge - The parameter "edge" is of type E, which represents an edge in a graph.
414
- * @returns an array containing two vertices [V, V] if the edge is found in the graph. If the edge is not found, it
415
- * returns null.
416
- */
417
- getEndsOfEdge(edge: E): [V, V] | null {
418
- if (!this.hasEdge(edge.src, edge.dest)) {
419
- return null;
420
- }
421
- const v1 = this.getVertex(edge.src);
422
- const v2 = this.getVertex(edge.dest);
423
- if (v1 && v2) {
424
- return [v1, v2];
425
- } else {
426
- return null;
427
- }
428
- }
429
- }
@@ -1,3 +0,0 @@
1
- export * from './abstract-graph';
2
- export * from './directed-graph';
3
- export * from './undirected-graph';