Trying to create a tree in matrix form

2 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2018-8-31
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by