function dists = spq_dijkstra(adjmat, src)
%SPQ_DIJKSTRA
%
% Input:
%
%   adjmat -- transpose of the adjacency matrix of the graph
%
%   src -- index of the source node to start search from
%
% Output:
%
%   dists -- a vector of the lengths the shortest paths from src to every other node
%

%% Validation
if(nargin < 2)
    error('Not enough arguments.');
end
if(~ismatrix(adjmat) || min(size(adjmat)) ~= max(size(adjmat)))
   error('Adjmat is either not a matrix or non-square.');
end
if(src < 1 || src > size(adjmat, 1))
    error('Invalid index of the source node.');
end
if(~issparse(adjmat))
   error('A sparse adjmat is expected.');
end
if(any(adjmat < 0))
   error('Adjmat contains a negative edge.');
end

%% MEX Check
if(exist('spq_dijkstra_mex', 'file') ~= 3)
    compiling = false;
    if(exist('spq_dijkstra_mex_make.m', 'file') == 2)
        fprintf(2, 'spq_dijkstra_mex.mex* file was not found. Trying to compile it.\n');
        compiling = true;
        spq_dijkstra_mex_make;
    end
    if(exist('spq_dijkstra_mex', 'file') ~= 3)
        error([
            'Accompanying spq_dijkstra_mex.mex* file is not found. ', ...
            'The attempt to compile it has probably failed. ', ...
            'Try to compile it manually.'
        ]);
    elseif(compiling)
        fprintf(2, 'spq_dijkstra_mex.mex* has been successfully compiled.\n');
    end
end

%% Computing shortest paths
dists = spq_dijkstra_mex(adjmat, src);

end
