import java.util.*; public class GraphComponents { public static int countComponents(int n, int[][] edges) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); for (int[] edge : edges) { graph.get(edge[0]).add(edge[1]); graph.get(edge[1]).add(edge[0]); } boolean[] visited = new boolean[n]; int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { bfs(i, graph, visited); count++; } } return count; } private static void bfs(int start, List<List<Integer>> graph, boolean[] visited) { Queue<Integer> queue = new LinkedList<>(); queue.offer(start); visited[start] = true; while (!queue.isEmpty()) { int node = queue.poll(); for (int neighbor : graph.get(node)) { if (!visited[neighbor]) { visited[neighbor] = true; queue.offer(neighbor); } } } } public static void main(String[] args) { int n = 5; int[][] edges = {{0,1},{1,2},{3,4}}; System.out.println(countComponents(n, edges)); } }
The graph has 5 nodes and edges connecting 0-1, 1-2, and 3-4. This forms two connected groups: {0,1,2} and {3,4}. So the output is 2.
BFS starts from a node and visits all nodes reachable from it, marking them visited. This fully explores one connected component before moving to another unvisited node.
import java.util.*; public class BuggyGraph { public static int countComponents(int n, int[][] edges) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) graph.add(new ArrayList<>()); for (int[] edge : edges) { graph.get(edge[0]).add(edge[1]); graph.get(edge[1]).add(edge[0]); } boolean[] visited = new boolean[n]; int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { bfs(i, graph, visited); count++; } } return count; } private static void bfs(int start, List<List<Integer>> graph, boolean[] visited) { Queue<Integer> queue = new LinkedList<>(); queue.offer(start); visited[start] = true; while (!queue.isEmpty()) { int node = queue.poll(); for (int neighbor : graph.get(node)) { if (!visited[neighbor]) { visited[neighbor] = true; queue.offer(neighbor); } } } }
The graph list is initialized with n+1 lists but visited array has size n. This mismatch can cause IndexOutOfBoundsException when accessing visited[neighbor] for neighbor == n.
import java.util.*; public class SyntaxGraph { public static int countComponents(int n, int[][] edges) { List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); for (int[] edge : edges) { graph.get(edge[0]).add(edge[1]); graph.get(edge[1]).add(edge[0]); } boolean[] visited = new boolean[n]; int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { bfs(i, graph, visited); count++; } } return count; } private static void bfs(int start, List<List<Integer>> graph, boolean[] visited) { Queue<Integer> queue = new LinkedList<>(); queue.offer(start); visited[start] = true; while (!queue.isEmpty()) { int node = queue.poll(); for (int neighbor : graph.get(node)) { if (!visited[neighbor]) { visited[neighbor] = true; queue.offer(neighbor); } } } }
Option C has a syntax error: the enhanced for loop must have parentheses around the variable declaration. Missing '(' causes syntax error.
Initially, components are {0,1,2}, {3,4}, and {5}. Adding edge {2,3} connects {0,1,2} and {3,4} into one component. Node 5 remains isolated. So total components = 2.
