function undag = moralize( dag, flag, gnames ) % function undag = moralize( dag, flag, gnames ) % ARGS: dag = adjacency matrix % flag = whether to print comments or not % gnames = names of nodes for printing % RET: undag = undirected acyclic graph that has been moralized % % dag = % 1->2 % 0 1 1 0 0 0 0 0 % 2->1 0 0 0 1 0 0 0 0 % 0 0 0 0 1 0 1 0 % 0 0 0 0 0 1 0 0 % 0 0 0 0 0 1 0 1 % 0 0 0 0 0 0 0 0 % 0 0 0 0 0 0 0 1 % undag = % A->B C D E F G H % A 0 1 1 0 0 0 0 0 % B->A 1 0 0 1 0 0 0 0 % C 1 0 0 0 1 0 1 0 % D 0 1 0 0 1 1 0 0 % E 0 0 1 1 0 1 1 1 % F 0 0 0 1 1 0 0 0 % G 0 0 1 0 1 0 0 1 % H 0 0 0 0 1 0 1 0 if nargin < 2, flag = 0; end; N = length(dag); for i=1:N, parents = find(dag(:,i)); % drop directionality len = length(parents); for p=1:len thisp = parents(p); dag(i,thisp) = 1; end; % marry the parents for p=1:len thisp = parents(p); otherp = parents(~ismember(parents,thisp)); if ~isempty(otherp) dag(thisp,otherp) = 1; if flag == 1, input(sprintf('married node %d(%s) with...',thisp,char(gnames(thisp)))); for o=1:length(otherp), disp(sprintf(' ... %d(%s)',otherp(o),char(gnames(otherp(o))))); end; % end_for end; % end_flag end; end; end; undag = dag;