Your question is Topological Sort Implementation. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Siemens NX workflows may contain modules that depend on other modules being initialized first. Given num_modules and directed dependency pairs, return a valid initialization order using topological sort. If multiple valid orders exist, return the lexicographically smallest one. If the dependencies contain a cycle, return an empty list.
Implement topological_order(num_modules, prerequisites), where module IDs are integers from 0 through num_modules - 1. Each pair [module, prerequisite] means prerequisite must appear before module in the output.
Return a list containing every module exactly once, or [] when no valid ordering exists.
def topological_order(num_modules, prerequisites):