function jtree = mk_jtree_topology(MG, ns, elim_order)
% MK_JTREE_TOPOLOGY Make a junction tree whose nodes are cliques of triangulate(moralize(dag)).
% jtree = mk_jtree_topology(MG, ns, elim_order)
%
% MG is the undirected (moral) graph.
% ns(i) is the num. of values node i can take on.

[MTG, jtree.cliques, jtree.fill_in_edges]  = triangulate(MG, elim_order); % MTG = triangulate(MG)
num_cliques = length(jtree.cliques);
num_bn_nodes = length(ns(:));
[C1, C2, jtree.clique_sizes] = jgraph_costs(jtree.cliques, num_bn_nodes, ns);
jtree.adj_mat = sparse(minimum_spanning_tree(-C1, C2));
jtree.cliques_containing_node = inverse_relation(jtree.cliques, num_bn_nodes);
check_jtree_property(jtree.cliques_containing_node, jtree.adj_mat);

% The root is arbitrary, but since the first pass is towards the root,
% we would like this to correspond to going forward in time in a DBN.
root = num_cliques;
%root = 1;
jtree.root = root;
jtree.elim_order = elim_order;
[jtree.adj_mat, jtree.preorder, jtree.postorder] = mk_rooted_tree(jtree.adj_mat, root);

