Bird
Raised Fist0
DSA Javaprogramming~5 mins

Topological Sort Using Kahn's Algorithm BFS in DSA Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the main idea behind Kahn's Algorithm for topological sorting?
Kahn's Algorithm uses BFS by repeatedly removing nodes with zero incoming edges (in-degree) and updating the in-degree of their neighbors until all nodes are processed.
Click to reveal answer
beginner
In Kahn's Algorithm, what does a node with zero in-degree represent?
A node with zero in-degree has no dependencies and can be placed next in the topological order.
Click to reveal answer
intermediate
Why does Kahn's Algorithm detect cycles in a graph?
If after processing all nodes with zero in-degree, some nodes remain unprocessed, it means there is a cycle preventing those nodes from reaching zero in-degree.
Click to reveal answer
beginner
What data structure is commonly used to store nodes with zero in-degree in Kahn's Algorithm?
A queue is used to store nodes with zero in-degree to process them in BFS order.
Click to reveal answer
intermediate
How does Kahn's Algorithm update the in-degree of neighboring nodes?
When a node is removed from the queue, the algorithm decreases the in-degree of each of its neighbors by one, reflecting the removal of a dependency.
Click to reveal answer
What is the first step in Kahn's Algorithm for topological sorting?
ARemove edges randomly
BStart DFS from any node
CFind all nodes with zero in-degree and add them to a queue
DSort nodes alphabetically
What does it mean if after running Kahn's Algorithm, not all nodes are included in the topological order?
AThe graph is disconnected
BThe graph has a cycle
CThe graph is empty
DThe graph is a tree
Which data structure is used to process nodes in Kahn's Algorithm?
AQueue
BStack
CPriority Queue
DSet
How is the in-degree of a node defined?
ANumber of edges going out from the node
BNumber of nodes reachable from it
CNumber of neighbors
DNumber of edges coming into the node
What happens to the in-degree of neighbors when a node is removed in Kahn's Algorithm?
AIt decreases by one
BIt increases by one
CIt stays the same
DIt resets to zero
Explain step-by-step how Kahn's Algorithm performs a topological sort on a directed graph.
Think about how dependencies are removed layer by layer.
You got /6 concepts.
    How does Kahn's Algorithm help detect cycles in a directed graph?
    Focus on what happens when some nodes never become free of dependencies.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of Kahn's Algorithm in graph theory?
      easy
      A. To find a topological ordering of a Directed Acyclic Graph (DAG)
      B. To detect cycles in an undirected graph
      C. To find the shortest path between two nodes
      D. To calculate the minimum spanning tree

      Solution

      1. Step 1: Understand Kahn's Algorithm goal

        Kahn's Algorithm is designed to produce a linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for every directed edge u -> v, u comes before v in the ordering.
      2. Step 2: Compare with other graph algorithms

        Other options describe different algorithms: cycle detection, shortest path, and minimum spanning tree are unrelated to Kahn's Algorithm.
      3. Final Answer:

        To find a topological ordering of a Directed Acyclic Graph (DAG) -> Option A
      4. Quick Check:

        Topological sort = Kahn's Algorithm purpose [OK]
      Hint: Kahn's Algorithm always finds order in DAGs [OK]
      Common Mistakes:
      • Confusing topological sort with shortest path
      • Thinking it detects cycles in undirected graphs
      • Mixing with minimum spanning tree algorithms
      2. Which Java data structure is typically used to store nodes with zero incoming edges in Kahn's Algorithm?
      easy
      A. Queue
      B. HashSet
      C. Stack
      D. PriorityQueue

      Solution

      1. Step 1: Identify the data structure for zero in-degree nodes

        Kahn's Algorithm uses a queue to process nodes with zero incoming edges in FIFO order to maintain correct topological order.
      2. Step 2: Eliminate other options

        Stack is LIFO, which can change order; HashSet does not maintain order; PriorityQueue is not required for basic Kahn's Algorithm.
      3. Final Answer:

        Queue -> Option A
      4. Quick Check:

        Zero in-degree nodes stored in queue [OK]
      Hint: Use queue for BFS order in Kahn's Algorithm [OK]
      Common Mistakes:
      • Using stack instead of queue
      • Choosing unordered sets instead of ordered structures
      • Confusing with priority queue usage
      3. What is the output of the following Java code snippet using Kahn's Algorithm on this graph?
      Graph edges: 5->0, 5->2, 4->0, 4->1, 2->3, 3->1
      
      // After running Kahn's Algorithm, print the topological order.
      medium
      A. [5, 4, 2, 3, 1, 0]
      B. [4, 5, 2, 3, 1, 0]
      C. [4, 5, 0, 2, 3, 1]
      D. [5, 4, 3, 2, 1, 0]

      Solution

      1. Step 1: Calculate in-degree and start nodes

        Nodes 4 and 5 have zero in-degree. Start queue with [4,5] (assuming ascending order).
      2. Step 2: Process nodes in queue

        Process 4: output [4], reduce 0 to 1, 1 to 1. Process 5: output [4,5], reduce 0 to 0 (add 0), 2 to 0 (add 2); queue=[0,2]. Process 0: [4,5,0], no neighbors. Process 2: [4,5,0,2], reduce 3 to 0 (add 3). Process 3: [4,5,0,2,3], reduce 1 to 0 (add 1). Process 1: [4,5,0,2,3,1].
      3. Final Answer:

        [4, 5, 0, 2, 3, 1] -> Option C
      4. Quick Check:

        Topological order = [4,5,0,2,3,1] [OK]
      Hint: Start with zero in-degree nodes, process neighbors, repeat [OK]
      Common Mistakes:
      • Mixing order of nodes with zero in-degree
      • Not updating in-degree after removing edges
      • Adding nodes to queue too early or late
      4. Identify the bug in this Java snippet implementing Kahn's Algorithm:
      while (!queue.isEmpty()) {
        int node = queue.poll();
        result.add(node);
        for (int neighbor : graph.get(node)) {
          inDegree[neighbor]--;
          if (inDegree[node] == 0) {
            queue.add(neighbor);
          }
      }
      medium
      A. Neighbors should not be iterated in this loop
      B. Queue should be a stack for correct order
      C. Result list should be cleared inside the loop
      D. Incorrect check: should check inDegree[neighbor] == 0, not inDegree[node]

      Solution

      1. Step 1: Analyze the in-degree update and condition

        After decrementing inDegree[neighbor], the code incorrectly checks if inDegree[node] == 0 instead of inDegree[neighbor] == 0.
      2. Step 2: Correct the condition to add neighbors with zero in-degree

        The correct condition is if (inDegree[neighbor] == 0) to add that neighbor to the queue for processing.
      3. Final Answer:

        Incorrect check: should check inDegree[neighbor] == 0, not inDegree[node] -> Option D
      4. Quick Check:

        Check zero in-degree on neighbor, not current node [OK]
      Hint: Check in-degree of neighbor after decrement [OK]
      Common Mistakes:
      • Checking in-degree of wrong node
      • Using stack instead of queue
      • Clearing result inside loop mistakenly
      5. Given a graph with edges: 1->2, 1->3, 3->4, 2->4, 4->5, and 5->6, which of the following is a valid topological order using Kahn's Algorithm?
      hard
      A. [1, 2, 3, 5, 4, 6]
      B. [1, 3, 2, 4, 5, 6]
      C. [6, 5, 4, 3, 2, 1]
      D. [1, 2, 3, 4, 6, 5]

      Solution

      1. Step 1: Identify nodes with zero in-degree and dependencies

        Node 1 has zero in-degree. Nodes 2 and 3 depend on 1; 4 depends on 2 and 3; 5 depends on 4; 6 depends on 5.
      2. Step 2: Validate each option for correct order

        [1, 3, 2, 4, 5, 6] respects dependencies: 1 before 2 and 3, 3 and 2 before 4, 4 before 5, 5 before 6. [1, 2, 3, 4, 6, 5] places 6 before 5, invalid. [6, 5, 4, 3, 2, 1] reverses order, invalid. [1, 2, 3, 5, 4, 6] places 5 before 4, invalid.
      3. Final Answer:

        [1, 3, 2, 4, 5, 6] -> Option B
      4. Quick Check:

        Dependencies respected in order D [OK]
      Hint: Parents always appear before children in topological order [OK]
      Common Mistakes:
      • Placing dependent nodes before their parents
      • Reversing the order of edges
      • Ignoring multiple valid topological orders
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard