Add Tarjan's algorithm.

This commit is contained in:
Oleksii Trekhleb 2018-05-11 15:42:42 +03:00
parent 1dd480b906
commit 670ec093f4

View File

@ -65,13 +65,19 @@ export default function articulationPoints(graph) {
// Update the low time with the smallest time of adjacent vertices. // Update the low time with the smallest time of adjacent vertices.
// Get minimum low discovery time from all neighbors. // Get minimum low discovery time from all neighbors.
/** @param {GraphVertex} neighbor */ /** @param {GraphVertex} neighbor */
visitedSet[currentVertex.getKey()].lowDiscoveryTime = currentVertex.getNeighbors().reduce( visitedSet[currentVertex.getKey()].lowDiscoveryTime = currentVertex.getNeighbors()
(lowestDiscoveryTime, neighbor) => { .filter(earlyNeighbor => earlyNeighbor.getKey() !== previousVertex.getKey())
const neighborLowTime = visitedSet[neighbor.getKey()].lowDiscoveryTime; /**
return neighborLowTime < lowestDiscoveryTime ? neighborLowTime : lowestDiscoveryTime; * @param {number} lowestDiscoveryTime
}, * @param {GraphVertex} neighbor
visitedSet[currentVertex.getKey()].lowDiscoveryTime, */
); .reduce(
(lowestDiscoveryTime, neighbor) => {
const neighborLowTime = visitedSet[neighbor.getKey()].lowDiscoveryTime;
return neighborLowTime < lowestDiscoveryTime ? neighborLowTime : lowestDiscoveryTime;
},
visitedSet[currentVertex.getKey()].lowDiscoveryTime,
);
// Detect whether previous vertex is articulation point or not. // Detect whether previous vertex is articulation point or not.
// To do so we need to check two [OR] conditions: // To do so we need to check two [OR] conditions: