wolffd@0: function rte = pred2path(P,s,t) wolffd@0: %PRED2PATH Convert predecessor indices to shortest paths from node 's' to 't'. wolffd@0: % rte = pred2path(P,s,t) wolffd@0: % P = |s| x n matrix of predecessor indices (from DIJK) wolffd@0: % s = FROM node indices wolffd@0: % = [] (default), paths from all nodes wolffd@0: % t = TO node indices wolffd@0: % = [] (default), paths to all nodes wolffd@0: % rte = |s| x |t| cell array of paths (or routes) from 's' to 't', where wolffd@0: % rte{i,j} = path from s(i) to t(j) wolffd@0: % = [], if no path exists from s(i) to t(j) wolffd@0: % wolffd@0: % (Used with output of DIJK) wolffd@0: wolffd@0: % Copyright (c) 1998-2001 by Michael G. Kay wolffd@0: % Matlog Version 5 22-Aug-2001 wolffd@0: wolffd@0: % Input Error Checking ****************************************************** wolffd@0: error(nargchk(1,3,nargin)); wolffd@0: wolffd@0: [rP,n] = size(P); wolffd@0: wolffd@0: if nargin < 2 | isempty(s), s = (1:n)'; else s = s(:); end wolffd@0: if nargin < 3 | isempty(t), t = (1:n)'; else t = t(:); end wolffd@0: wolffd@0: if any(P < 0 | P > n) wolffd@0: error(['Elements of P must be integers between 1 and ',num2str(n)]); wolffd@0: elseif any(s < 1 | s > n) wolffd@0: error(['''s'' must be an integer between 1 and ',num2str(n)]); wolffd@0: elseif any(t < 1 | t > n) wolffd@0: error(['''t'' must be an integer between 1 and ',num2str(n)]); wolffd@0: end wolffd@0: % End (Input Error Checking) ************************************************ wolffd@0: wolffd@0: rte = cell(length(s),length(t)); wolffd@0: wolffd@0: for i = 1:length(s) wolffd@0: if rP == 1 wolffd@0: si = 1; wolffd@0: else wolffd@0: si = s(i); wolffd@0: if si < 1 | si > rP wolffd@0: error('Invalid P matrix.') wolffd@0: end wolffd@0: end wolffd@0: for j = 1:length(t) wolffd@0: tj = t(j); wolffd@0: if tj == s(i) wolffd@0: r = tj; wolffd@0: elseif P(si,tj) == 0 wolffd@0: r = []; wolffd@0: else wolffd@0: r = tj; wolffd@0: while tj ~= s(i) wolffd@0: if tj < 1 | tj > n wolffd@0: error('Invalid element of P matrix found.') wolffd@0: end wolffd@0: r = [P(si,tj) r]; wolffd@0: tj = P(si,tj); wolffd@0: end wolffd@0: end wolffd@0: rte{i,j} = r; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: if length(s) == 1 & length(t) == 1 wolffd@0: rte = rte{:}; wolffd@0: end wolffd@0: wolffd@0: %rte = t; wolffd@0: while 0%t ~= s wolffd@0: if t < 1 | t > n | round(t) ~= t wolffd@0: error('Invalid ''pred'' element found prior to reaching ''s'''); wolffd@0: end wolffd@0: rte = [P(t) rte]; wolffd@0: t = P(t); wolffd@0: end wolffd@0: