Problem 2190. Order of things - 2

This problem is closely related to Problem 2189, Order of things - 1. For the details, see the description for that problem. Basically, we have to find the order in which to execute tasks of which the results and prerequisites depend on each other.

  • However, this time it may be impossible to find a solution, since dependencies may be cyclic. In that case, return an empty vector.
  • Furthermore, if there are multiple orders possible, return them as multiple rows of the output vector.

Again, the dependencies of the tasks on each other is expressed in a matrix, where each row and column corresponds to a specific task. Each row expresses on which result that task depends. A 1 indicates that the calculation on that row depends on the one mentioned at the top of that column.

Return the new row/column order as a numeric row-vector, referring to the rows/columns of the input matrix. Of an empty array when no solution is possible. Or a matrix of rows containing the orders, where each row is a different solution, in case multiple solutions exist.

   A  B  C  D  E
A  0  1  0  0  0
B  0  0  0  0  0
C  1  0  0  1  0
D  1  0  1  0  0
E  1  1  1  1  0

The above problem can not be solved, since C depends on D, which in its place depends on C. The returned value would be [] .

   A  B  C  D  E
A  0  1  0  0  0
B  0  0  0  0  0
C  1  0  0  0  0
D  1  0  0  0  0
E  1  1  1  1  0

The returned matrix should be

 [
   2 1 3 4 5 
   2 1 4 3 5
 ]

Good luck!

Solution Stats

73.33% Correct | 26.67% Incorrect
Last Solution submitted on Nov 06, 2021

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers10

Suggested Problems

More from this Author31

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!