Articulation Point - I | Practice | GeeksforGeeks

Given an undirected connected graph with V vertices and adjacency list adj. You are required to find all the vertices removing which (and edges through it) disconnects the graph into 2 or more components.
Note: Indexing is zero-based i.e nodes numbering from (0 to V-1). There might be loops present in the graph.
Example 1:
Input:

Output:{1,4}
Explanation: Removing the vertex 1 will
discconect the graph as-

Removing the vertex 4 will disconnect the
graph as-


 
Your Task:
You don't need to read or print anything. Your task is to complete the function articulationPoints() which takes V and adj as input parameters and returns a list containing all the vertices removing which turn the graph into two or more disconnected components in sorted order. If there are no such vertices then returns a list containing -1.
 
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)
 
Constraints:
1 ≤ V ≤ 104
0
amishasahu3283 days ago
void dfs(int node, int parent, vector<int> &tin, vector<int> &low, vector<int> &vis,
             vector<int> adj[], int &timer, vector<int> &isArticulation){
                 vis[node] = 1;
                 tin[node] = low[node] = timer++;
                 int child = 0;
                 for(auto it: adj[node]){
                     if(!vis[it]){
                         dfs(it, node, tin, low, vis, adj, timer, isArticulation);
                         low[node] = min(low[it], low[node]);
                         if(low[it] >= tin[node] && parent != -1)
                            isArticulation[node] = 1;
                        child++;
                     }
                     else
                        low[node] = min(low[node], tin[it]);
                 }
                 
                 if(parent == -1 && child > 1)
                    isArticulation[node] = 1;
             }
    vector<int> articulationPoints(int V, vector<int>adj[]) {
        // Code here
        vector<int> tin(V, -1);
        vector<int> low(V, -1);
        vector<int> vis(V, 0);
        vector<int> isArticulation(V, 0);
        
        int timer = 0;
        for(int i = 0; i < V; i++){
            if(!vis[i])
                dfs(i, -1, tin, low, vis, adj, timer, isArticulation);
        }
        vector<int> ans;
        for(int i = 0; i < V; i++){
            if(isArticulation[i])
                ans.push_back(i);
        }
        if(ans.size() == 0) ans.push_back(-1);
        return ans;
    }
0
saurabhsathe12343 weeks ago
void AP(int V, vector<int>adj[] , bool visited[] , int disc[] , int low[] ,
            vector<int> &isAP, int parent , int &disc_time , int u  )
    {
        visited[u] = true ;
        disc_time ++ ;
        disc[u] = low[u] = disc_time ;
        int children = 0;
        
        for(int v : adj[u])
        {
            if(!visited[v])
            {
                AP(V,adj,visited,disc,low,isAP,u,disc_time,v);
                children++;
                
                low[u] = min(low[u], low[v]);
                
                if(parent != -1 && low[v] >= disc[u])
                {
                    isAP[u] = 1;
                }
                    
            }
            else if(parent != v)
            {
                low[u] = min(low[u] , disc[v]);
            }
                
                
        }
        if(parent== -1 && children>1)
            isAP[u] = 1;
    }
    
    vector<int> articulationPoints(int V, vector<int>adj[]) {
        // Code here
        
        bool visited[V] = {false};
        int disc[V] = {0};
        int low[V] = {0};
        vector<int> isAP(V,0);
        
        int parent = -1 , disc_time = 0 ;
        
        for(int i=0 ; i<V ; i++)
        {
            if(!visited[i])
            {
                AP(V,adj,visited,disc,low,isAP,-1,disc_time,i);
            }
        }
        vector<int> res;
        for(int i=0;i<V;i++)
        {
            if(isAP[i] == 1)
                res.push_back(i);
                
        }
        if (res.size() == 0)
            res.push_back(-1);
            
        return res;
    }
+1
itsmohitmk991 month ago
Java - 0.8 sec
 
class Solution
{
    HashSet<Integer> set;   
    public ArrayList<Integer> articulationPoints(int v , ArrayList<ArrayList<Integer>> adj)
    {
        int[] tin = new int[v];
        int[] low = new int[v];
        int[] visited = new int[v];
        set = new HashSet<>();
        
        solve(adj , 0 , -1 , tin , low ,visited , 1);
        
        Iterator<Integer> it = set.iterator();
        
        ArrayList<Integer> ll = new ArrayList<>();
        
        while(it.hasNext()){
            ll.add(it.next());
        }
        if(ll.size() == 0)
            ll.add(-1);
            
        Collections.sort(ll);
        return ll;
        
    }
    
