function [Y,F,M] = map(C,X,M)
% MAP Finds a maximum a posteriori configuration of data instance X
%
% [Y,F,M] = MAP(C,X) where C is the gcrf, F is the exit flag from BP
% and X is the data instance. Y are the node labels.
%
% MAP(C,X,M) where M are the messages to start BP with

 
 % GCRF.MAP


% Jerod Weinman
% jerod@acm.org
    
% $Id: map.m 1 2009-05-07 13:41:00Z weinman $
 
% Jerod Weinman
% jerod@acm.org
% Copyright 2004, 2008, 2009
 
%    This file is part of Matlab GridCRF.
%
%    Matlab GridCRF is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    Matlab GridCRF is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with Matlab GridCRF.  If not, see <http://www.gnu.org/licenses/>.
     
   if 0
     G = unrollgm(C,X.node,X.hEdge,X.vEdge);
     
     % Do max-product
     if nargin==2
       [Y,B,F,M] = asyncbp(G.adjmat, G.nodeEnergies, G.edgeEnergies, 1);
     else
       [Y,B,F,M] = asyncbp(G.adjmat, G.nodeEnergies, G.edgeEnergies, 1, M);
     end;
     
     %Y = reshape(Y,[size(X.node,1) size(X.node,2)]);
   else
     G = unrollgridflat(get(C,'weights'),X.node,X.hEdge,X.vEdge,get(C,'defEdge'));
     K = nodenum(gridsize(X.node,X.hEdge));
     [Y,F,M] = gridsyncbp(K,G,1);
   end;
