I'm trying to rank/resolve a set of tasks with dependencies among them. For example, I have below objects representing tasks
class Task:
def __init__(self, name):
self.name = name
self.execute_after = []
if __name__ == "__main__":
task_A = Task("A")
task_B = Task("B")
task_C = Task("C")
task_D = Task("D")
task_A.execute_after = [task_C, task_D]
task_B.execute_after = []
task_C.execute_after = [task_B, task_D]
task_D.execute_after = [task_B]
Now I need to be able to rank the order in which these tasks need to be executed.
For example, here
- task_B would be executed first as it has no dependency.
- Next, task_D would be executed as its dependency is resolved
- Next, task_C would be executed as both its dependencies are resolved.
- Finally task_A would be executed.
I could do a DFS but trying to figure out if there is an alternative way of ranking this or coming up with the correct order of solving this problem?