2
votes

I have an adjacency matrix describing two trees that merges in the middle. An exemple for 10 nodes: enter image description here

The corresponding adjacency matrix is a 10x10 matrix where the first row correspond to the first node (start of the first tree, node #1) and the last row to the root of the second tree (end of the second tree, node #10).

Here is the adjacency matrix corresponding to a larger example with 22 nodes:

0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
1   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
1   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
0   1   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0
0   1   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   0
0   0   1   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0
0   0   1   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0
0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0
0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0   0
0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0
0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0   0
0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0
0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0   0
0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0
0   0   0   0   0   0   1   0   0   0   0   0   0   0   0   0   0   0   1   0   0   0
0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   0   0   1   0   0
0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   0   1   0   0
0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   0   0   1   0
0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   0   1   0
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   0   0   1
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0   0   1
0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   1   1   0

I'm trying to plot this adjacency matrix as the picture showed above using Matlab. Matlab as some tools for plotting trees, for example the code given in: https://blogs.mathworks.com/cleve/2017/03/20/morse-code-binary-trees-and-graphs/

However, using the previous matrix (let's label it 'A') and the following code:

G = digraph(A);
Gp = plot(G);

does not produce a tree, but a graph (not ordered as a tree).

Thus, how to produce a picture of a tree (as showed above) using 'A' in Matlab?

Please note that I also have matrices describing trees where the degree between the children nodes are 3 (or more) rather than 2.

1

1 Answers

2
votes

Zero-out half of your adjacency matrix to make the connection one-way.


By default, MATLAB tries to decide the layout of graphs automatically, based on the structure of your graph. There are a few different graph layouts you can choose from.

The option you wanted is the 'Layered' layout; but I tested it with your example, and it definitely still doesn't look like a tree. The reason being that your adjacency matrix is symmetrical, and the connection is two-way. This confuses MATLAB when placing the nodes, and it doesn't think it's a tree.

The easy fix, is you can zero-out the lower triangular half of your adjacency matrix. I used the function tril for this.

% Create a lower triangular matrix with the dimension of A,
idx = tril(ones(size(A)));

% Make it a logical array to select matrix elements with
idx = logical(idx);

% Select the defined lower triangular part, and set that to zero
A(idx) = 0;

% Generate-Plot graph as you did
G = digraph(A);
plot(G)

Result