0001 function gi = slgraphinfo(G, conds)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 if nargin < 2 || isempty(conds)
0054 cc = {};
0055 fc = [];
0056 else
0057 nconds = length(conds);
0058 tf = false(1, nconds);
0059 pf = 0;
0060 for i = 1 : nconds
0061 curcond = conds{i};
0062 if ischar(curcond)
0063 switch curcond
0064 case {'adjmat', 'edgeset', 'adjlist'}
0065 tf(i) = 1;
0066 pf = i;
0067 end
0068 end
0069 end
0070 if pf > 0
0071 fc = conds{pf};
0072 else
0073 fc = [];
0074 end
0075 cc = conds(~tf);
0076 end
0077
0078
0079
0080
0081 if isnumeric(G) || islogical(G)
0082
0083 if ~isempty(fc)
0084 chk_form('adjmat', fc);
0085 end
0086 gi = ginfo_adjmat(G);
0087
0088 elseif isstruct(G)
0089 if isfield(G, 'edges') && ~isfield(G, 'targets')
0090
0091 if ~isempty(fc)
0092 chk_form('edgeset', fc);
0093 end
0094 gi = ginfo_edgeset(G);
0095
0096 elseif isfield(G, 'targets') && ~isfield(G, 'edges')
0097
0098 if ~isempty(fc)
0099 chk_form('adjlist', fc);
0100 end
0101 gi = ginfo_adjlist(G);
0102
0103 else
0104 report_unreg();
0105 end
0106 else
0107 report_unreg();
0108 end
0109
0110
0111
0112
0113 if ~isempty(cc)
0114 ncc = numel(cc);
0115 for i = 1 : ncc
0116 chk_cond(G, gi, cc{i});
0117 end
0118 end
0119
0120
0121
0122
0123
0124 function gi = ginfo_edgeset(G)
0125
0126 gi.form = 'edgeset';
0127
0128 if ~isfield(G, 'n') || ~isfield(G, 'edges')
0129 chkerr('The edgeset representation of a graph should have the fields n and edges');
0130 end
0131
0132 ncols = size(G.edges, 2);
0133 if ~isempty(G.edges) && ncols ~= 2 && ncols ~= 3
0134 chkerr('The non-empty edges should have 2 or 3 columns');
0135 end
0136
0137 if isfield(G, 'nt')
0138 gi.type = 'bi';
0139 gi.n = G.n;
0140 gi.nt = G.nt;
0141
0142 else
0143 gi.type = 'ge';
0144 gi.n = G.n;
0145 gi.nt = G.n;
0146
0147 end
0148
0149 gi.valued = (ncols == 3);
0150
0151
0152 function gi = ginfo_adjlist(G)
0153
0154 gi.form = 'adjlist';
0155
0156 if ~isfield(G, 'n') || ~isfield(G, 'targets')
0157 chkerr('The adjlist representation of a graph should have the fields n and targets');
0158 end
0159
0160 tars = G.targets;
0161 if ~iscell(tars)
0162 chkerr('The targets should be a cell array');
0163 end
0164 if ~isequal(size(tars), [1, G.n]) && ~isequal(size(tars), [G.n, 1])
0165 chkerr('The targets should be a cell array of size 1 x n or n x 1');
0166 end
0167
0168 if isfield(G, 'nt')
0169 gi.type = 'bi';
0170 gi.n = G.n;
0171 gi.nt = G.nt;
0172
0173 else
0174 gi.type = 'ge';
0175 gi.n = G.n;
0176 gi.nt = G.n;
0177
0178 end
0179
0180 n = G.n;
0181 gi.valued = false;
0182 for i = 1 : n
0183 if ~isempty(tars{i})
0184 ncols = size(tars{i}, 2);
0185 gi.valued = (ncols > 1);
0186 break;
0187 end
0188 end
0189
0190
0191
0192 function gi = ginfo_adjmat(G)
0193
0194 gi.form = 'adjmat';
0195
0196 gi.n = size(G, 1);
0197 gi.nt = size(G, 2);
0198
0199 if gi.n == gi.nt
0200 gi.type = 'ge';
0201 else
0202 gi.type = 'bi';
0203 end
0204
0205 gi.valued = isnumeric(G);
0206
0207
0208
0209
0210
0211 function chk_cond(G, gi, cond)
0212
0213 if ischar(cond)
0214 switch cond
0215 case 'square'
0216 if gi.n ~= gi.nt
0217 conderr('The graph is square');
0218 end
0219 case 'logical'
0220 if isnumeric(G)
0221 conderr('The value type is logical');
0222 end
0223 case 'numeric'
0224 if islogical(G)
0225 conderr('The value type is numeric');
0226 end
0227 otherwise
0228 error('sltoolbox:invalidarg', ...
0229 'Unknown condition: %s', cond);
0230 end
0231 else
0232 if isscalar(cond)
0233 if gi.n ~= cond
0234 conderr(sprintf('n = %d', cond));
0235 end
0236 elseif isvector(cond) && length(cond) == 2
0237 if gi.n ~= cond(1) || gi.nt ~= cond(2)
0238 conderr(sprintf('n = %d, nt = %d', ...
0239 cond(1), cond(2)));
0240 end
0241 else
0242 error('sltoolbox:invalidarg', ...
0243 'Invalid condition is specified');
0244 end
0245 end
0246
0247
0248
0249
0250 function chk_form(df, cf)
0251
0252 if ~strcmp(df, cf)
0253 error('sltoolbox:invalidarg', ...
0254 'The given graph is like the form %s, which is not the required form %s', ...
0255 df, cf);
0256 end
0257
0258
0259 function report_unreg()
0260
0261 error('sltoolbox:invalidarg', ...
0262 'The graph form is unrecognizable');
0263
0264 function chkerr(msg)
0265
0266 error('sltoolbox:invalidarg', ...
0267 'Invalid graph representation: %s', msg);
0268
0269 function conderr(msg)
0270
0271 error('sltoolbox:invalidarg', ...
0272 'The given graph does not meet the requirement: %s', msg);
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282