Trying to create a tree in matrix form

4 visualizaciones (últimos 30 días)
Niels de Vries
Niels de Vries el 31 de Ag. de 2018
Comentada: Niels de Vries el 31 de Ag. de 2018
Hey all,
I got an input matrix P, where each number represents a task:
P = [1 2;1 5;2 4;5 3;2 6;4 7]
[1 2;1 5] should be read as, task 2 start after task 1, and task 5 starts after task 1
Now lets say we start with task 1, i want to compute the tree structure, which should be:
In matrix form i would like it to be:
Output = [1 2 6 0;1 2 4 7;1 5 3 0]
I am having trouble converting P to the desired Output matrix, any idea's are welcome.
Thanks in advance

Respuesta aceptada

Steven Lord
Steven Lord el 31 de Ag. de 2018
This doesn't exactly get you the format you want, but it gets you something pretty close.
P = [1 2;1 5;2 4;5 3;2 6;4 7];
D = digraph(P(:, 1), P(:, 2));
leaves = outdegree(D) == 0;
tree = shortestpathtree(D, 1, find(leaves), 'OutputForm', 'cell')
Each cell in the tree cell array contains the shortest path from 1 to the corresponding leaf node.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by