data-structure-typed 1.15.1 → 1.16.1

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