dag_to_dot.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 80 行

M
80
字号
function dag_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]%% For details on dotty, See http://www.research.att.com/sw/tools/graphviz% set default argsfilename = [];node_label = [];arc_label = [];width = 10;height = 10;leftright = 0;% get optional argsargs = 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};  endendif isempty(filename)  make_file(G, 'tmp.dot', node_label, arc_label, width, height, leftright);  !dot -Tps tmp.dot -o tmp.ps  !ghostview tmp.ps &else  make_file(G, filename, node_label, arc_label, width, height, leftright);end%%%%%%function make_file(G, filename, node_label, arc_label, width, height, leftright)n = length(G);fid = fopen(filename, 'w');fprintf(fid, 'digraph G {\n');fprintf(fid, 'center = 1;\n');fprintf(fid, 'size=\"%d,%d\";\n', width, height);if leftright  fprintf(fid, 'rankdir=LR;\n');endfor i=1:n  if isempty(node_label)    fprintf(fid, '%d;\n', i);  else    fprintf(fid, '%d [ label = "%s" ];\n', i, node_label{i});  endendfor i=1:n  cs = children(G,i);  for j=1:length(cs)    c = cs(j);    if isempty(arc_label)      fprintf(fid, '%d -> %d [dir=none];\n', i, c);    else      fprintf(fid, '%d -> %d [label="%s"];\n', i, c, arc_label{i,c});    end  endendfprintf(fid, '\n}');fclose(fid);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?