-
Notifications
You must be signed in to change notification settings - Fork 31.1k
Expand file tree
/
Copy pathdetectUndirectedCycle.test.js
More file actions
66 lines (55 loc) · 2.24 KB
/
detectUndirectedCycle.test.js
File metadata and controls
66 lines (55 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import GraphVertex from '../../../../data-structures/graph/GraphVertex';
import GraphEdge from '../../../../data-structures/graph/GraphEdge';
import Graph from '../../../../data-structures/graph/Graph';
import detectUndirectedCycle from '../detectUndirectedCycle';
describe('detectUndirectedCycle', () => {
it('should detect undirected cycle', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const vertexC = new GraphVertex('C');
const vertexD = new GraphVertex('D');
const vertexE = new GraphVertex('E');
const vertexF = new GraphVertex('F');
const edgeAF = new GraphEdge(vertexA, vertexF);
const edgeAB = new GraphEdge(vertexA, vertexB);
const edgeBE = new GraphEdge(vertexB, vertexE);
const edgeBC = new GraphEdge(vertexB, vertexC);
const edgeCD = new GraphEdge(vertexC, vertexD);
const edgeDE = new GraphEdge(vertexD, vertexE);
const graph = new Graph();
graph
.addEdge(edgeAF)
.addEdge(edgeAB)
.addEdge(edgeBE)
.addEdge(edgeBC)
.addEdge(edgeCD);
// no cycle yet
expect(detectUndirectedCycle(graph)).toBeNull();
// add the final edge that closes cycle B-C-D-E-B
graph.addEdge(edgeDE);
const cycle = detectUndirectedCycle(graph);
// should return ordered array of vertices representing cycle (first === last)
expect(Array.isArray(cycle)).toBe(true);
expect(cycle.length).toBeGreaterThanOrEqual(3);
expect(cycle[0].getKey()).toBe(cycle[cycle.length - 1].getKey());
// Extract keys for easier assertions
const keys = cycle.map((v) => v.getKey());
// The expected cycle is B -> C -> D -> E -> B (but the returned cycle may be a rotation),
// so accept any rotation of that sequence.
const allowedRotations = [
['B', 'C', 'D', 'E', 'B'],
['C', 'D', 'E', 'B', 'C'],
['D', 'E', 'B', 'C', 'D'],
['E', 'B', 'C', 'D', 'E'],
];
// Check that keys match one of the allowed rotations
const matchesRotation = allowedRotations.some((rot) => {
if (rot.length !== keys.length) return false;
for (let i = 0; i < rot.length; i += 1) {
if (rot[i] !== keys[i]) return false;
}
return true;
});
expect(matchesRotation).toBe(true);
});
});