function [ tsp_dist, tsp_trip ] = tsp_2opt ( distance, tsp_trip, tries )

%*****************************************************************************80
%
  [ n, ~ ] = size ( distance );

  tsp_dist = trip_distance ( distance, tsp_trip );

  for try_num = 1 : tries
%
%  Randomly select links i and j=i+1.
%
    i = randi ( [ 1, n ] );
    if ( i < n )
      j = i + 1;
    else
      j = 1;
    end
%
%  Randomly select links k and l=k+1.
%
    k = randi ( [ 1, n - 1 ] );
    k = i + k;
    if ( n < k )
      k = k - n;
    end

    if ( k < n )
      l = k + 1;
    else
      l = 1;
    end

    ti = tsp_trip(i);
    tj = tsp_trip(j);
    tk = tsp_trip(k);
    tl = tsp_trip(l);

    trip2 = tsp_trip;
    trip2(j) = tsp_trip(k);
    trip2(k) = tsp_trip(j);

    d2 = trip_distance ( distance, trip2 );

    if ( d2 < tsp_dist )
      tsp_trip = trip2;
      tsp_dist = d2;
      fprintf ( '%d: Decrease trip length to %g\n', try_num, tsp_dist );
    end

  end

  return
end
