@raikuxq/alg-ds 1.1.2 → 1.1.5

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 (257) hide show
  1. package/README.md +26 -2
  2. package/lib/algorithms/binary-search.d.ts +5 -0
  3. package/lib/algorithms/binary-search.js +27 -0
  4. package/lib/algorithms/factorial.d.ts +9 -0
  5. package/lib/algorithms/factorial.js +17 -0
  6. package/lib/algorithms/fibonacci.d.ts +9 -0
  7. package/lib/algorithms/fibonacci.js +17 -0
  8. package/lib/algorithms/memoize.d.ts +5 -0
  9. package/lib/algorithms/memoize.js +22 -0
  10. package/lib/algorithms/sorts/bubble-sort.d.ts +9 -0
  11. package/lib/algorithms/sorts/bubble-sort.js +23 -0
  12. package/lib/algorithms/sorts/insertion-sort.d.ts +9 -0
  13. package/lib/algorithms/sorts/insertion-sort.js +25 -0
  14. package/lib/algorithms/sorts/merge-sort.d.ts +9 -0
  15. package/lib/algorithms/sorts/merge-sort.js +61 -0
  16. package/lib/algorithms/sorts/quick-sort.d.ts +9 -0
  17. package/lib/algorithms/sorts/quick-sort.js +45 -0
  18. package/lib/algorithms/sorts/select-sort.d.ts +9 -0
  19. package/lib/algorithms/sorts/select-sort.js +20 -0
  20. package/lib/algorithms/transpose-matrix.d.ts +5 -0
  21. package/lib/algorithms/transpose-matrix.js +20 -0
  22. package/lib/constants.d.ts +2 -0
  23. package/lib/constants.js +6 -0
  24. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.d.ts +15 -0
  25. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.js +53 -0
  26. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.d.ts +60 -0
  27. package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.js +36 -0
  28. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.d.ts +13 -0
  29. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.js +59 -0
  30. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.d.ts +70 -0
  31. package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.js +268 -0
  32. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.d.ts +16 -0
  33. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.js +70 -0
  34. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.d.ts +57 -0
  35. package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.js +234 -0
  36. package/lib/data-structures/Graph/AbstractGraph.d.ts +84 -0
  37. package/lib/data-structures/Graph/AbstractGraph.js +141 -0
  38. package/lib/data-structures/Graph/DirectedGraph.d.ts +24 -0
  39. package/lib/data-structures/Graph/DirectedGraph.js +85 -0
  40. package/lib/data-structures/Graph/GraphEdge.d.ts +16 -0
  41. package/lib/data-structures/Graph/GraphEdge.js +43 -0
  42. package/lib/data-structures/Graph/UndirectedGraph.d.ts +28 -0
  43. package/lib/data-structures/Graph/UndirectedGraph.js +102 -0
  44. package/lib/data-structures/Graph/demo/generateRandomGraph.d.ts +4 -0
  45. package/lib/data-structures/Graph/demo/generateRandomGraph.js +72 -0
  46. package/lib/data-structures/Graph/iterator/AbstractGraphIterator.d.ts +35 -0
  47. package/lib/data-structures/Graph/iterator/AbstractGraphIterator.js +90 -0
  48. package/lib/data-structures/Graph/iterator/GraphIteratorBFS.d.ts +28 -0
  49. package/lib/data-structures/Graph/iterator/GraphIteratorBFS.js +70 -0
  50. package/lib/data-structures/Graph/iterator/GraphIteratorDFS.d.ts +28 -0
  51. package/lib/data-structures/Graph/iterator/GraphIteratorDFS.js +70 -0
  52. package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.d.ts +32 -0
  53. package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.js +99 -0
  54. package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.d.ts +19 -0
  55. package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.js +28 -0
  56. package/{src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts → lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.d.ts} +32 -51
  57. package/lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.js +48 -0
  58. package/lib/data-structures/Graph/searching/hasPath.d.ts +9 -0
  59. package/lib/data-structures/Graph/searching/hasPath.js +29 -0
  60. package/lib/data-structures/Graph/searching/shortestPath.d.ts +9 -0
  61. package/lib/data-structures/Graph/searching/shortestPath.js +29 -0
  62. package/lib/data-structures/Graph/strategy/BFSIterationStrategy.d.ts +6 -0
  63. package/lib/data-structures/Graph/strategy/BFSIterationStrategy.js +13 -0
  64. package/lib/data-structures/Graph/strategy/DFSIterationStrategy.d.ts +6 -0
  65. package/lib/data-structures/Graph/strategy/DFSIterationStrategy.js +13 -0
  66. package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.d.ts +6 -0
  67. package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.js +13 -0
  68. package/lib/data-structures/Graph/transposing/transposeDirectedGraph.d.ts +2 -0
  69. package/lib/data-structures/Graph/transposing/transposeDirectedGraph.js +14 -0
  70. package/lib/data-structures/HashTable/HashTable.d.ts +73 -0
  71. package/lib/data-structures/HashTable/HashTable.js +169 -0
  72. package/lib/data-structures/HashTable/HashTableNode.d.ts +11 -0
  73. package/lib/data-structures/HashTable/HashTableNode.js +39 -0
  74. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.d.ts +125 -0
  75. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.js +236 -0
  76. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.d.ts +20 -0
  77. package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.js +41 -0
  78. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.d.ts +48 -0
  79. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.js +150 -0
  80. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.d.ts +25 -0
  81. package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.js +65 -0
  82. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.d.ts +52 -0
  83. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.js +137 -0
  84. package/{src/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.ts → lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.d.ts} +7 -10
  85. package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.js +29 -0
  86. package/lib/data-structures/LoopedArray/LoopedArray.d.ts +86 -0
  87. package/lib/data-structures/LoopedArray/LoopedArray.js +161 -0
  88. package/lib/data-structures/Queue/Queue.d.ts +50 -0
  89. package/lib/data-structures/Queue/Queue.js +83 -0
  90. package/lib/data-structures/Stack/Stack.d.ts +50 -0
  91. package/lib/data-structures/Stack/Stack.js +83 -0
  92. package/lib/exports/algorithms.d.ts +16 -0
  93. package/lib/exports/algorithms.js +36 -0
  94. package/lib/exports/constants.d.ts +2 -0
  95. package/lib/exports/constants.js +7 -0
  96. package/lib/exports/data-structures.d.ts +11 -0
  97. package/lib/exports/data-structures.js +24 -0
  98. package/lib/exports/helpers.d.ts +6 -0
  99. package/lib/exports/helpers.js +14 -0
  100. package/lib/exports/sorts.d.ts +6 -0
  101. package/lib/exports/sorts.js +14 -0
  102. package/lib/exports/utils.d.ts +3 -0
  103. package/lib/exports/utils.js +14 -0
  104. package/lib/exports.d.ts +44 -0
  105. package/lib/exports.js +89 -0
  106. package/lib/helpers/createBinaryTree.d.ts +6 -0
  107. package/lib/helpers/createBinaryTree.js +22 -0
  108. package/lib/helpers/createGraph.d.ts +6 -0
  109. package/lib/helpers/createGraph.js +24 -0
  110. package/lib/helpers/createGraphFromMatrix.d.ts +7 -0
  111. package/lib/helpers/createGraphFromMatrix.js +37 -0
  112. package/lib/helpers/createLinkedList.d.ts +3 -0
  113. package/lib/helpers/createLinkedList.js +21 -0
  114. package/lib/index.d.ts +3 -0
  115. package/lib/index.js +6 -0
  116. package/lib/types/ArrayMatrix.d.ts +1 -0
  117. package/lib/types/ArrayMatrix.js +3 -0
  118. package/lib/types/EnumBinarySearchTreeType.d.ts +4 -0
  119. package/lib/types/EnumBinarySearchTreeType.js +9 -0
  120. package/lib/types/EnumGraphType.d.ts +4 -0
  121. package/lib/types/EnumGraphType.js +9 -0
  122. package/lib/types/EnumLinkedListType.d.ts +4 -0
  123. package/lib/types/EnumLinkedListType.js +9 -0
  124. package/lib/types/EnumRandomGenerationFormat.d.ts +4 -0
  125. package/lib/types/EnumRandomGenerationFormat.js +9 -0
  126. package/lib/types/EnumTreeTraversalType.d.ts +5 -0
  127. package/lib/types/EnumTreeTraversalType.js +10 -0
  128. package/lib/types/FnCompareTwo.d.ts +1 -0
  129. package/lib/types/FnCompareTwo.js +3 -0
  130. package/lib/types/FnToMemoize.d.ts +1 -0
  131. package/lib/types/FnToMemoize.js +3 -0
  132. package/{src/types/ILinkedList.ts → lib/types/IArrayFacade.d.ts} +4 -6
  133. package/lib/types/IArrayFacade.js +3 -0
  134. package/{src/types/IBiDirectIterable.ts → lib/types/IBiDirectIterable.d.ts} +5 -6
  135. package/lib/types/IBiDirectIterable.js +3 -0
  136. package/lib/types/IBiDirectIterator.d.ts +11 -0
  137. package/lib/types/IBiDirectIterator.js +3 -0
  138. package/lib/types/IBinaryTree.d.ts +12 -0
  139. package/lib/types/IBinaryTree.js +3 -0
  140. package/lib/types/IConvertableToArray.d.ts +4 -0
  141. package/lib/types/IConvertableToArray.js +3 -0
  142. package/lib/types/IGraph.d.ts +14 -0
  143. package/lib/types/IGraph.js +3 -0
  144. package/{src/types/IGraphIterationStrategy.ts → lib/types/IGraphIterationStrategy.d.ts} +5 -6
  145. package/lib/types/IGraphIterationStrategy.js +3 -0
  146. package/lib/types/IGraphIterator.d.ts +11 -0
  147. package/lib/types/IGraphIterator.js +3 -0
  148. package/{src/types/IIterable.ts → lib/types/IIterable.d.ts} +4 -5
  149. package/lib/types/IIterable.js +3 -0
  150. package/lib/types/IIterator.d.ts +14 -0
  151. package/lib/types/IIterator.js +3 -0
  152. package/lib/types/IKeyValueStorage.d.ts +8 -0
  153. package/lib/types/IKeyValueStorage.js +3 -0
  154. package/lib/types/ILinearStorage.d.ts +11 -0
  155. package/lib/types/ILinearStorage.js +3 -0
  156. package/{src/types/ILinearStorageRA.ts → lib/types/ILinearStorageRA.d.ts} +13 -14
  157. package/lib/types/ILinearStorageRA.js +3 -0
  158. package/{src/types/IArrayFacade.ts → lib/types/ILinkedList.d.ts} +4 -6
  159. package/lib/types/ILinkedList.js +3 -0
  160. package/lib/utils.d.ts +29 -0
  161. package/lib/utils.js +95 -0
  162. package/package.json +9 -3
  163. package/.idea/algorythmes.iml +0 -15
  164. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  165. package/.idea/deployment.xml +0 -14
  166. package/.idea/inspectionProfiles/Project_Default.xml +0 -7
  167. package/.idea/jsLinters/eslint.xml +0 -6
  168. package/.idea/misc.xml +0 -6
  169. package/.idea/modules.xml +0 -8
  170. package/.idea/vcs.xml +0 -6
  171. package/lib/algotirhms.ts +0 -35
  172. package/lib/constants.ts +0 -3
  173. package/lib/data-structures.ts +0 -23
  174. package/lib/helpers.ts +0 -13
  175. package/lib/sorts.ts +0 -7
  176. package/lib/types.ts +0 -53
  177. package/lib/utils.ts +0 -21
  178. package/src/algorithms/binary-search.ts +0 -28
  179. package/src/algorithms/factorial.ts +0 -18
  180. package/src/algorithms/fibonacci.ts +0 -18
  181. package/src/algorithms/memoize.ts +0 -21
  182. package/src/algorithms/sorts/bubble-sort.ts +0 -21
  183. package/src/algorithms/sorts/insertion-sort.ts +0 -25
  184. package/src/algorithms/sorts/merge-sort.ts +0 -74
  185. package/src/algorithms/sorts/quick-sort.ts +0 -54
  186. package/src/algorithms/sorts/select-sort.ts +0 -19
  187. package/src/algorithms/transpose-matrix.ts +0 -19
  188. package/src/constants.ts +0 -2
  189. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts +0 -45
  190. package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts +0 -80
  191. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts +0 -38
  192. package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts +0 -286
  193. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts +0 -48
  194. package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts +0 -228
  195. package/src/data-structures/Graph/AbstractGraph.ts +0 -189
  196. package/src/data-structures/Graph/DirectedGraph.ts +0 -84
  197. package/src/data-structures/Graph/GraphEdge.ts +0 -33
  198. package/src/data-structures/Graph/UndirectedGraph.ts +0 -108
  199. package/src/data-structures/Graph/demo/generateRandomGraph.ts +0 -93
  200. package/src/data-structures/Graph/iterator/AbstractGraphIterator.ts +0 -99
  201. package/src/data-structures/Graph/iterator/GraphIteratorBFS.ts +0 -60
  202. package/src/data-structures/Graph/iterator/GraphIteratorDFS.ts +0 -60
  203. package/src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts +0 -94
  204. package/src/data-structures/Graph/presenter/presenterAdjacencyLists.ts +0 -29
  205. package/src/data-structures/Graph/searching/hasPath.ts +0 -38
  206. package/src/data-structures/Graph/searching/shortestPath.ts +0 -38
  207. package/src/data-structures/Graph/strategy/BFSIterationStrategy.ts +0 -11
  208. package/src/data-structures/Graph/strategy/DFSIterationStrategy.ts +0 -11
  209. package/src/data-structures/Graph/strategy/DijkstraIterationStrategy.ts +0 -11
  210. package/src/data-structures/Graph/transposing/transposeDirectedGraph.ts +0 -19
  211. package/src/data-structures/HashTable/HashTable.ts +0 -202
  212. package/src/data-structures/HashTable/HashTableNode.ts +0 -31
  213. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts +0 -310
  214. package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.ts +0 -33
  215. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts +0 -156
  216. package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.ts +0 -47
  217. package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts +0 -147
  218. package/src/data-structures/LoopedArray/LoopedArray.ts +0 -182
  219. package/src/data-structures/Queue/Queue.ts +0 -92
  220. package/src/data-structures/Stack/Stack.ts +0 -92
  221. package/src/demo/demo.bst.ts +0 -67
  222. package/src/demo/demo.graph.ts +0 -246
  223. package/src/demo/demo.hashtable.ts +0 -28
  224. package/src/demo/demo.linked-list.ts +0 -78
  225. package/src/demo/demo.looped-array.ts +0 -104
  226. package/src/demo/demo.queue.ts +0 -40
  227. package/src/demo/demo.stack.ts +0 -40
  228. package/src/demo/performance/bst-compare.ts +0 -35
  229. package/src/demo/performance/ds-compare.ts +0 -58
  230. package/src/demo/performance/hash-table.compare.ts +0 -40
  231. package/src/demo/performance/sort-compare.ts +0 -60
  232. package/src/helpers/createBinaryTree.ts +0 -24
  233. package/src/helpers/createGraph.ts +0 -24
  234. package/src/helpers/createGraphFromMatrix.ts +0 -47
  235. package/src/helpers/createLinkedList.ts +0 -24
  236. package/src/index.ts +0 -44
  237. package/src/types/ArrayMatrix.ts +0 -1
  238. package/src/types/EnumBinarySearchTreeType.ts +0 -4
  239. package/src/types/EnumGraphTraversalType.ts +0 -5
  240. package/src/types/EnumGraphType.ts +0 -4
  241. package/src/types/EnumLinkedListType.ts +0 -4
  242. package/src/types/EnumRandomGenerationFormat.ts +0 -4
  243. package/src/types/EnumSortType.ts +0 -7
  244. package/src/types/EnumTreeTraversalType.ts +0 -5
  245. package/src/types/FnCompareTwo.ts +0 -1
  246. package/src/types/FnSort.ts +0 -1
  247. package/src/types/FnToMemoize.ts +0 -1
  248. package/src/types/IBiDirectIterator.ts +0 -12
  249. package/src/types/IBinaryTree.ts +0 -13
  250. package/src/types/IConvertableToArray.ts +0 -4
  251. package/src/types/IGraph.ts +0 -16
  252. package/src/types/IGraphCreator.ts +0 -5
  253. package/src/types/IGraphIterator.ts +0 -13
  254. package/src/types/IIterator.ts +0 -14
  255. package/src/types/IKeyValueStorage.ts +0 -8
  256. package/src/types/ILinearStorage.ts +0 -11
  257. package/src/utils.ts +0 -65
