Effacer les filtres
Effacer les filtres

Trying to create a tree in matrix form

4 vues (au cours des 30 derniers jours)
Niels de Vries
Niels de Vries le 31 Août 2018
Commenté : Niels de Vries le 31 Août 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

Réponse acceptée

Steven Lord
Steven Lord le 31 Août 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.
  1 commentaire
Niels de Vries
Niels de Vries le 31 Août 2018
This is even better tbh, thanks !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by