function OUT = weights(B)

%WEIGHTS computes both a connectivity weight matrix and a 
%percent-of-perimeter weights matrix for boundary files in
%INFOMAP boundary format. Let K = no. of polygons.
%
%INPUTS: B = Boundary file in (N,2)-matrix form, where N is the 
%            number of boundary points plus K separating rows and
%            K repeat points at the end of each polygon. 
%            [NOTE: It is assumed that all blank positions in 
%            separator rows between polygons have been replaced
%            by the MATLAB symbol 'NaN' (denoting 'Not a Number')].
%              
%        INFOMAP Boundary File Format:  row  col.1  col.2
%                                        1    5      NaN
%													  2    75     34
%                                        .     .      .
%        First Polygon has FOUR points:  .     .      .
%         (first point = last point)     6    75     34
%                                        7    14     NaN
%                                        8    78     39
%                                        9     .      .

%OUTPUTS: OUT = (2,1)-cell , where:
%
%         OUT{1} = sparse (KxK)-matrix with elements (pi,qi,vi) denoting
%                  the (ordered) adjacent polygon pairs (pi,qi)  
%                  with weight values vi denoting the percent of pi's 
%                  perimeter shared with qi .
%         OUT{2} = sparse (KxK)-matrix with elements (pi,qi,1) denoting
%                  the adjacent polygon pairs (pi,qi) with weight
%                  values, 1, denoting connectedness.

%Create Cell Array of Polygons

k = 0 ;

N1 = 0 ;

del = 1 ;

while del > 0
   
   k = k+1 ;
   
   N2 = B(N1+1,1) ;
   
   if ~isnan(B(N1+1,2))
      
      error('wrong format for separator')
      
   end
   
   %Form polygon P{k} (check for repeat point at end)
   
   if B(N1+1+N2,:) ~= B(N1+2,:)
      
      error('end point not equal first point') ;
      
   end
   
      
   P{k} = B(N1+2:N1+N2,:) ; %Keep only distinct points
      
   N1 = N1+1+N2 ;
   
   if N1 >= length(B)
      
      del = 0 ;
      
   end
   
end


%Now form percent-share matrix, S.

K = k ;  %number of polygons

s = zeros(K) ;

for i = 1:K-1
   
   P1 = P{i} ;
   
   U1 = ones(length(P1),1) ;
   
   for j = i+1:K
      
      P2 = P{j} ;
      
      U2 = ones(length(P2),1) ;
      
      T1 = kron(P1,U2);
      
      TT1 = T1(40:60,:) ;
      
      T2 = kron(U1,P2) ;
      
      TT2 = T2(40:60,:);
      
      Diff = kron(P1,U2) - kron(U1,P2) ;
      
      DD = Diff(40:60,:);
      
      Diff = abs(Diff) ;
      
      
      
      Res = Diff(:,1) + Diff(:,2) ;
      
      RR = Res(40:60,:) ;
      
      Shared = length(Diff) - nnz(Res);
      
      S(i,j) = Shared/length(P1) ;
      
      S(j,i) = Shared/length(P2) ;
      
   end
   
end

S = sparse(S) ;

W = spones(S) ;

OUT{1} = S ;

OUT{2} = W ;
      
