function [findultra,vafultra,conperm,vafarob,arobprox,fitlinear,vaflinear,...
    coord,addcon] = cat_vs_con_orderfnd(prox,inperm)

% CAT_VS_CON_ORDERFND finds a constraining order to fit a best
% ultrametric, anti-Robinson form, and linear unidimensional scale; all 
% three of these representations conform to this order.
%
% syntax: [findultra,vafultra,conperm,vafarob,arobprox,fitlinear,vaflinear,...
%    coord,addcon] = cat_vs_con_orderfnd(prox,inperm)
%    
% PROX is the input dissimilarity matrix and INPERM is
% a starting permutation for how the ultrametric constraints are searched. 
% The permutation CONPERM is a given order found and used to
% constrain the various representations. FINDULTRA is the best ultrametric
% found with VAF of VAFULTRA; AROBPROX is the best AR form identified 
% with VAF of VAFAROB; FITLINEAR is the best LUS model with VAF of VAFLINEAR
% with COORD constraining the coordinates and ADDCON the additive constant.


n = size(prox,1);
work = zeros(n*(n-1)*(n-2),1);
[arobprox,vafarob,conperm] = arobfnd(prox,inperm,2);
find = arobprox;
cr = 1.0;
iterate = 0;
proxave = zeros(n,n);

while (cr >= 1.0e-006)
   cr = 0.0;
   indexll = 0;
   iterate = iterate + 1;
   
   
   for ione = 1:(n-2)
      for itwo = (ione+1):(n-1)
        for ithree = (itwo+1):n
            
            indxord(1) = inperm(ione);
            indxord(2) = inperm(itwo);
            indxord(3) = inperm(ithree);
            
            for i = 1:2
               for j = (i+1):3
                  
                  if(indxord(i) > indxord(j))
                     
                     temp = indxord(i);
                     indxord(i) = indxord(j);
                     indxord(j) = temp;
                     
                  end
               end
            end
            
               
            jone = indxord(1);
            jtwo = indxord(2);
            jthree = indxord(3);
                          
            p1 = find(jone,jtwo);
            p2 = find(jone,jthree);
            p3 = find(jtwo,jthree);
            
            if(iterate <= 100)
            
            find(jone,jtwo) = find(jone,jtwo) - work(indexll+1);
            find(jone,jthree) = find(jone,jthree) - work(indexll+2);
            find(jtwo,jthree) = find(jtwo,jthree) - work(indexll+3);
            
            end
         
            
            if((find(jone,jtwo) <= find(jone,jthree)) && ...
                  (find(jone,jtwo) <= find(jtwo,jthree))) 
               
               ave = (find(jone,jthree) + find(jtwo,jthree))/2.0;
   
               work(indexll+1) = 0.0;
               work(indexll+2) = ave - find(jone,jthree);
               work(indexll+3) = ave - find(jtwo,jthree); 
               
               find(jone,jthree) = ave;
               find(jtwo,jthree) = ave;
               
               indexll = indexll + 3;
                       
             elseif((find(jone,jthree) <= find(jone,jtwo)) && ...
                  (find(jone,jthree) <= find(jtwo,jthree))) 
               
               ave = (find(jone,jtwo) + find(jtwo,jthree))/2.0;
   
               work(indexll+1) = ave - find(jone,jtwo);
               work(indexll+2) = 0.0;
               work(indexll+3) = ave - find(jtwo,jthree); 
               
               find(jone,jtwo) = ave;
               find(jtwo,jthree) = ave;
               
               indexll = indexll + 3;
   
             elseif((find(jtwo,jthree) <= find(jone,jthree)) && ...
                  (find(jtwo,jthree) <= find(jone,jtwo))) 
               
               ave = (find(jone,jthree) + find(jone,jtwo))/2.0;
   
               work(indexll+1) = ave - find(jone,jtwo);
               work(indexll+2) = ave - find(jone,jthree);
               work(indexll+3) = 0.0; 
               
               find(jone,jtwo) = ave;
               find(jone,jthree) = ave;
               
               indexll = indexll + 3;
            end
            
               cr = cr + abs(p1-find(jone,jtwo)) + abs(p2-find(jone,jthree)) ... 
               + abs(p3-find(jtwo,jthree)); 
            
         end
      end
   end
end

for jone = 1:(n-1)
   for jtwo = (jone+1):n
      
      find(jtwo,jone) = find(jone,jtwo);
      
   end
end

[fit, vaf] = ultrafit(arobprox,find);

findultra = fit;

aveprox = sum(sum(prox))/(n*(n-1));

for i = 1:n
   for j = 1:n
      if( i ~= j)
         proxave(i,j) = aveprox;
      else
         proxave(i,j) = 0;
     end
   end
end

diff = sum(sum((prox(conperm,conperm) - findultra).^2));

denom = sum(sum((prox(conperm,conperm) - proxave).^2));

vafultra = 1 - (diff/denom);

[fitlinear,vaflinear,coord,addcon] = linfitac(prox,conperm);


   
     
                        
               
      
               
   
   
   
   
