@raikuxq/alg-ds 1.0.2 → 1.1.2
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/.idea/algorythmes.iml +15 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/deployment.xml +14 -0
- package/.idea/inspectionProfiles/Project_Default.xml +7 -0
- package/.idea/jsLinters/eslint.xml +6 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/README.md +12 -0
- package/lib/algotirhms.ts +35 -0
- package/lib/constants.ts +3 -0
- package/lib/data-structures.ts +23 -0
- package/lib/helpers.ts +13 -0
- package/lib/sorts.ts +7 -0
- package/lib/types.ts +53 -0
- package/{src/exports → lib}/utils.ts +2 -2
- package/package.json +4 -3
- package/src/data-structures/HashTable/HashTable.ts +202 -0
- package/src/data-structures/HashTable/HashTableNode.ts +31 -0
- package/src/demo/demo.hashtable.ts +28 -0
- package/src/demo/performance/bst-compare.ts +1 -4
- package/src/demo/performance/hash-table.compare.ts +40 -0
- package/src/index.ts +44 -40
- package/src/types/IKeyValueStorage.ts +8 -0
- package/.eslintrc.js +0 -14
- package/jest.config.js +0 -4
- package/nodemon.json +0 -6
- package/src/exports/algotirhms.ts +0 -35
- package/src/exports/constants.ts +0 -3
- package/src/exports/helpers.ts +0 -13
- package/src/exports/main.ts +0 -21
- package/src/exports/sorts.ts +0 -7
- package/src/exports/types.ts +0 -53
- package/test/unit/algorithms/binary-search.test.ts +0 -25
- package/test/unit/algorithms/factorial.test.ts +0 -43
- package/test/unit/algorithms/fibonacci.test.ts +0 -41
- package/test/unit/algorithms/sorts.test.ts +0 -74
- package/test/unit/algorithms/transpose-matrix.test.ts +0 -39
- package/test/unit/data-structures/binary-tree/binary-search-tree.test.ts +0 -230
- package/test/unit/data-structures/graph/graph.create-from-matrix.test.ts +0 -106
- package/test/unit/data-structures/graph/graph.has-path.test.ts +0 -115
- package/test/unit/data-structures/graph/graph.presenter.lists.test.ts +0 -76
- package/test/unit/data-structures/graph/graph.presenter.matrix.test.ts +0 -73
- package/test/unit/data-structures/graph/graph.shortest-path.test.ts +0 -207
- package/test/unit/data-structures/graph/graph.test.ts +0 -394
- package/test/unit/data-structures/graph/graph.transpose.test.ts +0 -52
- package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -477
- package/test/unit/data-structures/looped-array/looped-array.test.ts +0 -387
- package/test/unit/data-structures/queue/queue.test.ts +0 -147
- package/test/unit/data-structures/stack/stack.test.ts +0 -155
- package/tsconfig.json +0 -18
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
import { EnumBinarySearchTreeType } from "../../../../src/types/EnumBinarySearchTreeType";
|
|
2
|
-
import { createBinaryTree } from "../../../../src/helpers/createBinaryTree";
|
|
3
|
-
import { EnumTreeTraversalType } from "../../../../src/types/EnumTreeTraversalType";
|
|
4
|
-
|
|
5
|
-
describe.each([
|
|
6
|
-
EnumBinarySearchTreeType.BST,
|
|
7
|
-
EnumBinarySearchTreeType.RANDOMIZED_BST,
|
|
8
|
-
])("%s", (treeType: EnumBinarySearchTreeType) => {
|
|
9
|
-
describe("constructor", () => {
|
|
10
|
-
it("should create an empty instance", () => {
|
|
11
|
-
const tree = createBinaryTree(treeType);
|
|
12
|
-
|
|
13
|
-
expect(tree.length()).toBe(0);
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
describe("method insert", function () {
|
|
18
|
-
it("should have added elements to be in order", () => {
|
|
19
|
-
const tree = createBinaryTree(treeType);
|
|
20
|
-
tree.insert(5);
|
|
21
|
-
tree.insert(15);
|
|
22
|
-
tree.insert(10);
|
|
23
|
-
tree.insert(0);
|
|
24
|
-
tree.insert(20);
|
|
25
|
-
|
|
26
|
-
expect(tree.traverse(EnumTreeTraversalType.InOrder)).toEqual([
|
|
27
|
-
0,
|
|
28
|
-
5,
|
|
29
|
-
10,
|
|
30
|
-
15,
|
|
31
|
-
20,
|
|
32
|
-
]);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("method length", function () {
|
|
37
|
-
describe("when tree is non empty", () => {
|
|
38
|
-
describe("after adding", () => {
|
|
39
|
-
it("should return updated length value", () => {
|
|
40
|
-
const tree = createBinaryTree(treeType);
|
|
41
|
-
tree.insert(5);
|
|
42
|
-
tree.insert(15);
|
|
43
|
-
tree.insert(10);
|
|
44
|
-
|
|
45
|
-
expect(tree.length()).toBe(3);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe("after deleting", () => {
|
|
50
|
-
it("should return updated length value", () => {
|
|
51
|
-
const tree = createBinaryTree(treeType);
|
|
52
|
-
tree.insert(5);
|
|
53
|
-
tree.insert(15);
|
|
54
|
-
tree.insert(10);
|
|
55
|
-
tree.delete(5);
|
|
56
|
-
|
|
57
|
-
expect(tree.length()).toBe(2);
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
describe("when tree is empty", () => {
|
|
63
|
-
it("should return zero value", () => {
|
|
64
|
-
const tree = createBinaryTree(treeType);
|
|
65
|
-
|
|
66
|
-
expect(tree.length()).toBe(0);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe("method height", function () {
|
|
72
|
-
describe("when tree is non empty", () => {
|
|
73
|
-
describe("after adding", () => {
|
|
74
|
-
const tree = createBinaryTree(treeType);
|
|
75
|
-
const length = 300;
|
|
76
|
-
const arraySrc = Array.from(Array(length).keys());
|
|
77
|
-
const arrayShuffled = arraySrc
|
|
78
|
-
.map((item) => item)
|
|
79
|
-
.sort(() => Math.random() - 0.5);
|
|
80
|
-
arrayShuffled.forEach((num, index) => {
|
|
81
|
-
tree.insert(num);
|
|
82
|
-
});
|
|
83
|
-
const height = tree.height();
|
|
84
|
-
|
|
85
|
-
it("should be equal or greater than log(length)", () => {
|
|
86
|
-
const expected = Math.ceil(Math.log2(length));
|
|
87
|
-
expect(height).toBeGreaterThanOrEqual(expected);
|
|
88
|
-
});
|
|
89
|
-
it("should be equal or smaller than length", () => {
|
|
90
|
-
expect(height).toBeLessThanOrEqual(tree.length());
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe("when tree is empty", () => {
|
|
96
|
-
const tree = createBinaryTree(treeType);
|
|
97
|
-
|
|
98
|
-
it("should return zero value", () => {
|
|
99
|
-
expect(tree.height()).toBe(0);
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
describe("method has", function () {
|
|
105
|
-
const tree = createBinaryTree(treeType);
|
|
106
|
-
tree.insert(5);
|
|
107
|
-
|
|
108
|
-
it("should return true when value exists", () => {
|
|
109
|
-
expect(tree.has(5)).toBe(true);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("should return false when value does not exist", () => {
|
|
113
|
-
expect(tree.has(10)).toBe(false);
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe("method delete", function () {
|
|
118
|
-
const tree = createBinaryTree(treeType);
|
|
119
|
-
tree.insert(5);
|
|
120
|
-
tree.insert(12);
|
|
121
|
-
tree.insert(7);
|
|
122
|
-
tree.insert(20);
|
|
123
|
-
|
|
124
|
-
tree.delete(12);
|
|
125
|
-
|
|
126
|
-
it("should delete element from the tree", () => {
|
|
127
|
-
expect(tree.has(12)).toBe(false);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it("should restructure the tree correctly", () => {
|
|
131
|
-
expect(tree.traverse(EnumTreeTraversalType.InOrder)).toEqual([5, 7, 20]);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
describe("method max", function () {
|
|
136
|
-
const tree = createBinaryTree(treeType);
|
|
137
|
-
tree.insert(5);
|
|
138
|
-
tree.insert(12);
|
|
139
|
-
tree.insert(7);
|
|
140
|
-
tree.insert(20);
|
|
141
|
-
|
|
142
|
-
it("should be in order", () => {
|
|
143
|
-
expect(tree.max()).toBe(20);
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
describe("method min", function () {
|
|
148
|
-
const tree = createBinaryTree(treeType);
|
|
149
|
-
tree.insert(5);
|
|
150
|
-
tree.insert(12);
|
|
151
|
-
tree.insert(7);
|
|
152
|
-
tree.insert(20);
|
|
153
|
-
tree.insert(2);
|
|
154
|
-
|
|
155
|
-
it("should be in order", () => {
|
|
156
|
-
expect(tree.min()).toBe(2);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
if (treeType === EnumBinarySearchTreeType.BST) {
|
|
161
|
-
describe("method subtree", function () {
|
|
162
|
-
const bst = createBinaryTree(treeType);
|
|
163
|
-
bst.insert(22);
|
|
164
|
-
bst.insert(4);
|
|
165
|
-
bst.insert(16);
|
|
166
|
-
bst.insert(19);
|
|
167
|
-
bst.insert(15);
|
|
168
|
-
bst.insert(8);
|
|
169
|
-
bst.insert(23);
|
|
170
|
-
const subtree = bst.subtree(16);
|
|
171
|
-
|
|
172
|
-
it("should correctly create subtree", () => {
|
|
173
|
-
expect(subtree.traverse(EnumTreeTraversalType.InOrder)).toEqual([
|
|
174
|
-
8,
|
|
175
|
-
15,
|
|
176
|
-
16,
|
|
177
|
-
19,
|
|
178
|
-
]);
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
describe("method traverse", function () {
|
|
183
|
-
const bst = createBinaryTree(treeType);
|
|
184
|
-
|
|
185
|
-
bst.insert(22);
|
|
186
|
-
bst.insert(4);
|
|
187
|
-
bst.insert(16);
|
|
188
|
-
bst.insert(19);
|
|
189
|
-
bst.insert(15);
|
|
190
|
-
bst.insert(8);
|
|
191
|
-
bst.insert(23);
|
|
192
|
-
|
|
193
|
-
it("should correctly convert in-order type", () => {
|
|
194
|
-
expect(bst.traverse(EnumTreeTraversalType.InOrder)).toEqual([
|
|
195
|
-
4,
|
|
196
|
-
8,
|
|
197
|
-
15,
|
|
198
|
-
16,
|
|
199
|
-
19,
|
|
200
|
-
22,
|
|
201
|
-
23,
|
|
202
|
-
]);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it("should correctly convert pre-order type", () => {
|
|
206
|
-
expect(bst.traverse(EnumTreeTraversalType.PreOrder)).toEqual([
|
|
207
|
-
22,
|
|
208
|
-
4,
|
|
209
|
-
8,
|
|
210
|
-
15,
|
|
211
|
-
16,
|
|
212
|
-
19,
|
|
213
|
-
23,
|
|
214
|
-
]);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("should correctly convert post-order type", () => {
|
|
218
|
-
expect(bst.traverse(EnumTreeTraversalType.PostOrder)).toEqual([
|
|
219
|
-
4,
|
|
220
|
-
8,
|
|
221
|
-
15,
|
|
222
|
-
16,
|
|
223
|
-
19,
|
|
224
|
-
23,
|
|
225
|
-
22,
|
|
226
|
-
]);
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
});
|
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { EnumGraphType } from "../../../../src/types/EnumGraphType";
|
|
2
|
-
import { createGraphFromMatrix } from "../../../../src/helpers/createGraphFromMatrix";
|
|
3
|
-
import { ArrayMatrix } from "../../../../src/types/ArrayMatrix";
|
|
4
|
-
import IGraph from "../../../../src/types/IGraph";
|
|
5
|
-
|
|
6
|
-
describe("in Directed graph", () => {
|
|
7
|
-
/**
|
|
8
|
-
* Get graph adjacency matrix N x N
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
*
|
|
12
|
-
* Directed graph:
|
|
13
|
-
* - Bob [Maria]
|
|
14
|
-
* - Maria [Bob, John]
|
|
15
|
-
* - John []
|
|
16
|
-
*
|
|
17
|
-
* Its matrix:
|
|
18
|
-
* | Bob | Maria | John |
|
|
19
|
-
* Bob | 0 | 1 | 0 |
|
|
20
|
-
* Maria | 1 | 0 | 1 |
|
|
21
|
-
* John | 0 | 0 | 0 |
|
|
22
|
-
*/
|
|
23
|
-
describe("created graph", () => {
|
|
24
|
-
const fieldsList: Array<string> = ["Bob", "Maria", "John"];
|
|
25
|
-
|
|
26
|
-
const matrix: ArrayMatrix = [
|
|
27
|
-
[0, 1, 0],
|
|
28
|
-
[1, 0, 1],
|
|
29
|
-
[0, 0, 0],
|
|
30
|
-
];
|
|
31
|
-
const graph: IGraph<string> = createGraphFromMatrix<string>(
|
|
32
|
-
matrix,
|
|
33
|
-
fieldsList,
|
|
34
|
-
EnumGraphType.Directed
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
it("should contain all vertices", () => {
|
|
38
|
-
expect(graph.vertices()).toEqual(fieldsList);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should contain all edges", () => {
|
|
42
|
-
expect(graph.hasEdge("Bob", "Bob")).toBe(false);
|
|
43
|
-
expect(graph.hasEdge("Bob", "Maria")).toBe(true);
|
|
44
|
-
expect(graph.hasEdge("Bob", "John")).toBe(false);
|
|
45
|
-
|
|
46
|
-
expect(graph.hasEdge("Maria", "Maria")).toBe(false);
|
|
47
|
-
expect(graph.hasEdge("Maria", "Bob")).toBe(true);
|
|
48
|
-
expect(graph.hasEdge("Maria", "John")).toBe(true);
|
|
49
|
-
|
|
50
|
-
expect(graph.hasEdge("John", "John")).toBe(false);
|
|
51
|
-
expect(graph.hasEdge("John", "Bob")).toBe(false);
|
|
52
|
-
expect(graph.hasEdge("John", "Maria")).toBe(false);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe("in Undirected graph", () => {
|
|
58
|
-
/**
|
|
59
|
-
* Get graph adjacency matrix N x N
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
*
|
|
63
|
-
* Directed graph:
|
|
64
|
-
* - Bob [Maria]
|
|
65
|
-
* - Maria [Bob, John]
|
|
66
|
-
* - John []
|
|
67
|
-
*
|
|
68
|
-
* Its matrix:
|
|
69
|
-
* | Bob | Maria | John |
|
|
70
|
-
* Bob | 0 | 1 | 0 |
|
|
71
|
-
* Maria | 1 | 0 | 1 |
|
|
72
|
-
* John | 0 | 1 | 0 |
|
|
73
|
-
*/
|
|
74
|
-
describe("created graph", () => {
|
|
75
|
-
const fieldsList: Array<string> = ["Bob", "Maria", "John"];
|
|
76
|
-
|
|
77
|
-
const matrix: ArrayMatrix = [
|
|
78
|
-
[0, 1, 0],
|
|
79
|
-
[1, 0, 1],
|
|
80
|
-
[0, 1, 0],
|
|
81
|
-
];
|
|
82
|
-
const graph: IGraph<string> = createGraphFromMatrix<string>(
|
|
83
|
-
matrix,
|
|
84
|
-
fieldsList,
|
|
85
|
-
EnumGraphType.Undirected
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
it("should contain all vertices", () => {
|
|
89
|
-
expect(graph.vertices()).toEqual(fieldsList);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it("should contain all edges", () => {
|
|
93
|
-
expect(graph.hasEdge("Bob", "Bob")).toBe(false);
|
|
94
|
-
expect(graph.hasEdge("Bob", "Maria")).toBe(true);
|
|
95
|
-
expect(graph.hasEdge("Bob", "John")).toBe(false);
|
|
96
|
-
|
|
97
|
-
expect(graph.hasEdge("Maria", "Maria")).toBe(false);
|
|
98
|
-
expect(graph.hasEdge("Maria", "Bob")).toBe(true);
|
|
99
|
-
expect(graph.hasEdge("Maria", "John")).toBe(true);
|
|
100
|
-
|
|
101
|
-
expect(graph.hasEdge("John", "John")).toBe(false);
|
|
102
|
-
expect(graph.hasEdge("John", "Bob")).toBe(false);
|
|
103
|
-
expect(graph.hasEdge("John", "Maria")).toBe(true);
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
});
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import AbstractGraph from "../../../../src/data-structures/Graph/AbstractGraph";
|
|
2
|
-
import UndirectedGraph from "../../../../src/data-structures/Graph/UndirectedGraph";
|
|
3
|
-
import DirectedGraph from "../../../../src/data-structures/Graph/DirectedGraph";
|
|
4
|
-
import IGraphIterationStrategy from "../../../../src/types/IGraphIterationStrategy";
|
|
5
|
-
import BFSIterationStrategy from "../../../../src/data-structures/Graph/strategy/BFSIterationStrategy";
|
|
6
|
-
import DFSIterationStrategy from "../../../../src/data-structures/Graph/strategy/DFSIterationStrategy";
|
|
7
|
-
|
|
8
|
-
import { hasPath } from "../../../../src/data-structures/Graph/searching/hasPath";
|
|
9
|
-
import { shortestPath } from "../../../../src/data-structures/Graph/searching/shortestPath";
|
|
10
|
-
import { EnumGraphTraversalType } from "../../../../src/types/EnumGraphTraversalType";
|
|
11
|
-
|
|
12
|
-
describe("Has Path Algorithm", () => {
|
|
13
|
-
const strategy: IGraphIterationStrategy<string> = new BFSIterationStrategy();
|
|
14
|
-
|
|
15
|
-
describe("Any type of traversal in empty graph", () => {
|
|
16
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
17
|
-
|
|
18
|
-
it("should throw when graph is empty", () => {
|
|
19
|
-
expect(() => {
|
|
20
|
-
shortestPath(graph, "-", "-", strategy);
|
|
21
|
-
}).toThrowError();
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("Any type of traversal in non empty graph", () => {
|
|
26
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
27
|
-
|
|
28
|
-
graph.addVertex("Mike").addVertex("Bob").addEdge("Mike", "Bob");
|
|
29
|
-
|
|
30
|
-
it("should throw when first node does not exist", () => {
|
|
31
|
-
expect(() => {
|
|
32
|
-
shortestPath(graph, "Mike", "NOT_EXISTED_NODE", strategy);
|
|
33
|
-
}).toThrowError();
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it("should throw when second node does not exist", () => {
|
|
37
|
-
expect(() => {
|
|
38
|
-
shortestPath(graph, "NOT_EXISTED_NODE", "Bob", strategy);
|
|
39
|
-
}).toThrowError();
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
describe.each([EnumGraphTraversalType.BFS, EnumGraphTraversalType.DFS])(
|
|
44
|
-
"%s",
|
|
45
|
-
(strategyType: EnumGraphTraversalType) => {
|
|
46
|
-
let strategy: IGraphIterationStrategy<string>;
|
|
47
|
-
|
|
48
|
-
switch (strategyType) {
|
|
49
|
-
case EnumGraphTraversalType.BFS:
|
|
50
|
-
strategy = new BFSIterationStrategy();
|
|
51
|
-
break;
|
|
52
|
-
case EnumGraphTraversalType.DFS:
|
|
53
|
-
strategy = new DFSIterationStrategy();
|
|
54
|
-
break;
|
|
55
|
-
default:
|
|
56
|
-
throw new Error("Invalid search method");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
describe("in undirected graph", () => {
|
|
60
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
61
|
-
|
|
62
|
-
graph
|
|
63
|
-
.addVertex("Mike")
|
|
64
|
-
.addVertex("Bob")
|
|
65
|
-
.addVertex("Lisa")
|
|
66
|
-
.addVertex("Aaron")
|
|
67
|
-
.addVertex("James")
|
|
68
|
-
.addVertex("Anna")
|
|
69
|
-
.addVertex("John")
|
|
70
|
-
.addEdge("Mike", "Bob")
|
|
71
|
-
.addEdge("Mike", "Lisa")
|
|
72
|
-
.addEdge("Lisa", "James")
|
|
73
|
-
.addEdge("Lisa", "Aaron")
|
|
74
|
-
.addEdge("James", "Aaron")
|
|
75
|
-
.addEdge("James", "Anna");
|
|
76
|
-
|
|
77
|
-
it("should find element if it has an edge", () => {
|
|
78
|
-
expect(hasPath(graph, "Mike", "Anna", strategy)).toBe(true);
|
|
79
|
-
});
|
|
80
|
-
it("should not find element if it has not an edge", () => {
|
|
81
|
-
expect(hasPath(graph, "Mike", "John", strategy)).toBe(false);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
describe("in directed graph", () => {
|
|
86
|
-
const graph: AbstractGraph<string> = new DirectedGraph();
|
|
87
|
-
|
|
88
|
-
graph
|
|
89
|
-
.addVertex("Mike")
|
|
90
|
-
.addVertex("Bob")
|
|
91
|
-
.addVertex("Lisa")
|
|
92
|
-
.addVertex("Aaron")
|
|
93
|
-
.addVertex("James")
|
|
94
|
-
.addVertex("Anna")
|
|
95
|
-
.addVertex("John")
|
|
96
|
-
.addEdge("Mike", "Bob")
|
|
97
|
-
.addEdge("Mike", "Lisa")
|
|
98
|
-
.addEdge("Lisa", "James")
|
|
99
|
-
.addEdge("Lisa", "Aaron")
|
|
100
|
-
.addEdge("James", "Aaron")
|
|
101
|
-
.addEdge("James", "Anna");
|
|
102
|
-
|
|
103
|
-
it("should find element if it has an edge", () => {
|
|
104
|
-
expect(hasPath(graph, "Mike", "Anna", strategy)).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
it("should not find element if it has not an edge", () => {
|
|
107
|
-
expect(hasPath(graph, "Mike", "John", strategy)).toBe(false);
|
|
108
|
-
});
|
|
109
|
-
it("should not find element if and edge has wrong direction", () => {
|
|
110
|
-
expect(hasPath(graph, "Lisa", "Mike", strategy)).toBe(false);
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
);
|
|
115
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import IGraph from "../../../../src/types/IGraph";
|
|
2
|
-
import UndirectedGraph from "../../../../src/data-structures/Graph/UndirectedGraph";
|
|
3
|
-
import DirectedGraph from "../../../../src/data-structures/Graph/DirectedGraph";
|
|
4
|
-
import { createGraph } from "../../../../src/helpers/createGraph";
|
|
5
|
-
import { EnumGraphType } from "../../../../src/types/EnumGraphType";
|
|
6
|
-
import { presenterAdjacencyLists } from "../../../../src/data-structures/Graph/presenter/presenterAdjacencyLists";
|
|
7
|
-
|
|
8
|
-
describe.each([EnumGraphType.Directed, EnumGraphType.Undirected])(
|
|
9
|
-
"%s graph",
|
|
10
|
-
(graphType: EnumGraphType) => {
|
|
11
|
-
describe("in empty graph", () => {
|
|
12
|
-
const graph: IGraph<number> = createGraph(graphType);
|
|
13
|
-
|
|
14
|
-
it("should return empty list", () => {
|
|
15
|
-
const map = presenterAdjacencyLists(graph);
|
|
16
|
-
const emptyMap = new Map<number, number>();
|
|
17
|
-
expect(map).toEqual(emptyMap);
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
22
|
-
describe("in non-empty graph", () => {
|
|
23
|
-
const graph: IGraph<number> = new UndirectedGraph();
|
|
24
|
-
graph
|
|
25
|
-
.addVertex(1)
|
|
26
|
-
.addVertex(2)
|
|
27
|
-
.addVertex(3)
|
|
28
|
-
.addVertex(4)
|
|
29
|
-
.addEdge(1, 2)
|
|
30
|
-
.addEdge(1, 3)
|
|
31
|
-
.addEdge(3, 4);
|
|
32
|
-
|
|
33
|
-
it("should return correct list", () => {
|
|
34
|
-
const list = presenterAdjacencyLists(graph);
|
|
35
|
-
const expectedList = new Map<number, Array<number>>();
|
|
36
|
-
|
|
37
|
-
// eslint-disable-next-line
|
|
38
|
-
expectedList
|
|
39
|
-
.set(1, [2, 3])
|
|
40
|
-
.set(2, [1])
|
|
41
|
-
.set(3, [1, 4])
|
|
42
|
-
.set(4, [3]);
|
|
43
|
-
|
|
44
|
-
expect(list).toEqual(expectedList);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
if (graphType === EnumGraphType.Directed) {
|
|
49
|
-
describe("in non-empty graph", () => {
|
|
50
|
-
const graph: IGraph<number> = new DirectedGraph();
|
|
51
|
-
graph
|
|
52
|
-
.addVertex(1)
|
|
53
|
-
.addVertex(2)
|
|
54
|
-
.addVertex(3)
|
|
55
|
-
.addVertex(4)
|
|
56
|
-
.addEdge(1, 2)
|
|
57
|
-
.addEdge(1, 3)
|
|
58
|
-
.addEdge(3, 4);
|
|
59
|
-
|
|
60
|
-
it("should return correct list", () => {
|
|
61
|
-
const list = presenterAdjacencyLists(graph);
|
|
62
|
-
const expectedList = new Map<number, Array<number>>();
|
|
63
|
-
|
|
64
|
-
// eslint-disable-next-line
|
|
65
|
-
expectedList
|
|
66
|
-
.set(1, [2, 3])
|
|
67
|
-
.set(2, [])
|
|
68
|
-
.set(3, [4])
|
|
69
|
-
.set(4, []);
|
|
70
|
-
|
|
71
|
-
expect(list).toEqual(expectedList);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
);
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import IGraph from "../../../../src/types/IGraph";
|
|
2
|
-
import UndirectedGraph from "../../../../src/data-structures/Graph/UndirectedGraph";
|
|
3
|
-
import DirectedGraph from "../../../../src/data-structures/Graph/DirectedGraph";
|
|
4
|
-
import { createGraph } from "../../../../src/helpers/createGraph";
|
|
5
|
-
import { EnumGraphType } from "../../../../src/types/EnumGraphType";
|
|
6
|
-
import { presenterAdjacencyMatrix } from "../../../../src/data-structures/Graph/presenter/presenterAdjacencyMatrix";
|
|
7
|
-
|
|
8
|
-
describe("Graph Presenter Matrix", () => {
|
|
9
|
-
describe.each([EnumGraphType.Directed, EnumGraphType.Undirected])(
|
|
10
|
-
"%s graph",
|
|
11
|
-
(graphType: EnumGraphType) => {
|
|
12
|
-
describe("in empty graph", () => {
|
|
13
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
14
|
-
|
|
15
|
-
it("should return empty list", () => {
|
|
16
|
-
const matrix = presenterAdjacencyMatrix(graph);
|
|
17
|
-
|
|
18
|
-
expect(matrix).toEqual([]);
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
23
|
-
describe("in non-empty graph", () => {
|
|
24
|
-
const graph: IGraph<number> = new UndirectedGraph();
|
|
25
|
-
graph
|
|
26
|
-
.addVertex(1)
|
|
27
|
-
.addVertex(2)
|
|
28
|
-
.addVertex(3)
|
|
29
|
-
.addVertex(4)
|
|
30
|
-
.addEdge(1, 2)
|
|
31
|
-
.addEdge(1, 3)
|
|
32
|
-
.addEdge(3, 4);
|
|
33
|
-
|
|
34
|
-
it("should return correct matrix", () => {
|
|
35
|
-
const matrix = presenterAdjacencyMatrix(graph);
|
|
36
|
-
|
|
37
|
-
expect(matrix).toEqual([
|
|
38
|
-
[0, 1, 1, 0],
|
|
39
|
-
[1, 0, 0, 0],
|
|
40
|
-
[1, 0, 0, 1],
|
|
41
|
-
[0, 0, 1, 0],
|
|
42
|
-
]);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (graphType === EnumGraphType.Directed) {
|
|
48
|
-
describe("in non-empty graph", () => {
|
|
49
|
-
const graph: IGraph<number> = new DirectedGraph();
|
|
50
|
-
graph
|
|
51
|
-
.addVertex(1)
|
|
52
|
-
.addVertex(2)
|
|
53
|
-
.addVertex(3)
|
|
54
|
-
.addVertex(4)
|
|
55
|
-
.addEdge(1, 2)
|
|
56
|
-
.addEdge(1, 3)
|
|
57
|
-
.addEdge(3, 4);
|
|
58
|
-
|
|
59
|
-
it("should return correct matrix", () => {
|
|
60
|
-
const matrix = presenterAdjacencyMatrix(graph);
|
|
61
|
-
|
|
62
|
-
expect(matrix).toEqual([
|
|
63
|
-
[0, 1, 1, 0],
|
|
64
|
-
[0, 0, 0, 0],
|
|
65
|
-
[0, 0, 0, 1],
|
|
66
|
-
[0, 0, 0, 0],
|
|
67
|
-
]);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
);
|
|
73
|
-
});
|