@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,60 +0,0 @@
1
- import Stack from "../../Stack/Stack";
2
- import IGraph from "../../../types/IGraph";
3
- import AbstractGraphIterator from "./AbstractGraphIterator";
4
-
5
- /**
6
- * Deep first graph traversal
7
- */
8
- export default class GraphIteratorDFS<T> extends AbstractGraphIterator<T> {
9
- private readonly stack: Stack<T>;
10
-
11
- /**
12
- * @inheritDoc
13
- */
14
- public constructor(graph: IGraph<T>) {
15
- super(graph);
16
- this.stack = new Stack();
17
- }
18
-
19
- /**
20
- * @inheritDoc
21
- */
22
- public hasNextImpl(): boolean {
23
- return !this.stack.isEmpty();
24
- }
25
-
26
- /**
27
- @inheritDoc
28
- */
29
- public initIteratorImpl(startVertex: T): void {
30
- this.stack.push(startVertex);
31
- this.visited.set(startVertex, true);
32
- }
33
-
34
- /**
35
- * @inheritDoc
36
- */
37
- public currentImpl(): T {
38
- return this.stack.peek();
39
- }
40
-
41
- /**
42
- * @inheritDoc
43
- */
44
- public nextImpl(): T {
45
- const next = this.stack.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.stack.push(neighbor);
53
- this.visited.set(neighbor, true);
54
- this.parents.set(neighbor, next);
55
- }
56
- });
57
-
58
- return next;
59
- }
60
- }
@@ -1,94 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import AbstractGraphIterator from "./AbstractGraphIterator";
3
-
4
- /**
5
- * Dijkstra method graph traversal
6
- */
7
- export default class GraphIteratorDijkstra<T> extends AbstractGraphIterator<T> {
8
- private readonly costs: Map<T, number>;
9
-
10
- /**
11
- * @inheritDoc
12
- */
13
- public constructor(graph: IGraph<T>) {
14
- super(graph);
15
- this.costs = new Map();
16
- }
17
-
18
- /**
19
- * Get not visited vertex with minimal cost
20
- */
21
- private getClosestNotVisited(): T {
22
- const keys = Array.from(this.costs.keys());
23
- const priorityList = keys
24
- .filter((key: T) => !this.visited.get(key))
25
- .sort((aKey: T, bKey: T) => {
26
- const aCost = this.costs.get(aKey) || 0;
27
- const bCost = this.costs.get(bKey) || 0;
28
-
29
- return aCost - bCost;
30
- });
31
-
32
- if (priorityList[0] === undefined) {
33
- throw new Error("No more vertices found");
34
- }
35
-
36
- return priorityList[0];
37
- }
38
-
39
- /**
40
- * @inheritDoc
41
- */
42
- public initIteratorImpl(startVertex: T): void {
43
- if (!this.graph.hasVertex(startVertex)) {
44
- throw new Error("Start vertex does not exist");
45
- }
46
-
47
- this.visited.set(startVertex, true);
48
- this.costs.set(startVertex, 0);
49
-
50
- this.graph.getVertexNeighbors(startVertex).forEach((neighbor: T) => {
51
- const edgeWeight = this.graph.getEdgeWeight(startVertex, neighbor);
52
- this.costs.set(neighbor, edgeWeight);
53
- this.parents.set(neighbor, startVertex);
54
- });
55
- }
56
-
57
- /**
58
- * @inheritDoc
59
- */
60
- public hasNextImpl(): boolean {
61
- return !(this.getClosestNotVisited() === null);
62
- }
63
-
64
- /**
65
- * @inheritDoc
66
- */
67
- public currentImpl(): T {
68
- return this.getClosestNotVisited();
69
- }
70
-
71
- /**
72
- * @inheritDoc
73
- */
74
- public nextImpl(): T {
75
- const next = this.getClosestNotVisited();
76
-
77
- this.visited.set(next, true);
78
- const nextNeighbors = this.graph.getVertexNeighbors(next);
79
- const nextCost = this.costs.get(next);
80
-
81
- nextNeighbors.forEach((neighbor: T) => {
82
- const edgeWeight = this.graph.getEdgeWeight(next, neighbor);
83
- const currentNeighborCost = this.costs.get(neighbor) || Infinity;
84
- const newNeighborCost = (nextCost || 0) + edgeWeight;
85
-
86
- if (newNeighborCost < currentNeighborCost) {
87
- this.costs.set(neighbor, newNeighborCost);
88
- this.parents.set(neighbor, next);
89
- }
90
- });
91
-
92
- return next;
93
- }
94
- }
@@ -1,29 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
-
3
- /**
4
- * Get graph adjacency list
5
- *
6
- * @example
7
- *
8
- * Directed graph:
9
- * - Bob [Maria]
10
- * - Maria [Bob, John]
11
- * - John []
12
- *
13
- * @example
14
- *
15
- * Undirected graph:
16
- * - Bob [Maria]
17
- * - Maria [Bob, John]
18
- * - John [Maria]
19
- **/
20
- export const presenterAdjacencyLists = <T>(
21
- graph: IGraph<T>
22
- ): Map<T, Array<T>> => {
23
- return graph.vertices().reduce((map: Map<T, Array<T>>, vertex: T) => {
24
- const neighbors = graph.getVertexNeighbors(vertex);
25
- map.set(vertex, neighbors);
26
-
27
- return map;
28
- }, new Map());
29
- };
@@ -1,38 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import IGraphIterator from "../../../types/IGraphIterator";
3
- import IGraphIterationStrategy from "../../../types/IGraphIterationStrategy";
4
-
5
- /**
6
- * Check if graph has a path between two vertices
7
- * @throws when start vertex was not found
8
- * @throws when end vertex was not found
9
- * @throws when there is no path between two vertices
10
- */
11
- export const hasPath = <T>(
12
- graph: IGraph<T>,
13
- from: T,
14
- to: T,
15
- strategy: IGraphIterationStrategy<T>
16
- ): boolean => {
17
- /* Validate */
18
- if (!graph.hasVertex(from)) {
19
- throw new Error("Start vertex was not found");
20
- }
21
- if (!graph.hasVertex(to)) {
22
- throw new Error("End vertex was not found");
23
- }
24
-
25
- const iterator: IGraphIterator<T> = strategy.createIterator(graph);
26
- iterator.initIterator(from);
27
-
28
- /* Find target element */
29
- while (iterator.hasNext()) {
30
- const next = iterator.next();
31
-
32
- if (next === to) {
33
- return true;
34
- }
35
- }
36
-
37
- return false;
38
- };
@@ -1,38 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import IGraphIterator from "../../../types/IGraphIterator";
3
- import IGraphIterationStrategy from "../../../types/IGraphIterationStrategy";
4
-
5
- /**
6
- * Find the shortest path between two vertices
7
- * @throws when start vertex was not found
8
- * @throws when end vertex was not found
9
- * @throws when there is no path between two vertices
10
- */
11
- export const shortestPath = <T>(
12
- graph: IGraph<T>,
13
- from: T,
14
- to: T,
15
- strategy: IGraphIterationStrategy<T>
16
- ): Array<T> => {
17
- /* Validate */
18
- if (!graph.hasVertex(from)) {
19
- throw new Error("Start vertex was not found");
20
- }
21
- if (!graph.hasVertex(to)) {
22
- throw new Error("End vertex was not found");
23
- }
24
-
25
- const iterator: IGraphIterator<T> = strategy.createIterator(graph);
26
- iterator.initIterator(from);
27
-
28
- /* Find target element */
29
- while (iterator.hasNext()) {
30
- const next = iterator.next();
31
-
32
- if (next === to) {
33
- break;
34
- }
35
- }
36
-
37
- return iterator.getPath(from, to);
38
- };
@@ -1,11 +0,0 @@
1
- import IGraphIterationStrategy from "../../../types/IGraphIterationStrategy";
2
- import IGraphIterator from "../../../types/IGraphIterator";
3
- import IGraph from "../../../types/IGraph";
4
- import GraphIteratorBFS from "../iterator/GraphIteratorBFS";
5
-
6
- export default class BFSIterationStrategy<T>
7
- implements IGraphIterationStrategy<T> {
8
- public createIterator(graph: IGraph<T>): IGraphIterator<T> {
9
- return new GraphIteratorBFS<T>(graph);
10
- }
11
- }
@@ -1,11 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import IGraphIterationStrategy from "../../../types/IGraphIterationStrategy";
3
- import IGraphIterator from "../../../types/IGraphIterator";
4
- import GraphIteratorDFS from "../iterator/GraphIteratorDFS";
5
-
6
- export default class DFSIterationStrategy<T>
7
- implements IGraphIterationStrategy<T> {
8
- public createIterator(graph: IGraph<T>): IGraphIterator<T> {
9
- return new GraphIteratorDFS(graph);
10
- }
11
- }
@@ -1,11 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import IGraphIterationStrategy from "../../../types/IGraphIterationStrategy";
3
- import IGraphIterator from "../../../types/IGraphIterator";
4
- import GraphIteratorDijkstra from "../iterator/GraphIteratorDijkstra";
5
-
6
- export default class DijkstraIterationStrategy<T>
7
- implements IGraphIterationStrategy<T> {
8
- public createIterator(graph: IGraph<T>): IGraphIterator<T> {
9
- return new GraphIteratorDijkstra(graph);
10
- }
11
- }
@@ -1,19 +0,0 @@
1
- import IGraph from "../../../types/IGraph";
2
- import { createGraphFromMatrix } from "../../../helpers/createGraphFromMatrix";
3
- import { EnumGraphType } from "../../../types/EnumGraphType";
4
- import { presenterAdjacencyMatrix } from "../presenter/presenterAdjacencyMatrix";
5
- import { transposeMatrix } from "../../../algorithms/transpose-matrix";
6
-
7
- export const transposeDirectedGraph = <T>(
8
- sourceGraph: IGraph<T>
9
- ): IGraph<T> => {
10
- const verticesList = sourceGraph.vertices();
11
- const matrix = presenterAdjacencyMatrix(sourceGraph);
12
- const transposedMatrix = transposeMatrix(matrix);
13
-
14
- return createGraphFromMatrix(
15
- transposedMatrix,
16
- verticesList,
17
- EnumGraphType.Directed
18
- );
19
- };
@@ -1,202 +0,0 @@
1
- import IKeyValueStorage from "../../types/IKeyValueStorage";
2
- import HashTableNode from "./HashTableNode";
3
-
4
- /**
5
- * Implementation of open addressing hash table using quadratic probing
6
- */
7
- export default class HashTable<T> implements IKeyValueStorage<T> {
8
- /**
9
- Constants
10
- */
11
- private static DEFAULT_MAX_CAPACITY = 100;
12
- private static MAX_LOAD_FACTOR = 0.5;
13
-
14
- /**
15
- * State
16
- */
17
- private storage: Array<HashTableNode<T>>;
18
- private maxCapacity: number;
19
- private storageCapacity = 0;
20
-
21
- /**
22
- * Given init capacity size will be used
23
- * @throws when initial capacity value is not larger than 0
24
- */
25
- public constructor(initialCapacity: number = HashTable.DEFAULT_MAX_CAPACITY) {
26
- if (initialCapacity <= 0) {
27
- throw new Error("Size must be larger than 0");
28
- }
29
- this.maxCapacity = initialCapacity;
30
- this.storage = new Array(this.maxCapacity).fill(null);
31
- }
32
-
33
- /**
34
- * Main-hash function
35
- */
36
- private hashFn(key: string, number: number): number {
37
- return (
38
- (this.innerHashFn(key) + 127 * number + 365 * number * number) %
39
- this.maxCapacity
40
- );
41
- }
42
-
43
- /**
44
- * Helper-hash function
45
- */
46
- private innerHashFn(key: string): number {
47
- const length: number = key.length;
48
-
49
- if (length === 0) {
50
- return 0;
51
- }
52
- const substring = key.substring(0, length - 1);
53
- const symbol = key.charCodeAt(length - 1);
54
-
55
- return (127 * this.innerHashFn(substring) + symbol) % this.maxCapacity;
56
- }
57
-
58
- /**
59
- * Max capacity will be increased and storage will be overwritten
60
- */
61
- private resizeStorage(): Array<HashTableNode<T>> {
62
- this.maxCapacity *= 2;
63
-
64
- const newArray = new Array(this.maxCapacity).fill(null);
65
-
66
- for (let i = 0; i < this.storage.length; i++) {
67
- const element = this.storage[i];
68
-
69
- if (element != null) {
70
- for (let j = 0; j < this.maxCapacity; j++) {
71
- const newIndex = this.hashFn(element.key, j);
72
-
73
- if (newArray[newIndex] == null) {
74
- newArray[newIndex] = element;
75
-
76
- break;
77
- }
78
- }
79
- }
80
- }
81
-
82
- return newArray;
83
- }
84
-
85
- /**
86
- * Will find node instance by its key
87
- * @throws when element does not exist
88
- */
89
- private findNode(key: string): HashTableNode<T> {
90
- for (let i = 0; i < this.maxCapacity; i++) {
91
- const index = this.hashFn(key, i);
92
- const node = this.storage[index];
93
-
94
- if (node?.key === key) {
95
- if (node?.isDeleted) {
96
- break;
97
- }
98
- return node;
99
- }
100
- }
101
-
102
- throw new Error("Element does not exist");
103
- }
104
-
105
- /**
106
- * Will create new node instance and set in to storage by index
107
- */
108
- private addNode(key: string, data: T, index: number): void {
109
- this.storage[index] = new HashTableNode<T>(key, data);
110
-
111
- this.storageCapacity++;
112
- const loadFactor = this.storageCapacity / this.maxCapacity;
113
-
114
- if (loadFactor >= HashTable.MAX_LOAD_FACTOR) {
115
- this.storage = this.resizeStorage();
116
- }
117
- }
118
-
119
- /**
120
- * Will create new node instance and set in to storage by index
121
- */
122
- private updateNode(data: T, index: number): void {
123
- this.storage[index].data = data;
124
- this.storage[index].isDeleted = false;
125
- }
126
-
127
- /**
128
- * Will insert item to hash table
129
- */
130
- public set(key: string, data: T): void {
131
- for (let i = 0; i < this.maxCapacity; i++) {
132
- const index = this.hashFn(key, i);
133
- const node = this.storage[index];
134
-
135
- const shouldAddNode = node === null;
136
- if (shouldAddNode) {
137
- this.addNode(key, data, index);
138
- break;
139
- }
140
-
141
- const shouldUpdateNode = node.key === key;
142
- if (shouldUpdateNode) {
143
- this.updateNode(data, index);
144
- break;
145
- }
146
- }
147
- }
148
-
149
- /**
150
- * Will update item property isDeleted to false
151
- * @throws when element does not exist
152
- */
153
- public delete(key: string): void {
154
- for (let i = 0; i < this.maxCapacity; i++) {
155
- const index = this.hashFn(key, i);
156
-
157
- if (this.storage[index] !== null) {
158
- this.storage[index].isDeleted = true;
159
- return;
160
- }
161
- }
162
-
163
- throw new Error("Element does not exist");
164
- }
165
-
166
- /**
167
- * Will find item in hash table
168
- * @throws when element does not exist
169
- */
170
- public get(key: string): T {
171
- return this.findNode(key).data;
172
- }
173
-
174
- /**
175
- * Check if node with given key exists
176
- */
177
- public has(key: string): boolean {
178
- try {
179
- return Boolean(this.findNode(key));
180
- } catch (e) {
181
- return false;
182
- }
183
- }
184
-
185
- /**
186
- * Added elements count
187
- */
188
- public length(): number {
189
- const actualItems = this.storage.filter((item) => {
190
- return item !== null && !item.isDeleted;
191
- });
192
-
193
- return actualItems.length;
194
- }
195
-
196
- /**
197
- * Will overwrite storage with array of null elements
198
- */
199
- public clear(): void {
200
- this.storage = new Array(HashTable.DEFAULT_MAX_CAPACITY).fill(null);
201
- }
202
- }
@@ -1,31 +0,0 @@
1
- export default class HashTableNode<T> {
2
- private readonly _key: string;
3
- private _data: T;
4
- private _isDeleted: boolean;
5
-
6
- public constructor(key: string, data: T) {
7
- this._key = key;
8
- this._data = data;
9
- this._isDeleted = false;
10
- }
11
-
12
- get data(): T {
13
- return this._data;
14
- }
15
-
16
- set data(value: T) {
17
- this._data = value;
18
- }
19
-
20
- get key(): string {
21
- return this._key;
22
- }
23
-
24
- get isDeleted(): boolean {
25
- return this._isDeleted;
26
- }
27
-
28
- set isDeleted(value: boolean) {
29
- this._isDeleted = value;
30
- }
31
- }