1. Vertex Removal and Component Counting
Intuition
Count initial connected components. Then for each vertex i, ignore vertex i and count connected components of the remaining graph using BFS. If components increase, i is an articulation point.
Algorithm
- Count initial connected components in the graph with BFS.
- For each vertex
ifrom 0 to V-1: - Run BFS/DFS skipping vertex
i. Count connected components among the remaining vertices. - If remaining components > initial components, mark
ias an articulation point. - Return sorted articulation points, or
[-1]if none found.
#include <iostream>#include <vector>#include <queue>#include <algorithm> using namespace std; class Solution { int countComponents(int V, const vector<vector<int>>& adj, int skip) { vector<bool> visited(V, false); int comps = 0; for (int i = 0; i < V; ++i) { if (i == skip || visited[i]) continue; comps++; queue<int> q; q.push(i); visited[i] = true; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (v != skip && !visited[v]) { visited[v] = true; q.push(v); } } } } return comps; } public: vector<int> articulationPoints(int V, vector<vector<int>>& adj) { int initialComps = countComponents(V, adj, -1); vector<int> result; for (int i = 0; i < V; ++i) { int comps = countComponents(V, adj, i); if (comps > initialComps) { result.push_back(i); } } if (result.empty()) return {-1}; return result; }}; int main() { int V = 5; vector<vector<int>> adj(V); vector<pair<int, int>> edges = {{0, 1}, {1, 2}, {2, 0}, {1, 3}, {3, 4}}; for (auto& e : edges) { adj[e.first].push_back(e.second); adj[e.second].push_back(e.first); } Solution solver; vector<int> aps = solver.articulationPoints(V, adj); for (int u : aps) cout << u << " "; cout << endl; return 0;}Time & Space Complexity
- Time complexity:
O(V * (V + E)) - Space complexity:
O(V + E)