Search code examples
pythonmatplotlibnetworkxgraph-theorydirected-acyclic-graphs

how to create a DAG using Python (for Task execution based on dependencies.)


I'm trying to write a program in python that will execute the following tasks below as efficiently as possible using parallel and/or serial execution. These tasks have dependencies for each of them and in order for a task to execute, all of its dependencies must be executed.

ie. Task A can only be executed if Tasks B, C, D are executed before. we can also see that Tasks C, H, J have no dependencies so they can be executed parallelly.

I realize that it can be done with help of directed acyclic graphs but I'm not sure how to create a DAG using the following list. Any help is really appreciated. Thank you!!

"A" : ["B", "C", "D"],
"B" : ["E"],
"C" : [],
"D" : ["C", "F"],
"E" : ["H"],
"F" : ["B", "C"],
"G" : ["H", "C"],
"H" : [],
"I" : ["H", "C", "F"],
"J" : []

Solution

  • This is called a "topological sort". There are several Python modules to do that, including https://pypi.org/project/toposort/ . Note that Linux includes a command line tool that can do this, tsort, no programming required.