0
0
DSA Cprogramming~20 mins

Topological Sort Using Kahn's Algorithm BFS in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kahn's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Kahn's Algorithm on a Simple DAG
What is the output of the following C code implementing Kahn's Algorithm for topological sorting on the given graph?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define MAX 6

int main() {
    int indegree[MAX] = {0};
    int graph[MAX][MAX] = {
        {0, 1, 1, 0, 0, 0},
        {0, 0, 0, 1, 0, 0},
        {0, 0, 0, 1, 1, 0},
        {0, 0, 0, 0, 0, 1},
        {0, 0, 0, 0, 0, 1},
        {0, 0, 0, 0, 0, 0}
    };

    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < MAX; j++) {
            if (graph[i][j]) indegree[j]++;
        }
    }

    int queue[MAX], front = 0, rear = 0;
    for (int i = 0; i < MAX; i++) {
        if (indegree[i] == 0) queue[rear++] = i;
    }

    int count = 0;
    int topo_order[MAX];

    while (front < rear) {
        int u = queue[front++];
        topo_order[count++] = u;
        for (int v = 0; v < MAX; v++) {
            if (graph[u][v]) {
                indegree[v]--;
                if (indegree[v] == 0) queue[rear++] = v;
            }
        }
    }

    if (count != MAX) {
        printf("Cycle detected\n");
        return 1;
    }

    for (int i = 0; i < MAX; i++) {
        printf("%d", topo_order[i]);
        if (i != MAX - 1) printf(" -> ");
    }
    printf(" -> null\n");
    return 0;
}
A0 -> 2 -> 1 -> 4 -> 3 -> 5 -> null
B0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
C0 -> 2 -> 1 -> 3 -> 4 -> 5 -> null
D0 -> 1 -> 2 -> 4 -> 3 -> 5 -> null
Attempts:
2 left
💡 Hint
Look at the indegree array and how nodes with zero indegree are processed in order.
🧠 Conceptual
intermediate
1:00remaining
Understanding Indegree in Kahn's Algorithm
In Kahn's Algorithm for topological sorting, what does the 'indegree' of a node represent?
AThe number of edges coming into the node
BThe number of edges going out from the node
CThe total number of edges connected to the node
DThe number of nodes reachable from this node
Attempts:
2 left
💡 Hint
Indegree counts incoming edges.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Kahn's Algorithm Implementation
What error does the following code produce when running Kahn's Algorithm on a graph with a cycle?
DSA C
#include <stdio.h>
#include <stdlib.h>

#define MAX 3

int main() {
    int indegree[MAX] = {0};
    int graph[MAX][MAX] = {
        {0, 1, 0},
        {0, 0, 1},
        {1, 0, 0}
    };

    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < MAX; j++) {
            if (graph[i][j]) indegree[j]++;
        }
    }

    int queue[MAX], front = 0, rear = 0;
    for (int i = 0; i < MAX; i++) {
        if (indegree[i] == 0) queue[rear++] = i;
    }

    int count = 0;
    int topo_order[MAX];

    while (front < rear) {
        int u = queue[front++];
        topo_order[count++] = u;
        for (int v = 0; v < MAX; v++) {
            if (graph[u][v]) {
                indegree[v]--;
                if (indegree[v] == 0) queue[rear++] = v;
            }
        }
    }

    if (count != MAX) {
        printf("Cycle detected\n");
        return 1;
    }

    for (int i = 0; i < MAX; i++) {
        printf("%d", topo_order[i]);
        if (i != MAX - 1) printf(" -> ");
    }
    printf(" -> null\n");
    return 0;
}
AThe program enters an infinite loop
BThe program prints a partial topological order and then terminates normally
CThe program crashes with segmentation fault
DThe program prints 'Cycle detected' and exits with code 1
Attempts:
2 left
💡 Hint
Check the condition when count is not equal to number of nodes.
🚀 Application
advanced
2:00remaining
Order of Nodes in Kahn's Algorithm with Multiple Zero Indegree Nodes
Given the graph below, which topological order will Kahn's Algorithm produce if nodes with zero indegree are enqueued in ascending order?
DSA C
Graph edges:
0 -> 2
1 -> 2
2 -> 3
3 -> 4
4 -> 5

Nodes: 0, 1, 2, 3, 4, 5
A0 -> 1 -> 2 -> 3 -> 4 -> 5 -> null
B1 -> 0 -> 2 -> 3 -> 4 -> 5 -> null
C0 -> 2 -> 1 -> 3 -> 4 -> 5 -> null
D1 -> 2 -> 0 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Nodes 0 and 1 have zero indegree initially. Which is enqueued first?
🧠 Conceptual
expert
1:30remaining
Why Kahn's Algorithm Uses a Queue
Why does Kahn's Algorithm use a queue data structure to store nodes with zero indegree during topological sorting?
ATo process nodes in reverse order of their indegree values
BTo implement a depth-first search traversal of the graph
CTo process nodes in the order they become zero indegree, ensuring BFS traversal
DTo store nodes temporarily before sorting them by their values
Attempts:
2 left
💡 Hint
Think about the order nodes are processed and how BFS works.