    void solve(ArrayList<ArrayList<Integer>> adj ,int src , int parent , int[] tin , int[] low , int[] visited, int timer){
        
        visited[src] = 1;
        low[src] = tin[src] = timer++;
        
        int child = 0;
        for(Integer nn : adj.get(src)){
            
            if(visited[nn] == 0 ){
                solve(adj , nn , src, tin , low , visited , timer);
                low[src] = Math.min(low[src] , low[nn]);
                
                if(low[nn] >= tin[src] && parent != -1){
                    set.add(src);
                }
                
                child++;
            }
            else{
                low[src] = Math.min(low[src] , tin[nn] ); 
            }
            
        }
        
        if(child > 1 && parent == -1){
            set.add(src);
        } 
    } 
}
+2
jk30012 months ago
If your code is failing at 2nd test case try handling the case when there is no articulation point.
0
soumo2k153 months ago
public: int t=0; void dfs(int x,set<int>&s,vector<int>&vis,vector<int>&parent,vector<int>&low,vector<int>&act,vector<int>adj[]){     vis[x]=1;low[x]=act[x]=t;t++;     int child=0;     for(auto it:adj[x]){         if(!vis[it]){             vis[it]=1;             parent[it]=x;             child++;             dfs(it,s,vis,parent,low,act,adj);             low[x]=min(low[x],low[it]);             if(parent[x]==-1 && child>1)s.insert(x);             if(parent[x]!=-1 && low[it]>=act[x])s.insert(x);         }         else if(it!=parent[x]){             low[x]=min(low[x],act[it]);         }     } }   vector<int> articulationPoints(int v, vector<int>adj[]) {       vector<int>vis(v,0),parent(v,-1),low(v,0),act(v,0);       set<int>s;       for(int i=0;i<v;i++)if(!vis[i])dfs(i,s,vis,parent,low,act,adj);       if(s.empty())return {-1};       vector<int>ans(s.begin(),s.end());       return ans;   }
+1
aditya112gaikwad3 months ago
class Solution { public:    void dfs(int node, int parent, vector<int> &vis, vector<int> &tin, vector<int> &low, int &timer, vector<int> adj[], vector<int> &isArticulation)   {       vis[node] = 1;       tin[node] = low[node] = timer++;       int child = 0;       for(auto it : adj[node])       {                      if(!vis[it])           {               child++;               dfs(it, node, vis, tin, low, timer, adj, isArticulation);                              low[node] = min(low[node], low[it]);                              if(low[it] >= tin[node] && parent != -1)               {                   isArticulation[node] = 1;               }                          }           else if(it != parent)           {               low[node] = min(low[node], tin[it]);           }       }              if(parent == -1 && child>1)           isArticulation[node] = 1;   }    vector<int> articulationPoints(int n, vector<int>adj[]) {       // Code here       vector<int>ans;       vector<int> tin(n,-1);       vector<int> low(n,-1);       vector<int> vis(n,0);       vector<int> isArticulation(n,0);       int timer = 0;       for(int i=0;i<n;i++)       {           if(!vis[i])           {               dfs(i,-1,vis,tin,low,timer,adj,isArticulation);           }       }       for(int i=0;i<n;i++)       {           if(isArticulation[i]==1)                ans.push_back(i);       }       if(ans.size()==0)       {           return {-1};       }       return ans;   }};
0
amanpandey30073 months ago
void dfs(int u,unordered_set<int>&s,vector<int>&dis,vector<int>&low,vector<int>&parent,vector<int>adj[])   {       static int t=0;       low[u]=dis[u]=t;       t++;              int children=0;              for(auto v:adj[u])       {           if(dis[v]==-1)           {                  parent[v]=u;               children++;               dfs(v,s,dis,low,parent,adj);                              low[u]=min(low[u],low[v]);                              if(parent[u]==-1 and children>1)                 s.insert(u);                              if(parent[u]!=-1 and low[v]>=dis[u])                 s.insert(u);                          }           else if(v!=parent[u])           {               low[u]=min(low[u],dis[v]);           }                  }   }   vector<int> articulationPoints(int v, vector<int>adj[])    {      unordered_set<int>s;      vector<int>dis(v,-1),low(v,-1),parent(v,-1);            for(int i=0;i<v;i++)      {          if(dis[i]==-1)          dfs(i,s,dis,low,parent,adj);      }            if(s.empty())      return {-1};            vector<int>ans(s.begin(),s.end());            sort(ans.begin(),ans.end());      return ans;   }
0
sk77887754 months ago
class Solution
{
    //Function to return Breadth First Traversal of given graph.
    int cou=0;
    int[] disc;
    int[] low;
    public ArrayList<Integer> articulationPoints(int V,ArrayList<ArrayList<Integer>> adj)
    {
        // Code here
        ArrayList<Integer> res=new ArrayList<>();
        HashSet<Integer> hs=new HashSet<>();
        boolean[] vis=new boolean[V];
        disc=new int[V];low=new int[V];
        solve(adj,0,-1,hs,vis); 
        if(hs.size()==0) res.add(-1); 
        for(int x:hs) res.add(x);
        Collections.sort(res); return res;
    }
    
