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