Add Tarjan's algorithm.

This commit is contained in:
Oleksii Trekhleb 2018-05-11 06:58:19 +03:00
parent 70af57f11d
commit ff9877cf6b
2 changed files with 32 additions and 3 deletions

View File

@ -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');

View File

@ -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 {