    public void solve(ArrayList<ArrayList<Integer>> adj,int u,int p, 
        HashSet<Integer> res,boolean[] vis) {
        disc[u]=low[u]=(++cou);
        vis[u]=true;int cou=0;
        for(int v:adj.get(u)) {
            if(!vis[v]) {
                cou++;
                solve(adj,v,u,res,vis);
                low[u]=Math.min(low[u],low[v]);
                if(p==-1 && cou>1)
                    res.add(u);
                if(p!=-1 && low[v]>=disc[u])
                    res.add(u);
            } else if(v!=p) {
                low[u]=Math.min(low[u],disc[v]);
            }
        }
    }
}
0
devangrao255 months ago
void DFS(vector<int> adj[], bool visited[], int u, int parent, int disc[], int low[],    int &timer, bool isAP[])   {      visited[u] = true;            low[u] = disc[u] = ++timer;      int child = 0;            for(auto v : adj[u])      {          if(!visited[v])          {               child++;                              DFS(adj, visited, v, u, disc, low, timer, isAP);                              low[u] = min(low[u], low[v]);                              if(parent != -1 && low[v] >= disc[u])                   isAP[u]= true;          }          else if(v != parent)          {              low[u] = min(low[u], disc[v]);          }      }            if(parent == -1 && child > 1)         isAP[u] = true;   }      vector<int> articulationPoints(int V, vector<int>adj[])    {       bool visited[V] = {false};       int disc[V] = {0};       int low[V];       int timer = 0, parent = -1;              bool isAP[V] = {false};              for(int i = 0; i < V; i++)       {           if(!visited[i])              DFS(adj, visited, i, parent, disc, low, timer, isAP);       }              vector<int> res;       for(int i = 0; i < V; i++)       {            if(isAP[i] == true)               res.push_back(i);       }              if(res.size() < 1)         res.push_back(-1);              return res;;   }
0
ghazanferwahab26 months ago
 private void dfs(int node,int parent,boolean vis[],int tin[],int low[],   ArrayList<ArrayList<Integer>> adj,int timer, ArrayList<Integer> a){       vis[node]=true;       tin[node]=low[node]=timer++;       int child=0;       for(Integer x:adj.get(node)){           if(x==parent)               continue;           if(!vis[x]){               dfs(x,node,vis,tin,low,adj,timer,a);               low[node]=Math.min(low[node],low[x]);               if(low[x]>=tin[node] && parent!=-1){                   if(!a.contains(node))                       a.add(node);               }               child++;//if there is dfs  call for multiple childs we take note of it           }else if(vis[x] && x!=parent){               low[node]=Math.min(low[node],tin[x]);           }       }       if(parent ==-1 && child>1){//if the starting node has more than 1 components       //that are not connceted to each other           a.add(node);       }   }   public ArrayList<Integer> articulationPoints(int V,ArrayList<ArrayList<Integer>> adj)   {       // Code here       boolean vis[]=new boolean[V];       int low[]=new int[V];       int tin[]=new int[V];       int timer=0;       ArrayList<Integer> a=new ArrayList<Integer>();       for(int i=0;i<V;i++){           if(!vis[i]){               dfs(i,-1,vis,tin,low,adj,timer,a);           }       }       if(a.size()==0)           {               a.add(-1);               return a;           }       Collections.sort(a);       //a.remove(a.get(0));       return a;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.