How can I implement this equation in Java?

OK, this is a more important question: How to calculate the optimal paths for a bitonic tour for a salesman?

First of all, for the bittonic tour of the traveling salesman problem, I have the following recurrence relation:

(a) When i = 1 and j = 2, l(i; j) = dist(pi; pj )
(b) When i < j - 1; l(i; j) = l(i; j - 1) + dist(pj-1; pj)
(c) When i = j - 1 and j > 2, min 1<=k<i (l(k; i) + dist(pk; pj ))

l- table of previous results. My question is part C: Assuming that l(k,i)u are dist(pk,pj)defined, how to implement part C in Java? My initial thought was that I iterate kfrom 1to iand keep a minimal result (l(k,i) + dist(pk,pj)), but I don’t think it is right.

eg:

for (int k = 1; k < i; ++k) {
  tmp = l(k,i) + dist(pk,pj);
  if (tmp < min) {
    min = tmp;
  }
}

// min is the result

This may seem like a silly question (and probably there is, I miss it a lot), but I hope someone can help.

+3
3

, dist(pk,pj)

dist_pk_pj = dist(pk,pj);

/* then do as you did before */
for (int k = 1; k < i; ++k) {
  tmp = l(k,i) + dist_pk_pj;
  if (tmp < min) {
    min = tmp;
  }
}

. l ( precompute l), , . , :)

, , Java . , Java-, :)

, - , l(k,i)? , l(i,k) = l(k,i) ( , , , ). - , , .

+2

, Java . .

+1
(a) When i = 1 and j = 2, l(i; j) = dist(pi; pj )

(b) When i < j - 1; l(i; j) = l(i; j - 1) + dist(pj-1; pj)

(c) When i = j - 1 and j > 2, min 1<=k<i (l(k; i) + dist(pk; pj ))
import system.math

unsigned long i;

unsigned long j;

if ((i==j-1)&& (j>2))

{

unsigned long k=1;

unsigned long result;

while (k<i)

{

  result = l(k; i) + dist(pk; pj )

  min = minus(min, result);

  k++;

}

return min;

}
0
source

Source: https://habr.com/ru/post/1708524/


All Articles