dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
75 lines (69 loc) • 4.13 kB
JavaScript
"use strict";
var _GraphVertex = _interopRequireDefault(require("../../../../data-structures/graph/GraphVertex"));
var _GraphEdge = _interopRequireDefault(require("../../../../data-structures/graph/GraphEdge"));
var _Graph = _interopRequireDefault(require("../../../../data-structures/graph/Graph"));
var _stronglyConnectedComponents = _interopRequireDefault(require("../stronglyConnectedComponents"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('stronglyConnectedComponents', () => {
it('should detect strongly connected components in simple graph', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const vertexD = new _GraphVertex.default('D');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeCA = new _GraphEdge.default(vertexC, vertexA);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const graph = new _Graph.default(true);
graph.addEdge(edgeAB).addEdge(edgeBC).addEdge(edgeCA).addEdge(edgeCD);
const components = (0, _stronglyConnectedComponents.default)(graph);
expect(components).toBeDefined();
expect(components.length).toBe(2);
expect(components[0][0].getKey()).toBe(vertexA.getKey());
expect(components[0][1].getKey()).toBe(vertexC.getKey());
expect(components[0][2].getKey()).toBe(vertexB.getKey());
expect(components[1][0].getKey()).toBe(vertexD.getKey());
});
it('should detect strongly connected components in graph', () => {
const vertexA = new _GraphVertex.default('A');
const vertexB = new _GraphVertex.default('B');
const vertexC = new _GraphVertex.default('C');
const vertexD = new _GraphVertex.default('D');
const vertexE = new _GraphVertex.default('E');
const vertexF = new _GraphVertex.default('F');
const vertexG = new _GraphVertex.default('G');
const vertexH = new _GraphVertex.default('H');
const vertexI = new _GraphVertex.default('I');
const vertexJ = new _GraphVertex.default('J');
const vertexK = new _GraphVertex.default('K');
const edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeCA = new _GraphEdge.default(vertexC, vertexA);
const edgeBD = new _GraphEdge.default(vertexB, vertexD);
const edgeDE = new _GraphEdge.default(vertexD, vertexE);
const edgeEF = new _GraphEdge.default(vertexE, vertexF);
const edgeFD = new _GraphEdge.default(vertexF, vertexD);
const edgeGF = new _GraphEdge.default(vertexG, vertexF);
const edgeGH = new _GraphEdge.default(vertexG, vertexH);
const edgeHI = new _GraphEdge.default(vertexH, vertexI);
const edgeIJ = new _GraphEdge.default(vertexI, vertexJ);
const edgeJG = new _GraphEdge.default(vertexJ, vertexG);
const edgeJK = new _GraphEdge.default(vertexJ, vertexK);
const graph = new _Graph.default(true);
graph.addEdge(edgeAB).addEdge(edgeBC).addEdge(edgeCA).addEdge(edgeBD).addEdge(edgeDE).addEdge(edgeEF).addEdge(edgeFD).addEdge(edgeGF).addEdge(edgeGH).addEdge(edgeHI).addEdge(edgeIJ).addEdge(edgeJG).addEdge(edgeJK);
const components = (0, _stronglyConnectedComponents.default)(graph);
expect(components).toBeDefined();
expect(components.length).toBe(4);
expect(components[0][0].getKey()).toBe(vertexG.getKey());
expect(components[0][1].getKey()).toBe(vertexJ.getKey());
expect(components[0][2].getKey()).toBe(vertexI.getKey());
expect(components[0][3].getKey()).toBe(vertexH.getKey());
expect(components[1][0].getKey()).toBe(vertexK.getKey());
expect(components[2][0].getKey()).toBe(vertexA.getKey());
expect(components[2][1].getKey()).toBe(vertexC.getKey());
expect(components[2][2].getKey()).toBe(vertexB.getKey());
expect(components[3][0].getKey()).toBe(vertexD.getKey());
expect(components[3][1].getKey()).toBe(vertexF.getKey());
expect(components[3][2].getKey()).toBe(vertexE.getKey());
});
});