From 97d0b71f9acbb85ac669d17643ec627deb48a7f4 Mon Sep 17 00:00:00 2001 From: MAYANK JAIN Date: Sat, 12 Oct 2019 00:56:32 +0530 Subject: [PATCH] Added Articulation Points finding Algorithm in C++ This adds an algorithm that can be used to find the articulation points in a Graph. --- .../C++/articulationpoints.cpp | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Graphs/Articulation_Point/C++/articulationpoints.cpp diff --git a/Graphs/Articulation_Point/C++/articulationpoints.cpp b/Graphs/Articulation_Point/C++/articulationpoints.cpp new file mode 100644 index 00000000..cf6d772b --- /dev/null +++ b/Graphs/Articulation_Point/C++/articulationpoints.cpp @@ -0,0 +1,69 @@ +//this code can be used to find Articulation points in a Graph +#include +using namespace std; + +int timee;// a variable to keep track of the time at which a particular vertex is encounterd + +vector graph[100];//adjacency list for the graph (100 is the max number of vertices.. you can change as per your needs) +bool visited[100] , AP[100]; +//visited - bool array used in DFS to know whether the node is visited or not +//AP - bool array to tell whether ith vertex is a AP or NOT + +int disc[100] , low[100] , parent[100]; +//disc - discovery time of a vertex +//low - exit time of a source +//parent - array to tell the parent of ith vertex + +//finding all APs +void printAP2(int source){ + visited[source] = true; + disc[source] = timee++; + low[source] = disc[source]; + int child = 0; + for(auto i : graph[source]){ + if(!visited[i]){ + child++; + parent[i] = source; + printAP2(i); + low[source] = min(low[i] , low[source]); + + if(parent[source] == source){ + if(child > 1) AP[source] = true; + } + else{ + if(low[i] >= disc[source]) AP[source] = true; // IMP + } + } + else if(i != parent[source]) low[source] = min(low[i] , low[source]); + } +} + +//find and print all APs +void printAP(int n , int source){ + for(int i = 0 ; i < n ; i++) low[i] = INT_MAX; + parent[source] = source; //initializing source os source vertex to itself + printAP2(source); + for(int i = 0 ; i < n ; i++){ + if(!visited[i]){ + parent[i] = i; + printAP2(i); + } + } + for(int i = 0 ; i < n ; i++){ + if(AP[i]) cout << i << " "; + } + cout << endl; +} + +int main(){ + //n - no. of vertices , e - no. of edges + int n , e , u , v; + cin >> n >> e; + //inputting e edges + for(int i = 0 ; i < e ; i++){ + cin >> u >> v; + graph[u].push_back(v); + graph[v].push_back(u); + } + printAP(n , 0); +}