dist-javascript-algorithms-and-data-structures
Version:
Algorithms and data-structures implemented on JavaScript
102 lines (95 loc) • 5.61 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 _eulerianPath = _interopRequireDefault(require("../eulerianPath"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
describe('eulerianPath', () => {
it('should throw an error when graph is not Eulerian', () => {
function findEulerianPathInNotEulerianGraph() {
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 edgeBD = new _GraphEdge.default(vertexB, vertexD);
const edgeCE = new _GraphEdge.default(vertexC, vertexE);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAC).addEdge(edgeBC).addEdge(edgeBD).addEdge(edgeCE);
(0, _eulerianPath.default)(graph);
}
expect(findEulerianPathInNotEulerianGraph).toThrowError();
});
it('should find Eulerian Circuit 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 edgeAB = new _GraphEdge.default(vertexA, vertexB);
const edgeAE = new _GraphEdge.default(vertexA, vertexE);
const edgeAF = new _GraphEdge.default(vertexA, vertexF);
const edgeAG = new _GraphEdge.default(vertexA, vertexG);
const edgeGF = new _GraphEdge.default(vertexG, vertexF);
const edgeBE = new _GraphEdge.default(vertexB, vertexE);
const edgeEB = new _GraphEdge.default(vertexE, vertexB);
const edgeBC = new _GraphEdge.default(vertexB, vertexC);
const edgeED = new _GraphEdge.default(vertexE, vertexD);
const edgeCD = new _GraphEdge.default(vertexC, vertexD);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAE).addEdge(edgeAF).addEdge(edgeAG).addEdge(edgeGF).addEdge(edgeBE).addEdge(edgeEB).addEdge(edgeBC).addEdge(edgeED).addEdge(edgeCD);
const graphEdgesCount = graph.getAllEdges().length;
const eulerianPathSet = (0, _eulerianPath.default)(graph);
expect(eulerianPathSet.length).toBe(graphEdgesCount + 1);
expect(eulerianPathSet[0].getKey()).toBe(vertexA.getKey());
expect(eulerianPathSet[1].getKey()).toBe(vertexB.getKey());
expect(eulerianPathSet[2].getKey()).toBe(vertexE.getKey());
expect(eulerianPathSet[3].getKey()).toBe(vertexB.getKey());
expect(eulerianPathSet[4].getKey()).toBe(vertexC.getKey());
expect(eulerianPathSet[5].getKey()).toBe(vertexD.getKey());
expect(eulerianPathSet[6].getKey()).toBe(vertexE.getKey());
expect(eulerianPathSet[7].getKey()).toBe(vertexA.getKey());
expect(eulerianPathSet[8].getKey()).toBe(vertexF.getKey());
expect(eulerianPathSet[9].getKey()).toBe(vertexG.getKey());
expect(eulerianPathSet[10].getKey()).toBe(vertexA.getKey());
});
it('should find Eulerian Path 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 edgeAC = new _GraphEdge.default(vertexA, vertexC);
const edgeBD = new _GraphEdge.default(vertexB, vertexD);
const edgeDC = new _GraphEdge.default(vertexD, vertexC);
const edgeCE = new _GraphEdge.default(vertexC, vertexE);
const edgeEF = new _GraphEdge.default(vertexE, vertexF);
const edgeFH = new _GraphEdge.default(vertexF, vertexH);
const edgeFG = new _GraphEdge.default(vertexF, vertexG);
const edgeHG = new _GraphEdge.default(vertexH, vertexG);
const graph = new _Graph.default();
graph.addEdge(edgeAB).addEdge(edgeAC).addEdge(edgeBD).addEdge(edgeDC).addEdge(edgeCE).addEdge(edgeEF).addEdge(edgeFH).addEdge(edgeFG).addEdge(edgeHG);
const graphEdgesCount = graph.getAllEdges().length;
const eulerianPathSet = (0, _eulerianPath.default)(graph);
expect(eulerianPathSet.length).toBe(graphEdgesCount + 1);
expect(eulerianPathSet[0].getKey()).toBe(vertexC.getKey());
expect(eulerianPathSet[1].getKey()).toBe(vertexA.getKey());
expect(eulerianPathSet[2].getKey()).toBe(vertexB.getKey());
expect(eulerianPathSet[3].getKey()).toBe(vertexD.getKey());
expect(eulerianPathSet[4].getKey()).toBe(vertexC.getKey());
expect(eulerianPathSet[5].getKey()).toBe(vertexE.getKey());
expect(eulerianPathSet[6].getKey()).toBe(vertexF.getKey());
expect(eulerianPathSet[7].getKey()).toBe(vertexH.getKey());
expect(eulerianPathSet[8].getKey()).toBe(vertexG.getKey());
expect(eulerianPathSet[9].getKey()).toBe(vertexF.getKey());
});
});