I am trying to create a workflow management where I am representing the workflow my a graph. The graph is a simple digraph where the nodes represent some tasks and the edges point to the tasks which depend on this task with the parent->child relationship. Now, what I would like to implement is that no task should start until all its parents have finished.
So say I have the following graph represented as python dictionaries:
graph = {'A': ['B', 'D'], 'B': ['D', 'C'], 'C': ['D', 'E'],
'D': ['E'], 'E': []}
In this case, A
should execute first followed by B
which is then followed by C
. After which D
gets executed and finally task E
finished which depends on D
.
One way I can envision doing this is using a breadth or depth first search to visit all the nodes and then checking at each node if all the parents have been finished. If yes, I start that task and I keep doing this till all the tasks have been started.
I was wondering if there are any more sophisticated/efficient solution to this problem rather than traversing the graph many times?