Optimized algorithm to schedule tasks with dependency?

后端 未结 5 2109
梦毁少年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:34

    Your tasks are an oriented graph with (hopefully) no cycles.

    I contains sources and wells (sources being tasks that don't depends (have no inbound edge), wells being tasks that unlock no task (no outbound edge)).

    A simple solution would be to give priority to your tasks based on their usefulness (lets call that U.

    Typically, starting by the wells, they have a usefulness U = 1, because we want them to finish.

    Put all the wells' predecessors in a list L of currently being assessed node.

    Then, taking each node in L, it's U value is the sum of the U values of the nodes that depends on him + 1. Put all parents of the current node in the L list.

    Loop until all nodes have been treated.

    Then, start the task that can be started and have the biggest U value, because it is the one that will unlock the largest number of tasks.

    In your example,

    U(C) = U(D) = U(F) = 1
    U(B) = U(E) = 2
    U(A) = 4
    

    Meaning you'll start A first with E if possible, then B and C (if possible), then D and F

提交回复
热议问题