I need to implement TSP algorithm by brute force for learning purposes.
I've understood there's a set of cities, let's call it V
and it's possible to get a matrix representation for the costs for going from a v1
city to a v2
city. I'll assume there are not cycles, so it's no possible to going from v1
back to v1
Then, I should generate a matrix after these sum series:
However, I really can't see in a practice way how a matrix would be outputed from restrictions.
Let's say we got 3 cities:
Madrid, Berlin and Malmo
So the path costs are (and they're not forced to be the same for ways back):
From Madrid to:
Berlin: 12
Malmo: 20
From Berlin to
Madrid: 32
Malmo: 12
From Malmo to:
Madrid: 14
Berlin: 17
So my input is:
$$\begin{bmatrix} 0 & 12 & 20 \\ 32 & 0 & 12 \\ 14 & 17 & 0\end{bmatrix}$$
How should the matrix be outputed according to the sum series?
I assume the algorithm would need to generate a matrix from this exampple:
There are 3 cities x1
, x2
and x3
and got the costs matrix shown below:
$$\begin{bmatrix} 10 & 2 & 1 \\ 3 & 10 & 2 \\ 11 & 2 & 10\end{bmatrix}$$
Following, example shows the next matrix:
$$\begin{bmatrix} x_{11}+x_{12}+x_{13} & & \\ & x_{21}+x_{22}+x_{23} & \\ & & x_{31}+x_{32}+x_{33}\\x_{11} & x_{21} & x_{31} \\ x_{12} & x_{22} & x_{32} \\ x_{13}& x_{23} & x_{33} \end{bmatrix}$$
Which would be the same as:
$$\begin{bmatrix} 1&1&1&0&0&0&0&0&0\\ 0&0&0&1&1&1&0&0&0\\ 0&0&0&0&0&0&1&1&1\\ 1&0&0&1&0&0&0&1&0\\ 0&0&1&0&0&1&0&0&1\\ 0&1&0&0&1&0&0&1&0\\ \end{bmatrix}$$
Then, considering the power ser of the three cities, matrix got the following rows added:
$$\begin{bmatrix} 0&1&1 & 0&0&0 & 0&0&0 \\ 0&0&0 & 1&0&1 & 0&0&0 \\ 0&0&0 & 0&0&0 & 1&1&0 \\ 0&0&1 & 0&0&1 & 0&0&0 \\ 0&1&0 & 0&0&0 & 0&1&0 \\ 0&0&0 & 1&0&0 & 1&0&0 \end{bmatrix}$$
Finally, the whole generated matrix is:
$$\begin{bmatrix} 1&1&1&0&0&0&0&0&0\\ 0&0&0&1&1&1&0&0&0\\ 0&0&0&0&0&0&1&1&1\\ 1&0&0&1&0&0&0&1&0\\ 0&0&1&0&0&1&0&0&1\\ 0&1&0&0&1&0&0&1&0\\ 0&1&1 & 0&0&0 & 0&0&0 \\ 0&0&0 & 1&0&1 & 0&0&0 \\ 0&0&0 & 0&0&0 & 1&1&0 \\ 0&0&1 & 0&0&1 & 0&0&0 \\ 0&1&0 & 0&0&0 & 0&1&0 \\ 0&0&0 & 1&0&0 & 1&0&0 \end{bmatrix}$$