Take recipes = ["cake", "frosting"], ingredients = [["flour", "frosting"], ["sugar", "butter"]], supplies = ["flour", "sugar", "butter"].
Build the graph. "cake" has unmet count (needs flour, frosting). "frosting" has unmet count (needs sugar, butter). Enqueue all supplies.
Process flour: cake's count drops to . Process sugar: frosting drops to . Process butter: frosting drops to , so enqueue it. Process frosting: cake drops to , enqueue it. Result: ["frosting", "cake"].
Let be recipes plus ingredients and be dependency edges. BFS visits each node and edge once, so time is . Space is for the graph and counts.