dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
155 lines (149 loc) • 8.88 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 _articulationPoints = _interopRequireDefault(require("../articulationPoints"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('articulationPoints', () => {
it('should find articulation points 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 edgeCD = new _GraphEdge.default(vertexC, vertexD);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeBC).addEdge(edgeCD);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(2);
expect(articulationPointsSet[0].getKey()).toBe(vertexC.getKey());
expect(articulationPointsSet[1].getKey()).toBe(vertexB.getKey());
});
it('should find articulation points in simple graph with back edge', () => {
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 edgeCD = new _GraphEdge.default(vertexC, vertexD);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAC).addEdge(edgeBC).addEdge(edgeCD);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(1);
expect(articulationPointsSet[0].getKey()).toBe(vertexC.getKey());
});
it('should find articulation points in simple graph with back edge #2', () => {
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 edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const edgeAE = new _GraphEdge.default(vertexA, vertexE);
const edgeCE = new _GraphEdge.default(vertexC, vertexE);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAE).addEdge(edgeCE).addEdge(edgeBC).addEdge(edgeCD);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(1);
expect(articulationPointsSet[0].getKey()).toBe(vertexC.getKey());
});
it('should find articulation points 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 edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const edgeDE = new _GraphEdge.default(vertexD, vertexE);
const edgeEG = new _GraphEdge.default(vertexE, vertexG);
const edgeEF = new _GraphEdge.default(vertexE, vertexF);
const edgeGF = new _GraphEdge.default(vertexG, vertexF);
const edgeFH = new _GraphEdge.default(vertexF, vertexH);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeBC).addEdge(edgeAC).addEdge(edgeCD).addEdge(edgeDE).addEdge(edgeEG).addEdge(edgeEF).addEdge(edgeGF).addEdge(edgeFH);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(4);
expect(articulationPointsSet[0].getKey()).toBe(vertexF.getKey());
expect(articulationPointsSet[1].getKey()).toBe(vertexE.getKey());
expect(articulationPointsSet[2].getKey()).toBe(vertexD.getKey());
expect(articulationPointsSet[3].getKey()).toBe(vertexC.getKey());
});
it('should find articulation points in graph starting with articulation root vertex', () => {
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 edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const edgeDE = new _GraphEdge.default(vertexD, vertexE);
const edgeEG = new _GraphEdge.default(vertexE, vertexG);
const edgeEF = new _GraphEdge.default(vertexE, vertexF);
const edgeGF = new _GraphEdge.default(vertexG, vertexF);
const edgeFH = new _GraphEdge.default(vertexF, vertexH);
const graph = new _Graph.default();
graph.addEdge(edgeDE).addEdge(edgeAB).addEdge(edgeBC).addEdge(edgeAC).addEdge(edgeCD).addEdge(edgeEG).addEdge(edgeEF).addEdge(edgeGF).addEdge(edgeFH);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(4);
expect(articulationPointsSet[0].getKey()).toBe(vertexF.getKey());
expect(articulationPointsSet[1].getKey()).toBe(vertexE.getKey());
expect(articulationPointsSet[2].getKey()).toBe(vertexC.getKey());
expect(articulationPointsSet[3].getKey()).toBe(vertexD.getKey());
});
it('should find articulation points in yet another graph #1', () => {
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 edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const edgeDE = new _GraphEdge.default(vertexD, vertexE);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAC).addEdge(edgeBC).addEdge(edgeCD).addEdge(edgeDE);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(2);
expect(articulationPointsSet[0].getKey()).toBe(vertexD.getKey());
expect(articulationPointsSet[1].getKey()).toBe(vertexC.getKey());
});
it('should find articulation points in yet another graph #2', () => {
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 edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeAC = new _GraphEdge.default(vertexA, vertexC);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const edgeCE = new _GraphEdge.default(vertexC, vertexE);
const edgeCF = new _GraphEdge.default(vertexC, vertexF);
const edgeEG = new _GraphEdge.default(vertexE, vertexG);
const edgeFG = new _GraphEdge.default(vertexF, vertexG);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAC).addEdge(edgeBC).addEdge(edgeCD).addEdge(edgeCE).addEdge(edgeCF).addEdge(edgeEG).addEdge(edgeFG);
const articulationPointsSet = Object.values((0, _articulationPoints.default)(graph));
expect(articulationPointsSet.length).toBe(1);
expect(articulationPointsSet[0].getKey()).toBe(vertexC.getKey());
});
});