Topological Sort in Layers
Group the nodes of a directed graph into layers that respect every edge, or detect a cycle.
The problem
Implement kahn_frontier_layers(node_count, edges). The nodes are numbered 0 to node_count - 1, and each edge [before, after] means before must come before after. Split the nodes into layers: the first layer holds every node with no incoming edge; after you remove a layer and its outgoing edges, the next layer holds every node that now has no incoming edge, and so on. Return the layers in that order, each one sorted in increasing order. If the edges contain a cycle, so that some nodes can never be placed, return [].
Starter code
def kahn_frontier_layers(node_count, edges):
passPublic examples
node_count = 5, edges = [[0, 2], [1, 2], [2, 3], [2, 4]]gives[[0, 1], [2], [3, 4]]node_count = 3, edges = []gives[[0, 1, 2]]
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
