Complete the code to declare the adjacency list for the graph.
vector<int> [1][V];The adjacency list is commonly named adj to represent edges from each vertex.
Complete the code to mark the current node as visited in DFS.
visited[[1]] = true;In DFS, the current node is often named u. Marking visited[u] as true prevents revisiting.
Fix the error in the DFS recursive call to visit neighbors.
if (!visited[[1]]) dfs([1], visited, adj, stack);
We check and visit neighbors named v, not the current node u.
Fill both blanks to correctly push the node onto the stack after DFS.
void dfs(int [1], vector<bool>& visited, vector<int> adj[], stack<int>& [2]) { visited[node] = true; for (int v : adj[node]) { if (!visited[v]) dfs(v, visited, adj, stack); } [2].push([1]); }
The DFS function parameter is named node. The stack variable is stack. We push node onto stack.
Fill all three blanks to complete the topological sort function using DFS.
vector<int> topologicalSort(int V, vector<int> adj[]) {
vector<bool> visited(V, false);
stack<int> [1];
for (int [2] = 0; [2] < V; [2]++) {
if (!visited[[2]]) dfs([2], visited, adj, [1]);
}
vector<int> order;
while (![1].empty()) {
order.push_back([1].top());
[1].pop();
}
return order;
}The stack variable is named stack. The loop variable is i. We use stack consistently to store nodes.
