A vertex is called an articulation point if removing it and all the edges associated with it results in an increase in the number of connected components in the graph.
Example:
Input: n=6 , edges={{0,1},{0,5},{1,2},{1,3},{2,3},{2,4},{3,4}}
Output: Articulation points are: 1 0
Approach
Java
import java.util.ArrayList;public class ArticulationPointsInGraph {// visited listpublic static int visited[];static int low[];static int tin[];static int timer = 0;// Adjacency Listsstatic ArrayList<ArrayList<Integer>> adj = new ArrayList<>();public static void main(String[] args) {int n = 6;visited = new int[n];tin = new int[n];low = new int[n];for (int i = 0; i < n; i++)adj.add(new ArrayList<>());addEdge(adj, 0, 1);addEdge(adj, 0, 5);addEdge(adj, 1, 2);addEdge(adj, 1, 3);addEdge(adj, 2, 3);addEdge(adj, 2, 4);addEdge(adj, 3, 4);// Iterate all connected componentSystem.out.println("Articulation points are: ");for (int i = 0; i < n; i++) {if (visited[i] == 0)dfs(i, -1);}}// dfs function to find the// articulation points in the graphstatic void dfs(int node, int par) {// mark the current node ad visitedvisited[node] = 1;// assign the in time and low time// as cuurent dfs timertin[node] = timer;low[node] = timer;// increment the dfs timertimer++;int children = 0;// iterate for all adjacent nodes// of the current nodefor (int child : adj.get(node)) {// if node is parent then continueif (child == par)continue;// if already visitedif (visited[child] != 0) {low[node] = Math.min(low[node], tin[child]);} else {dfs(child, node);low[node] = Math.min(low[node], low[child]);if (low[child] >= tin[node] && par != -1)System.out.print(node + " ");children++;}}if (par == -1 && children > 1)System.out.println(node + " ");}private static void addEdge(ArrayList<ArrayList<Integer>> g,int u, int v) {// undirected graphg.get(u).add(v);g.get(v).add(u);}}
C++
#include <bits/stdc++.h>using namespace std;vector<bool> vis;vector<int> tin,low;int timer;vector<int> arr[10001];//dfs function to find the//articulation points in the graphvoid dfs(int node,int par=-1){//mark the current node ad visitedvis[node]=true;//assign the in time and low time//as cuurent dfs timertin[node]=low[node]=timer;//increment the dfs timertimer++;int children=0;//iterate for all adjacent nodes//of the current nodefor(int child:arr[node]){//if node is parent then continueif(child==par)continue;//if already visitedif(vis[child]==true){low[node]=min(low[node],tin[child]);}else{dfs(child,node);low[node]=min(low[node],low[child]);if(low[child]>=tin[node]&&par!=-1)cout<<node<<" ";children++;}}if(par==-1&&children>1)cout<<node<<" ";}void find_cutpoints(int n){timer=0;vis.assign(n+1,false);tin.assign(n+1,-1);low.assign(n+1,-1);}void addEdge(int u,int v){arr[u].push_back(v);arr[v].push_back(u);}int main(){int n=6;addEdge(0,1);addEdge(0,5);addEdge(1,2);addEdge(1,3);addEdge(2,3);addEdge(2,4);addEdge(3,4);find_cutpoints(n);cout<<"Articulation points are: ";for(int i=0;i<n;i++){if(vis[i]==false)dfs(i);}return 0;}
No comments:
Post a Comment