Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ff9877c

Browse files
committedMay 11, 2018
Add Tarjan's algorithm.
1 parent 70af57f commit ff9877c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed
 

‎src/algorithms/graph/articulation-points/__test__/articulationPoints.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ describe('articulationPoints', () => {
5555
]);
5656
});
5757

58+
it('should find articulation points in simple graph with back edge #2', () => {
59+
const vertexA = new GraphVertex('A');
60+
const vertexB = new GraphVertex('B');
61+
const vertexC = new GraphVertex('C');
62+
const vertexD = new GraphVertex('D');
63+
const vertexE = new GraphVertex('E');
64+
65+
const edgeAB = new GraphEdge(vertexA, vertexB);
66+
const edgeBC = new GraphEdge(vertexB, vertexC);
67+
const edgeCD = new GraphEdge(vertexC, vertexD);
68+
const edgeAE = new GraphEdge(vertexA, vertexE);
69+
const edgeCE = new GraphEdge(vertexC, vertexE);
70+
71+
const graph = new Graph();
72+
73+
graph
74+
.addEdge(edgeAB)
75+
.addEdge(edgeAE)
76+
.addEdge(edgeCE)
77+
.addEdge(edgeBC)
78+
.addEdge(edgeCD);
79+
80+
const articulationPointsSet = articulationPoints(graph);
81+
82+
expect(articulationPointsSet).toEqual([
83+
vertexC,
84+
]);
85+
});
86+
5887
it('should find articulation points in graph', () => {
5988
const vertexA = new GraphVertex('A');
6089
const vertexB = new GraphVertex('B');

‎src/algorithms/graph/articulation-points/articulationPoints.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class VisitMetadata {
1414

1515
// We need this in order to check graph root node, whether it has two
1616
// disconnected children or not.
17-
this.childrenCount = 0;
17+
this.independantChildrenCount = 0;
1818
}
1919
}
2020

@@ -57,7 +57,7 @@ export default function articulationPoints(graph) {
5757
visitedSet[previousVertex.getKey()].childVertex = currentVertex;
5858

5959
// Update children counter for previous vertex.
60-
visitedSet[previousVertex.getKey()].childrenCount += 1;
60+
visitedSet[previousVertex.getKey()].independantChildrenCount += 1;
6161
}
6262
},
6363
/**
@@ -71,7 +71,7 @@ export default function articulationPoints(graph) {
7171
// 2. If its visited time is <= low time of adjacent vertex.
7272
if (currentVertex === startVertex) {
7373
// Check that it has at least two independent children.
74-
if (visitedSet[currentVertex.getKey()].childrenCount >= 2) {
74+
if (visitedSet[currentVertex.getKey()].independantChildrenCount >= 2) {
7575
articulationPointsSet.push(currentVertex);
7676
}
7777
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.