UNPKG

dist-javascript-algorithms-and-data-structures

Version:

Algorithms and data-structures implemented on JavaScript

137 lines (131 loc) 7.33 kB
"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 _graphBridges = _interopRequireDefault(require("../graphBridges")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } describe('graphBridges', () => { it('should find bridges 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 bridges = Object.values((0, _graphBridges.default)(graph)); expect(bridges.length).toBe(3); expect(bridges[0].getKey()).toBe(edgeCD.getKey()); expect(bridges[1].getKey()).toBe(edgeBC.getKey()); expect(bridges[2].getKey()).toBe(edgeAB.getKey()); }); it('should find bridges 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 bridges = Object.values((0, _graphBridges.default)(graph)); expect(bridges.length).toBe(1); expect(bridges[0].getKey()).toBe(edgeCD.getKey()); }); it('should find bridges 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 bridges = Object.values((0, _graphBridges.default)(graph)); expect(bridges.length).toBe(3); expect(bridges[0].getKey()).toBe(edgeFH.getKey()); expect(bridges[1].getKey()).toBe(edgeDE.getKey()); expect(bridges[2].getKey()).toBe(edgeCD.getKey()); }); it('should find bridges in graph starting with different 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 bridges = Object.values((0, _graphBridges.default)(graph)); expect(bridges.length).toBe(3); expect(bridges[0].getKey()).toBe(edgeFH.getKey()); expect(bridges[1].getKey()).toBe(edgeDE.getKey()); expect(bridges[2].getKey()).toBe(edgeCD.getKey()); }); it('should find bridges 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 bridges = Object.values((0, _graphBridges.default)(graph)); expect(bridges.length).toBe(2); expect(bridges[0].getKey()).toBe(edgeDE.getKey()); expect(bridges[1].getKey()).toBe(edgeCD.getKey()); }); it('should find bridges 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 bridges = Object.values((0, _graphBridges.default)(graph)); expect(bridges.length).toBe(1); expect(bridges[0].getKey()).toBe(edgeCD.getKey()); }); });