[go: up one dir, main page]

0% found this document useful (0 votes)
167 views4 pages

Articulation Points

This C++ program finds articulation points in an undirected graph. It uses depth-first search (DFS) recursion to calculate discovery and low times to identify articulation points. The program takes a graph as input, performs DFS to find articulation points, and outputs the articulation points and connected components after removing each articulation point.

Uploaded by

aekdho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views4 pages

Articulation Points

This C++ program finds articulation points in an undirected graph. It uses depth-first search (DFS) recursion to calculate discovery and low times to identify articulation points. The program takes a graph as input, performs DFS to find articulation points, and outputs the articulation points and connected components after removing each articulation point.

Uploaded by

aekdho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// A C++ program to find articulation points in a given undirected graph

#include<iostream>
#include <list>
#define NIL -1
using namespace std;
// A class that represents an undirected graph
class Graph
{
int V;
// No. of vertices
list<int> *adj;
// A dynamic array of adjacency lists
void APUtil(int v, bool visited[], int disc[], int low[],
int parent[], bool ap[]);
void DFSUtil(int v, bool visited[]);
public:
bool *ap; // To store articulation points
Graph(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void AP();
// prints articulation points
void DFS();
};
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v); // Note: the graph is undirected
}
// A recursive function that find articulation points using DFS traversal
// u --> The vertex to be visited next
// visited[] --> keeps tract of visited vertices
// disc[] --> Stores discovery times of visited vertices
// parent[] --> Stores parent vertices in DFS tree
// ap[] --> Store articulation points
void Graph::APUtil(int u, bool visited[], int disc[],
int low[], int parent[], bool ap[])
{
// A static variable is used for simplicity, we can avoid use of static
// variable by passing a pointer.
static int time = 0;
// Count of children in DFS Tree
int children = 0;
// Mark the current node as visited
visited[u] = true;
// Initialize discovery time and low value
disc[u] = low[u] = ++time;
// Go through all vertices aadjacent to this
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)

{
int v = *i; // v is current adjacent of u
// If v is not visited yet, then make it a child of u
// in DFS tree and recur for it
if (!visited[v])
{
children++;
parent[v] = u;
APUtil(v, visited, disc, low, parent, ap);
// Check if the subtree rooted with v has a connection to
// one of the ancestors of u
low[u] = min(low[u], low[v]);
// u is an articulation point in following cases
// (1) u is root of DFS tree and has two or more chilren.
if (parent[u] == NIL && children > 1)
ap[u] = true;
// (2) If u is not root and low value of one of its child is more
// than discovery value of u.
if (parent[u] != NIL && low[v] >= disc[u])
ap[u] = true;
}
// Update low value of u for parent function calls.
else if (v != parent[u])
low[u] = min(low[u], disc[v]);
}
}
// The function to do DFS traversal. It uses recursive function APUtil()
void Graph::AP()
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
int *disc = new int[V];
int *low = new int[V];
int *parent = new int[V];
*ap = new bool[V];
// Initialize parent and visited, and ap(articulation point) arrays
for (int i = 0; i < V; i++)
{
parent[i] = NIL;
visited[i] = false;
ap[i] = false;
}
// Call the recursive helper function to find articulation points
// in DFS tree rooted with vertex 'i'
for (int i = 0; i < V; i++)
if (visited[i] == false)
APUtil(i, visited, disc, low, parent, ap);

class Graph1
{
int V;
// No. of vertices
list<int> *adj;
// Pointer to an array containing adjacency lists
void DFSUtil(int v, bool visited[]); // A function used by DFS
public:
Graph1(int V); // Constructor
void addEdge(int v, int w); // function to add an edge to graph
void DFS(int not1);
// prints DFS traversal of the complete graph
};
Graph1::Graph1(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph1::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v s list.
}
void Graph1::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices adjacent to this vertex
list<int>::iterator i;
for(i = adj[v].begin(); i != adj[v].end(); ++i)
if(!visited[*i])
DFSUtil(*i, visited);
}
// The function to do DFS traversal. It uses recursive DFSUtil()
void Graph1::DFS(int not1)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for(int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to print DFS traversal
// starting from all vertices one by one
for(int i = 0; i < V; i++)
{
if(i == not1)
continue;
if(visited[i] == false)
{
cout<<endl<<not1<<" ";
DFSUtil(i, visited);
}
}
}
// Driver program to test above function
int main()

{
// Create graphs given in above diagrams
cout << "\nArticulation points in first graph \n";
int n,i,j,k,temp,temp1;
cout<<"enter no of nodes..";
cin>>n;
int A[20][20];
Graph g1(5);
bool ap[100];
cout<<"enter the graph..\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
cin>>temp;
A[i][j] = temp;
if(temp == 1)
g1.addEdge(i, j);
}
g1.AP();
for (int i = 0; i < n; i++)
ap[i] = g1.ap[i];
cout<<"The articulation points are..";
for (int i = 0; i < n; i++)
if (ap[i] == true)
cout << i << " ";

for (int i = 0; i < n; i++)


if (ap[i] == true)
{
Graph1 g(n);
cout<<"\n components with articulation point " << i<<"\n";
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(A[j][k] == 1 && i!=j && i!=k)
{
//cout<<" Edge added bwten "<<j<<" "<<k<<"\n";
g.addEdge(j,k);
}
g.DFS(i);
}
return 0;
}

You might also like