Daniel@0: function graph_to_dot(G, varargin) Daniel@0: % DAG_TO_DOT Make a file representing the directed graph in dotty format. Daniel@0: % dag_to_dot(G, ...) Daniel@0: % Daniel@0: % Optional arguments should be passed as name/value pairs [default] Daniel@0: % Daniel@0: % 'filename' - if omitted, we write to 'tmp.dot', convert this to 'tmp.ps', Daniel@0: % and then call ghostview automatically Daniel@0: % 'arc_label' - arc_label{i,j} is a string attached to the i->j arc. [""] Daniel@0: % 'node_label' - node_label{i} is a string attached to node i. ["i"] Daniel@0: % 'width' - width in inches [10] Daniel@0: % 'height' - height in inches [10] Daniel@0: % 'leftright' - 1 means layout left-to-right, 0 means top-to-bottom [0] Daniel@0: % 'directed' - 1 means use directed arcs, 0 means undirected [1] Daniel@0: % Daniel@0: % For details on dotty, See http://www.research.att.com/sw/tools/graphviz Daniel@0: % Daniel@0: % Example: Daniel@0: % G = rand(5,5); Daniel@0: % names = cell(5,5); Daniel@0: % names{1,2} = 'arc 1-2'; Daniel@0: % graph_to_dot(G, 'arc_label', names) Daniel@0: % or graph_to_dot(G, 'arc_label', 'numbers') % prints value of G(i,j) on i->j arc Daniel@0: Daniel@0: % Kevin Murphy, 1998 Daniel@0: Daniel@0: % set default args Daniel@0: filename = []; Daniel@0: node_label = []; Daniel@0: arc_label = []; Daniel@0: width = 10; Daniel@0: height = 10; Daniel@0: leftright = 0; Daniel@0: directed = 1; Daniel@0: % get optional args Daniel@0: args = varargin; Daniel@0: for i=1:2:length(args) Daniel@0: switch args{i} Daniel@0: case 'filename', filename = args{i+1}; Daniel@0: case 'node_label', node_label = args{i+1}; Daniel@0: case 'arc_label', arc_label = args{i+1}; Daniel@0: case 'width', width = args{i+1}; Daniel@0: case 'height', height = args{i+1}; Daniel@0: case 'leftright', leftright = args{i+1}; Daniel@0: case 'directed', directed = args{i+1}; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if isstr(arc_label) & strcmp(arc_label, 'numbers') Daniel@0: N = length(G); Daniel@0: arc_label = cell(N,N); Daniel@0: for i=1:N Daniel@0: for j=1:N Daniel@0: arc_label{i,j} = sprintf('%4.2f', G(i,j)); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if isempty(filename) Daniel@0: make_file(G, 'tmp.dot', node_label, arc_label, width, height, leftright, directed); Daniel@0: if isunix Daniel@0: !dot -Tps tmp.dot -o tmp.ps Daniel@0: Daniel@0: !gs tmp.ps & Daniel@0: else Daniel@0: dos('dot -Tps tmp.dot -o tmp.ps'); Daniel@0: dos('gsview32 tmp.ps &'); Daniel@0: end Daniel@0: else Daniel@0: Daniel@0: Daniel@0: make_file(G, filename, node_label, arc_label, width, height, leftright, directed); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: %%%%%% Daniel@0: Daniel@0: function make_file(G, filename, node_label, arc_label, width, height, leftright, directed) Daniel@0: Daniel@0: n = length(G); Daniel@0: fid = fopen(filename, 'w'); Daniel@0: if directed Daniel@0: fprintf(fid, 'digraph G {\n'); Daniel@0: else Daniel@0: fprintf(fid, 'graph G {\n'); Daniel@0: end Daniel@0: fprintf(fid, 'center = 1;\n'); Daniel@0: fprintf(fid, 'size=\"%d,%d\";\n', width, height); Daniel@0: if leftright Daniel@0: fprintf(fid, 'rankdir=LR;\n'); Daniel@0: end Daniel@0: for i=1:n Daniel@0: if isempty(node_label) Daniel@0: fprintf(fid, '%d;\n', i); Daniel@0: else Daniel@0: fprintf(fid, '%d [ label = "%s" ];\n', i, node_label{i}); Daniel@0: end Daniel@0: end Daniel@0: if directed Daniel@0: for i=1:n Daniel@0: cs = children(G,i); Daniel@0: for j=1:length(cs) Daniel@0: c = cs(j); Daniel@0: if isempty(arc_label) Daniel@0: fprintf(fid, '%d -> %d;\n', i, c); Daniel@0: else Daniel@0: fprintf(fid, '%d -> %d [label="%s"];\n', i, c, arc_label{i,c}); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: else Daniel@0: for i=1:n Daniel@0: ns = intersect(neighbors(G,i), i+1:n); % remove duplicate arcs Daniel@0: for j=1:length(ns) Daniel@0: c = ns(j); Daniel@0: if isempty(arc_label) Daniel@0: fprintf(fid, '%d -- %d [dir=none];\n', i, c); Daniel@0: else Daniel@0: fprintf(fid, '%d -- %d [label="%s",dir=none];\n', i, c, arc_label{i,c}); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: fprintf(fid, '\n}'); Daniel@0: fclose(fid); Daniel@0: Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%%%% Daniel@0: Daniel@0: function cs = children(adj_mat, i, t) Daniel@0: % CHILDREN Return the indices of a node's children in sorted order Daniel@0: % c = children(adj_mat, i, t) Daniel@0: % Daniel@0: % t is an optional argument: if present, dag is assumed to be a 2-slice DBN Daniel@0: Daniel@0: if nargin < 3 Daniel@0: cs = find(adj_mat(i,:)); Daniel@0: else Daniel@0: if t==1 Daniel@0: cs = find(adj_mat(i,:)); Daniel@0: else Daniel@0: ss = length(adj_mat)/2; Daniel@0: j = i+ss; Daniel@0: cs = find(adj_mat(j,:)) + (t-2)*ss; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%% Daniel@0: Daniel@0: function ps = parents(adj_mat, i) Daniel@0: % PARENTS Return the list of parents of node i Daniel@0: % ps = parents(adj_mat, i) Daniel@0: Daniel@0: ps = find(adj_mat(:,i))'; Daniel@0: Daniel@0: %%%%%%%%%%%%% Daniel@0: Daniel@0: function ns = neighbors(adj_mat, i) Daniel@0: % NEIGHBORS Find the parents and children of a node in a graph. Daniel@0: % ns = neighbors(adj_mat, i) Daniel@0: Daniel@0: ns = union(children(adj_mat, i), parents(adj_mat, i)); Daniel@0: Daniel@0: Daniel@0: