Daniel@0: function [jtree, root, cliques, B, w, elim_order] = graph_to_jtree(MG, ns, partial_order, stages, clusters) Daniel@0: % GRAPH_TO_JTREE Triangulate a graph and make a junction tree from its cliques. Daniel@0: % [jtree, root, cliques, B, w, elim_order] = ... Daniel@0: % graph_to_jtree(graph, node_sizes, partial_order, stages, clusters) Daniel@0: % Daniel@0: % INPUT: Daniel@0: % graph(i,j) = 1 iff there is an edge between i,j Daniel@0: % node_weights(i) = num discrete values node i can take on [1 if observed] Daniel@0: % partial_order = {} if no constraints on elimination ordering Daniel@0: % stages{i} = nodes that must be eliminated at i'th stage (if porder is empty) Daniel@0: % clusters{i} = list of nodes that must get connected together in the moral graph Daniel@0: % Daniel@0: % OUTPUT: Daniel@0: % jtree(i,j) = 1 iff there is an arc between clique i and clique j Daniel@0: % root = the root clique Daniel@0: % cliques{i} = the nodes in clique i Daniel@0: % B(i,j) = 1 iff node j occurs in clique i Daniel@0: % w(i) = weight of clique i Daniel@0: Daniel@0: N = length(MG); Daniel@0: Daniel@0: if nargin >= 5 Daniel@0: % Add extra arcs between nodes in each cluster to ensure they occur in the same clique Daniel@0: for i=1:length(clusters) Daniel@0: c = clusters{i}; Daniel@0: MG(c,c) = 1; Daniel@0: end Daniel@0: end Daniel@0: MG = setdiag(MG, 0); Daniel@0: Daniel@0: % Find an optimal elimination ordering (NP-hard problem!) Daniel@0: if nargin < 4 Daniel@0: stages = {1:N}; Daniel@0: end Daniel@0: if nargin < 3 Daniel@0: partial_order = {}; Daniel@0: end Daniel@0: if isempty(partial_order) Daniel@0: strong = 0; Daniel@0: elim_order = best_first_elim_order(MG, ns, stages); Daniel@0: else Daniel@0: strong = 1; Daniel@0: elim_order = strong_elim_order(MG, ns, partial_order); Daniel@0: end Daniel@0: Daniel@0: [MTG, cliques, fill_in_edges] = triangulate(MG, elim_order); Daniel@0: Daniel@0: % Connect the cliques up into a jtree, Daniel@0: [jtree, root, B, w] = cliques_to_jtree(cliques, ns); Daniel@0: Daniel@0: if 0 Daniel@0: disp('testing dag to jtree'); Daniel@0: % Find the cliques containing each node, and check they form a connected subtree Daniel@0: clqs_con_node = cell(1,N); Daniel@0: for i=1:N Daniel@0: clqs_con_node{i} = find(B(:,i))'; Daniel@0: end Daniel@0: check_jtree_property(clqs_con_node, jtree); Daniel@0: end