This is the last assignment in the Order of Things-series. If that past incompleteness kept you from solving it, you may start now. Open that bottle of wine, and spend an entertaining evening coding.
This problem is closely related to Problem 2189, Order of things - 1, Problem 2190, Order of things - 2, and Problem 2191, Order of things - 3. For the details, see the description for those problems. Basically, we have to find the order in which to execute tasks of which the results and prerequisites depend on each other.
However, this is the most complex case, as * Calculations are preferably grouped, but may, due to their dependencies, not all be clustered. So groups of events may be split over different clusters. The number of clusters per group should be kept to a minimum.
The same complications as in problem 3 may apply: * It may (still) be impossible to find a solution, since dependencies may be cyclic. But this is now no longer due to the grouping of tasks, as you may, if necessary, break the rule of grouping. In any case, return an empty vector if no order is found. * There are still multiple orders possible, return them as multiple rows of the output vector. * The tasks within a group should of course be specified in the right order, when interdependent.
The dependencies of the tasks on each other is expressed in a matrix, where each row corresponds to the execution of a task, and each column to the dependency.
A B C D E A 0 0 0 0 0 B 0 0 1 0 0 C 1 0 0 0 0 D 0 0 1 0 0 E 1 0 0 0 0
The 1 on row C, column B, indicates that task C depends on task B.
The grouping of tasks is expressed as an input vector with groups assigned an integer value > 0, for every task, e.g.
[ 1 1 2 2 3 ]
Return the new row/column order as a numeric row-vector, referring to the rows/columns of the input matrix. If multiple orders exist, return them all as rows of a matrix. In this example:
[ 1 3 4 2 5 1 3 4 5 2 1 5 3 4 2 ]
If no order fulfilling the dependencies exists, return an empty vector.