annotate toolboxes/FullBNT-1.0.7/graph/graph_to_jtree.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 [jtree, root, cliques, B, w, elim_order] = graph_to_jtree(MG, ns, partial_order, stages, clusters)
Daniel@0 2 % GRAPH_TO_JTREE Triangulate a graph and make a junction tree from its cliques.
Daniel@0 3 % [jtree, root, cliques, B, w, elim_order] = ...
Daniel@0 4 % graph_to_jtree(graph, node_sizes, partial_order, stages, clusters)
Daniel@0 5 %
Daniel@0 6 % INPUT:
Daniel@0 7 % graph(i,j) = 1 iff there is an edge between i,j
Daniel@0 8 % node_weights(i) = num discrete values node i can take on [1 if observed]
Daniel@0 9 % partial_order = {} if no constraints on elimination ordering
Daniel@0 10 % stages{i} = nodes that must be eliminated at i'th stage (if porder is empty)
Daniel@0 11 % clusters{i} = list of nodes that must get connected together in the moral graph
Daniel@0 12 %
Daniel@0 13 % OUTPUT:
Daniel@0 14 % jtree(i,j) = 1 iff there is an arc between clique i and clique j
Daniel@0 15 % root = the root clique
Daniel@0 16 % cliques{i} = the nodes in clique i
Daniel@0 17 % B(i,j) = 1 iff node j occurs in clique i
Daniel@0 18 % w(i) = weight of clique i
Daniel@0 19
Daniel@0 20 N = length(MG);
Daniel@0 21
Daniel@0 22 if nargin >= 5
Daniel@0 23 % Add extra arcs between nodes in each cluster to ensure they occur in the same clique
Daniel@0 24 for i=1:length(clusters)
Daniel@0 25 c = clusters{i};
Daniel@0 26 MG(c,c) = 1;
Daniel@0 27 end
Daniel@0 28 end
Daniel@0 29 MG = setdiag(MG, 0);
Daniel@0 30
Daniel@0 31 % Find an optimal elimination ordering (NP-hard problem!)
Daniel@0 32 if nargin < 4
Daniel@0 33 stages = {1:N};
Daniel@0 34 end
Daniel@0 35 if nargin < 3
Daniel@0 36 partial_order = {};
Daniel@0 37 end
Daniel@0 38 if isempty(partial_order)
Daniel@0 39 strong = 0;
Daniel@0 40 elim_order = best_first_elim_order(MG, ns, stages);
Daniel@0 41 else
Daniel@0 42 strong = 1;
Daniel@0 43 elim_order = strong_elim_order(MG, ns, partial_order);
Daniel@0 44 end
Daniel@0 45
Daniel@0 46 [MTG, cliques, fill_in_edges] = triangulate(MG, elim_order);
Daniel@0 47
Daniel@0 48 % Connect the cliques up into a jtree,
Daniel@0 49 [jtree, root, B, w] = cliques_to_jtree(cliques, ns);
Daniel@0 50
Daniel@0 51 if 0
Daniel@0 52 disp('testing dag to jtree');
Daniel@0 53 % Find the cliques containing each node, and check they form a connected subtree
Daniel@0 54 clqs_con_node = cell(1,N);
Daniel@0 55 for i=1:N
Daniel@0 56 clqs_con_node{i} = find(B(:,i))';
Daniel@0 57 end
Daniel@0 58 check_jtree_property(clqs_con_node, jtree);
Daniel@0 59 end