3. What is the output of the following Java code snippet performing topological sort using DFS on a graph with edges: 5->2, 5->0, 4->0, 4->1, 2->3, 3->1?
import java.util.*;
public class TopoSort {
static void dfs(int node, boolean[] visited, Stack<Integer> stack, List graph) {
visited[node] = true;
for (int adj : graph.get(node)) {
if (!visited[adj]) dfs(adj, visited, stack, graph);
}
stack.push(node);
}
public static void main(String[] args) {
int V = 6;
List graph = new ArrayList<>();
for (int i = 0; i < V; i++) graph.add(new ArrayList<>());
graph.get(5).add(2);
graph.get(5).add(0);
graph.get(4).add(0);
graph.get(4).add(1);
graph.get(2).add(3);
graph.get(3).add(1);
boolean[] visited = new boolean[V];
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < V; i++) {
if (!visited[i]) dfs(i, visited, stack, graph);
}
while (!stack.isEmpty()) System.out.print(stack.pop() + " ");
}
}