## Basic Sequential Algorithmic Scheme for clustering

function [clist, ncluster] = bsas (x, threshold, maxc)

   [N,l] = size(x); ## samples are row vectors, cols is number of samples

## randomize the inputs

   newx = randomize (1, N);
   
## Build a proximity matrix.

   pmatrix = proxmat (newx);

## Build a membership list for the clusters;

   mlist = zeros (1, N);
   clist = zeros (N, N);

## initialize - the first cluster is the first x vector
  
   ncluster = 1;
   mlist (1) = 1;
   clist (1,1) = 1;
   csize = zeros (1,N);
   csize (1) = 1;

## For each vector, assign it to a cluster or form a new cluster.

   for ct = 2:N
      for c = 1:ncluster
         minp = 2^20;
         for v = 1:csize (c)

##          Use the proximity matrix to compare to each member of a cluster

            p = pmatrix (clist (c,v), ct);
            if (p < minp)
               minp = p;
               minc = c;
               minx = v;
            endif
         endfor ## for v
      endfor ## for c
               
##    Form a new cluster or add to an existing cluster

      if ((minp > threshold) && (ncluster < maxc))
         ncluster = ncluster + 1;
         clist (ncluster,1) = ct; 
         csize (ncluster) = 1;
      else
         csize (minc) = csize (minc) + 1;
         clist (minc, csize (minc)) = ct;
      endif   
         
   endfor

endfunction
         


