% merge_equal( p1, p2) - check path_index equality for merging, for K == 1
%
% p1 and p2 are path_index vectors.
%
function r = merge_equal( p1, p2)

global DEBUG;
global MM_max;

% if( DEBUG) disp('merge_equal'), p1, p2, end;

r = 0;		% return value

if( any(p1==0) | any(p2==0))	% unreachable paths
  return;
end

n = length(p1);

match = 0;

% last index <= MM_max must be equal
%
for i=n:-1:1
  if( p1(i) ~= p2(i))
    return;
  end
  if( p1(i) <= MM_max)
    match = 1;
    break;
  end
end

% count matches and mismatches
%
mismatch = 0;

for j=1:i-1
  if( p1(j) == p2(j))
    match = match + 1;
  else
    mismatch = mismatch + 1;
  end
end

% if( DEBUG) disp('merge_equal'), match, mismatch, end;

% at least 2 matches and no more than 1 mismatch ==>> equal
%
if( (match >= 2) & (mismatch <= 1))
  r = 1;
end
