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

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