@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.
- package/README.md +26 -2
- package/lib/algorithms/binary-search.d.ts +5 -0
- package/lib/algorithms/binary-search.js +27 -0
- package/lib/algorithms/factorial.d.ts +9 -0
- package/lib/algorithms/factorial.js +17 -0
- package/lib/algorithms/fibonacci.d.ts +9 -0
- package/lib/algorithms/fibonacci.js +17 -0
- package/lib/algorithms/memoize.d.ts +5 -0
- package/lib/algorithms/memoize.js +22 -0
- package/lib/algorithms/sorts/bubble-sort.d.ts +9 -0
- package/lib/algorithms/sorts/bubble-sort.js +23 -0
- package/lib/algorithms/sorts/insertion-sort.d.ts +9 -0
- package/lib/algorithms/sorts/insertion-sort.js +25 -0
- package/lib/algorithms/sorts/merge-sort.d.ts +9 -0
- package/lib/algorithms/sorts/merge-sort.js +61 -0
- package/lib/algorithms/sorts/quick-sort.d.ts +9 -0
- package/lib/algorithms/sorts/quick-sort.js +45 -0
- package/lib/algorithms/sorts/select-sort.d.ts +9 -0
- package/lib/algorithms/sorts/select-sort.js +20 -0
- package/lib/algorithms/transpose-matrix.d.ts +5 -0
- package/lib/algorithms/transpose-matrix.js +20 -0
- package/lib/constants.d.ts +2 -0
- package/lib/constants.js +6 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.d.ts +15 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.js +53 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.d.ts +60 -0
- package/lib/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.js +36 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.d.ts +13 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.js +59 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.d.ts +70 -0
- package/lib/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.js +268 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.d.ts +16 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.js +70 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.d.ts +57 -0
- package/lib/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.js +234 -0
- package/lib/data-structures/Graph/AbstractGraph.d.ts +84 -0
- package/lib/data-structures/Graph/AbstractGraph.js +141 -0
- package/lib/data-structures/Graph/DirectedGraph.d.ts +24 -0
- package/lib/data-structures/Graph/DirectedGraph.js +85 -0
- package/lib/data-structures/Graph/GraphEdge.d.ts +16 -0
- package/lib/data-structures/Graph/GraphEdge.js +43 -0
- package/lib/data-structures/Graph/UndirectedGraph.d.ts +28 -0
- package/lib/data-structures/Graph/UndirectedGraph.js +102 -0
- package/lib/data-structures/Graph/demo/generateRandomGraph.d.ts +4 -0
- package/lib/data-structures/Graph/demo/generateRandomGraph.js +72 -0
- package/lib/data-structures/Graph/iterator/AbstractGraphIterator.d.ts +35 -0
- package/lib/data-structures/Graph/iterator/AbstractGraphIterator.js +90 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorBFS.d.ts +28 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorBFS.js +70 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDFS.d.ts +28 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDFS.js +70 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.d.ts +32 -0
- package/lib/data-structures/Graph/iterator/GraphIteratorDijkstra.js +99 -0
- package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.d.ts +19 -0
- package/lib/data-structures/Graph/presenter/presenterAdjacencyLists.js +28 -0
- package/{src/data-structures/Graph/presenter/presenterAdjacencyMatrix.ts → lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.d.ts} +32 -51
- package/lib/data-structures/Graph/presenter/presenterAdjacencyMatrix.js +48 -0
- package/lib/data-structures/Graph/searching/hasPath.d.ts +9 -0
- package/lib/data-structures/Graph/searching/hasPath.js +29 -0
- package/lib/data-structures/Graph/searching/shortestPath.d.ts +9 -0
- package/lib/data-structures/Graph/searching/shortestPath.js +29 -0
- package/lib/data-structures/Graph/strategy/BFSIterationStrategy.d.ts +6 -0
- package/lib/data-structures/Graph/strategy/BFSIterationStrategy.js +13 -0
- package/lib/data-structures/Graph/strategy/DFSIterationStrategy.d.ts +6 -0
- package/lib/data-structures/Graph/strategy/DFSIterationStrategy.js +13 -0
- package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.d.ts +6 -0
- package/lib/data-structures/Graph/strategy/DijkstraIterationStrategy.js +13 -0
- package/lib/data-structures/Graph/transposing/transposeDirectedGraph.d.ts +2 -0
- package/lib/data-structures/Graph/transposing/transposeDirectedGraph.js +14 -0
- package/lib/data-structures/HashTable/HashTable.d.ts +73 -0
- package/lib/data-structures/HashTable/HashTable.js +169 -0
- package/lib/data-structures/HashTable/HashTableNode.d.ts +11 -0
- package/lib/data-structures/HashTable/HashTableNode.js +39 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.d.ts +125 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.js +236 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.d.ts +20 -0
- package/lib/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.js +41 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.d.ts +48 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.js +150 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.d.ts +25 -0
- package/lib/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.js +65 -0
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.d.ts +52 -0
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.js +137 -0
- package/{src/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.ts → lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.d.ts} +7 -10
- package/lib/data-structures/LinkedList/SingleLinkedList/SingleLinkedNode.js +29 -0
- package/lib/data-structures/LoopedArray/LoopedArray.d.ts +86 -0
- package/lib/data-structures/LoopedArray/LoopedArray.js +161 -0
- package/lib/data-structures/Queue/Queue.d.ts +50 -0
- package/lib/data-structures/Queue/Queue.js +83 -0
- package/lib/data-structures/Stack/Stack.d.ts +50 -0
- package/lib/data-structures/Stack/Stack.js +83 -0
- package/lib/exports/algorithms.d.ts +16 -0
- package/lib/exports/algorithms.js +36 -0
- package/lib/exports/constants.d.ts +2 -0
- package/lib/exports/constants.js +7 -0
- package/lib/exports/data-structures.d.ts +11 -0
- package/lib/exports/data-structures.js +24 -0
- package/lib/exports/helpers.d.ts +6 -0
- package/lib/exports/helpers.js +14 -0
- package/lib/exports/sorts.d.ts +6 -0
- package/lib/exports/sorts.js +14 -0
- package/lib/exports/utils.d.ts +3 -0
- package/lib/exports/utils.js +14 -0
- package/lib/exports.d.ts +44 -0
- package/lib/exports.js +89 -0
- package/lib/helpers/createBinaryTree.d.ts +6 -0
- package/lib/helpers/createBinaryTree.js +22 -0
- package/lib/helpers/createGraph.d.ts +6 -0
- package/lib/helpers/createGraph.js +24 -0
- package/lib/helpers/createGraphFromMatrix.d.ts +7 -0
- package/lib/helpers/createGraphFromMatrix.js +37 -0
- package/lib/helpers/createLinkedList.d.ts +3 -0
- package/lib/helpers/createLinkedList.js +21 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +6 -0
- package/lib/types/ArrayMatrix.d.ts +1 -0
- package/lib/types/ArrayMatrix.js +3 -0
- package/lib/types/EnumBinarySearchTreeType.d.ts +4 -0
- package/lib/types/EnumBinarySearchTreeType.js +9 -0
- package/lib/types/EnumGraphType.d.ts +4 -0
- package/lib/types/EnumGraphType.js +9 -0
- package/lib/types/EnumLinkedListType.d.ts +4 -0
- package/lib/types/EnumLinkedListType.js +9 -0
- package/lib/types/EnumRandomGenerationFormat.d.ts +4 -0
- package/lib/types/EnumRandomGenerationFormat.js +9 -0
- package/lib/types/EnumTreeTraversalType.d.ts +5 -0
- package/lib/types/EnumTreeTraversalType.js +10 -0
- package/lib/types/FnCompareTwo.d.ts +1 -0
- package/lib/types/FnCompareTwo.js +3 -0
- package/lib/types/FnToMemoize.d.ts +1 -0
- package/lib/types/FnToMemoize.js +3 -0
- package/{src/types/ILinkedList.ts → lib/types/IArrayFacade.d.ts} +4 -6
- package/lib/types/IArrayFacade.js +3 -0
- package/{src/types/IBiDirectIterable.ts → lib/types/IBiDirectIterable.d.ts} +5 -6
- package/lib/types/IBiDirectIterable.js +3 -0
- package/lib/types/IBiDirectIterator.d.ts +11 -0
- package/lib/types/IBiDirectIterator.js +3 -0
- package/lib/types/IBinaryTree.d.ts +12 -0
- package/lib/types/IBinaryTree.js +3 -0
- package/lib/types/IConvertableToArray.d.ts +4 -0
- package/lib/types/IConvertableToArray.js +3 -0
- package/lib/types/IGraph.d.ts +14 -0
- package/lib/types/IGraph.js +3 -0
- package/{src/types/IGraphIterationStrategy.ts → lib/types/IGraphIterationStrategy.d.ts} +5 -6
- package/lib/types/IGraphIterationStrategy.js +3 -0
- package/lib/types/IGraphIterator.d.ts +11 -0
- package/lib/types/IGraphIterator.js +3 -0
- package/{src/types/IIterable.ts → lib/types/IIterable.d.ts} +4 -5
- package/lib/types/IIterable.js +3 -0
- package/lib/types/IIterator.d.ts +14 -0
- package/lib/types/IIterator.js +3 -0
- package/lib/types/IKeyValueStorage.d.ts +8 -0
- package/lib/types/IKeyValueStorage.js +3 -0
- package/lib/types/ILinearStorage.d.ts +11 -0
- package/lib/types/ILinearStorage.js +3 -0
- package/{src/types/ILinearStorageRA.ts → lib/types/ILinearStorageRA.d.ts} +13 -14
- package/lib/types/ILinearStorageRA.js +3 -0
- package/{src/types/IArrayFacade.ts → lib/types/ILinkedList.d.ts} +4 -6
- package/lib/types/ILinkedList.js +3 -0
- package/lib/utils.d.ts +29 -0
- package/lib/utils.js +95 -0
- package/package.json +9 -3
- package/.idea/algorythmes.iml +0 -15
- package/.idea/codeStyles/codeStyleConfig.xml +0 -5
- package/.idea/deployment.xml +0 -14
- package/.idea/inspectionProfiles/Project_Default.xml +0 -7
- package/.idea/jsLinters/eslint.xml +0 -6
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/lib/algotirhms.ts +0 -35
- package/lib/constants.ts +0 -3
- package/lib/data-structures.ts +0 -23
- package/lib/helpers.ts +0 -13
- package/lib/sorts.ts +0 -7
- package/lib/types.ts +0 -53
- package/lib/utils.ts +0 -21
- package/src/algorithms/binary-search.ts +0 -28
- package/src/algorithms/factorial.ts +0 -18
- package/src/algorithms/fibonacci.ts +0 -18
- package/src/algorithms/memoize.ts +0 -21
- package/src/algorithms/sorts/bubble-sort.ts +0 -21
- package/src/algorithms/sorts/insertion-sort.ts +0 -25
- package/src/algorithms/sorts/merge-sort.ts +0 -74
- package/src/algorithms/sorts/quick-sort.ts +0 -54
- package/src/algorithms/sorts/select-sort.ts +0 -19
- package/src/algorithms/transpose-matrix.ts +0 -19
- package/src/constants.ts +0 -2
- package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryNode.ts +0 -45
- package/src/data-structures/BinaryTree/AbstractBinaryTree/AbstractBinaryTree.ts +0 -80
- package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchNode.ts +0 -38
- package/src/data-structures/BinaryTree/BinarySearchTree/BinarySearchTree.ts +0 -286
- package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchNode.ts +0 -48
- package/src/data-structures/BinaryTree/RandBinarySearchTree/RandBinarySearchTree.ts +0 -228
- package/src/data-structures/Graph/AbstractGraph.ts +0 -189
- package/src/data-structures/Graph/DirectedGraph.ts +0 -84
- package/src/data-structures/Graph/GraphEdge.ts +0 -33
- package/src/data-structures/Graph/UndirectedGraph.ts +0 -108
- package/src/data-structures/Graph/demo/generateRandomGraph.ts +0 -93
- package/src/data-structures/Graph/iterator/AbstractGraphIterator.ts +0 -99
- package/src/data-structures/Graph/iterator/GraphIteratorBFS.ts +0 -60
- package/src/data-structures/Graph/iterator/GraphIteratorDFS.ts +0 -60
- package/src/data-structures/Graph/iterator/GraphIteratorDijkstra.ts +0 -94
- package/src/data-structures/Graph/presenter/presenterAdjacencyLists.ts +0 -29
- package/src/data-structures/Graph/searching/hasPath.ts +0 -38
- package/src/data-structures/Graph/searching/shortestPath.ts +0 -38
- package/src/data-structures/Graph/strategy/BFSIterationStrategy.ts +0 -11
- package/src/data-structures/Graph/strategy/DFSIterationStrategy.ts +0 -11
- package/src/data-structures/Graph/strategy/DijkstraIterationStrategy.ts +0 -11
- package/src/data-structures/Graph/transposing/transposeDirectedGraph.ts +0 -19
- package/src/data-structures/HashTable/HashTable.ts +0 -202
- package/src/data-structures/HashTable/HashTableNode.ts +0 -31
- package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedList.ts +0 -310
- package/src/data-structures/LinkedList/AbstractLinkedList/AbstractLinkedNode.ts +0 -33
- package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedList.ts +0 -156
- package/src/data-structures/LinkedList/DoubleLinkedList/DoubleLinkedNode.ts +0 -47
- package/src/data-structures/LinkedList/SingleLinkedList/SingleLinkedList.ts +0 -147
- package/src/data-structures/LoopedArray/LoopedArray.ts +0 -182
- package/src/data-structures/Queue/Queue.ts +0 -92
- package/src/data-structures/Stack/Stack.ts +0 -92
- package/src/demo/demo.bst.ts +0 -67
- package/src/demo/demo.graph.ts +0 -246
- package/src/demo/demo.hashtable.ts +0 -28
- package/src/demo/demo.linked-list.ts +0 -78
- package/src/demo/demo.looped-array.ts +0 -104
- package/src/demo/demo.queue.ts +0 -40
- package/src/demo/demo.stack.ts +0 -40
- package/src/demo/performance/bst-compare.ts +0 -35
- package/src/demo/performance/ds-compare.ts +0 -58
- package/src/demo/performance/hash-table.compare.ts +0 -40
- package/src/demo/performance/sort-compare.ts +0 -60
- package/src/helpers/createBinaryTree.ts +0 -24
- package/src/helpers/createGraph.ts +0 -24
- package/src/helpers/createGraphFromMatrix.ts +0 -47
- package/src/helpers/createLinkedList.ts +0 -24
- package/src/index.ts +0 -44
- package/src/types/ArrayMatrix.ts +0 -1
- package/src/types/EnumBinarySearchTreeType.ts +0 -4
- package/src/types/EnumGraphTraversalType.ts +0 -5
- package/src/types/EnumGraphType.ts +0 -4
- package/src/types/EnumLinkedListType.ts +0 -4
- package/src/types/EnumRandomGenerationFormat.ts +0 -4
- package/src/types/EnumSortType.ts +0 -7
- package/src/types/EnumTreeTraversalType.ts +0 -5
- package/src/types/FnCompareTwo.ts +0 -1
- package/src/types/FnSort.ts +0 -1
- package/src/types/FnToMemoize.ts +0 -1
- package/src/types/IBiDirectIterator.ts +0 -12
- package/src/types/IBinaryTree.ts +0 -13
- package/src/types/IConvertableToArray.ts +0 -4
- package/src/types/IGraph.ts +0 -16
- package/src/types/IGraphCreator.ts +0 -5
- package/src/types/IGraphIterator.ts +0 -13
- package/src/types/IIterator.ts +0 -14
- package/src/types/IKeyValueStorage.ts +0 -8
- package/src/types/ILinearStorage.ts +0 -11
- 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
|
-
}
|