From 40ebc8c10d2f4abe6548186432e4c0654837a620 Mon Sep 17 00:00:00 2001 From: Mustafa Azyoksul <32205084+m-azyoksul@users.noreply.github.com> Date: Sun, 22 May 2022 19:23:06 +0300 Subject: [PATCH] Fixed articulation points alg at the root node In the current implementation, the noot node is always marked as an articulation point but this is not correct. To determine if the root node is an articulation point, an additional variable called "components" is introduced. This variable counts the number of connected components connected to the root node in the case where the root node is removed. If it is more than 1, that means that the root node would split the graph. --- .../graphtheory/ArticulationPointsAdjacencyList.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/ArticulationPointsAdjacencyList.java b/src/main/java/com/williamfiset/algorithms/graphtheory/ArticulationPointsAdjacencyList.java index ea4cf20cf..f119beb20 100644 --- a/src/main/java/com/williamfiset/algorithms/graphtheory/ArticulationPointsAdjacencyList.java +++ b/src/main/java/com/williamfiset/algorithms/graphtheory/ArticulationPointsAdjacencyList.java @@ -56,14 +56,16 @@ private void dfs(int root, int at, int parent) { visited[at] = true; low[at] = ids[at] = id++; + int components = 0; List edges = graph.get(at); for (Integer to : edges) { if (to == parent) continue; if (!visited[to]) { + components++; dfs(root, to, at); low[at] = min(low[at], low[to]); - if (ids[at] <= low[to]) { + if (parent == -1 && components > 1 || parent != -1 && ids[at] <= low[to]) { isArticulationPoint[at] = true; } } else {