Optimized algorithm to schedule tasks with dependency?

后端 未结 5 2105
梦毁少年i
梦毁少年i 2021-01-30 17:50

There are tasks that read from a file, do some processing and write to a file. These tasks are to be scheduled based on the dependency. Also tasks can be run in parallel, so the

5条回答
  •  余生分开走
    2021-01-30 18:52

    Let each task (e.g. A,B,...) be nodes in a directed acyclic graph and define the arcs between the nodes based on your 1,2,....

    http://en.wikipedia.org/wiki/Topological_sorting

    You can then topologically order your graph (or use a search based method like BFS). In your example, C<-A->B->D and E->F so, A & E have depth of 0 and need to be run first. Then you can run F,B and C in parallel followed by D.

    Also, take a look at PERT.

    Update:

    How do you know whether B has a higher priority than F?

    This is the intuition behind the topological sort used to find the ordering.

    It first finds the root (no incoming edges) nodes (since one must exist in a DAG). In your case, that's A & E. This settles the first round of jobs which needs to be completed. Next, the children of the root nodes (B,C and F) need to be finished. This is easily obtained by querying your graph. The process is then repeated till there are no nodes (jobs) to be found (finished).

提交回复
热议问题