function graph = generate_graph(nLabels,options)
%
% GENERATE_GRAPH    Returns a graph (specified in options) which can be 
%                   used with the function GENERATE_PATHCOMPLETE_SDP.
%
%     graph = GENERATE_GRAPH(nLabels)
%       The input nLabels is the size of the set of matrices M.
%       By default, the generated graph is a de Bruijn graph of dimension 1.
%       This graph is path-complete.
%       The structure graph contains the following fields :
%           graph.nNodes    is the number of nodes in the graph
%           graph.edges     is a nedges x 3 matrix where the nth line
%                           contains [i,j,k] if the nth edge of the graph is
%                           from node i to node j with the label k. The
%                           edge [i,j,k] describes also the constraint
%                           gamma^2 Mk' Pj Mk - Pi <= 0 (SPD sense) for a
%                           given gamma (type
%                           HELP GENERATE_PATHCOMPLETE_SDP for more
%                           details.)
%
%     graph = GENERATE_GRAPH(nLabels,options)
%       Does the same as above but with specified parameters described in
%       fields of the structure options. See JSRSETTINGS and below for
%       available parameters and options.
%
%   The field opts.graphOptions (generated by jsrsettings) can be used to
%   tune the method :
%
%       -graphOptions.type      Specifies the type of the generated graph.
%                               The different types are specified below.
%
%           DeBruijn :  Generates a de Bruijn graph (by default of
%                       dimension 1). Type HELP DEBRUIJN for more information
%                       about the graph and parameters.
%
% REFERENCES
%    [1] AHMADI, JUNGERS, PARRILO and ROOZBEHANI,
%      "Joint spectral radius and path-complete graph Lyapunov functions"
%      Vol. 52, No1, pp. 687-717 in SIAM J. CONTROL OPTIM, 2014.
%
% See also : GENERATE_PATHCOMPLETE_SDP, DEBRUIJN.


% Note for future developers :
% Do not forget to update the help of jsr_pathcomplete when you add a new
% graph !
graph_found = 0;


%%%%%%%%%%%%%%%%
% CHECK INPUTS %
%%%%%%%%%%%%%%%%

if(nargin == 0)
    error('Input nLabels must be specified.');
end

if(nLabels < 1)
    error('nLabels must be positive.');
end

if( round(nLabels) ~= nLabels )
    error('nLabels must be an integer.');
end

if(nargin < 2 || not(isfield(options,'graphOptions')))
    warning('generate_graph : No type specified. de Bruijn graph of dimension 1 is used.');
    options.graphOptions.type = 'debruijn';
    options.debruijn.dimension = 1;
end

if(not(isfield(options.graphOptions,'type')))
    warning('generate_graph : No type specified. de Bruijn graph of dimension 1 is used.');
    options.graphOptions.type = 'debruijn';
    options.debruijn.dimension = 1;
end

options.graphOptions.type = lower(options.graphOptions.type);




%%%%%%%%%%%%
% DEBRUIJN %
%%%%%%%%%%%%
if strcmp(options.graphOptions.type,'debruijn') && graph_found == 0
    graph_found = 1;
    graph = debruijn(nLabels,options.debruijn.dimension);
end


% Error if there is no correct graph
if(graph_found == 0)
    error('Unknow type of graph. Check graphOptions.type field.')
end



end

