function dottree(tree, fname, nodelabel,arclabel)
% DOTTREE Make a file representing directed and undirected trees 
% in dotty format.
%
% dottree(tree, filename, nodelabels, arclabels)
%
% nodelabel{i} is a string attached to node i. Default "i".
% arclabel{i,j} is a string attached to the i->j arc. Default ""
%
% See http://www.research.att.com/sw/tools/graphviz
% To print the directed graph, type 'dot -Tps fname.d -o fname.ps'
% To print the undirected graph, type 'neato -Tps fname.u -o fname.ps'

n = length(tree);
if ~exist('nodelabel')
  nodelabel = cell(1,n);
  for i=1:n
    nodelabel{i} = num2str(i);
  end
end

fid1 = fopen([fname,'.d'], 'w');
fid2 = fopen([fname,'.u'], 'w');
fprintf(fid1, 'digraph G {\n');
fprintf(fid2, 'graph G {\n');
fprintf(fid1, 'center = 1;\n');
fprintf(fid2, 'center = 1;\n');
fprintf(fid1, 'size=\"10,10\";\n'); % 4,4?
fprintf(fid2, 'size=\"10,10\";\n');
for i=1:n
  fprintf(fid1, 'n%d [ label = "%s" ];\n', i, nodelabel{i});
  fprintf(fid2, 'n%d [ label = "%s" ];\n', i, nodelabel{i});
end

for i=1:n
    if(tree(i))
    if exist('arclabel')
 fprintf(fid1, 'n%d -> n%d [label="%s"];\n', tree(i), i, arclabel{tree(i),i});
 fprintf(fid2, 'n%d -- n%d [label="%s"];\n', tree(i), i, arclabel{tree(i),i});
    else
 fprintf(fid1, 'n%d -> n%d;\n', tree(i), i);
 fprintf(fid2, 'n%d -- n%d;\n', tree(i), i);
    end
    end
end  

fprintf(fid1, '\n}');
fprintf(fid2, '\n}');
fclose(fid1);
fclose(fid2);