@@ -1,189 +0,0 @@
1
- import GraphEdge from "./GraphEdge";
2
-
3
- export default abstract class AbstractGraph<T> {
4
- protected _vertices: Map<T, Array<T>>;
5
- protected _edges: Array<GraphEdge<T>>;
6
-
7
- /**
8
- * Created empty instance
9
- */
10
- protected constructor() {
11
- this._vertices = new Map<T, Array<T>>();
12
- this._edges = new Array<GraphEdge<T>>();
13
- }
14
-
15
- /**
16
- * Find edge by its from and to vertices
17
- */
18
- protected abstract getEdgeByValue(from: T, to: T): GraphEdge<T>;
19
-
20
- /**
21
- * Get vertices list in array format
22
- */
23
- protected getVerticesArrayFormat(): Array<T> {
24
- return Array.from(this._vertices.keys());
25
- }
26
-
27
- /**
28
- * Find vertex in vertices list by its data
29
- * @throws when vertex was not found
30
- */
31
- protected tryFindVertex(data: T): T {
32
- const isExists = this._vertices.has(data);
33
-
34
- if (!isExists) {
35
- throw new Error("Vertex not found");
36
- }
37
-
38
- return data;
39
- }
40
-
41
- /**
42
- * Update edge weight between from and to vertices
43
- */
44
- protected updateEdgeWeight(from: T, to: T, weight: number): void {
45
- const edge = this.getEdgeByValue(from, to);
46
-
47
- edge.weight = weight;
48
- }
49
-
50
- /**
51
- * Will remove all vertex relations with others vertices
52
- */
53
- protected cascadeRemoveVertexRelations(vertexToRemove: T): void {
54
- this.getVerticesArrayFormat().forEach((neighbor: T) => {
55
- const neighborVertexNeighbors = this._vertices.get(neighbor);
56
-
57
- if (neighborVertexNeighbors) {
58
- const neighborVertexFilteredNeighbors = neighborVertexNeighbors.filter(
59
- (newNeighbor: T) => newNeighbor !== vertexToRemove
60
- );
61
-
62
- this._vertices.set(neighbor, neighborVertexFilteredNeighbors);
63
- }
64
- });
65
- }
66
-
67
- /**
68
- * Will remove all vertices edges with vertex to remove
69
- */
70
- protected cascadeRemoveVertexEdges(vertexToRemove: T): void {
71
- this._edges = this._edges.filter((edge: GraphEdge<T>) => {
72
- const shouldBeDeleted =
73
- edge.toVertex === vertexToRemove || edge.fromVertex === vertexToRemove;
74
-
75
- return !shouldBeDeleted;
76
- });
77
- }
78
-
79
- /**
80
- * Get sum of all graph edges
81
- */
82
- public weight(): number {
83
- return this._edges.reduce(
84
- (acc: number, edge: GraphEdge<T>) => acc + edge.weight,
85
- 0
86
- );
87
- }
88
-
89
- /**
90
- * Get array of vertices
91
- */
92
- public vertices(): Array<T> {
93
- return this.getVerticesArrayFormat().map((vertex: T) => vertex);
94
- }
95
-
96
- /**
97
- * Get vertices count
98
- */
99
- public verticesCount(): number {
100
- return this.vertices().length;
101
- }
102
-
103
- /**
104
- * Get edges count
105
- */
106
- public edgesCount(): number {
107
- return this._edges.length;
108
- }
109
-
110
- /**
111
- * Add vertex
112
- * @throws when vertex is already exists
113
- */
114
- public addVertex(data: T): this {
115
- if (this.hasVertex(data)) {
116
- throw new Error("Vertex is already exist");
117
- }
118
-
119
- this._vertices.set(data, new Array<T>());
120
-
121
- return this;
122
- }
123
-
124
- /**
125
- * Remove vertex
126
- * @throws when vertex is already does not exist
127
- */
128
- public removeVertex(data: T): this {
129
- try {
130
- const vertexToRemove = this.tryFindVertex(data);
131
-
132
- this.cascadeRemoveVertexEdges(vertexToRemove);
133
- this.cascadeRemoveVertexRelations(vertexToRemove);
134
- this._vertices.delete(vertexToRemove);
135
- } catch (e) {
136
- throw new Error("Vertex does not exist already");
137
- }
138
-
139
- return this;
140
- }
141
-
142
- /**
143
- * Add edge between two vertices
144
- */
145
- public abstract addEdge(from: T, to: T, weight?: number): this;
146
-
147
- /**
148
- * Remove edge between two vertices
149
- */
150
- public abstract removeEdge(from: T, to: T): this;
151
-
152
- /**
153
- * Get vertex neighbors by its data
154
- */
155
- public getVertexNeighbors(data: T): Array<T> {
156
- const vertex = this.tryFindVertex(data);
157
- return this._vertices.get(vertex) || [];
158
- }
159
-
160
- /**
161
- * Check if graph has vertex
162
- */
163
- public hasVertex(data: T): boolean {
164
- return this._vertices.has(data);
165
- }
166
-
167
- /**
168
- * Check if graph has edge between from and to vertices
169
- */
170
- public hasEdge(from: T, to: T): boolean {
171
- return Boolean(
172
- this._edges.find((edge) => {
173
- return edge.fromVertex === from && edge.toVertex === to;
174
- })
175
- );
176
- }
177
-
178
- /**
179
- * Get edge weight between from and to vertices
180
- */
181
- public getEdgeWeight(from: T, to: T): number {
182
- const fromVertex = this.tryFindVertex(from);
183
- const toVertex = this.tryFindVertex(to);
184
-
185
- const edge = this.getEdgeByValue(fromVertex, toVertex);
186
-
187
- return edge.weight;
188
- }
189
- }
@@ -1,84 +0,0 @@
1
- import AbstractGraph from "./AbstractGraph";
2
- import GraphEdge from "./GraphEdge";
3
-
4
- /**
5
- * Directed graph - data structure where edges with same pair of vertices are not equal
6
- * @example A-B is not the same as B-A
7
- */
8
- export default class DirectedGraph<T> extends AbstractGraph<T> {
9
- /**
10
- * @inheritDoc
11
- */
12
- public constructor() {
13
- super();
14
- }
15
-
16
- /**
17
- * @inheritDoc
18
- */
19
- protected getEdgeByValue(from: T, to: T): GraphEdge<T> {
20
- const edge = this._edges.find(
21
- (edge: GraphEdge<T>) => edge.fromVertex === from && edge.toVertex === to
22
- );
23
-
24
- if (!edge) {
25
- throw new Error("Edge not found");
26
- }
27
-
28
- return edge;
29
- }
30
-
31
- /**
32
- * @inheritDoc
33
- */
34
- public addEdge(from: T, to: T, weight?: number): this {
35
- try {
36
- const fromVertex = this.tryFindVertex(from);
37
- const toVertex = this.tryFindVertex(to);
38
-
39
- if (this.hasEdge(fromVertex, toVertex)) {
40
- if (typeof weight === "number") {
41
- this.updateEdgeWeight(fromVertex, toVertex, weight);
42
- }
43
- } else {
44
- const edge = new GraphEdge(fromVertex, toVertex, weight);
45
-
46
- this._edges.push(edge);
47
- this._vertices.get(fromVertex)?.push(toVertex);
48
- }
49
- } catch {
50
- throw new Error(
51
- "Edge cannot be added because one of vertices was not found"
52
- );
53
- }
54
-
55
- return this;
56
- }
57
-
58
- /**
59
- * @inheritDoc
60
- */
61
- public removeEdge(from: T, to: T): this {
62
- try {
63
- const fromVertex = this.tryFindVertex(from);
64
- const toVertex = this.tryFindVertex(to);
65
- const edgeToRemove = this.getEdgeByValue(fromVertex, toVertex);
66
-
67
- const fromVertexNeighbors = this._vertices.get(fromVertex) || [];
68
- const fromNewNeighbors = fromVertexNeighbors.filter(
69
- (vertex: T) => toVertex !== vertex
70
- );
71
-
72
- this._vertices.set(fromVertex, fromNewNeighbors);
73
- this._edges = this._edges.filter(
74
- (edge: GraphEdge<T>) => edge !== edgeToRemove
75
- );
76
- } catch {
77
- throw new Error(
78
- "Edge cannot be removed because one of vertices was not found"
79
- );
80
- }
81
-
82
- return this;
83
- }
84
- }
@@ -1,33 +0,0 @@
1
- /**
2
- * Graph edge between two vertices
3
- */
4
- export default class GraphEdge<T> {
5
- private readonly _fromVertex: T;
6
- private readonly _toVertex: T;
7
- private _weight: number;
8
-
9
- /**
10
- * Create instance with linked "from" and "to" vertices
11
- */
12
- public constructor(fromVertex: T, toVertex: T, weight = 0) {
13
- this._fromVertex = fromVertex;
14
- this._toVertex = toVertex;
15
- this._weight = weight;
16
- }
17
-
18
- get fromVertex(): T {
19
- return this._fromVertex;
20
- }
21
-
22
- get toVertex(): T {
23
- return this._toVertex;
24
- }
25
-
26
- get weight(): number {
27
- return this._weight;
28
- }
29
-
30
- set weight(value: number) {
31
- this._weight = value;
32
- }
33
- }
@@ -1,108 +0,0 @@
1
- import AbstractGraph from "./AbstractGraph";
2
- import GraphEdge from "./GraphEdge";
3
-
4
- /**
5
- * Undirected graph - data structure where edges with same pair of vertices are equal
6
- * @example A-B is same as B-A
7
- */
8
- export default class UndirectedGraph<T> extends AbstractGraph<T> {
9
- /**
10
- * @inheritDoc
11
- */
12
- public constructor() {
13
- super();
14
- }
15
-
16
- /**
17
- * @inheritDoc
18
- */
19
- protected getEdgeByValue(from: T, to: T): GraphEdge<T> {
20
- const edge = this._edges.find(
21
- (edge: GraphEdge<T>) =>
22
- (edge.fromVertex === from && edge.toVertex === to) ||
23
- (edge.fromVertex === to && edge.toVertex === from)
24
- );
25
-
26
- if (!edge) {
27
- throw new Error("Edge not found");
28
- }
29
-
30
- return edge;
31
- }
32
-
33
- /**
34
- * @inheritDoc
35
- */
36
- public hasEdge(from: T, to: T): boolean {
37
- return Boolean(
38
- this._edges.find((edge) => {
39
- return (
40
- (edge.fromVertex === from && edge.toVertex === to) ||
41
- (edge.fromVertex === to && edge.toVertex === from)
42
- );
43
- })
44
- );
45
- }
46
-
47
- /**
48
- * @inheritDoc
49
- */
50
- public addEdge(from: T, to: T, weight?: number): this {
51
- try {
52
- const fromVertex = this.tryFindVertex(from);
53
- const toVertex = this.tryFindVertex(to);
54
-
55
- /** When edge is already exist, we should only update its weight */
56
- if (this.hasEdge(fromVertex, toVertex)) {
57
- if (typeof weight === "number") {
58
- this.updateEdgeWeight(fromVertex, toVertex, weight);
59
- }
60
- } else {
61
- const edge = new GraphEdge(fromVertex, toVertex, weight);
62
-
63
- this._edges.push(edge);
64
- this._vertices.get(fromVertex)?.push(toVertex);
65
- this._vertices.get(toVertex)?.push(fromVertex);
66
- }
67
- } catch {
68
- throw new Error(
69
- "Edge cannot be added because one of vertices was not found"
70
- );
71
- }
72
-
73
- return this;
74
- }
75
-
76
- /**
77
- * @inheritDoc
78
- */
79
- public removeEdge(from: T, to: T): this {
80
- try {
81
- const fromVertex = this.tryFindVertex(from);
82
- const toVertex = this.tryFindVertex(to);
83
- const edgeToRemove = this.getEdgeByValue(fromVertex, toVertex);
84
-
85
- const fromVertexNeighbors = this._vertices.get(fromVertex) || [];
86
- const toVertexNeighbors = this._vertices.get(toVertex) || [];
87
-
88
- const fromNewNeighbors = fromVertexNeighbors.filter(
89
- (vertex: T) => toVertex !== vertex
90
- );
91
- const toNewNeighbors = toVertexNeighbors.filter(
92
- (vertex: T) => fromVertex !== vertex
93
- );
94
-
95
- this._vertices.set(fromVertex, fromNewNeighbors);
96
- this._vertices.set(toVertex, toNewNeighbors);
97
- this._edges = this._edges.filter(
98
- (edge: GraphEdge<T>) => edge !== edgeToRemove
99
- );
100
- } catch {
101
- throw new Error(
102
- "Edge cannot be removed because one of vertices was not found"
103
- );
104
- }
105
-
106
- return this;
107
- }
108
- }
@@ -1,93 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import { EnumGraphType } from "../../../types/EnumGraphType";
3
- import { createGraph } from "../../../helpers/createGraph";
4
- import { randomizeNumberInRange } from "../../../utils";
5
- import { EnumRandomGenerationFormat } from "../../../types/EnumRandomGenerationFormat";
6
-
7
- const getRandomVertex = (): string => {
8
- return "_" + Math.random().toString(36).substr(2, 9);
9
- };
10
-
11
- const getPossibleEdgesCount = (
12
- type: EnumGraphType,
13
- verticesCount: number
14
- ): number => {
15
- let possibleEdgesCount = verticesCount * (verticesCount - 1);
16
-
17
- switch (type) {
18
- case EnumGraphType.Directed: {
19
- break;
20
- }
21
- case EnumGraphType.Undirected: {
22
- possibleEdgesCount = Math.floor(possibleEdgesCount / 2);
23
- break;
24
- }
25
- default: {
26
- throw new Error("Wrong random generation format");
27
- }
28
- }
29
-
30
- return possibleEdgesCount;
31
- };
32
-
33
- const fillGraphRandomly = (
34
- graph: IGraph<string>,
35
- format: EnumRandomGenerationFormat,
36
- verticesCount: number
37
- ): void => {
38
- switch (format) {
39
- case EnumRandomGenerationFormat.Hash: {
40
- for (let i = 0; i < verticesCount; i++) {
41
- graph.addVertex(getRandomVertex());
42
- }
43
- break;
44
- }
45
- case EnumRandomGenerationFormat.Numbers: {
46
- for (let i = 0; i < verticesCount; i++) {
47
- graph.addVertex((i + 1).toString());
48
- }
49
- break;
50
- }
51
- default: {
52
- throw new Error("Wrong random generation format");
53
- }
54
- }
55
- };
56
-
57
- export const generateRandomGraph = (
58
- verticesCount: number,
59
- edgesCount: number,
60
- type: EnumGraphType = EnumGraphType.Undirected,
61
- format: EnumRandomGenerationFormat = EnumRandomGenerationFormat.Numbers
62
- ): IGraph<string> => {
63
- const graph = createGraph<string>(type);
64
- const possibleEdgesCount = getPossibleEdgesCount(type, verticesCount);
65
-
66
- if (edgesCount <= 0 || edgesCount > possibleEdgesCount) {
67
- throw new Error(
68
- `Edges count must be in range between 0 and ${possibleEdgesCount}`
69
- );
70
- }
71
-
72
- fillGraphRandomly(graph, format, verticesCount);
73
- const addedVertices = graph.vertices();
74
- let addedEdgesCount = 0;
75
-
76
- while (addedEdgesCount < edgesCount) {
77
- const randomizeIndex = () => {
78
- return randomizeNumberInRange(0, addedVertices.length);
79
- };
80
- const randomVertexFrom = addedVertices[randomizeIndex()];
81
- const randomVertexTo = addedVertices[randomizeIndex()];
82
-
83
- const isEdgeAlreadyExists = graph.hasEdge(randomVertexFrom, randomVertexTo);
84
- const isTheSameVertex = randomVertexFrom === randomVertexTo;
85
-
86
- if (!isTheSameVertex && !isEdgeAlreadyExists) {
87
- graph.addEdge(randomVertexFrom, randomVertexTo);
88
- addedEdgesCount++;
89
- }
90
- }
91
-
92
- return graph;
93
- };
@@ -1,99 +0,0 @@
1
- import IGraphIterator from "../../../types/IGraphIterator";
2
- import IGraph from "../../../types/IGraph";
3
-
4
- export default abstract class AbstractGraphIterator<T>
5
- implements IGraphIterator<T> {
6
- protected readonly graph: IGraph<T>;
7
- protected readonly visited: Map<T, boolean>;
8
- protected readonly parents: Map<T, T>;
9
-
10
- /**
11
- * Creates empty instance
12
- */
13
- protected constructor(graph: IGraph<T>) {
14
- this.graph = graph;
15
- this.visited = new Map();
16
- this.parents = new Map();
17
- }
18
-
19
- protected abstract currentImpl(): T;
20
- protected abstract nextImpl(): T;
21
- protected abstract initIteratorImpl(from: T): void;
22
- protected abstract hasNextImpl(): boolean;
23
-
24
- /**
25
- * @inheritDoc
26
- */
27
- public initIterator(from: T): void {
28
- if (!this.graph.hasVertex(from)) {
29
- throw new Error("Start vertex does not exist");
30
- }
31
- this.initIteratorImpl(from);
32
- }
33
-
34
- /**
35
- * @inheritDoc
36
- */
37
- public hasNext(): boolean {
38
- return this.hasNextImpl();
39
- }
40
-
41
- /**
42
- * @inheritDoc
43
- */
44
- public next(): T {
45
- try {
46
- if (!this.hasNext()) {
47
- throw new Error();
48
- }
49
-
50
- return this.nextImpl();
51
- } catch (e) {
52
- throw new Error("Next element does not exist");
53
- }
54
- }
55
-
56
- /**
57
- * @inheritDoc
58
- */
59
- public current(): T {
60
- try {
61
- const current = this.currentImpl();
62
-
63
- if (current === null) {
64
- throw new Error();
65
- }
66
- return current;
67
- } catch (e) {
68
- throw new Error("Current element does not exist");
69
- }
70
- }
71
-
72
- /**
73
- * @inheritDoc
74
- */
75
- public getPath(from: T, to: T): Array<T> {
76
- const path: Array<T> = new Array<T>();
77
- const isLinkedDirectly = this.graph.hasEdge(from, to);
78
- let currentVertex = this.parents.get(to);
79
-
80
- if (isLinkedDirectly) {
81
- return [from, to];
82
- } else {
83
- while (currentVertex) {
84
- if (currentVertex === from) {
85
- break;
86
- }
87
-
88
- path.push(currentVertex);
89
- currentVertex = this.parents.get(currentVertex);
90
- }
91
-
92
- if (path.length === 0) {
93
- throw new Error("There is no path found");
94
- }
95
-
96
- return [from, ...path.reverse(), to];
97
- }
98
- }
99
- }
@@ -1,60 +0,0 @@
1
- import Queue from "../../Queue/Queue";
2
- import IGraph from "../../../types/IGraph";
3
- import AbstractGraphIterator from "./AbstractGraphIterator";
4
-
5
- /**
6
- * Breadth first graph traversal
7
- */
8
- export default class GraphIteratorBFS<T> extends AbstractGraphIterator<T> {
9
- private readonly queue: Queue<T>;
10
-
11
- /**
12
- * @inheritDoc
13
- */
14
- public constructor(graph: IGraph<T>) {
15
- super(graph);
16
- this.queue = new Queue();
17
- }
18
-
19
- /**
20
- * @inheritDoc
21
- */
22
- protected currentImpl(): T {
23
- return this.queue.peek();
24
- }
25
-
26
- /**
27
- @inheritDoc
28
- */
29
- public initIteratorImpl(startVertex: T): void {
30
- this.queue.push(startVertex);
31
- this.visited.set(startVertex, true);
32
- }
33
-
34
- /**
35
- * @inheritDoc
36
- */
37
- public hasNextImpl(): boolean {
38
- return !this.queue.isEmpty();
39
- }
40
-
41
- /**
42
- * @inheritDoc
43
- */
44
- protected nextImpl(): T {
45
- const next = this.queue.pop();
46
- const nextNeighbors = this.graph.getVertexNeighbors(next);
47
-
48
- nextNeighbors.forEach((neighbor) => {
49
- const isNotVisited = !this.visited.get(neighbor);
50
-
51
- if (isNotVisited) {
52
- this.queue.push(neighbor);
53
- this.visited.set(neighbor, true);
54
- this.parents.set(neighbor, next);
55
- }
56
- });
57
-
58
- return next;
59
- }
60
- }