Given an undirected graph with V vertices and E edges (edges[][]), Your task is to return all the articulation points in the graph. If no such point exists, return {-1}.
Note:
An articulation point is a vertex whose removal, along with all its connected edges, increases the number of connected components in the graph.
The graph may contain more than one connected component.
[Naive Approach] Using DFS - O(V * (V + E)) Time and O(V) Space
Condition for particular node to be an articulation point:
A node is an articulation point if, after removing it, you need more than one DFS traversal to visit all of its neighbors. This means that at least two of its neighbors (children) end up in different disconnected components, and cannot reach each other without this node.
Step by Step implementations:
Iterate over all nodes i (possible articulation candidates).
For each node i, pretend itâs removed from the graph by marking it as already visited, so DFS will skip it.
For each unvisited neighbor of i, start DFS
Count how many separate DFS calls are needed (stored in comp) to visit all neighbor.
If comp > 1, it means i connects multiple components , thus i is an articulation point.
After checking all nodes, return the list of articulation points, or {-1} if none found.
C++
// C++ program to find articulation points using a naive DFS approach#include<bits/stdc++.h>usingnamespacestd;// Standard DFS to mark all reachable nodesvoiddfs(intnode,vector<vector<int>>&adj,vector<bool>&visited){visited[node]=true;for(intneighbor:adj[node]){if(!visited[neighbor]){dfs(neighbor,adj,visited);}}}// Builds adjacency list from edge listvector<vector<int>>constructadj(intV,vector<vector<int>>&edges){vector<vector<int>>adj(V);for(autoit:edges){adj[it[0]].push_back(it[1]);adj[it[1]].push_back(it[0]);}returnadj;}// Finds articulation points using naive DFS approachvector<int>articulationPoints(intV,vector<vector<int>>&edges){vector<vector<int>>adj=constructadj(V,edges);vector<int>res;// Try removing each node one by onefor(inti=0;i<V;++i){vector<bool>visited(V,false);visited[i]=true;// count DFS calls from i's neighborsintcomp=0;for(autoit:adj[i]){// early stop if already more than 1 componentif(comp>1)break;if(!visited[it]){// explore connected partdfs(it,adj,visited);comp++;}}// if more than one component forms, it's an articulation pointif(comp>1)res.push_back(i);}if(res.empty())return{-1};returnres;}intmain(){intV=5;vector<vector<int>>edges={{0,1},{1,4},{2,3},{2,4},{3,4}};vector<int>ans=articulationPoints(V,edges);for(autoit:ans){cout<<it<<" ";}return0;}
Java
// Java program to find articulation points// using a naive DFS approachimportjava.util.*;classGfG{// Standard DFS to mark all reachable nodesstaticvoiddfs(intnode,ArrayList<ArrayList<Integer>>adj,boolean[]visited){visited[node]=true;for(intneighbor:adj.get(node)){if(!visited[neighbor]){dfs(neighbor,adj,visited);}}}// Builds adjacency list from edge liststaticArrayList<ArrayList<Integer>>constructadj(intV,int[][]edges){ArrayList<ArrayList<Integer>>adj=newArrayList<>();for(inti=0;i<V;i++){adj.add(newArrayList<>());}for(int[]edge:edges){adj.get(edge[0]).add(edge[1]);adj.get(edge[1]).add(edge[0]);}returnadj;}// Finds articulation points using naive DFS approachstaticArrayList<Integer>articulationPoints(intV,int[][]edges){ArrayList<ArrayList<Integer>>adj=constructadj(V,edges);ArrayList<Integer>res=newArrayList<>();// Try removing each node one by onefor(inti=0;i<V;++i){boolean[]visited=newboolean[V];visited[i]=true;// count DFS calls from i's neighborsintcomp=0;for(intit:adj.get(i)){// early stop if already more than 1 componentif(comp>1)break;if(!visited[it]){// explore connected partdfs(it,adj,visited);comp++;}}// if more than one component forms, it's an articulation pointif(comp>1)res.add(i);}if(res.isEmpty())returnnewArrayList<>(Arrays.asList(-1));returnres;}publicstaticvoidmain(String[]args){intV=5;int[][]edges={{0,1},{1,4},{2,3},{2,4},{3,4}};ArrayList<Integer>ans=articulationPoints(V,edges);for(intit:ans){System.out.print(it+" ");}}}
Python
# Python program to find articulation points using a naive DFS approachdefdfs(node,adj,visited):# Standard DFS to mark all reachable nodesvisited[node]=Trueforneighborinadj[node]:ifnotvisited[neighbor]:dfs(neighbor,adj,visited)defconstructadj(V,edges):# Builds adjacency list from edge listadj=[[]for_inrange(V)]foru,vinedges:adj[u].append(v)adj[v].append(u)returnadjdefarticulationPoints(V,edges):# Finds articulation points using naive DFS approachadj=constructadj(V,edges)res=[]# Try removing each node one by oneforiinrange(V):visited=[False]*Vvisited[i]=True# count DFS calls from i's neighborscomp=0foritinadj[i]:ifcomp>1:breakifnotvisited[it]:# explore connected partdfs(it,adj,visited)comp+=1# if more than one component forms, it's an articulation pointifcomp>1:res.append(i)ifnotres:return[-1]returnresif__name__=="__main__":V=5edges=[[0,1],[1,4],[2,3],[2,4],[3,4]]ans=articulationPoints(V,edges)foritinans:print(it,end=" ")
C#
// C# program to find articulation points// using a naive DFS approachusingSystem;usingSystem.Collections.Generic;classGfG{// Standard DFS to mark all reachable nodesstaticvoidDFS(intnode,List<List<int>>adj,bool[]visited){visited[node]=true;foreach(intneighborinadj[node]){if(!visited[neighbor]){DFS(neighbor,adj,visited);}}}// Builds adjacency list from edge liststaticList<List<int>>constructAdj(intV,int[,]edges){List<List<int>>adj=newList<List<int>>();for(inti=0;i<V;i++){adj.Add(newList<int>());}intE=edges.GetLength(0);for(inti=0;i<E;i++){intu=edges[i,0];intv=edges[i,1];adj[u].Add(v);adj[v].Add(u);}returnadj;}// Finds articulation points using naive DFS approachstaticList<int>articulationPoints(intV,int[,]edges){List<List<int>>adj=constructAdj(V,edges);List<int>res=newList<int>();// Try removing each node one by onefor(inti=0;i<V;++i){bool[]visited=newbool[V];visited[i]=true;// count DFS calls from i's neighborsintcomp=0;foreach(intitinadj[i]){// early stop if already more than 1 componentif(comp>1)break;if(!visited[it]){// explore connected partDFS(it,adj,visited);comp++;}}// if more than one component forms, it's an articulation pointif(comp>1)res.Add(i);}if(res.Count==0)returnnewList<int>{-1};returnres;}publicstaticvoidMain(string[]args){intV=5;int[,]edges=newint[,]{{0,1},{1,4},{2,3},{2,4},{3,4}};List<int>ans=articulationPoints(V,edges);foreach(intitinans){Console.Write(it+" ");}}}
JavaScript
// JavaScript program to find articulation points// using a naive DFS approach// Standard DFS to mark all reachable nodesfunctiondfs(node,adj,visited){visited[node]=true;for(letneighborofadj[node]){if(!visited[neighbor]){dfs(neighbor,adj,visited);}}}// Builds adjacency list from edge listfunctionconstructadj(V,edges){letadj=Array.from({length:V},()=>[]);for(let[u,v]ofedges){adj[u].push(v);adj[v].push(u);}returnadj;}// Finds articulation points using naive DFS approachfunctionarticulationPoints(V,edges){constadj=constructadj(V,edges);constres=[];// Try removing each node one by onefor(leti=0;i<V;++i){letvisited=Array(V).fill(false);visited[i]=true;// count DFS calls from i's neighborsletcomp=0;for(letitofadj[i]){// early stop if already more than 1 componentif(comp>1)break;if(!visited[it]){// explore connected partdfs(it,adj,visited);comp++;}}// if more than one component forms, it's an articulation pointif(comp>1)res.push(i);}if(res.length===0)return[-1];returnres;}// Driver Code constV=5;constedges=[[0,1],[1,4],[2,3],[2,4],[3,4]];constans=articulationPoints(V,edges);console.log(ans.join(" "));
Output
1 4
Time Complexity: O(V * ( V + E )) where V is number of vertices, and E is number of edges. We are performing DFS operation which have O(V + E) time complexity, for each of the vertex. Thus the overall time taken will be O(V * ( V + E )). Auxiliary Space: O(V), For storing the visited[] array.
[Expected Approach] Using Tarjan's Algorithm - O(V + E) Time and O(V) Space
The idea is to use DFS (Depth First Search). In DFS, follow vertices in a tree form called the DFS tree. In the DFS tree, a vertex u is the parent of another vertex v, if v is discovered by u.
In DFS tree, a vertex u is an articulation point if one of the following two conditions is true.
u is the root of the DFS tree and it has at least two children.
u is not the root of the DFS tree and it has a child v such that no vertex in the subtree rooted with v has a back edge to one of the ancestors in DFS tree of u.
Let's understand with an example:
For the vertex 3 (which is not the root), vertex 4 is the child of vertex 3. No vertex in the subtree rooted at vertex 4 has a back edge to one of ancestors of vertex 3. Thus on removal of vertex 3 and its associated edges the graph will get disconnected or the number of components in the graph will increase as the subtree rooted at vertex 4 will form a separate component. Hence vertex 3 is an articulation point.
Now consider the following graph:
Again the vertex 4 is the child of vertex 3. For the subtree rooted at vertex 4, vertex 7 in this subtree has a back edge to one of the ancestors of vertex 3 (which is vertex 1). Thus this subtree will not get disconnected on the removal of vertex 3 because of this back edge. Since there is no child v of vertex 3, such that subtree rooted at vertex v does not have a back edge to one of the ancestors of vertex 3. Hence vertex 3 is not an articulation point in this case.
Step by Step implementation:
Maintain these arrays and integers and perform a DFS traversal.
disc[]: Discovery time of each vertex during DFS.
low[]: The lowest discovery time reachable from the subtree rooted at that vertex (via tree or back edges).
parent: To keep track of each nodeâs parent in the DFS tree.
visited[]: To mark visited nodes.
Root Node Case:
For the root node of DFS (i.e., parent[u] == -1), check how many child DFS calls it makes.
If the root has two or more children, it is an articulation point
For any non-root node u, check all its adjacent nodes:
If v is an unvisited child:
Recur for v, and after returning update low[u] = min(low[u], low[v])
If low[v] >= disc[u], then u is an articulation point because v and its subtree cannot reach any ancestor of u, so removing u would disconnect v.
Back Edge Case
If v is already visited and is not the parent of u then Itâs a back edge. Update low[u] = min(low[u], disc[v])
This helps bubble up the lowest reachable ancestor through a back edge.
After DFS traversal completes, all nodes marked as articulation points are stored in result array
C++
#include<bits/stdc++.h>usingnamespacestd;vector<vector<int>>constructAdj(intV,vector<vector<int>>&edges){vector<vector<int>>adj(V);for(auto&edge:edges){adj[edge[0]].push_back(edge[1]);adj[edge[1]].push_back(edge[0]);}returnadj;}// Helper function to perform DFS and find articulation points// using Tarjan's algorithm.voidfindPoints(vector<vector<int>>&adj,intu,vector<int>&visited,vector<int>&disc,vector<int>&low,int&time,intparent,vector<int>&isAP){// Mark vertex u as visited and assign discovery// time and low valuevisited[u]=1;disc[u]=low[u]=++time;intchildren=0;// Process all adjacent vertices of ufor(intv:adj[u]){// If v is not visited, then recursively visit itif(!visited[v]){children++;findPoints(adj,v,visited,disc,low,time,u,isAP);// Check if the subtree rooted at v has a // connection to one of the ancestors of ulow[u]=min(low[u],low[v]);// If u is not a root and low[v] is greater than or equal to disc[u],// then u is an articulation pointif(parent!=-1&&low[v]>=disc[u]){isAP[u]=1;}}// Update low value of u for back edgeelseif(v!=parent){low[u]=min(low[u],disc[v]);}}// If u is root of DFS tree and has more than // one child, it is an articulation pointif(parent==-1&&children>1){isAP[u]=1;}}// Main function to find articulation points in the graphvector<int>articulationPoints(intV,vector<vector<int>>&edges){vector<vector<int>>adj=constructAdj(V,edges);vector<int>disc(V,0),low(V,0),visited(V,0),isAP(V,0);inttime=0;// Run DFS from each vertex if not// already visited (to handle disconnected graphs)for(intu=0;u<V;u++){if(!visited[u]){findPoints(adj,u,visited,disc,low,time,-1,isAP);}}// Collect all vertices that are articulation pointsvector<int>result;for(intu=0;u<V;u++){if(isAP[u]){result.push_back(u);}}// If no articulation points are found, return vector containing -1returnresult.empty()?vector<int>{-1}:result;}intmain(){intV=5;vector<vector<int>>edges={{0,1},{1,4},{2,3},{2,4},{3,4}};vector<int>ans=articulationPoints(V,edges);for(intu:ans){cout<<u<<" ";}cout<<endl;return0;}
Java
importjava.util.*;classGfG{staticArrayList<ArrayList<Integer>>constructAdj(intV,int[][]edges){ArrayList<ArrayList<Integer>>adj=newArrayList<>();for(inti=0;i<V;i++)adj.add(newArrayList<>());for(int[]edge:edges){adj.get(edge[0]).add(edge[1]);adj.get(edge[1]).add(edge[0]);}returnadj;}// Helper function to perform DFS and find articulation points// using Tarjan's algorithm.staticvoidfindPoints(ArrayList<ArrayList<Integer>>adj,intu,int[]visited,int[]disc,int[]low,int[]time,intparent,int[]isAP){// Mark vertex u as visited and assign discovery// time and low valuevisited[u]=1;disc[u]=low[u]=++time[0];intchildren=0;// Process all adjacent vertices of ufor(intv:adj.get(u)){// If v is not visited, then recursively visit itif(visited[v]==0){children++;findPoints(adj,v,visited,disc,low,time,u,isAP);// Check if the subtree rooted at v has a // connection to one of the ancestors of ulow[u]=Math.min(low[u],low[v]);// If u is not a root and low[v] is greater // than or equal to disc[u],// then u is an articulation pointif(parent!=-1&&low[v]>=disc[u]){isAP[u]=1;}}// Update low value of u for back edgeelseif(v!=parent){low[u]=Math.min(low[u],disc[v]);}}// If u is root of DFS tree and has more than // one child, it is an articulation pointif(parent==-1&&children>1){isAP[u]=1;}}// Main function to find articulation points in the graphstaticArrayList<Integer>articulationPoints(intV,int[][]edges){ArrayList<ArrayList<Integer>>adj=constructAdj(V,edges);int[]disc=newint[V],low=newint[V],visited=newint[V],isAP=newint[V];int[]time={0};// Run DFS from each vertex if not// already visited (to handle disconnected graphs)for(intu=0;u<V;u++){if(visited[u]==0){findPoints(adj,u,visited,disc,low,time,-1,isAP);}}// Collect all vertices that are articulation pointsArrayList<Integer>result=newArrayList<>();for(intu=0;u<V;u++){if(isAP[u]==1){result.add(u);}}// If no articulation points are found, return list containing -1if(result.isEmpty())result.add(-1);returnresult;}publicstaticvoidmain(String[]args){intV=5;int[][]edges={{0,1},{1,4},{2,3},{2,4},{3,4}};ArrayList<Integer>ans=articulationPoints(V,edges);for(intu:ans){System.out.print(u+" ");}System.out.println();}}
Python
defconstructAdj(V,edges):adj=[[]for_inrange(V)]foredgeinedges:adj[edge[0]].append(edge[1])adj[edge[1]].append(edge[0])returnadj# Helper function to perform DFS and find articulation points# using Tarjan's algorithm.deffindPoints(adj,u,visited,disc,low,time,parent,isAP):# Mark vertex u as visited and assign discovery# time and low valuevisited[u]=1time[0]+=1disc[u]=low[u]=time[0]children=0# Process all adjacent vertices of uforvinadj[u]:# If v is not visited, then recursively visit itifnotvisited[v]:children+=1findPoints(adj,v,visited,disc,low,time,u,isAP)# Check if the subtree rooted at v has a # connection to one of the ancestors of ulow[u]=min(low[u],low[v])# If u is not a root and low[v] is greater than or equal to disc[u],# then u is an articulation pointifparent!=-1andlow[v]>=disc[u]:isAP[u]=1# Update low value of u for back edgeelifv!=parent:low[u]=min(low[u],disc[v])# If u is root of DFS tree and has more than # one child, it is an articulation pointifparent==-1andchildren>1:isAP[u]=1# Main function to find articulation points in the graphdefarticulationPoints(V,edges):adj=constructAdj(V,edges)disc=[0]*Vlow=[0]*Vvisited=[0]*VisAP=[0]*Vtime=[0]# Run DFS from each vertex if not# already visited (to handle disconnected graphs)foruinrange(V):ifnotvisited[u]:findPoints(adj,u,visited,disc,low,time,-1,isAP)# Collect all vertices that are articulation pointsresult=[uforuinrange(V)ifisAP[u]]# If no articulation points are found, return list containing -1returnresultifresultelse[-1]if__name__=="__main__":V=5edges=[[0,1],[1,4],[2,3],[2,4],[3,4]]ans=articulationPoints(V,edges)foruinans:print(u,end=' ')print()
C#
usingSystem;usingSystem.Collections.Generic;classGfG{staticList<List<int>>constructAdj(intV,int[,]edges){List<List<int>>adj=newList<List<int>>();for(inti=0;i<V;i++){adj.Add(newList<int>());}intM=edges.GetLength(0);for(inti=0;i<M;i++){intu=edges[i,0];intv=edges[i,1];adj[u].Add(v);adj[v].Add(u);}returnadj;}// Helper function to perform DFS and find articulation points// using Tarjan's algorithm.staticvoidfindPoints(List<List<int>>adj,intu,List<int>visited,List<int>disc,List<int>low,refinttime,intparent,List<int>isAP){// Mark vertex u as visited and assign discovery// time and low valuevisited[u]=1;disc[u]=low[u]=++time;intchildren=0;// Process all adjacent vertices of uforeach(intvinadj[u]){// If v is not visited, then recursively visit itif(visited[v]==0){children++;findPoints(adj,v,visited,disc,low,reftime,u,isAP);// Check if the subtree rooted at v has a // connection to one of the ancestors of ulow[u]=Math.Min(low[u],low[v]);// If u is not a root and low[v] is greater // than or equal to disc[u],// then u is an articulation pointif(parent!=-1&&low[v]>=disc[u]){isAP[u]=1;}}// Update low value of u for back edgeelseif(v!=parent){low[u]=Math.Min(low[u],disc[v]);}}// If u is root of DFS tree and has more than // one child, it is an articulation pointif(parent==-1&&children>1){isAP[u]=1;}}// Main function to find articulation points in the graphstaticList<int>articulationPoints(intV,int[,]edges){List<List<int>>adj=constructAdj(V,edges);List<int>disc=newList<int>(newint[V]);List<int>low=newList<int>(newint[V]);List<int>visited=newList<int>(newint[V]);List<int>isAP=newList<int>(newint[V]);inttime=0;// Run DFS from each vertex if not// already visited (to handle disconnected graphs)for(intu=0;u<V;u++){if(visited[u]==0){findPoints(adj,u,visited,disc,low,reftime,-1,isAP);}}// Collect all vertices that are articulation pointsList<int>result=newList<int>();for(intu=0;u<V;u++){if(isAP[u]==1){result.Add(u);}}// If no articulation points are found, return list containing -1returnresult.Count==0?newList<int>{-1}:result;}staticvoidMain(){intV=5;int[,]edges={{0,1},{1,4},{2,3},{2,4},{3,4}};List<int>ans=articulationPoints(V,edges);foreach(intuinans){Console.Write(u+" ");}Console.WriteLine();}}
JavaScript
// Build adjacency list from edge listfunctionconstructAdj(V,edges){constadj=Array.from({length:V},()=>[]);for(leti=0;i<edges.length;i++){const[u,v]=edges[i];adj[u].push(v);adj[v].push(u);}returnadj;}// Helper function to perform DFS and find articulation points// using Tarjan's algorithm.functionfindPoints(adj,u,visited,disc,low,timeRef,parent,isAP){// Mark vertex u as visited and assign discovery// time and low valuevisited[u]=1;disc[u]=low[u]=++timeRef.value;letchildren=0;// Process all adjacent vertices of ufor(letvofadj[u]){// If v is not visited, then recursively visit itif(!visited[v]){children++;findPoints(adj,v,visited,disc,low,timeRef,u,isAP);// Check if the subtree rooted at v has a // connection to one of the ancestors of ulow[u]=Math.min(low[u],low[v]);// If u is not a root and low[v] is greater // than or equal to disc[u],// then u is an articulation pointif(parent!==-1&&low[v]>=disc[u]){isAP[u]=1;}}// Update low value of u for back edgeelseif(v!==parent){low[u]=Math.min(low[u],disc[v]);}}// If u is root of DFS tree and has more than // one child, it is an articulation pointif(parent===-1&&children>1){isAP[u]=1;}}// Main function to find articulation points in the graphfunctionarticulationPoints(V,edges){constadj=constructAdj(V,edges);constdisc=Array(V).fill(0);constlow=Array(V).fill(0);constvisited=Array(V).fill(0);constisAP=Array(V).fill(0);consttimeRef={value:0};// Run DFS from each vertex if not// already visited (to handle disconnected graphs)for(letu=0;u<V;u++){if(!visited[u]){findPoints(adj,u,visited,disc,low,timeRef,-1,isAP);}}// Collect all vertices that are articulation pointsconstresult=[];for(letu=0;u<V;u++){if(isAP[u]){result.push(u);}}// If no articulation points are found, return array containing -1returnresult.length===0?[-1]:result;}// Driver CodeconstV=5;constedges=[[0,1],[1,4],[2,3],[2,4],[3,4]];constans=articulationPoints(V,edges);console.log(ans.join(' '));
Output
1 4
Time Complexity: O(V + E), we are performing a single DFS operation that works on O(V + E) time complexity. Auxiliary Space: O(V), used by five arrays, each of size V