function paths = vit_paths( vnum)
%
% get L-best trellis paths for generalized Viterbi algorithm
%
% R. Perry, June 2001
%
% Usage:
%
%   paths = vit_paths( vnum)
%
%     vnum - Viterbi instance number
%
% If vnum is not specified, it defaults to 1.
%
% vit_update() must be called at least once after vit_init()
% before invoking vit_paths().  The paths returned will
% extend up to the time of the last trellis update.
%
% Besides returning the paths array, vit_paths() also sets
% the global VW array to contain the L-best path costs.
%
% See also:
%
%   vit_init, vit_update

% global variables
%
global HUGE_VAL; % 1e99
global DEBUG;    % for debugging
global V;        % main state array
global Vl;       % array for L-best indices
global Vw;       % cost array
global Vparam;   % parameters
global Vprev;    % previous state indices
global Vlprev;   % previous state el indices
global VW;       % L-best path costs

% get parameters
%
L  = Vparam(1);
M  = Vparam(2);
N  = Vparam(3);
Nv = Vparam(4);

% vnum set to 1 if not specified
%
if( nargin < 1)
  vnum = 1;
end

% check vnum
%
if( vnum < 1 | vnum > Nv)
  error('vnum out of bounds');
end

% get/check time index
%
T  = Vparam( 4 + vnum);

if( T < 1)
  error( 'trellis has not yet advanced to t>1');
end

% get the L-best paths
%
paths = zeros( L, T);

w = Vw(:,:,vnum);
[s1 s2] = sort(w(:));
VW = s1(1:L);
s2 = s2(1:L);

smin  =  Vprev(s2);  % L-best state indices
elmin = Vlprev(s2);  %  and el-indices

paths(:,T) = smin;

for t = T:-1:2 % backtrack through the trellis

  new_smin  = diag(  V( elmin, smin, t, vnum));
  new_elmin = diag( Vl( elmin, smin, t, vnum));

  smin = new_smin;
  elmin = new_elmin;

  paths(:,t-1) = smin;

end % t

paths = paths';
