% try to grow a chain of matches starting with the best
clear imgseq;

ms = ssds;

imgseq(1,:) = ms(3,:);
ms(3,:) = [];

curleft = imgseq(1, 5);
curright = imgseq(1, 6);

% remove all other pairs that start with curleft or end with curright
ms(find(ms(:,5) == curleft),:) = [];
ms(find(ms(:,6) == curright),:) = [];

i = 1;

going = true;
while (going)
	% find possible new edges
    leftCandidates = ms(find(ms(:,6) == curleft), :);
	rightCandidates = ms(find(ms(:,5) == curright), :);
    
    if (size(leftCandidates))
        topLeft = leftCandidates(1,1);
    else
        topLeft = -1;
    end
    
    if (size(rightCandidates))
        topRight = rightCandidates(1,1);
    else
        topRight = -1;
    end
    
    if ((topLeft > topRight) && (topLeft ~= -1))
        % pick top left candidate
        imgseq = [leftCandidates(1,:); imgseq];
        ms(find(ms(:,1) == leftCandidates(1,1)), :) = [];
        curleft = leftCandidates(1,5);
        % now we need to clean out all other pairs that use this image!
        ms(find(ms(:,5) == curleft),:) = [];
    elseif (topRight ~= -1)
        % pick top right candidate
        imgseq = [imgseq; rightCandidates(1,:)];
        ms(find(ms(:,1) == rightCandidates(1,1)), :) = [];
        curright = rightCandidates(1,6);
        % now we need to clean out all other pairs that use this image!
        ms(find(ms(:,6) == curright),:) = [];
    else
        going = false;
    end
    
end
    
    
    
    
    
    
    
    
    
    