%COMPUTEINITIALPOTENTIALS Sets up the cliques in the clique tree that is
%passed in as a parameter.
%
%   P = COMPUTEINITIALPOTENTIALS(C) Takes the clique tree skeleton C which is a
%   struct with three fields:
%   - nodes: cell array representing the cliques in the tree.
%   - edges: represents the adjacency matrix of the tree.
%   - factorList: represents the list of factors that were used to build
%   the tree.
%
%   It returns the standard form of a clique tree P that we will use through
%   the rest of the assigment. P is struct with two fields:
%   - cliqueList: represents an array of cliques with appropriate factors
%   from factorList assigned to each clique. Where the .val of each clique
%   is initialized to the initial potential of that clique.
%   - edges: represents the adjacency matrix of the tree.
%
% Copyright (C) Daphne Koller, Stanford University, 2012


function P = ComputeInitialPotentials(C)

% number of cliques
N = length(C.nodes);

% initialize cluster potentials
P.cliqueList = repmat(struct('var', [], 'card', [], 'val', []), N, 1);
P.edges = C.edges;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% YOUR CODE HERE
%
% First, compute an assignment of factors from factorList to cliques.
% Then use that assignment to initialize the cliques in cliqueList to
% their initial potentials.

% C.nodes is a list of cliques.
% So in your code, you should start with: P.cliqueList(i).var = C.nodes{i};
% Print out C to get a better understanding of its structure.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

cardVars = [];

for j = 1:length(C.factorList)
  for k = 1:length(C.factorList(j).var)
    cardVars(C.factorList(j).var(k)) = C.factorList(j).card(k);
  endfor
endfor

for i = 1:N
  P.cliqueList(i).var = C.nodes{i};
  P.cliqueList(i).card = cardVars(C.nodes{i});
  P.cliqueList(i).val = ones(1, prod(P.cliqueList(i).card));
endfor

asg = cell(1, N);

for j = 1:length(C.factorList)
  for i = 1:N
    if all(ismember(C.factorList(j).var, C.nodes{i}))
      asg{i}(end+1) = j;
      break;
    endif
  endfor
endfor

for i = 1:N
  fak = struct('var', [], 'card', [], 'val', []);
  for j = 1:length(asg{i})
    fak = FactorProduct(fak, C.factorList(asg{i}(j)));
  endfor
  if !isempty(fak.var)
    P.cliqueList(i) = FactorProduct(P.cliqueList(i), fak);
  endif
endfor







