about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/GraphViz/Old/pre_pesha_graph_to_dot.m
blob: 49f226f67623fd27d880c6ec8d82f7c2f96f4cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function graph_to_dot(G, varargin)
% DAG_TO_DOT Make a file representing the directed graph in dotty format.
% dag_to_dot(G, ...)
%
% Optional arguments should be passed as name/value pairs [default]
%
% 'filename' - if omitted, we write to 'tmp.dot', convert this to 'tmp.ps',
%              and then call ghostview automatically 
% 'arc_label' - arc_label{i,j} is a string attached to the i->j arc. [""]
% 'node_label' - node_label{i} is a string attached to node i. ["i"]
% 'width'      - width in inches [10]
% 'height'     - height in inches [10]
% 'leftright'  - 1 means layout left-to-right, 0 means top-to-bottom [0]
% 'directed'  - 1 means use directed arcs, 0 means undirected [1]
%
% For details on dotty, See http://www.research.att.com/sw/tools/graphviz
%
% Example:
% G = rand(5,5);
% names = cell(5,5);
% names{1,2} = 'arc 1-2';
% graph_to_dot(G, 'arc_label', names)
% or graph_to_dot(G, 'arc_label', 'numbers') % prints value of G(i,j) on i->j arc 

% Kevin Murphy, 1998

% set default args
filename = [];
node_label = [];
arc_label = [];
width = 10;
height = 10;
leftright = 0;
directed = 1;
% get optional args
args = varargin;
for i=1:2:length(args)
  switch args{i}
   case 'filename', filename = args{i+1};
   case 'node_label', node_label = args{i+1};
   case 'arc_label', arc_label = args{i+1};
   case 'width', width = args{i+1};
   case 'height', height = args{i+1};
   case 'leftright', leftright = args{i+1};
   case 'directed', directed = args{i+1};
  end
end

if isstr(arc_label) & strcmp(arc_label, 'numbers')
  N = length(G);
  arc_label = cell(N,N);
  for i=1:N
    for j=1:N
      arc_label{i,j} = sprintf('%4.2f', G(i,j));
    end
  end
end

if isempty(filename)
  make_file(G, 'tmp.dot', node_label, arc_label, width, height, leftright, directed);
  if isunix
    !dot -Tps tmp.dot -o tmp.ps

    !gs tmp.ps &
  else
    dos('dot -Tps tmp.dot -o tmp.ps');
    dos('gsview32 tmp.ps &');
  end
else
  
  
  make_file(G, filename, node_label, arc_label, width, height, leftright, directed);
end


%%%%%%

function make_file(G, filename, node_label, arc_label, width, height, leftright, directed)

n = length(G);
fid = fopen(filename, 'w');
if directed
  fprintf(fid, 'digraph G {\n');
else
  fprintf(fid, 'graph G {\n');
end
fprintf(fid, 'center = 1;\n');
fprintf(fid, 'size=\"%d,%d\";\n', width, height);
if leftright
  fprintf(fid, 'rankdir=LR;\n');
end
for i=1:n
  if isempty(node_label)
    fprintf(fid, '%d;\n', i);
  else
    fprintf(fid, '%d [ label = "%s" ];\n', i, node_label{i});
  end
end
if directed
  for i=1:n
    cs = children(G,i);
    for j=1:length(cs)
      c = cs(j);
      if isempty(arc_label)
	fprintf(fid, '%d -> %d;\n', i, c);
      else
	fprintf(fid, '%d -> %d [label="%s"];\n', i, c, arc_label{i,c});
      end
    end
  end
else
  for i=1:n
    ns = intersect(neighbors(G,i), i+1:n); % remove duplicate arcs
    for j=1:length(ns)
      c = ns(j);
      if isempty(arc_label)
	fprintf(fid, '%d -- %d [dir=none];\n', i, c);
      else
	fprintf(fid, '%d -- %d [label="%s",dir=none];\n', i, c, arc_label{i,c});
      end
    end
  end
end
fprintf(fid, '\n}');
fclose(fid);



%%%%%%%%%%%%%%%

function cs = children(adj_mat, i, t)
% CHILDREN Return the indices of a node's children in sorted order
% c = children(adj_mat, i, t)
%
% t is an optional argument: if present, dag is assumed to be a 2-slice DBN

if nargin < 3 
  cs = find(adj_mat(i,:));
else
  if t==1
    cs = find(adj_mat(i,:));
  else
    ss = length(adj_mat)/2;
    j = i+ss;
    cs = find(adj_mat(j,:)) + (t-2)*ss;
  end
end

%%%%%%%%%%%%

function ps = parents(adj_mat, i)
% PARENTS Return the list of parents of node i
% ps = parents(adj_mat, i)

ps = find(adj_mat(:,i))';

%%%%%%%%%%%%%

function ns = neighbors(adj_mat, i)
% NEIGHBORS Find the parents and children of a node in a graph.
% ns = neighbors(adj_mat, i)

ns = union(children(adj_mat, i), parents(adj_mat, i));