Articulation Points (or Cut Vertices)
Algorithm · Stretch
Step through it: Depth-First Search pattern
start at 7
Step 1 of 6
Goal: visit every node. Depth-first search picks a direction and goes as deep as it can before turning back, like exploring a maze by always taking the next corridor.
Step 2 of 6
From the root, 7, go down the left side to 9.
Step 3 of 6
Keep going down to 5. It has no children: a dead end.
Step 4 of 6
Back up to 9 and try its other child, 2. Another dead end.
Step 5 of 6
Back up to the root and take the last branch: 11. Recursion remembers where to return to (a stack does the same). On a graph also keep a visited list so you never loop. This is depth-first search.
Step 6 of 6
Your turn.
A vertex in an undirected connected graph is an articulation point (or cut vertex) if removing it (and edges through it) disconnects the graph. Articulation points represent vulnerabilities in a connected network – single points whose failure would split the network into 2 or more disconnected components. They are useful for designing reliable networks.
For a disconnected undirected graph, an articulation point is a vertex removing which increases number of connected components.
Code
import depthFirstSearch from '../depth-first-search/depthFirstSearch';
/**
* Helper class for visited vertex metadata.
*/
class VisitMetadata {
constructor({ discoveryTime, lowDiscoveryTime }) {
this.discoveryTime = discoveryTime;
this.lowDiscoveryTime = lowDiscoveryTime;
// We need this in order to check graph root node, whether it has two
// disconnected children or not.
this.independentChildrenCount = 0;
}
}
/**
* Tarjan's algorithm for finding articulation points in graph.
*
* @param {Graph} graph
* @return {Object}
*/
export default function articulationPoints(graph) {
// Set of vertices we've already visited during DFS.
const visitedSet = {};
// Set of articulation points.
const articulationPointsSet = {};
// Time needed to discover to the current vertex.
let discoveryTime = 0;
// Peek the start vertex for DFS traversal.
const startVertex = graph.getAllVertices()[0];
const dfsCallbacks = {
/**
* @param {GraphVertex} currentVertex
* @param {GraphVertex} previousVertex
*/
enterVertex: ({ currentVertex, previousVertex }) => {
// Tick discovery time.
discoveryTime += 1;
// Put current vertex to visited set.
visitedSet[currentVertex.getKey()] = new VisitMetadata({
discoveryTime,
lowDiscoveryTime: discoveryTime,
});
if (previousVertex) {
// Update children counter for previous vertex.
visitedSet[previousVertex.getKey()].independentChildrenCount += 1;
}
},
/**
* @param {GraphVertex} currentVertex
* @param {GraphVertex} previousVertex
*/
leaveVertex: ({ currentVertex, previousVertex }) => {
if (previousVertex === null) {
// Don't do anything for the root vertex if it is already current (not previous one)
return;
}
// Update the low time with the smallest time of adjacent vertices.
// Get minimum low discovery time from all neighbors.
/** @param {GraphVertex} neighbor */
visitedSet[currentVertex.getKey()].lowDiscoveryTime = currentVertex.getNeighbors()
.filter((earlyNeighbor) => earlyNeighbor.getKey() !== previousVertex.getKey())
/**
* @param {number} lowestDiscoveryTime
* @param {GraphVertex} neighbor
*/
.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.
// To do so we need to check two [OR] conditions:
// 1. Is it a root vertex with at least two independent children.
// 2. If its visited time is <= low time of adjacent vertex.
if (previousVertex === startVertex) {
// Check that root vertex has at least two independent children.
if (visitedSet[previousVertex.getKey()].independentChildrenCount >= 2) {
articulationPointsSet[previousVertex.getKey()] = previousVertex;
}
} else {
// Get current vertex low discovery time.
const currentLowDiscoveryTime = visitedSet[currentVertex.getKey()].lowDiscoveryTime;
// Compare current vertex low discovery time with parent discovery time. Check if there
// are any short path (back edge) exists. If we can't get to current vertex other then
// via parent then the parent vertex is articulation point for current one.
const parentDiscoveryTime = visitedSet[previousVertex.getKey()].discoveryTime;
if (parentDiscoveryTime <= currentLowDiscoveryTime) {
articulationPointsSet[previousVertex.getKey()] = previousVertex;
}
}
},
allowTraversal: ({ nextVertex }) => {
return !visitedSet[nextVertex.getKey()];
},
};
// Do Depth First Search traversal over submitted graph.
depthFirstSearch(graph, startVertex, dfsCallbacks);
return articulationPointsSet;
}References
Practice
Depth-First Search
- Maximum Depth of Binary TreeEasy
- Path SumEasy
- Diameter of Binary TreeEasy
- Balanced Binary TreeEasy
- Find if Path Exists in GraphEasy
- Path Sum IIMedium
- Path Sum IIIMedium
- Clone GraphMedium
- Number of ProvincesMedium
- All Paths From Source to TargetMedium
- Course ScheduleMedium
- Pacific Atlantic Water FlowMedium
- Count Good Nodes in Binary TreeMedium
- Sum Root to Leaf NumbersMedium
- Reconstruct ItineraryMedium
- Is Graph Bipartite?Medium
- Binary Tree Maximum Path SumHard
- Longest Increasing Path in a MatrixHard
Spotted a mistake or something unclear? Report it on GitHub.