From 70af57f11d8b02a6ec9ba1d50f95f4006f8f3910 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Thu, 10 May 2018 16:28:44 +0300 Subject: [PATCH] Add Tarjan's algorithm. --- .../__test__/articulationPoints.test.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js b/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js index 402dfc73..748f5dee 100644 --- a/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js +++ b/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js @@ -140,4 +140,71 @@ describe('articulationPoints', () => { vertexD, ]); }); + + it('should find articulation points in yet another graph #1', () => { + 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 edgeAB = new GraphEdge(vertexA, vertexB); + const edgeAC = new GraphEdge(vertexA, vertexC); + const edgeBC = new GraphEdge(vertexB, vertexC); + const edgeCD = new GraphEdge(vertexC, vertexD); + const edgeDE = new GraphEdge(vertexD, vertexE); + + const graph = new Graph(); + + graph + .addEdge(edgeAB) + .addEdge(edgeAC) + .addEdge(edgeBC) + .addEdge(edgeCD) + .addEdge(edgeDE); + + const articulationPointsSet = articulationPoints(graph); + + expect(articulationPointsSet).toEqual([ + vertexD, + vertexC, + ]); + }); + + it('should find articulation points in yet another graph #2', () => { + 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 vertexG = new GraphVertex('G'); + + const edgeAB = new GraphEdge(vertexA, vertexB); + const edgeAC = new GraphEdge(vertexA, vertexC); + const edgeBC = new GraphEdge(vertexB, vertexC); + const edgeCD = new GraphEdge(vertexC, vertexD); + const edgeCE = new GraphEdge(vertexC, vertexE); + const edgeCF = new GraphEdge(vertexC, vertexF); + const edgeEG = new GraphEdge(vertexE, vertexG); + const edgeFG = new GraphEdge(vertexF, vertexG); + + const graph = new Graph(); + + graph + .addEdge(edgeAB) + .addEdge(edgeAC) + .addEdge(edgeBC) + .addEdge(edgeCD) + .addEdge(edgeCE) + .addEdge(edgeCF) + .addEdge(edgeEG) + .addEdge(edgeFG); + + const articulationPointsSet = articulationPoints(graph); + + expect(articulationPointsSet).toEqual([ + vertexC, + ]); + }); });