function [C1, C2, cl_weight] = jgraph_costs(cliques, num_bn_nodes, node_sizes)
% JGRAPH_COSTS Compute the cost of edges in the junction graph.
% [C1, C2, cl_weight] = jgraph_costs(cliques, num_bn_nodes, node_sizes)
%
% In the junction graph, edge i,j has mass |C_i intersect C_j| and cost ns(C_i) + ns(C_j).
% We want to maximize the mass and minimize the cost.
% See Jensen and Jensen, "Optimal Junction Trees", UAI 94.

n = length(cliques);
C1 = zeros(n);
C2 = zeros(n);
cl_bit_vector = zeros(n, num_bn_nodes);
cl_weight = zeros(1,n);
for i=1:n
  cl_bit_vector(i, cliques{i}) = 1;
  cl_weight(i) = prod(node_sizes(cliques{i}));
end

for i=1:n
  for j=1:n
    if j ~= i
      %C1(i,j) = length(intersect(cliques{i}, cliques{j})); % slow!
      C1(i,j) = sum(cl_bit_vector(i,:) & cl_bit_vector(j,:));
      C2(i,j) = cl_weight(i) + cl_weight(j);
    end
  end
end



