@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,207 +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 DijkstraIterationStrategy from "../../../../src/data-structures/Graph/strategy/DijkstraIterationStrategy";
|
|
7
|
-
|
|
8
|
-
import { shortestPath } from "../../../../src/data-structures/Graph/searching/shortestPath";
|
|
9
|
-
import { EnumGraphTraversalType } from "../../../../src/types/EnumGraphTraversalType";
|
|
10
|
-
|
|
11
|
-
describe("Shortest path algorithm", () => {
|
|
12
|
-
const strategy: IGraphIterationStrategy<string> = new BFSIterationStrategy();
|
|
13
|
-
|
|
14
|
-
describe("Any type of traversal in empty graph", () => {
|
|
15
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
16
|
-
|
|
17
|
-
it("should throw when graph is empty", () => {
|
|
18
|
-
expect(() => {
|
|
19
|
-
shortestPath(graph, "-", "-", strategy);
|
|
20
|
-
}).toThrowError();
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe("Any type of traversal in non empty graph", () => {
|
|
25
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
26
|
-
|
|
27
|
-
graph.addVertex("Mike").addVertex("Bob").addEdge("Mike", "Bob");
|
|
28
|
-
|
|
29
|
-
it("should throw when first node does not exist", () => {
|
|
30
|
-
expect(() => {
|
|
31
|
-
shortestPath(graph, "Mike", "NOT_EXISTED_NODE", strategy);
|
|
32
|
-
}).toThrowError();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("should throw when second node does not exist", () => {
|
|
36
|
-
expect(() => {
|
|
37
|
-
shortestPath(graph, "NOT_EXISTED_NODE", "Bob", strategy);
|
|
38
|
-
}).toThrowError();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should find correct path between neighbor nodes", () => {
|
|
42
|
-
expect(shortestPath(graph, "Mike", "Bob", strategy)).toEqual([
|
|
43
|
-
"Mike",
|
|
44
|
-
"Bob",
|
|
45
|
-
]);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
describe.each([EnumGraphTraversalType.DIJKSTRA])(
|
|
50
|
-
"Weighted graph by %s",
|
|
51
|
-
(strategyType: EnumGraphTraversalType) => {
|
|
52
|
-
let strategy: IGraphIterationStrategy<string>;
|
|
53
|
-
|
|
54
|
-
switch (strategyType) {
|
|
55
|
-
case EnumGraphTraversalType.DIJKSTRA: {
|
|
56
|
-
strategy = new DijkstraIterationStrategy();
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
default: {
|
|
60
|
-
throw new Error("Invalid search method");
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
describe("in undirected graph", () => {
|
|
65
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
66
|
-
|
|
67
|
-
graph
|
|
68
|
-
.addVertex("Mike")
|
|
69
|
-
.addVertex("Bob")
|
|
70
|
-
.addVertex("Lisa")
|
|
71
|
-
.addVertex("Aaron")
|
|
72
|
-
.addVertex("James")
|
|
73
|
-
.addVertex("Anna")
|
|
74
|
-
.addEdge("Mike", "Bob", 5)
|
|
75
|
-
.addEdge("Mike", "Lisa", 3)
|
|
76
|
-
.addEdge("Lisa", "Aaron", 3)
|
|
77
|
-
.addEdge("Lisa", "James", 3)
|
|
78
|
-
.addEdge("James", "Aaron", 3)
|
|
79
|
-
.addEdge("James", "Anna", 6)
|
|
80
|
-
.addEdge("Bob", "Anna", 15);
|
|
81
|
-
|
|
82
|
-
it("should find correct path between multiple nodes", () => {
|
|
83
|
-
/**
|
|
84
|
-
* Bob -> Mike (5) -> Anna (15) = 20
|
|
85
|
-
* Mike -> Lisa (3) -> James (3) -> Anna (6) = 12
|
|
86
|
-
*/
|
|
87
|
-
expect(shortestPath(graph, "Mike", "Anna", strategy)).toEqual([
|
|
88
|
-
"Mike",
|
|
89
|
-
"Lisa",
|
|
90
|
-
"James",
|
|
91
|
-
"Anna",
|
|
92
|
-
]);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
describe("in directed graph", () => {
|
|
97
|
-
const graph: AbstractGraph<string> = new DirectedGraph();
|
|
98
|
-
|
|
99
|
-
graph
|
|
100
|
-
.addVertex("Mike")
|
|
101
|
-
.addVertex("Bob")
|
|
102
|
-
.addVertex("Lisa")
|
|
103
|
-
.addVertex("Aaron")
|
|
104
|
-
.addVertex("James")
|
|
105
|
-
.addVertex("Anna")
|
|
106
|
-
.addEdge("Mike", "Bob", 5)
|
|
107
|
-
.addEdge("Mike", "Lisa", 3)
|
|
108
|
-
.addEdge("Lisa", "Aaron", 3)
|
|
109
|
-
.addEdge("Aaron", "James", 3)
|
|
110
|
-
.addEdge("James", "Lisa", 10)
|
|
111
|
-
.addEdge("James", "Aaron", 10)
|
|
112
|
-
.addEdge("James", "Anna", 6)
|
|
113
|
-
.addEdge("Bob", "Anna", 15);
|
|
114
|
-
|
|
115
|
-
it("should find correct path between multiple nodes", () => {
|
|
116
|
-
/**
|
|
117
|
-
* Bob -> Mike (5) -> Anna (15) = 20
|
|
118
|
-
* Mike -> Lisa (3) -> Aaron (3) -> James (3) -> Anna (6) = 15
|
|
119
|
-
*/
|
|
120
|
-
expect(shortestPath(graph, "Mike", "Anna", strategy)).toEqual([
|
|
121
|
-
"Mike",
|
|
122
|
-
"Lisa",
|
|
123
|
-
"Aaron",
|
|
124
|
-
"James",
|
|
125
|
-
"Anna",
|
|
126
|
-
]);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
);
|
|
131
|
-
|
|
132
|
-
describe.each([EnumGraphTraversalType.BFS])(
|
|
133
|
-
"Unweighted graph by %s",
|
|
134
|
-
(strategyType: EnumGraphTraversalType) => {
|
|
135
|
-
let strategy: IGraphIterationStrategy<string>;
|
|
136
|
-
|
|
137
|
-
switch (strategyType) {
|
|
138
|
-
case EnumGraphTraversalType.BFS: {
|
|
139
|
-
strategy = new BFSIterationStrategy();
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
default: {
|
|
143
|
-
throw new Error("Invalid search method");
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
describe("in undirected graph", () => {
|
|
148
|
-
const graph: AbstractGraph<string> = new UndirectedGraph();
|
|
149
|
-
|
|
150
|
-
graph
|
|
151
|
-
.addVertex("Mike")
|
|
152
|
-
.addVertex("Bob")
|
|
153
|
-
.addVertex("Lisa")
|
|
154
|
-
.addVertex("Aaron")
|
|
155
|
-
.addVertex("James")
|
|
156
|
-
.addVertex("Anna")
|
|
157
|
-
.addEdge("Mike", "Bob")
|
|
158
|
-
.addEdge("Mike", "Aaron")
|
|
159
|
-
.addEdge("Mike", "Lisa")
|
|
160
|
-
.addEdge("Lisa", "James")
|
|
161
|
-
.addEdge("Lisa", "Aaron")
|
|
162
|
-
.addEdge("James", "Aaron")
|
|
163
|
-
.addEdge("James", "Anna")
|
|
164
|
-
.addEdge("Aaron", "Anna");
|
|
165
|
-
|
|
166
|
-
it("should find correct path between multiple nodes", () => {
|
|
167
|
-
/**
|
|
168
|
-
* Mike -> Aaron -> Lisa -> James -> Anna
|
|
169
|
-
* Mike -> Aaron -> Anna
|
|
170
|
-
* Mike -> Lisa -> James -> Anna
|
|
171
|
-
*/
|
|
172
|
-
expect(shortestPath(graph, "Mike", "Anna", strategy)).toEqual([
|
|
173
|
-
"Mike",
|
|
174
|
-
"Aaron",
|
|
175
|
-
"Anna",
|
|
176
|
-
]);
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
describe("in directed graph", () => {
|
|
181
|
-
const graph: AbstractGraph<string> = new DirectedGraph();
|
|
182
|
-
|
|
183
|
-
graph
|
|
184
|
-
.addVertex("Mike")
|
|
185
|
-
.addVertex("Bob")
|
|
186
|
-
.addVertex("Lisa")
|
|
187
|
-
.addVertex("Aaron")
|
|
188
|
-
.addVertex("James")
|
|
189
|
-
.addVertex("Anna")
|
|
190
|
-
.addEdge("Mike", "Bob")
|
|
191
|
-
.addEdge("Mike", "Lisa")
|
|
192
|
-
.addEdge("Lisa", "James")
|
|
193
|
-
.addEdge("James", "Aaron")
|
|
194
|
-
.addEdge("Aaron", "Anna")
|
|
195
|
-
.addEdge("Lisa", "Anna");
|
|
196
|
-
|
|
197
|
-
it("should find correct path between multiple nodes", () => {
|
|
198
|
-
expect(shortestPath(graph, "Mike", "Anna", strategy)).toEqual([
|
|
199
|
-
"Mike",
|
|
200
|
-
"Lisa",
|
|
201
|
-
"Anna",
|
|
202
|
-
]);
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
);
|
|
207
|
-
});
|
|
@@ -1,394 +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
|
-
|
|
7
|
-
describe.each([EnumGraphType.Directed, EnumGraphType.Undirected])(
|
|
8
|
-
"%s graph",
|
|
9
|
-
(graphType: EnumGraphType) => {
|
|
10
|
-
describe("method weight", () => {
|
|
11
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
12
|
-
|
|
13
|
-
graph
|
|
14
|
-
.addVertex("Mike")
|
|
15
|
-
.addVertex("Bob")
|
|
16
|
-
.addVertex("Lisa")
|
|
17
|
-
.addEdge("Mike", "Bob", 5)
|
|
18
|
-
.addEdge("Bob", "Lisa", 10)
|
|
19
|
-
.addEdge("Mike", "Lisa", 20);
|
|
20
|
-
|
|
21
|
-
it("should return correct weight value", () => {
|
|
22
|
-
const weight = graph.weight();
|
|
23
|
-
const expectedWeight = 35;
|
|
24
|
-
|
|
25
|
-
expect(weight).toBe(expectedWeight);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
describe("method verticesCount", () => {
|
|
30
|
-
describe("in empty graph", () => {
|
|
31
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
32
|
-
|
|
33
|
-
it("should return correct vertices count", () => {
|
|
34
|
-
expect(graph.verticesCount()).toBe(0);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
describe("in non empty graph", () => {
|
|
38
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
39
|
-
graph.addVertex("Mike").addVertex("Bob").addVertex("Lisa");
|
|
40
|
-
|
|
41
|
-
it("should return correct vertices count", () => {
|
|
42
|
-
expect(graph.verticesCount()).toBe(3);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
describe("method edgesCount", () => {
|
|
48
|
-
describe("in empty graph", () => {
|
|
49
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
50
|
-
|
|
51
|
-
it("should return correct vertices count", () => {
|
|
52
|
-
expect(graph.edgesCount()).toBe(0);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
57
|
-
describe("in non-empty graph", () => {
|
|
58
|
-
const graph: IGraph<string> = new UndirectedGraph();
|
|
59
|
-
graph
|
|
60
|
-
.addVertex("Mike")
|
|
61
|
-
.addVertex("Bob")
|
|
62
|
-
.addVertex("Lisa")
|
|
63
|
-
.addEdge("Mike", "Bob")
|
|
64
|
-
.addEdge("Mike", "Lisa")
|
|
65
|
-
.addEdge("Bob", "Lisa");
|
|
66
|
-
|
|
67
|
-
it("should return correct vertices count", () => {
|
|
68
|
-
expect(graph.edgesCount()).toBe(3);
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (graphType === EnumGraphType.Directed) {
|
|
74
|
-
describe("in non-empty graph", () => {
|
|
75
|
-
const graph: IGraph<string> = new DirectedGraph();
|
|
76
|
-
graph
|
|
77
|
-
.addVertex("Mike")
|
|
78
|
-
.addVertex("Bob")
|
|
79
|
-
.addVertex("Lisa")
|
|
80
|
-
.addEdge("Mike", "Bob")
|
|
81
|
-
.addEdge("Mike", "Lisa")
|
|
82
|
-
.addEdge("Lisa", "Mike");
|
|
83
|
-
|
|
84
|
-
it("should return correct vertices count", () => {
|
|
85
|
-
expect(graph.edgesCount()).toBe(3);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
describe("method vertices", () => {
|
|
92
|
-
describe("in empty graph", () => {
|
|
93
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
94
|
-
|
|
95
|
-
it("should return empty array of vertices", () => {
|
|
96
|
-
expect(graph.vertices()).toEqual([]);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
describe("in non empty graph", () => {
|
|
100
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
101
|
-
graph.addVertex("Mike").addVertex("Bob").addVertex("Lisa");
|
|
102
|
-
|
|
103
|
-
it("should return correct array of vertices", () => {
|
|
104
|
-
expect(graph.vertices()).toEqual(["Mike", "Bob", "Lisa"]);
|
|
105
|
-
});
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
describe("method addVertex", () => {
|
|
110
|
-
it("should correct add vertex", () => {
|
|
111
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
112
|
-
graph.addVertex("Mike").addVertex("Bob").addVertex("Lisa");
|
|
113
|
-
|
|
114
|
-
const vertices = graph.vertices();
|
|
115
|
-
const expectedVertices = ["Mike", "Bob", "Lisa"];
|
|
116
|
-
|
|
117
|
-
expect(vertices).toEqual(expectedVertices);
|
|
118
|
-
});
|
|
119
|
-
it("should throw when try to add an existed vertex", () => {
|
|
120
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
121
|
-
graph.addVertex("Mike");
|
|
122
|
-
|
|
123
|
-
expect(() => {
|
|
124
|
-
graph.addVertex("Mike");
|
|
125
|
-
}).toThrowError();
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
describe("method addEdge", () => {
|
|
130
|
-
describe("should throw when try to add edge between not existed vertices", () => {
|
|
131
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
132
|
-
graph.addVertex("Mike").addVertex("Bob");
|
|
133
|
-
|
|
134
|
-
it("when first node does not exist", () => {
|
|
135
|
-
expect(() => {
|
|
136
|
-
graph.addEdge("NOT_EXISTED_NODE", "Bob");
|
|
137
|
-
}).toThrowError();
|
|
138
|
-
});
|
|
139
|
-
it("when second node does not exist", () => {
|
|
140
|
-
expect(() => {
|
|
141
|
-
graph.addEdge("Mike", "NOT_EXISTED_NODE");
|
|
142
|
-
}).toThrowError();
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
147
|
-
describe("should correct add edge between two existed vertices", () => {
|
|
148
|
-
const graph: IGraph<string> = new UndirectedGraph();
|
|
149
|
-
graph.addVertex("Mike").addVertex("Bob").addEdge("Mike", "Bob");
|
|
150
|
-
|
|
151
|
-
it("should add second node to first node neighbors", () => {
|
|
152
|
-
expect(graph.getVertexNeighbors("Mike")).toContain("Bob");
|
|
153
|
-
});
|
|
154
|
-
it("should add first node to second node neighbors", () => {
|
|
155
|
-
expect(graph.getVertexNeighbors("Bob")).toContain("Mike");
|
|
156
|
-
});
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
describe("should override an existed edge and its weight", () => {
|
|
160
|
-
const graph: IGraph<string> = new UndirectedGraph();
|
|
161
|
-
graph
|
|
162
|
-
.addVertex("Mike")
|
|
163
|
-
.addVertex("Bob")
|
|
164
|
-
.addEdge("Mike", "Bob", 5)
|
|
165
|
-
.addEdge("Mike", "Bob", 10);
|
|
166
|
-
|
|
167
|
-
describe("should not multiple edges", () => {
|
|
168
|
-
it("should not add vertex to first edge", () => {
|
|
169
|
-
expect(graph.getVertexNeighbors("Mike")).toEqual(["Bob"]);
|
|
170
|
-
});
|
|
171
|
-
it("should not add vertex to second edge", () => {
|
|
172
|
-
expect(graph.getVertexNeighbors("Bob")).toEqual(["Mike"]);
|
|
173
|
-
});
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("should override edge weight", () => {
|
|
177
|
-
expect(graph.getEdgeWeight("Mike", "Bob")).toBe(10);
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (graphType === EnumGraphType.Directed) {
|
|
183
|
-
describe("should correct add edge between two existed vertices", () => {
|
|
184
|
-
const graph: IGraph<string> = new DirectedGraph();
|
|
185
|
-
graph.addVertex("Mike").addVertex("Bob").addEdge("Mike", "Bob");
|
|
186
|
-
|
|
187
|
-
it("should add second node to first node neighbors", () => {
|
|
188
|
-
expect(graph.getVertexNeighbors("Mike")).toContain("Bob");
|
|
189
|
-
});
|
|
190
|
-
it("should not add first node to second node neighbors", () => {
|
|
191
|
-
expect(graph.getVertexNeighbors("Bob")).not.toContain("Mike");
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
describe("should override an existed edge and its weight", () => {
|
|
196
|
-
const graph: IGraph<string> = new DirectedGraph();
|
|
197
|
-
graph
|
|
198
|
-
.addVertex("Mike")
|
|
199
|
-
.addVertex("Bob")
|
|
200
|
-
.addEdge("Mike", "Bob", 5)
|
|
201
|
-
.addEdge("Bob", "Mike", 15)
|
|
202
|
-
.addEdge("Mike", "Bob", 10);
|
|
203
|
-
|
|
204
|
-
describe("should not multiple edges", () => {
|
|
205
|
-
it("should not add vertex to first edge", () => {
|
|
206
|
-
expect(graph.getVertexNeighbors("Mike")).toEqual(["Bob"]);
|
|
207
|
-
});
|
|
208
|
-
it("should not add vertex to second edge", () => {
|
|
209
|
-
expect(graph.getVertexNeighbors("Bob")).toEqual(["Mike"]);
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it("should override edge weight", () => {
|
|
214
|
-
expect(graph.getEdgeWeight("Mike", "Bob")).toBe(10);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("should not override back edge weight", () => {
|
|
218
|
-
expect(graph.getEdgeWeight("Bob", "Mike")).toBe(15);
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
describe("method removeVertex", () => {
|
|
225
|
-
it("should throw when try to delete not existed vertex", () => {
|
|
226
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
227
|
-
|
|
228
|
-
expect(() => {
|
|
229
|
-
graph.removeVertex("NOT_EXISTED_VERTEX");
|
|
230
|
-
}).toThrowError();
|
|
231
|
-
});
|
|
232
|
-
|
|
233
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
234
|
-
describe("should cascade remove", () => {
|
|
235
|
-
const graph: IGraph<string> = new UndirectedGraph();
|
|
236
|
-
|
|
237
|
-
graph
|
|
238
|
-
.addVertex("Mike")
|
|
239
|
-
.addVertex("Bob")
|
|
240
|
-
.addVertex("John")
|
|
241
|
-
.addVertex("Lisa")
|
|
242
|
-
.addEdge("Mike", "Bob")
|
|
243
|
-
.addEdge("Mike", "Lisa")
|
|
244
|
-
.addEdge("John", "Lisa")
|
|
245
|
-
.addEdge("Bob", "John");
|
|
246
|
-
|
|
247
|
-
graph.removeVertex("Bob");
|
|
248
|
-
|
|
249
|
-
it("should remove edges", () => {
|
|
250
|
-
const hasEdge = graph.hasEdge("Mike", "Bob");
|
|
251
|
-
|
|
252
|
-
expect(hasEdge).toBe(false);
|
|
253
|
-
});
|
|
254
|
-
it("should remove related vertexes neighbors", () => {
|
|
255
|
-
const mikeNeighbors = graph.getVertexNeighbors("Mike");
|
|
256
|
-
const lisaNeighbors = graph.getVertexNeighbors("Lisa");
|
|
257
|
-
const johnNeighbors = graph.getVertexNeighbors("John");
|
|
258
|
-
|
|
259
|
-
expect(mikeNeighbors).toEqual(["Lisa"]);
|
|
260
|
-
expect(lisaNeighbors).toEqual(["Mike", "John"]);
|
|
261
|
-
expect(johnNeighbors).toEqual(["Lisa"]);
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (graphType === EnumGraphType.Directed) {
|
|
267
|
-
describe("should cascade remove", () => {
|
|
268
|
-
const graph: IGraph<string> = new DirectedGraph();
|
|
269
|
-
|
|
270
|
-
graph
|
|
271
|
-
.addVertex("Mike")
|
|
272
|
-
.addVertex("Bob")
|
|
273
|
-
.addVertex("John")
|
|
274
|
-
.addVertex("Lisa")
|
|
275
|
-
.addEdge("Mike", "Bob")
|
|
276
|
-
.addEdge("Mike", "Lisa")
|
|
277
|
-
.addEdge("John", "Lisa")
|
|
278
|
-
.addEdge("Bob", "John");
|
|
279
|
-
|
|
280
|
-
graph.removeVertex("Bob");
|
|
281
|
-
|
|
282
|
-
it("should remove edges", () => {
|
|
283
|
-
const hasEdge = graph.hasEdge("Mike", "Bob");
|
|
284
|
-
expect(hasEdge).toBe(false);
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
it("should remove related vertices neighbors", () => {
|
|
288
|
-
const mikeNeighbors = graph.getVertexNeighbors("Mike");
|
|
289
|
-
const lisaNeighbors = graph.getVertexNeighbors("Lisa");
|
|
290
|
-
const johnNeighbors = graph.getVertexNeighbors("John");
|
|
291
|
-
|
|
292
|
-
expect(mikeNeighbors).toEqual(["Lisa"]);
|
|
293
|
-
expect(lisaNeighbors).toEqual([]);
|
|
294
|
-
expect(johnNeighbors).toEqual(["Lisa"]);
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
describe("method removeEdge", () => {
|
|
301
|
-
it("should throw when try to delete not existed edge", () => {
|
|
302
|
-
const graph: IGraph<string> = createGraph(graphType);
|
|
303
|
-
|
|
304
|
-
expect(() => {
|
|
305
|
-
graph.removeEdge("NOT_EXISTED_VERTEX", "NOT_EXISTED_VERTEX");
|
|
306
|
-
}).toThrowError();
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
310
|
-
describe("should delete correct", () => {
|
|
311
|
-
const graph: IGraph<string> = new UndirectedGraph();
|
|
312
|
-
|
|
313
|
-
graph
|
|
314
|
-
.addVertex("Mike")
|
|
315
|
-
.addVertex("Bob")
|
|
316
|
-
.addVertex("Lisa")
|
|
317
|
-
.addEdge("Mike", "Bob")
|
|
318
|
-
.addEdge("Bob", "Lisa")
|
|
319
|
-
.addEdge("Mike", "Lisa");
|
|
320
|
-
|
|
321
|
-
graph.removeEdge("Mike", "Lisa");
|
|
322
|
-
|
|
323
|
-
it("should delete correct", () => {
|
|
324
|
-
expect(graph.hasEdge("Mike", "Lisa")).toBe(false);
|
|
325
|
-
});
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
if (graphType === EnumGraphType.Directed) {
|
|
330
|
-
describe("should delete correct", () => {
|
|
331
|
-
const graph: IGraph<string> = new DirectedGraph();
|
|
332
|
-
|
|
333
|
-
graph
|
|
334
|
-
.addVertex("Mike")
|
|
335
|
-
.addVertex("Bob")
|
|
336
|
-
.addVertex("Lisa")
|
|
337
|
-
.addEdge("Mike", "Bob")
|
|
338
|
-
.addEdge("Bob", "Lisa")
|
|
339
|
-
.addEdge("Mike", "Lisa")
|
|
340
|
-
.addEdge("Lisa", "Mike");
|
|
341
|
-
|
|
342
|
-
graph.removeEdge("Mike", "Lisa");
|
|
343
|
-
|
|
344
|
-
it("should delete edge from-to", () => {
|
|
345
|
-
expect(graph.hasEdge("Mike", "Lisa")).toBe(false);
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
it("should not delete edge to-from", () => {
|
|
349
|
-
expect(graph.hasEdge("Lisa", "Mike")).toBe(true);
|
|
350
|
-
});
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
describe("method getVertexNeighbors", () => {
|
|
356
|
-
it("should throw when vertex does not exist", () => {
|
|
357
|
-
const graph: IGraph<number> = createGraph(graphType);
|
|
358
|
-
graph.addVertex(1).addVertex(2).addEdge(1, 2);
|
|
359
|
-
|
|
360
|
-
expect(() => {
|
|
361
|
-
graph.getVertexNeighbors(0);
|
|
362
|
-
}).toThrowError();
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
if (graphType === EnumGraphType.Undirected) {
|
|
366
|
-
it("should return correct neighbors", () => {
|
|
367
|
-
const graph: IGraph<number> = new UndirectedGraph();
|
|
368
|
-
graph
|
|
369
|
-
.addVertex(1)
|
|
370
|
-
.addVertex(2)
|
|
371
|
-
.addVertex(3)
|
|
372
|
-
.addEdge(1, 2)
|
|
373
|
-
.addEdge(2, 3);
|
|
374
|
-
|
|
375
|
-
expect(graph.getVertexNeighbors(2)).toEqual([1, 3]);
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
if (graphType === EnumGraphType.Directed) {
|
|
380
|
-
it("should return correct neighbors", () => {
|
|
381
|
-
const graph: IGraph<number> = new DirectedGraph();
|
|
382
|
-
graph
|
|
383
|
-
.addVertex(1)
|
|
384
|
-
.addVertex(2)
|
|
385
|
-
.addVertex(3)
|
|
386
|
-
.addEdge(1, 2)
|
|
387
|
-
.addEdge(2, 3);
|
|
388
|
-
|
|
389
|
-
expect(graph.getVertexNeighbors(2)).toEqual([3]);
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
);
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { createGraph } from "../../../../src/helpers/createGraph";
|
|
2
|
-
import { EnumGraphType } from "../../../../src/types/EnumGraphType";
|
|
3
|
-
import { transposeDirectedGraph } from "../../../../src/data-structures/Graph/transposing/transposeDirectedGraph";
|
|
4
|
-
|
|
5
|
-
describe("Directed graph transpose", () => {
|
|
6
|
-
const sourceGraph = createGraph<string>(EnumGraphType.Directed);
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Source matrix
|
|
10
|
-
*
|
|
11
|
-
* * V1 V2 V3 V4
|
|
12
|
-
* V1 0 1 0 1
|
|
13
|
-
* V2 0 0 0 0
|
|
14
|
-
* V3 0 0 0 1
|
|
15
|
-
* V4 0 0 0 0
|
|
16
|
-
*/
|
|
17
|
-
sourceGraph
|
|
18
|
-
.addVertex("Vertex_1")
|
|
19
|
-
.addVertex("Vertex_2")
|
|
20
|
-
.addVertex("Vertex_3")
|
|
21
|
-
.addVertex("Vertex_4")
|
|
22
|
-
.addEdge("Vertex_1", "Vertex_2")
|
|
23
|
-
.addEdge("Vertex_1", "Vertex_4")
|
|
24
|
-
.addEdge("Vertex_3", "Vertex_4");
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Transposed matrix
|
|
28
|
-
*
|
|
29
|
-
* * V1 V2 V3 V4
|
|
30
|
-
* V1 0 0 0 0
|
|
31
|
-
* V2 1 0 0 0
|
|
32
|
-
* V3 0 0 0 0
|
|
33
|
-
* V4 1 0 1 0
|
|
34
|
-
*/
|
|
35
|
-
const transposedGraph = transposeDirectedGraph(sourceGraph);
|
|
36
|
-
|
|
37
|
-
it("should have the same vertices as source graph", () => {
|
|
38
|
-
expect(transposedGraph.vertices()).toEqual(sourceGraph.vertices());
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should have all reverted edges", () => {
|
|
42
|
-
expect(transposedGraph.hasEdge("Vertex_2", "Vertex_1")).toBe(true);
|
|
43
|
-
expect(transposedGraph.hasEdge("Vertex_4", "Vertex_1")).toBe(true);
|
|
44
|
-
expect(transposedGraph.hasEdge("Vertex_4", "Vertex_3")).toBe(true);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("should not have edges from source graph", () => {
|
|
48
|
-
expect(transposedGraph.hasEdge("Vertex_1", "Vertex_2")).toBe(false);
|
|
49
|
-
expect(transposedGraph.hasEdge("Vertex_1", "Vertex_4")).toBe(false);
|
|
50
|
-
expect(transposedGraph.hasEdge("Vertex_3", "Vertex_4")).toBe(false);
|
|
51
|
-
});
|
|
52
|
-
});
|