Skip to content
Snippets Groups Projects
Commit 76e2d085 authored by mark's avatar mark Committed by Mark Winter
Browse files

Added matlab_bgl dependencies for mKymoUI code.

parent 604a8958
Branches
No related tags found
No related merge requests found
Showing
with 624 additions and 0 deletions
function check_matlab_bgl(A,options)
% CHECK_MATLAB_BGL Checks the input A for various properties
%
% check_matlab_bgl(A,options) throws an input error if...
% A is not square
% if options.values and A is not double valued
% if options.sym = 1 and A is not symmetric
% if options.flow_graph = 1 and A is not a flow graph data structure
% if options.nosparse = 1 do not check if A is sparse
% if options.nodefault = 1 then do not check default cases
% if options.nodiag = 1 throw an error if A has any non-zero diagonal values
% David Gleich
% Copyright, Stanford University, 2006-2008
%% History
% 2007-04-20: Added nodefault option
% 2007-07-22: Fixed empty array error for noneg check
% 2008-09-23: Added no diagonal check, misc formatting fixes
%%
if ~isfield(options, 'nodefault') || options.nodefault == 0
if size(A,1) ~= size(A,2)
error('matlab_bgl:invalidParameter', 'the matrix A must be square.');
end
end
if isfield(options, 'values') && options.values == 1
if ~isa(A,'double')
error('matlab_bgl:invalidParameter', 'the matrix A must have double values.');
end
end
if isfield(options, 'noneg') && options.noneg == 1
v=min(min(A));
if ~isempty(v) && v < 0
error('matlab_bgl:invalidParameter', 'the matrix A must have non-negative values.');
end
end
if isfield(options, 'sym') && options.sym == 1
if ~isequal(A,A')
error('matlab_bgl:invalidParameter', 'the matrix A must be symmetric.');
end
end
if isfield(options, 'nosparse') && options.nosparse == 1
else
if ~issparse(A)
error('matlab_bgl:invalidParameter', 'the matrix A must be sparse. (See set_matlab_bgl_default.)');
end
end
if isfield(options,'nodiag') && options.nodiag == 1
if any(diag(A))
error('matlab_bgl:invalidParameter',...
'the matrix A must not have any diagonal values')
end
end
function [trans check full2sparse] = get_matlab_bgl_options(varargin)
%
% Internal private function.
%
% Example:
% Don't use this function!
%
%% History
% 2008-09-26: Changed to use merge_options instead
%%
doptions = set_matlab_bgl_default();
if nargin>0
options = merge_options(doptions,varargin{:});
else
options = doptions;
end
trans = ~options.istrans;
check = ~options.nocheck;
full2sparse = options.full2sparse;
File added
File added
function options = merge_options(default_options,varargin)
% MERGE_OPTIONS Merge a set of default options with options from varargin
% The set of options in varargin can be a list of key,value pairs, or a
% struct with the same information.
% David F. Gleich
% Copyright, Stanford University, 2008
%% History
% 2008-09-25: Initial coding
%%
if ~isempty(varargin) && mod(length(varargin),2) == 0
options = merge_structs(struct(varargin{:}),default_options);
elseif length(varargin)==1 && isstruct(varargin{1})
options = merge_structs(varargin{1},default_options);
elseif ~isempty(varargin)
error('matlag_bgl:optionsParsing',...
'There were an odd number of key-value pairs of options specified');
else
options = default_options;
end
\ No newline at end of file
function S = merge_structs(A, B)
% MERGE_STRUCTS Merge two structures.
%
% S = merge_structs(A, B) makes the structure S have all the fields from A
% and B. Conflicts are resolved by using the value in A.
%
%
% merge_structs.m
% David Gleich
%
% Revision 1.00
% 19 Octoboer 2005
%
S = A;
fn = fieldnames(B);
for ii = 1:length(fn)
if (~isfield(A, fn{ii}))
S.(fn{ii}) = B.(fn{ii});
end;
end;
function old_default = set_matlab_bgl_default(varargin)
% SET_MATLAB_BGL_DEFAULT Sets a default option for the Matlab BGL interface
%
% old_default = set_matlab_bgl_default(options) or
% old_default = set_matlab_bgl_default(...) for key-value pair version
% options.istrans: the input matrices are already transposed [{0} | 1]
% options.nocheck: skip the input checking [{0} | 1]
% options.full2sparse: convert full matrices to sparse [{0} | 1]
%
% to get the current set of default options, call
% options = set_matlab_bgl_default()
%
% These options can make the Matlab BGL interface more efficient by
% eliminating the copying operations that occur between Matlab's structures
% and the BGL structures. However, they are more difficult to use and are
% disabled by default.
%
% Generally, they are best used when you want to perform a large series of
% computations.
%
% Example:
% % tranpose the matrix initially...
% At = A'
% old_options = set_matlab_bgl_default(struct('istrans',1));
% % perform a bunch of graph work with At...
% d1 = dfs(At,1); d2 = dfs(At,2); ...
% % restore the old options
% set_matlab_bgl_default(old_options);
% David Gleich
% Copyright, Stanford University, 2006-2008
%% History
%%
persistent default_options;
if ~isa(default_options,'struct')
% initial default options
default_options = struct('istrans', 0, 'nocheck', 0, 'full2sparse', 0);
end
if nargin == 0
old_default = default_options;
else
old_default = default_options;
default_options = merge_options(default_options,varargin{:});
end
function [d pred] = shortest_paths(A,u,varargin)
% SHORTEST_PATHS Compute the weighted single source shortest path problem.
%
% [d pred] = shortest_paths(A,u) returns the distance (d) and the predecessor
% (pred) for each of the vertices along the shortest path from u to every
% other vertex in the graph.
%
% ... = shortest_paths(A,u,...) takes a set of
% key-value pairs or an options structure. See set_matlab_bgl_options
% for the standard options.
% options.algname: the algorithm to use
% [{'auto'} | 'dijkstra' | 'bellman_ford' | 'dag']
% options.inf: the value to use for unreachable vertices
% [double > 0 | {Inf}]
% options.target: a special vertex that will stop the search when hit
% [{'none'} | any vertex number besides the u]; target is ignored if
% visitor is set.
% options.visitor: a structure with visitor callbacks. This option only
% applies to dijkstra or bellman_ford algorithms. See dijkstra_sp or
% bellman_ford_sp for details on the visitors.
% options.edge_weight: a double array over the edges with an edge
% weight for each edge, see EDGE_INDEX and EXAMPLES/REWEIGHTED_GRAPHS
% for information on how to use this option correctly
% [{'matrix'} | length(nnz(A)) double vector]
%
% Note: if you need to compute shortest paths with 0 weight edges, you must
% use an edge_weight vector, see the examples for details.
%
% Note: 'auto' cannot be used with 'nocheck' = 1. The 'auto' algorithm
% checks if the graph has negative edges and uses bellman_ford in that
% case, otherwise, it uses 'dijkstra'. In the future, it may check if the
% graph is a dag and use 'dag'.
%
% Example:
% load graphs/clr-25-2.mat
% shortest_paths(A,1)
% shortest_paths(A,1,struct('algname','bellman_ford'))
%
% See also DIJKSTRA_SP, BELLMAN_FORD_SP, DAG_SP
% David Gleich
% Copyright, Stanford University, 2006-2008
%% History
% 2006-04-19: Initial coding
% 2007-04-18: Added edge_weight option.
% 2007-04-19: Added target option.
% Added additional error checks.
% 2007-07-12: Fixed edge_weight documentation
%%
[trans check full2sparse] = get_matlab_bgl_options(varargin{:});
if full2sparse && ~issparse(A), A = sparse(A); end
options = struct('algname', 'auto', 'inf', Inf, 'edge_weight', 'matrix', ...
'target', 'none');
options = merge_options(options,varargin{:});
% edge_weights is an indicator that is 1 if we are using edge_weights
% passed on the command line or 0 if we are using the matrix.
edge_weights = 0;
edge_weight_opt = 'matrix';
if strcmp(options.edge_weight, 'matrix')
% do nothing if we are using the matrix weights
else
edge_weights = 1;
edge_weight_opt = options.edge_weight;
end
if strcmp(options.target,'none')
target = 0; % a flag used to denote "no target" to the mex
elseif isa(options.target, 'double')
target = options.target;
else
error('matlab_bgl:invalidParameter', ...
'options.target is not ''none'' or a vertex number.');
end
if check
% check the values of the matrix
check_matlab_bgl(A,struct('values',edge_weights ~= 1));
if edge_weights && nnz(A) ~= length(edge_weight_opt)
error('matlab_bgl:invalidParameter', 'the vector of edge weights must have length nnz(A)');
end
% set the algname
if (strcmpi(options.algname, 'auto'))
if edge_weights
mv = min(edge_weights);
else
mv = min(min(A));
end
if (mv < 0)
options.algname = 'bellman_ford';
else
options.algname = 'dijkstra';
end
else
% check the data provided to match the algorithm
if strcmpi(options.algname, 'dijkstra')
if edge_weights
mv = min(edge_weight_opt);
else
mv = min(min(A));
end
if mv < 0
error('matlab_bgl:invalidParameter', ...
'dijkstra''s algorithm cannot be used with negative edge weights.');
end
end
end
else
if (strcmpi(options.algname, 'auto'))
error('shortest_paths:invalidParameter', ...
'algname auto is not compatible with no check');
end
end
if options.inf < 0, error('options.inf must be larger than 0'); end
if trans, A = A'; end
if isfield(options,'visitor')
[d pred] = matlab_bgl_sp_mex(A,u,target,lower(options.algname),options.inf,...
edge_weight_opt, options.visitor);
else
[d pred] = matlab_bgl_sp_mex(A,u,target,lower(options.algname),options.inf,...
edge_weight_opt);
end
function check_matlab_bgl(A,options)
% CHECK_MATLAB_BGL Checks the input A for various properties
%
% check_matlab_bgl(A,options) throws an input error if...
% A is not square
% if options.values and A is not double valued
% if options.sym = 1 and A is not symmetric
% if options.flow_graph = 1 and A is not a flow graph data structure
% if options.nosparse = 1 do not check if A is sparse
% if options.nodefault = 1 then do not check default cases
% if options.nodiag = 1 throw an error if A has any non-zero diagonal values
% David Gleich
% Copyright, Stanford University, 2006-2008
%% History
% 2007-04-20: Added nodefault option
% 2007-07-22: Fixed empty array error for noneg check
% 2008-09-23: Added no diagonal check, misc formatting fixes
%%
if ~isfield(options, 'nodefault') || options.nodefault == 0
if size(A,1) ~= size(A,2)
error('matlab_bgl:invalidParameter', 'the matrix A must be square.');
end
end
if isfield(options, 'values') && options.values == 1
if ~isa(A,'double')
error('matlab_bgl:invalidParameter', 'the matrix A must have double values.');
end
end
if isfield(options, 'noneg') && options.noneg == 1
v=min(min(A));
if ~isempty(v) && v < 0
error('matlab_bgl:invalidParameter', 'the matrix A must have non-negative values.');
end
end
if isfield(options, 'sym') && options.sym == 1
if ~isequal(A,A')
error('matlab_bgl:invalidParameter', 'the matrix A must be symmetric.');
end
end
if isfield(options, 'nosparse') && options.nosparse == 1
else
if ~issparse(A)
error('matlab_bgl:invalidParameter', 'the matrix A must be sparse. (See set_matlab_bgl_default.)');
end
end
if isfield(options,'nodiag') && options.nodiag == 1
if any(diag(A))
error('matlab_bgl:invalidParameter',...
'the matrix A must not have any diagonal values')
end
end
function [trans check full2sparse] = get_matlab_bgl_options(varargin)
%
% Internal private function.
%
% Example:
% Don't use this function!
%
%% History
% 2008-09-26: Changed to use merge_options instead
%%
doptions = set_matlab_bgl_default();
if nargin>0
options = merge_options(doptions,varargin{:});
else
options = doptions;
end
trans = ~options.istrans;
check = ~options.nocheck;
full2sparse = options.full2sparse;
File added
File added
function options = merge_options(default_options,varargin)
% MERGE_OPTIONS Merge a set of default options with options from varargin
% The set of options in varargin can be a list of key,value pairs, or a
% struct with the same information.
% David F. Gleich
% Copyright, Stanford University, 2008
%% History
% 2008-09-25: Initial coding
%%
if ~isempty(varargin) && mod(length(varargin),2) == 0
options = merge_structs(struct(varargin{:}),default_options);
elseif length(varargin)==1 && isstruct(varargin{1})
options = merge_structs(varargin{1},default_options);
elseif ~isempty(varargin)
error('matlag_bgl:optionsParsing',...
'There were an odd number of key-value pairs of options specified');
else
options = default_options;
end
\ No newline at end of file
function S = merge_structs(A, B)
% MERGE_STRUCTS Merge two structures.
%
% S = merge_structs(A, B) makes the structure S have all the fields from A
% and B. Conflicts are resolved by using the value in A.
%
%
% merge_structs.m
% David Gleich
%
% Revision 1.00
% 19 Octoboer 2005
%
S = A;
fn = fieldnames(B);
for ii = 1:length(fn)
if (~isfield(A, fn{ii}))
S.(fn{ii}) = B.(fn{ii});
end;
end;
function old_default = set_matlab_bgl_default(varargin)
% SET_MATLAB_BGL_DEFAULT Sets a default option for the Matlab BGL interface
%
% old_default = set_matlab_bgl_default(options) or
% old_default = set_matlab_bgl_default(...) for key-value pair version
% options.istrans: the input matrices are already transposed [{0} | 1]
% options.nocheck: skip the input checking [{0} | 1]
% options.full2sparse: convert full matrices to sparse [{0} | 1]
%
% to get the current set of default options, call
% options = set_matlab_bgl_default()
%
% These options can make the Matlab BGL interface more efficient by
% eliminating the copying operations that occur between Matlab's structures
% and the BGL structures. However, they are more difficult to use and are
% disabled by default.
%
% Generally, they are best used when you want to perform a large series of
% computations.
%
% Example:
% % tranpose the matrix initially...
% At = A'
% old_options = set_matlab_bgl_default(struct('istrans',1));
% % perform a bunch of graph work with At...
% d1 = dfs(At,1); d2 = dfs(At,2); ...
% % restore the old options
% set_matlab_bgl_default(old_options);
% David Gleich
% Copyright, Stanford University, 2006-2008
%% History
%%
persistent default_options;
if ~isa(default_options,'struct')
% initial default options
default_options = struct('istrans', 0, 'nocheck', 0, 'full2sparse', 0);
end
if nargin == 0
old_default = default_options;
else
old_default = default_options;
default_options = merge_options(default_options,varargin{:});
end
function [d pred] = shortest_paths(A,u,varargin)
% SHORTEST_PATHS Compute the weighted single source shortest path problem.
%
% [d pred] = shortest_paths(A,u) returns the distance (d) and the predecessor
% (pred) for each of the vertices along the shortest path from u to every
% other vertex in the graph.
%
% ... = shortest_paths(A,u,...) takes a set of
% key-value pairs or an options structure. See set_matlab_bgl_options
% for the standard options.
% options.algname: the algorithm to use
% [{'auto'} | 'dijkstra' | 'bellman_ford' | 'dag']
% options.inf: the value to use for unreachable vertices
% [double > 0 | {Inf}]
% options.target: a special vertex that will stop the search when hit
% [{'none'} | any vertex number besides the u]; target is ignored if
% visitor is set.
% options.visitor: a structure with visitor callbacks. This option only
% applies to dijkstra or bellman_ford algorithms. See dijkstra_sp or
% bellman_ford_sp for details on the visitors.
% options.edge_weight: a double array over the edges with an edge
% weight for each edge, see EDGE_INDEX and EXAMPLES/REWEIGHTED_GRAPHS
% for information on how to use this option correctly
% [{'matrix'} | length(nnz(A)) double vector]
%
% Note: if you need to compute shortest paths with 0 weight edges, you must
% use an edge_weight vector, see the examples for details.
%
% Note: 'auto' cannot be used with 'nocheck' = 1. The 'auto' algorithm
% checks if the graph has negative edges and uses bellman_ford in that
% case, otherwise, it uses 'dijkstra'. In the future, it may check if the
% graph is a dag and use 'dag'.
%
% Example:
% load graphs/clr-25-2.mat
% shortest_paths(A,1)
% shortest_paths(A,1,struct('algname','bellman_ford'))
%
% See also DIJKSTRA_SP, BELLMAN_FORD_SP, DAG_SP
% David Gleich
% Copyright, Stanford University, 2006-2008
%% History
% 2006-04-19: Initial coding
% 2007-04-18: Added edge_weight option.
% 2007-04-19: Added target option.
% Added additional error checks.
% 2007-07-12: Fixed edge_weight documentation
%%
[trans check full2sparse] = get_matlab_bgl_options(varargin{:});
if full2sparse && ~issparse(A), A = sparse(A); end
options = struct('algname', 'auto', 'inf', Inf, 'edge_weight', 'matrix', ...
'target', 'none');
options = merge_options(options,varargin{:});
% edge_weights is an indicator that is 1 if we are using edge_weights
% passed on the command line or 0 if we are using the matrix.
edge_weights = 0;
edge_weight_opt = 'matrix';
if strcmp(options.edge_weight, 'matrix')
% do nothing if we are using the matrix weights
else
edge_weights = 1;
edge_weight_opt = options.edge_weight;
end
if strcmp(options.target,'none')
target = 0; % a flag used to denote "no target" to the mex
elseif isa(options.target, 'double')
target = options.target;
else
error('matlab_bgl:invalidParameter', ...
'options.target is not ''none'' or a vertex number.');
end
if check
% check the values of the matrix
check_matlab_bgl(A,struct('values',edge_weights ~= 1));
if edge_weights && nnz(A) ~= length(edge_weight_opt)
error('matlab_bgl:invalidParameter', 'the vector of edge weights must have length nnz(A)');
end
% set the algname
if (strcmpi(options.algname, 'auto'))
if edge_weights
mv = min(edge_weights);
else
mv = min(min(A));
end
if (mv < 0)
options.algname = 'bellman_ford';
else
options.algname = 'dijkstra';
end
else
% check the data provided to match the algorithm
if strcmpi(options.algname, 'dijkstra')
if edge_weights
mv = min(edge_weight_opt);
else
mv = min(min(A));
end
if mv < 0
error('matlab_bgl:invalidParameter', ...
'dijkstra''s algorithm cannot be used with negative edge weights.');
end
end
end
else
if (strcmpi(options.algname, 'auto'))
error('shortest_paths:invalidParameter', ...
'algname auto is not compatible with no check');
end
end
if options.inf < 0, error('options.inf must be larger than 0'); end
if trans, A = A'; end
if isfield(options,'visitor')
[d pred] = matlab_bgl_sp_mex(A,u,target,lower(options.algname),options.inf,...
edge_weight_opt, options.visitor);
else
[d pred] = matlab_bgl_sp_mex(A,u,target,lower(options.algname),options.inf,...
edge_weight_opt);
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment