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