%%%%%%% 
% adjmat.m
% Author: Lance Harden, Davidson College
% June 2006
% 
% Description: Adjmat creates an adjacency matrix for all signed
% permutations of length k using sorting by reversals. The adjacency matrix
% is sorted by an inputted index matrix, ind. The input s allows
% the user to choose between 1 or 2 spatulas (prefix sorting by reversals
% vs. sorting by reversals).

function [adj] = adjmat(k,s,ind)
if nargin < 2, s = 2; ind = 1:(2^k*factorial(k));
elseif nargin < 3, ind = 1:(2^k*factorial(k)); end

A = signedperms(k);
b = length(A);
A = A(ind,:);

if s == 1
    adj = zeros(b);
    for j = 1:b
        P = A(j,:);
        for FL = 1:k
            range = FL - 1;
            G = [fliplr(P(1:range)) P(FL:k)];
            for f = 1:range
                G(f) = -G(f);
            end
            for i = 1:b
                if (A(i,:) == G)
                    adj(i,j) = 1;
                    break
                end
            end
        end
    end
else
    adj = zeros(b);
    for j = 1:b
        P = A(j,:);
        for FL = 1:k
            range = FL - 1;
            for t = 1:(k+1-FL)
                G = [P(1:t-1) fliplr(P(t:t+range)) P(t+FL:k)];
                for f = t:t+range
                    G(f) = -G(f);
                end
                for i = 1:b
                    if (A(i,:) == G)
                        adj(i,j) = 1;
                        break
                    end
                end
            end
        end
    end
end