//GT P5
#include<bits/stdc++.h>
using namespace std;
void APUtil(vector<int> adj[], int u, bool visited[],
int disc[], int low[], int& time, int parent,
bool isAP[])
{
int children = 0;
visited[u] = true;
disc[u] = low[u] = ++time;
for (auto v : adj[u]) {
if (!visited[v]) {
children++;
APUtil(adj, v, visited, disc, low, time, u,
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 && children > 1)
isAP[u] = true;
}
void AP(vector<int> adj[], int V)
{
int disc[V] = { 0 };
int low[V];
bool visited[V] = { false };
bool isAP[V] = { false };
int time = 0, par = -1;
for (int u = 0; u < V; u++)
if (!visited[u])
APUtil(adj, u, visited, disc, low,
time, par, isAP);
for (int u = 0; u < V; u++)
if (isAP[u] == true)
cout << u << " ";
}
void addEdge(vector<int> adj[], int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
int main()
{
cout << "cut vertices set in first graph \n";
int V = 5;
vector<int> adj1[V];
addEdge(adj1, 1, 0);
addEdge(adj1, 0, 2);
addEdge(adj1, 2, 1);
addEdge(adj1, 0, 3);
addEdge(adj1, 3, 4);
AP(adj1, V);
/*
int v1,e1;
cout<<"Enter The No. of Vertices and Edges for
First graph\n";
cin>>v1>>e1;
cout<<"Enter Adjacency List for First Graph: \n";
//cout<<"Enter The Adjacency List for First Graph:
";
vector <vector<int> > gph1(v1);
int vert1,vert2;
for(int i=0;i<e1;i++)
{
cin>>vert1>>vert2;
gph1[vert1].push_back(vert2);
gph1[vert2].push_back(vert1);
}
AP(gph1, v1);
*/
cout << "\ncut vertices set in second graph \n";
V = 7;
vector<int> adj3[V];
addEdge(adj3, 0, 1);
addEdge(adj3, 1, 2);
addEdge(adj3, 2, 0);
addEdge(adj3, 1, 3);
addEdge(adj3, 1, 4);
addEdge(adj3, 1, 6);
addEdge(adj3, 3, 5);
addEdge(adj3, 4, 5);
AP(adj3, V);
return 0;
}
Comments
Post a Comment