Skip to content

Commit b516d0d

Browse files
Merge pull request #369 from Saujanya77/feature/tarjan
Added Tarjan's Algorithm for Bridges & Articulation Points in Java
2 parents 0d44d7c + d018692 commit b516d0d

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Tarjan's Algorithm for Bridges and Articulation Points in a Graph
3+
*
4+
* 👉 What are Bridges?
5+
* An edge in a graph is a "bridge" if removing it increases the number of connected components.
6+
*
7+
* 👉 What are Articulation Points (Cut Vertices)?
8+
* A vertex is an articulation point if removing it increases the number of connected components.
9+
*
10+
* 📌 Why Important?
11+
* These concepts are critical in:
12+
* - Network reliability (finding weak links in a network).
13+
* - System design (critical servers or routers).
14+
* - Graph theory problems in competitive programming.
15+
*
16+
* 📌 Algorithm Idea (Tarjan’s DFS):
17+
* - Perform DFS traversal of the graph.
18+
* - Maintain:
19+
* 1. discovery[] = time when a node is discovered
20+
* 2. low[] = lowest discovery time reachable from that node
21+
* - For a node u and its neighbor v:
22+
* - If low[v] > discovery[u], then edge (u, v) is a BRIDGE.
23+
* - If discovery[u] <= low[v], then u is an ARTICULATION POINT.
24+
*
25+
* Time Complexity: O(V + E)
26+
* Space Complexity: O(V)
27+
*/
28+
29+
import java.util.*;
30+
31+
public class TarjanBridgesArticulation {
32+
private int V; // number of vertices
33+
private List<List<Integer>> adj; // adjacency list
34+
private int time; // global time for DFS traversal
35+
36+
private int[] discovery; // discovery time of nodes
37+
private int[] low; // lowest discovery time reachable
38+
private boolean[] visited;
39+
private boolean[] isArticulation; // articulation marker
40+
41+
// Constructor
42+
public TarjanBridgesArticulation(int vertices) {
43+
this.V = vertices;
44+
adj = new ArrayList<>();
45+
for (int i = 0; i < V; i++) {
46+
adj.add(new ArrayList<>());
47+
}
48+
discovery = new int[V];
49+
low = new int[V];
50+
visited = new boolean[V];
51+
isArticulation = new boolean[V];
52+
time = 0;
53+
}
54+
55+
// Add edge (undirected graph)
56+
public void addEdge(int u, int v) {
57+
adj.get(u).add(v);
58+
adj.get(v).add(u);
59+
}
60+
61+
// DFS helper
62+
private void dfs(int u, int parent, List<String> bridges) {
63+
visited[u] = true;
64+
discovery[u] = low[u] = ++time; // initialize discovery and low
65+
66+
int children = 0; // count of children in DFS Tree
67+
68+
for (int v : adj.get(u)) {
69+
if (v == parent) continue; // ignore the edge to parent
70+
71+
if (!visited[v]) {
72+
children++;
73+
dfs(v, u, bridges);
74+
75+
// Update low[u] considering child v
76+
low[u] = Math.min(low[u], low[v]);
77+
78+
// --- Bridge condition ---
79+
if (low[v] > discovery[u]) {
80+
bridges.add("Bridge found: (" + u + " - " + v + ")");
81+
}
82+
83+
// --- Articulation Point condition ---
84+
if (parent != -1 && low[v] >= discovery[u]) {
85+
isArticulation[u] = true;
86+
}
87+
} else {
88+
// Update low[u] for back edge
89+
low[u] = Math.min(low[u], discovery[v]);
90+
}
91+
}
92+
93+
// Root articulation condition
94+
if (parent == -1 && children > 1) {
95+
isArticulation[u] = true;
96+
}
97+
}
98+
99+
// Main function to find Bridges and Articulation Points
100+
public void findBridgesAndArticulationPoints() {
101+
List<String> bridges = new ArrayList<>();
102+
103+
for (int i = 0; i < V; i++) {
104+
if (!visited[i]) {
105+
dfs(i, -1, bridges);
106+
}
107+
}
108+
109+
// Print Bridges
110+
System.out.println("🔗 Bridges in the Graph:");
111+
if (bridges.isEmpty()) {
112+
System.out.println("No bridges found.");
113+
} else {
114+
for (String b : bridges) System.out.println(b);
115+
}
116+
117+
// Print Articulation Points
118+
System.out.println("\n⚡ Articulation Points in the Graph:");
119+
boolean foundAP = false;
120+
for (int i = 0; i < V; i++) {
121+
if (isArticulation[i]) {
122+
System.out.println("Articulation Point: " + i);
123+
foundAP = true;
124+
}
125+
}
126+
if (!foundAP) {
127+
System.out.println("No articulation points found.");
128+
}
129+
}
130+
131+
// --- Driver Code / Example ---
132+
public static void main(String[] args) {
133+
/*
134+
* Example Graph:
135+
* 0 ----- 1
136+
* | / |
137+
* | / |
138+
* | / |
139+
* 2 3
140+
* |
141+
* 4
142+
*/
143+
TarjanBridgesArticulation graph = new TarjanBridgesArticulation(5);
144+
145+
graph.addEdge(0, 1);
146+
graph.addEdge(0, 2);
147+
graph.addEdge(1, 2);
148+
graph.addEdge(1, 3);
149+
graph.addEdge(3, 4);
150+
151+
graph.findBridgesAndArticulationPoints();
152+
}
153+
}
154+

0 commit comments

Comments
 (0)