diff --git a/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js b/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js index 748f5dee..c0241241 100644 --- a/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js +++ b/src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js @@ -55,6 +55,35 @@ describe('articulationPoints', () => { ]); }); + it('should find articulation points in simple graph with back edge #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 edgeAB = new GraphEdge(vertexA, vertexB); + const edgeBC = new GraphEdge(vertexB, vertexC); + const edgeCD = new GraphEdge(vertexC, vertexD); + const edgeAE = new GraphEdge(vertexA, vertexE); + const edgeCE = new GraphEdge(vertexC, vertexE); + + const graph = new Graph(); + + graph + .addEdge(edgeAB) + .addEdge(edgeAE) + .addEdge(edgeCE) + .addEdge(edgeBC) + .addEdge(edgeCD); + + const articulationPointsSet = articulationPoints(graph); + + expect(articulationPointsSet).toEqual([ + vertexC, + ]); + }); + it('should find articulation points in graph', () => { const vertexA = new GraphVertex('A'); const vertexB = new GraphVertex('B'); diff --git a/src/algorithms/graph/articulation-points/articulationPoints.js b/src/algorithms/graph/articulation-points/articulationPoints.js index f34d3e72..dd653167 100644 --- a/src/algorithms/graph/articulation-points/articulationPoints.js +++ b/src/algorithms/graph/articulation-points/articulationPoints.js @@ -14,7 +14,7 @@ class VisitMetadata { // We need this in order to check graph root node, whether it has two // disconnected children or not. - this.childrenCount = 0; + this.independantChildrenCount = 0; } } @@ -57,7 +57,7 @@ export default function articulationPoints(graph) { visitedSet[previousVertex.getKey()].childVertex = currentVertex; // Update children counter for previous vertex. - visitedSet[previousVertex.getKey()].childrenCount += 1; + visitedSet[previousVertex.getKey()].independantChildrenCount += 1; } }, /** @@ -71,7 +71,7 @@ export default function articulationPoints(graph) { // 2. If its visited time is <= low time of adjacent vertex. if (currentVertex === startVertex) { // Check that it has at least two independent children. - if (visitedSet[currentVertex.getKey()].childrenCount >= 2) { + if (visitedSet[currentVertex.getKey()].independantChildrenCount >= 2) { articulationPointsSet.push(currentVertex); } } else {