Trying to create a tree in matrix form
4 views (last 30 days)
Show older comments
Niels de Vries
on 31 Aug 2018
Commented: Niels de Vries
on 31 Aug 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
0 Comments
Accepted Answer
Steven Lord
on 31 Aug 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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!