comparison scripts/sparse/treeplot.m @ 10549:95c3e38098bf

Untabify .m scripts
author Rik <code@nomad.inbox5.com>
date Fri, 23 Apr 2010 11:28:50 -0700
parents 1bf0ce0930be
children be55736a0783
comparison
equal deleted inserted replaced
10548:479536c5bb10 10549:95c3e38098bf
39 node_style = "k*"; 39 node_style = "k*";
40 edge_style = "r"; 40 edge_style = "r";
41 if (nargin > 2) 41 if (nargin > 2)
42 edge_style = edge_s; 42 edge_style = edge_s;
43 if (nargin > 1) 43 if (nargin > 1)
44 if (length (findstr (node_s, "*")) == 0 44 if (length (findstr (node_s, "*")) == 0
45 && length (findstr (node_s, "+")) == 0 45 && length (findstr (node_s, "+")) == 0
46 && length (findstr (node_s, "x")) == 0) 46 && length (findstr (node_s, "x")) == 0)
47 node_style = [node_s, "o"]; 47 node_style = [node_s, "o"];
48 else 48 else
49 node_style = node_s; 49 node_style = node_s;
50 endif 50 endif
51 endif 51 endif
52 endif 52 endif
53 53
54 ## Make it a row vector. 54 ## Make it a row vector.
55 tree = tree(:)'; 55 tree = tree(:)';
70 start = zeros (1, num_nodes+1); 70 start = zeros (1, num_nodes+1);
71 xhelp = zeros (1, num_nodes+1); 71 xhelp = zeros (1, num_nodes+1);
72 stop = zeros (1, num_nodes+1); 72 stop = zeros (1, num_nodes+1);
73 for i = 1:num_nodes+1 73 for i = 1:num_nodes+1
74 start(i) = pos; 74 start(i) = pos;
75 xhelp(i) = pos; 75 xhelp(i) = pos;
76 pos += num_children(i); 76 pos += num_children(i);
77 stop(i) = pos; 77 stop(i) = pos;
78 endfor 78 endfor
79 for i = 1:num_nodes 79 for i = 1:num_nodes
80 vec_of_child(xhelp(tree(i)+1)) = i; 80 vec_of_child(xhelp(tree(i)+1)) = i;
81 xhelp(tree(i)+1) = xhelp(tree(i)+1)+1; 81 xhelp(tree(i)+1) = xhelp(tree(i)+1)+1;
82 endfor 82 endfor
83 83
84 ## The number of "parent" (actual) node (it's descendants will be 84 ## The number of "parent" (actual) node (it's descendants will be
85 ## browse in the next iteration). 85 ## browse in the next iteration).
86 par_number = 0; 86 par_number = 0;
104 ## uninterupted line). 104 ## uninterupted line).
105 skelet = 0; 105 skelet = 0;
106 106
107 ## The top of the stack. 107 ## The top of the stack.
108 while (par_number != -1) 108 while (par_number != -1)
109 if (start(par_number+1) < stop(par_number+1)) 109 if (start(par_number+1) < stop(par_number+1))
110 idx = vec_of_child(start(par_number+1):stop(par_number+1)-1); 110 idx = vec_of_child(start(par_number+1):stop(par_number+1)-1);
111 else 111 else
112 idx = zeros (1, 0); 112 idx = zeros (1, 0);
113 endif 113 endif
114 ## Add to idx the vector of parent descendants. 114 ## Add to idx the vector of parent descendants.
115 stk = [stk; [idx', ones(fliplr(size(idx)))*par_number]]; 115 stk = [stk; [idx', ones(fliplr(size(idx)))*par_number]];
116 ## Add to stack the records relevant to parent descandant s. 116 ## Add to stack the records relevant to parent descandant s.
117 if (par_number != 0) 117 if (par_number != 0)
118 skelet = [skelet; ([ones(size(idx))*par_number; idx])(:)]; 118 skelet = [skelet; ([ones(size(idx))*par_number; idx])(:)];
119 endif 119 endif
120 120
121 ## If there is not any descendant of "parent node": 121 ## If there is not any descendant of "parent node":
122 if (stk(end,2) != par_number) 122 if (stk(end,2) != par_number)
123 left_most++; 123 left_most++;
124 x_coordinate_r(par_number) = left_most; 124 x_coordinate_r(par_number) = left_most;
125 max_ht = min (max_ht, level); 125 max_ht = min (max_ht, level);
126 if (length(stk) > 1 && find ((shift(stk,1)-stk) == 0) > 1 126 if (length(stk) > 1 && find ((shift(stk,1)-stk) == 0) > 1
127 && stk(end,2) != stk(end-1,2)) 127 && stk(end,2) != stk(end-1,2))
128 ## Return to the nearest branching the position to return 128 ## Return to the nearest branching the position to return
129 ## position is the position on the stack, where should be 129 ## position is the position on the stack, where should be
130 ## started further search (there are two nodes which has the 130 ## started further search (there are two nodes which has the
131 ## same parent node). 131 ## same parent node).
132 position = (find ((shift(stk(:,2),1)-stk(:,2)) == 0))(end) + 1; 132 position = (find ((shift(stk(:,2),1)-stk(:,2)) == 0))(end) + 1;
133 par_number_vec = stk(position:end,2); 133 par_number_vec = stk(position:end,2);
134 ## The vector of removed nodes (the content of stack form 134 ## The vector of removed nodes (the content of stack form
135 ## position to end). 135 ## position to end).
136 skelet = [skelet; flipud(par_number_vec)]; 136 skelet = [skelet; flipud(par_number_vec)];
137 level += length (par_number_vec); 137 level += length (par_number_vec);
138 ## The level have to be decreased. 138 ## The level have to be decreased.
139 x_coordinate_r(par_number_vec) = left_most; 139 x_coordinate_r(par_number_vec) = left_most;
140 stk(position:end,:) = []; 140 stk(position:end,:) = [];
141 endif 141 endif
142 ## Remove the next node from "searched branch". 142 ## Remove the next node from "searched branch".
143 stk(end,:) = []; 143 stk(end,:) = [];
144 ## Choose new "parent node". 144 ## Choose new "parent node".
145 par_number = stk(end,1); 145 par_number = stk(end,1);
146 ## If there is another branch start to search it. 146 ## If there is another branch start to search it.
147 if (par_number != -1) 147 if (par_number != -1)
148 skelet = [skelet; stk(end,2); par_number]; 148 skelet = [skelet; stk(end,2); par_number];
149 y_coordinate(par_number) = level; 149 y_coordinate(par_number) = level;
150 x_coordinate_l(par_number) = left_most + 1; 150 x_coordinate_l(par_number) = left_most + 1;
151 endif 151 endif
152 else 152 else
153 ## There were descendants of "parent nod" choose the last of 153 ## There were descendants of "parent nod" choose the last of
154 ## them and go on through it. 154 ## them and go on through it.
155 level--; 155 level--;
156 par_number = stk(end,1); 156 par_number = stk(end,1);
157 y_coordinate(par_number) = level; 157 y_coordinate(par_number) = level;
158 x_coordinate_l(par_number) = left_most + 1; 158 x_coordinate_l(par_number) = left_most + 1;
159 endif 159 endif
160 endwhile 160 endwhile
161 161
162 ## Calculate the x coordinates (the known values are the position 162 ## Calculate the x coordinates (the known values are the position
163 ## of most left and most right descendants). 163 ## of most left and most right descendants).
164 x_coordinate = (x_coordinate_l + x_coordinate_r) / 2; 164 x_coordinate = (x_coordinate_l + x_coordinate_r) / 2;
167 ## array and make a single call to plot here so we can avoid 167 ## array and make a single call to plot here so we can avoid
168 ## setting the hold state... 168 ## setting the hold state...
169 169
170 hold_is_on = ishold (); 170 hold_is_on = ishold ();
171 unwind_protect 171 unwind_protect
172 hold ("on"); 172 hold ("on");
173 173
174 ## Plot grah nodes. 174 ## Plot grah nodes.
175 plot (x_coordinate, y_coordinate, node_style); 175 plot (x_coordinate, y_coordinate, node_style);
176 176
177 ## Helping command - usable for plotting edges 177 ## Helping command - usable for plotting edges
178 skelet = [skelet; 0]; 178 skelet = [skelet; 0];
179 179
180 ## Draw graph edges. 180 ## Draw graph edges.
181 idx = find (skelet == 0); 181 idx = find (skelet == 0);
182 182
183 ## Plot each tree component in one loop. 183 ## Plot each tree component in one loop.
184 for i = 2:length(idx) 184 for i = 2:length(idx)
185 ## Tree component start. 185 ## Tree component start.
186 istart = idx(i-1) + 1; 186 istart = idx(i-1) + 1;
187 ## Tree component end. 187 ## Tree component end.
188 istop = idx(i) - 1; 188 istop = idx(i) - 1;
189 if (istop - istart < 1) 189 if (istop - istart < 1)
190 continue; 190 continue;
191 endif 191 endif
192 plot (x_coordinate(skelet(istart:istop)), 192 plot (x_coordinate(skelet(istart:istop)),
193 y_coordinate(skelet(istart:istop)), edge_style) 193 y_coordinate(skelet(istart:istop)), edge_style)
194 endfor 194 endfor
195 195
196 ## Set axis and graph size. 196 ## Set axis and graph size.
197 axis ([0.5, left_most+0.5, max_ht-0.5, num_nodes-0.5], "nolabel"); 197 axis ([0.5, left_most+0.5, max_ht-0.5, num_nodes-0.5], "nolabel");
198 198
199 unwind_protect_cleanup 199 unwind_protect_cleanup
200 if (! hold_is_on) 200 if (! hold_is_on)
201 hold ("off"); 201 hold ("off");
202 endif 202 endif
203 end_unwind_protect 203 end_unwind_protect
204 204
205 endif 205 endif
206 endif 206 endif
207 endfunction 207 endfunction