annotate toolboxes/FullBNT-1.0.7/graph/mk_all_dags.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function Gs = mk_all_dags(N, order)
wolffd@0 2 % MK_ALL_DAGS generate all DAGs on N variables
wolffd@0 3 % G = mk_all_dags(N)
wolffd@0 4 %
wolffd@0 5 % G = mk_all_dags(N, order) only generates DAGs in which node i has parents from
wolffd@0 6 % nodes in order(1:i-1). Default: order=[] (no constraints).
wolffd@0 7 %
wolffd@0 8 % G{i} is the i'th dag
wolffd@0 9 %
wolffd@0 10 % Note: the number of DAGs is super-exponential in N, so don't call this with N > 4.
wolffd@0 11
wolffd@0 12 if nargin < 2, order = []; end
wolffd@0 13
wolffd@0 14 use_file = 0;
wolffd@0 15
wolffd@0 16 global BNT_HOME
wolffd@0 17 fname = sprintf('%s/DAGS%d.mat', BNT_HOME, N);
wolffd@0 18 if use_file & exist(fname, 'file')
wolffd@0 19 S = load(fname, '-mat');
wolffd@0 20 fprintf('loading %s\n', fname);
wolffd@0 21 Gs = S.Gs;
wolffd@0 22 return;
wolffd@0 23 end
wolffd@0 24
wolffd@0 25 m = 2^(N*N);
wolffd@0 26 ind = ind2subv(2*ones(1,N^2), 1:m);
wolffd@0 27 Gs = {};
wolffd@0 28 j = 1;
wolffd@0 29 directed = 1;
wolffd@0 30 for i=1:m
wolffd@0 31 dag = reshape(ind(i,:)-1, N, N);
wolffd@0 32 if acyclic(dag, directed)
wolffd@0 33 out_of_order = 0;
wolffd@0 34 if ~isempty(order)
wolffd@0 35 for k=1:N-1
wolffd@0 36 if any(dag(order(k+1:end), k))
wolffd@0 37 out_of_order = 1;
wolffd@0 38 break;
wolffd@0 39 end
wolffd@0 40 end
wolffd@0 41 end
wolffd@0 42 if ~out_of_order
wolffd@0 43 Gs{j} = dag;
wolffd@0 44 j = j + 1;
wolffd@0 45 end
wolffd@0 46 end
wolffd@0 47 end
wolffd@0 48
wolffd@0 49 if use_file
wolffd@0 50 disp(['mk_all_dags: saving to ' fname '!']);
wolffd@0 51 save(fname, 'Gs');
wolffd@0 52 end