To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _FullBNT / BNT / graph / mk_all_dags.m @ 8:b5b38998ef3b
History | View | Annotate | Download (1.08 KB)
| 1 |
function Gs = mk_all_dags(N, order) |
|---|---|
| 2 |
% MK_ALL_DAGS generate all DAGs on N variables |
| 3 |
% G = mk_all_dags(N) |
| 4 |
% |
| 5 |
% G = mk_all_dags(N, order) only generates DAGs in which node i has parents from |
| 6 |
% nodes in order(1:i-1). Default: order=[] (no constraints). |
| 7 |
% |
| 8 |
% G{i} is the i'th dag
|
| 9 |
% |
| 10 |
% Note: the number of DAGs is super-exponential in N, so don't call this with N > 4. |
| 11 |
|
| 12 |
if nargin < 2, order = []; end |
| 13 |
|
| 14 |
use_file = 0; |
| 15 |
|
| 16 |
global BNT_HOME |
| 17 |
fname = sprintf('%s/DAGS%d.mat', BNT_HOME, N);
|
| 18 |
if use_file & exist(fname, 'file') |
| 19 |
S = load(fname, '-mat'); |
| 20 |
fprintf('loading %s\n', fname);
|
| 21 |
Gs = S.Gs; |
| 22 |
return; |
| 23 |
end |
| 24 |
|
| 25 |
m = 2^(N*N); |
| 26 |
ind = ind2subv(2*ones(1,N^2), 1:m); |
| 27 |
Gs = {};
|
| 28 |
j = 1; |
| 29 |
directed = 1; |
| 30 |
for i=1:m |
| 31 |
dag = reshape(ind(i,:)-1, N, N); |
| 32 |
if acyclic(dag, directed) |
| 33 |
out_of_order = 0; |
| 34 |
if ~isempty(order) |
| 35 |
for k=1:N-1 |
| 36 |
if any(dag(order(k+1:end), k)) |
| 37 |
out_of_order = 1; |
| 38 |
break; |
| 39 |
end |
| 40 |
end |
| 41 |
end |
| 42 |
if ~out_of_order |
| 43 |
Gs{j} = dag;
|
| 44 |
j = j + 1; |
| 45 |
end |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
if use_file |
| 50 |
disp(['mk_all_dags: saving to ' fname '!']); |
| 51 |
save(fname, 'Gs'); |
| 52 |
end |