function [cpts] = learnNet(inputs,parents)
%
% function [cpts] = learnNet(inputs,parents)
%
% Parents is an MxM matrix for each node listing
% which nodes are its parents. A one says that the
% corresponding column is a parent of the current row
% and zero means not a parents
% Make sure that parents come first (topological)
% Make sure that this is a valid DAG
% Make sure that nodes are not self-parents
%
% inputs is an NxM matrix where the values at the
% m'th column range from 0..D_m 
%
% cpts is a matrix of rows where each row corresponds
% to a node and contains its conditional probability
% hypercube rasterized as a long vector
%

[N,M] = size(inputs);

% Figure out the cptsizes
cards = max(inputs);
cards = cards+ones(size(cards));
cptsize = zeros(M,1);
for i=1:M
  cptsize(i) = cards(i);
  for j=1:(i-1)
    if (parents(i,j)>0.5)
      cptsize(i) = cptsize(i)*cards(j);
    end
  end
end


% Initialize the cpt structure to -1 where
% negative indicates it is currently an invalid value.
cpts = -1.0*ones(M,max(cptsize));





% NOW ENTER THE REST OF THE CODE HERE, OVERWRITE THE
% -1.0 ENTRIES IN cpts WITH THE CORRECT COUNTS
% YOU DON'T NEED TO WORRY ABOUT NORMALIZING BECAUSE
% THE FOLLOWING FUNCTION WILL DO IT FOR YOU





% Normalize the cpts
for i=1:M
  index = 1;
  while (index<cptsize(i))
    tiny = min(cpts(i,index:index+(cards(i)-1)));
    if (tiny<0.0)
      fprintf(1,'BAD: Found negative for cpt for node %d at index [%d,%d].\n',i,tot,index,index+cards(i)-1);
    end
    tot  = sum(cpts(i,index:index+(cards(i)-1)));
    cpts(i,index:index+(cards(i)-1)) = cpts(i,index:index+(cards(i)-1))*(1/tot);
    index = index+cards(i);
  end
end


