1 ## Copyright (C) 2008-2012 Ivana Varekova & Radek Salac
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} treelayout (@var{tree})
21 ## @deftypefnx {Function File} {} treelayout (@var{tree}, @var{permutation})
22 ## treelayout lays out a tree or a forest. The first argument @var{tree} is a
24 ## predecessors, optional parameter @var{permutation} is an optional postorder
26 ## The complexity of the algorithm is O(n) in
27 ## terms of time and memory requirements.
28 ## @seealso{etreeplot, gplot, treeplot}
31 function [x_coordinate, y_coordinate, height, s] = treelayout (tree, permutation)
32 if (nargin < 1 || nargin > 2 || nargout > 4)
34 elseif (! isvector (tree) || rows (tree) != 1 || ! isnumeric (tree)
35 || any (tree > length (tree)) || any (tree < 0))
36 error ("treelayout: the first input argument must be a vector of predecessors");
38 ## Make it a row vector.
41 ## The count of nodes of the graph.
42 num_nodes = length (tree);
43 ## The number of children.
44 num_children = zeros (1, num_nodes + 1);
46 ## Checking vector of predecessors.
49 ## This part of graph was checked before.
53 ## Try to find cicle in this part of graph using modified Floyd's
54 ## cycle-finding algorithm.
56 hare = tree(tortoise);
58 while (tortoise != hare)
59 ## End after finding a cicle or reaching a checked part of graph.
62 ## This part of graph was checked before.
66 tortoise = tree(tortoise);
67 ## Hare will move faster than tortoise so in cicle hare must
69 hare = tree(tree(hare));
74 ## If hare reach tortoise we found circle.
75 error ("treelayout: vector of predecessors has bad format");
79 ## Vector of predecessors has right format.
82 ## vec_of_child is helping vector which is used to speed up the
83 ## choice of descendant nodes.
85 num_children(tree(i)+1) = num_children(tree(i)+1) + 1;
89 start = zeros (1, num_nodes+1);
90 xhelp = zeros (1, num_nodes+1);
91 stop = zeros (1, num_nodes+1);
92 for i = 1 : num_nodes + 1
95 pos += num_children(i);
101 vec_of_child(xhelp(tree(i)+1)) = i;
102 xhelp(tree(i)+1) = xhelp(tree(i)+1) + 1;
105 vec_of_child = permutation;
108 ## The number of "parent" (actual) node (it's descendants will be
109 ## browse in the next iteration).
112 ## The x-coordinate of the left most descendant of "parent node"
113 ## this value is increased in each leaf.
116 ## The level of "parent" node (root level is num_nodes).
119 ## num_nodes - max_ht is the height of this graph.
122 ## Main stack - each item consists of two numbers - the number of
123 ## node and the number it's of parent node on the top of stack
124 ## there is "parent node".
127 ## Number of vertices s in the top-level separator.
129 ## Flag which says if we are in top level separator.
131 ## The top of the stack.
132 while (par_number != -1)
133 if (start(par_number+1) < stop(par_number+1))
134 idx = vec_of_child(start(par_number+1) : stop(par_number+1) - 1);
139 ## Add to idx the vector of parent descendants.
140 stk = [stk; [idx', ones(fliplr(size(idx))) * par_number]];
142 ## We are in top level separator when we have one child and the
144 if (columns(idx) == 1 && top_level == 1)
147 # We aren't in top level separator now.
150 ## If there is not any descendant of "parent node":
151 if (stk(end,2) != par_number)
153 x_coordinate_r(par_number) = left_most;
154 max_ht = min (max_ht, level);
155 if (length(stk) > 1 && find ((shift(stk,1)-stk) == 0) > 1
156 && stk(end,2) != stk(end-1,2))
157 ## Return to the nearest branching the position to return
158 ## position is the position on the stack, where should be
159 ## started further search (there are two nodes which has the
160 ## same parent node).
162 position = (find ((shift (stk(:,2), 1) - stk(:,2)) == 0))(end) + 1;
163 par_number_vec = stk(position:end,2);
165 ## The vector of removed nodes (the content of stack form
168 level += length (par_number_vec);
170 ## The level have to be decreased.
172 x_coordinate_r(par_number_vec) = left_most;
173 stk(position:end,:) = [];
176 ## Remove the next node from "searched branch".
179 ## Choose new "parent node".
180 par_number = stk(end,1);
181 ## If there is another branch start to search it.
182 if (par_number != -1)
183 y_coordinate(par_number) = level;
184 x_coordinate_l(par_number) = left_most + 1;
188 ## There were descendants of "parent nod" choose the last of
189 ## them and go on through it.
191 par_number = stk(end,1);
192 y_coordinate(par_number) = level;
193 x_coordinate_l(par_number) = left_most + 1;
197 ## Calculate the x coordinates (the known values are the position
198 ## of most left and most right descendants).
199 x_coordinate = (x_coordinate_l + x_coordinate_r) / 2;
201 height = num_nodes - max_ht - 1;
206 %! % Compute a simple tree layout
207 %! [x, y, h, s] = treelayout ([0, 1, 2, 2]);
208 %! assert (x, [1.5, 1.5, 2, 1]);
209 %! assert (y, [3, 2, 1, 1]);
214 %! % Compute a simple tree layout with defined postorder permutation
215 %! [x, y, h, s] = treelayout ([0, 1, 2, 2], [1, 2, 4, 3]);
216 %! assert (x, [1.5, 1.5, 1, 2]);
217 %! assert (y, [3, 2, 1, 1]);
222 %! % Compute a simple tree layout with defined postorder permutation
223 %! [x, y, h, s] = treelayout ([0, 1, 2, 2], [4, 2, 3, 1]);
224 %! assert (x, [0, 0, 0, 1]);
225 %! assert (y, [0, 0, 0, 3]);