Trying to create a tree in matrix form

조회 수: 4 (최근 30일)
Niels de Vries
Niels de Vries 2018년 8월 31일
댓글: Niels de Vries 2018년 8월 31일
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.
  댓글 수: 1
Niels de Vries
Niels de Vries 2018년 8월 31일
This is even better tbh, thanks !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by