Complete the code to initialize the adjacency list for a graph with 5 nodes.
List<List<Integer>> graph = new ArrayList<>(); for (int i = 0; i < [1]; i++) { graph.add(new ArrayList<>()); }
The graph has 5 nodes, so we initialize the adjacency list with 5 empty lists.
Complete the code to add an edge from node 0 to node 1 in the graph.
graph.get(0).[1](1);
To add an edge, we use the add method on the adjacency list of node 0.
Fix the error in the code to correctly check if a node has been visited.
if (!visited[[1]]) { // process node }
The visited array is indexed by the node number, so we check visited[node].
Fill both blanks to complete the BFS loop that finds the shortest path in a graph.
while (!queue.[1]()) { int current = queue.[2](); // process current node }
We check if the queue is empty with isEmpty(), and remove the front element with poll() in BFS.
Fill all three blanks to update the distance array during BFS traversal.
if (!visited[neighbor]) { visited[neighbor] = true; distance[neighbor] = distance[[1]] + [2]; queue.[3](neighbor); }
Distance to neighbor is distance[current] + 1, and we add neighbor to the queue with offer().
