0
0
DSA Cprogramming

Topological Sort Using Kahn's Algorithm BFS in DSA C

Choose your learning style9 modes available
Mental Model
We order tasks so that each task comes after all tasks it depends on. We do this by repeatedly picking tasks with no remaining dependencies.
Analogy: Imagine you have to bake a cake, but you must first prepare the batter and preheat the oven. You can only do a step when all its prerequisites are done. Kahn's algorithm finds the order to do all steps without breaking any rules.
Graph:
0 -> 1 -> 3
↓
2

In-degree array:
[0]=0, [1]=1, [2]=1, [3]=2

Queue initially holds nodes with in-degree 0:
[0]
Dry Run Walkthrough
Input: Graph edges: 0->1, 0->2, 1->3, 2->3; nodes = 4
Goal: Find a linear order of nodes so that every edge u->v means u comes before v
Step 1: Calculate in-degree for each node
In-degree array: [0]=0, [1]=1, [2]=1, [3]=2
Why: We need to know which nodes have no dependencies to start with
Step 2: Add nodes with in-degree 0 to queue
Queue: [0]
Why: Only nodes with no dependencies can be processed first
Step 3: Remove node 0 from queue and add to result
Result: 0
Queue: []
Why: Process node 0 as it has no dependencies
Step 4: Decrease in-degree of neighbors 1 and 2 by 1
In-degree array: [1]=0, [2]=0, [3]=2
Why: Removing 0 means its neighbors have one less dependency
Step 5: Add nodes 1 and 2 with in-degree 0 to queue
Queue: [1, 2]
Why: Now nodes 1 and 2 have no dependencies and can be processed
Step 6: Remove node 1 from queue and add to result
Result: 0 -> 1
Queue: [2]
Why: Process node 1 next
Step 7: Decrease in-degree of neighbor 3 by 1
In-degree array: [3]=1
Why: Removing 1 reduces dependencies of 3
Step 8: Remove node 2 from queue and add to result
Result: 0 -> 1 -> 2
Queue: []
Why: Process node 2 next
Step 9: Decrease in-degree of neighbor 3 by 1
In-degree array: [3]=0
Why: Removing 2 reduces dependencies of 3 to zero
Step 10: Add node 3 to queue
Queue: [3]
Why: Node 3 now has no dependencies
Step 11: Remove node 3 from queue and add to result
Result: 0 -> 1 -> 2 -> 3
Queue: []
Why: Process last node 3
Result:
Final topological order: 0 -> 1 -> 2 -> 3
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

#define MAX_NODES 100

// Queue structure for BFS
typedef struct {
    int items[MAX_NODES];
    int front, rear;
} Queue;

void initQueue(Queue* q) {
    q->front = 0;
    q->rear = -1;
}

int isEmpty(Queue* q) {
    return q->rear < q->front;
}

void enqueue(Queue* q, int val) {
    q->items[++q->rear] = val;
}

int dequeue(Queue* q) {
    return q->items[q->front++];
}

// Graph adjacency list
typedef struct Node {
    int vertex;
    struct Node* next;
} Node;

Node* createNode(int v) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->vertex = v;
    newNode->next = NULL;
    return newNode;
}

void addEdge(Node* adj[], int src, int dest) {
    Node* newNode = createNode(dest);
    newNode->next = adj[src];
    adj[src] = newNode;
}

void kahnTopologicalSort(Node* adj[], int n) {
    int inDegree[MAX_NODES] = {0};
    int i;

    // Calculate in-degree of each node
    for (i = 0; i < n; i++) {
        Node* temp = adj[i];
        while (temp) {
            inDegree[temp->vertex]++;
            temp = temp->next;
        }
    }

    Queue q;
    initQueue(&q);

    // Enqueue nodes with in-degree 0
    for (i = 0; i < n; i++) {
        if (inDegree[i] == 0) {
            enqueue(&q, i);
        }
    }

    int count = 0;
    int topOrder[MAX_NODES];

    while (!isEmpty(&q)) {
        int u = dequeue(&q); // Remove node with no dependencies
        topOrder[count++] = u;

        // Decrease in-degree of neighbors
        Node* temp = adj[u];
        while (temp) {
            inDegree[temp->vertex]--;
            if (inDegree[temp->vertex] == 0) {
                enqueue(&q, temp->vertex);
            }
            temp = temp->next;
        }
    }

    // Check if there was a cycle
    if (count != n) {
        printf("Graph has a cycle, topological sort not possible\n");
        return;
    }

    // Print topological order
    for (i = 0; i < count; i++) {
        printf("%d", topOrder[i]);
        if (i != count - 1) printf(" -> ");
    }
    printf("\n");
}

int main() {
    int n = 4;
    Node* adj[MAX_NODES] = {NULL};

    addEdge(adj, 0, 1);
    addEdge(adj, 0, 2);
    addEdge(adj, 1, 3);
    addEdge(adj, 2, 3);

    kahnTopologicalSort(adj, n);

    return 0;
}
for (i = 0; i < n; i++) { Node* temp = adj[i]; while (temp) { inDegree[temp->vertex]++; temp = temp->next; } }
Calculate in-degree of each node by counting incoming edges
for (i = 0; i < n; i++) { if (inDegree[i] == 0) { enqueue(&q, i); } }
Add all nodes with zero in-degree to the queue to start processing
while (!isEmpty(&q)) { int u = dequeue(&q); topOrder[count++] = u;
Remove node with no dependencies and add to topological order
Node* temp = adj[u]; while (temp) { inDegree[temp->vertex]--; if (inDegree[temp->vertex] == 0) { enqueue(&q, temp->vertex); } temp = temp->next; }
Decrease in-degree of neighbors and enqueue if they become zero
if (count != n) { printf("Graph has a cycle, topological sort not possible\n"); return; }
Detect cycle if not all nodes are processed
OutputSuccess
0 -> 1 -> 2 -> 3
Complexity Analysis
Time: O(V + E) because we visit each node and edge once during in-degree calculation and BFS
Space: O(V + E) for adjacency list and in-degree array
vs Alternative: Compared to DFS-based topological sort, Kahn's algorithm uses BFS and explicit in-degree tracking, which can be easier to understand and detect cycles early
Edge Cases
Graph with cycle
Algorithm detects cycle because not all nodes get processed; prints error message
DSA C
if (count != n) {
    printf("Graph has a cycle, topological sort not possible\n");
    return;
}
Graph with single node and no edges
Node has in-degree 0, gets processed immediately, output is that single node
DSA C
for (i = 0; i < n; i++) {
    if (inDegree[i] == 0) {
        enqueue(&q, i);
    }
}
Graph with multiple disconnected components
All nodes with zero in-degree from all components get processed in order, producing a valid topological order
DSA C
for (i = 0; i < n; i++) {
    if (inDegree[i] == 0) {
        enqueue(&q, i);
    }
}
When to Use This Pattern
When you need to order tasks with dependencies and want to detect cycles, use Kahn's algorithm BFS for topological sorting because it processes nodes with zero dependencies first.
Common Mistakes
Mistake: Not updating in-degree of neighbors after removing a node
Fix: Decrease in-degree of all neighbors and enqueue those whose in-degree becomes zero
Mistake: Not checking for cycles by comparing processed node count to total nodes
Fix: Add a check after BFS to detect if a cycle exists by verifying if all nodes were processed
Summary
Finds an order of nodes so all dependencies come before dependent nodes using BFS and in-degree counts.
Use when you have a directed acyclic graph and want to find a valid linear ordering of nodes.
The key insight is to repeatedly process nodes with zero in-degree and update neighbors' in-degree accordingly.