Complete the code to initialize the in-degree array to zero for all vertices.
for (int i = 0; i < V; i++) { in_degree[i] = [1]; }
In-degree counts the number of incoming edges. Initially, it should be zero for all vertices.
Complete the code to enqueue vertices with zero in-degree into the queue.
for (int i = 0; i < V; i++) { if (in_degree[i] == [1]) { enqueue(&q, i); } }
Vertices with zero in-degree have no dependencies and can be processed first.
Fix the error in the code that decreases the in-degree of adjacent vertices after removing a vertex from the queue.
while (!is_empty(&q)) { int u = dequeue(&q); for (int i = 0; i < adj[u].size; i++) { int v = adj[u].array[i]; in_degree[v] [1] 1; if (in_degree[v] == 0) { enqueue(&q, v); } } }
When a vertex is removed, the in-degree of its adjacent vertices decreases by one.
Fill both blanks to correctly update the topological order array and its index.
topo_order[[1]] = u; [2]++;
The variable 'index' tracks the position in the topological order array and should be incremented after assignment.
Fill all three blanks to complete the condition that checks if a cycle exists in the graph.
if ([1] != [2]) { printf("Cycle detected, topological sort not possible.\n"); return [3]; }
If the number of vertices processed is not equal to total vertices, a cycle exists. Returning -1 indicates failure.