Final Report - Solving Traveling Salesman Problem by Dynamic Programming Approach in Java Program Aditya Nugroho Ht083276e
Final Report - Solving Traveling Salesman Problem by Dynamic Programming Approach in Java Program Aditya Nugroho Ht083276e
ADITYA NUGROHO
HT083276E
1.0 INTRODUCTION
Traveling Salesman Problem (TSP) is one of the important method in the application on
transportation science. TSP can be illustrated as a salesman who must travel through all the
cities designated by the shortest distances, where each city may only be traversed once.
Solution of the TSP is the path traversed by these salesmen. Surely the best or optimal
solution of this problem is the path with the shortest distance or can be called with a minimum
of travel routes.
A very simple depiction of the term TSP is, a salesman who must travel n cities with the
rules: he must visit each city only once. He has to minimize the total travel distance and in the
end he had to return to his origin city. Thus, what had he done called a tour. In order to ease
the problem, mapping n the city will be illustrated with a graph, where the number of vertices
and edges are limited (a vertex will represent a city and an edge will represent the distance
between the two cities which are connected). Handling TSP problem is equivalent to find the
shortest Hamilton circuit.
Dynamic Programming is a method of solving problems by breaking the solution into a set of
steps or stages so that the solution of the problem can be viewed from a series of interrelated
decisions. The inventor and the person responsible for the popularity of dynamic
programming is Richard Bellman.
In dynamic programming, a series of optimal decisions are made by using the principle of
optimality. The principle of optimality: if the optimal total solution, then the solution to the k
th stage is also optimal. With the principle of optimality is guaranteed that at some stage of
decision making is the right decision for the later stages. The essence of dynamic
programming is to remove a small part of a problem at every step, and then solve the smaller
problems and use the results of the settlement to remedy the solution is added back to the
issue in the next step.
1.1 Objective
How does the dynamic programming approach in solving the TSP in determining the
most optimal path with minimum total distance or time by applied forward recursion.
Assessing the TSP as a graph Hamiltonian by using dynamic programming to obtain the
most minimum amount of weight and applying it into a java language program.
1.2 Method
In this paper TSP which would be covered only an asymmetric TSP, where the asymmetric
TSP is the distance from city A to city B is not equal to the distance from city B to city A
(asymmetry occurs because of a one-way)
2
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
Hamilton path is a path through every vertex in the graph exactly one times. If the path back
to the vertex of origin to form a closed path (circuit), then the closed trajectory is called the
Hamilton circuit. Therefore Hamilton circuit is a circuit through each vertex in the graph
exactly once. Graph that has Hamilton called the Hamilton circuit graph, whereas a trajectory
Hamilton called semi-Hamiltonian graph. Theorem: any complete graph is a Hamilton
graph
Adjacency matrix is defined as follows, suppose that order of nxn matrix (n rows and n
columns). If between two vertices connected (adjacent), then the matrix element is 1, and
if it is not connected is 0. Examples of adjacency matrix:
Incidency matrix is the matrix that represents relationship between the vertex and edge.
Suppose matrix with m rows for every vertex and n columns for each edge. If the vertex
connect with edge, then the matrix element is 1. Conversely, if the vertex is not
connected with edge then the matrix element is 0. Examples of incidence matrices are:
3
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
f Sn ,Dn = R + f
n n-1 n-1 n-1
*
S ,D (1)
where,
Now let consider application of DP in TSP. Let G (V , E ) is a complete directed graph with
its edge Cij 0 for every i and j, where i and j are the vertex inside of V. Suppose V n and
n 1 , therefore each node is numbered by 1, 2, ..., n.
f 1,v1 = min c1k f ( k ,V 1,k) (2)
2 k n
where,
f 1,v1 = length of optimal tour
V = non-empty finite set of vertices of a graph
v 1 = the set of vertices of a graph which vertices minus 1 (initial)
k = vertex that is connected to the vertex 1 (initial)
c
1k
= weight of vertex 1 (the beginning) to k
f (i , ) c , 2 i n (3)
ij
f (i, s ) min cij f ( j ,S j)
4
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
where,
f (i , s ) = weight of shortest path
S j = series of S minus of point vertex j
i and j = vertices in V
c
ij = weight of vertex i to j
Assume trip (tour) begins and ends at vertex 1. Each tour would consist of the side (1,k) for
some k V 1 and a path from vertex k to the vertex 1. Path from vertex k to vertex 1
through each vertex in V - {1, k} is only once. Let f (i, s ) is the weight of the shortest path that
starts from vertex i, which through all vertices in S and ends at vertex 1. The value of
f 1,v1 is the weight of the shortest tour.
From equation (2) can be obtained equation (3) if we know f (k, V - {1, k}) for all choices of k.
The value f can be obtained by using equation (3). We use equation (3) to obtain f (i, S) for
|S|=1, then we can obtain f (i, S) to, until |S| = n-2.
In dynamic programming, some variables and the steps necessary to determine the shortest
path with minimum weight using the forward recursive method.
Initialisation
Complete directed graph (G)
Non-empty finite set of vertices on a graph (V), V = {1, 2, 3, ... n}
The set of edges in a graph (E)
The distance from i to j (the distance between cities) cij , where cij c
ji
The series of lines (S), S 2, 3, ..., n
The weight of the shortest path that starts at vertex i that through all vertices in S and
ends at vertex 1 (f (i, S)), i S and S
Therefore the steps in solving the TSP with dynamic programming is as follows:
Step 1
Determining the basis of the graph Hamiltonian that has been represented to be a adjacency
matrix to the equation:
f (i , ) c , 2 i n
i,1
5
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
Step 2
Calculate f (i, S) for |S| = 1, then we can obtain f (i, S) for |S| = 2, until |S| = n-1. With the
equation:
f (i , s ) min cij f ( j ,S j)
jS
Step 3
Having obtained the results from step 2, then calculate the equation of a recursive relationship
by following equation:
f 1,v1 = min c1k f ( k ,V 1,k)
2 k n
Step 4
After calculating a recursive equation in step 3, will be obtained by weighting the shortest
path, then to obtain an optimal solution / length of the shortest path for a graph is to calculate f
(1, {2, 3, 4, ..., n}) which means long lines from the initial vertex (1) to vertex 1 after passing
through the vertices 2, 3, 4, .., n with any order (for the minimum) then locate the forming of
the minimum optimal solution which has been obtained.
Following are numerical examples of the solving TSP by using dynamic programming. Given
a complete directed graph, with the TSP problem for n = 4. The series of lines {2, 3, 4} can
be seen in Figure 4 as follow:
12
1 2
15
8 10
9 16 14 15
11 11
17
4 3
18
Here distance from i to j (the distance between cities) c , where c c have converted
ij ij ji
into matrix form:
0 12 11 16
15 0 15 10
c
8 14 0 18
9
11 17 0
6
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
Step 1
Calculate f (i , ) c , 2 i n
i,1
We get:
f (2, ) c , 15
21
f (3, ) c , 8
31
f (4, ) c , 9
41
Step 2
For | S | = 1, calculate f (i, s ) min cij f ( j ,S j)
jS
We get:
f (2, 3) c23 f (3,) 15 8 23
f (3, 2) c32 f (2,) 14 15 29
f (4, 2) c42 f (2,) 11 15 26
f (2, 4) c24 f (4,) 10 9 19
f (3, 4) c34 f (4,) 18 9 27
f (4, 3) c43 f (3,) 17 8 25
For | S | = 2, calculate f (i, s ) min cij f ( j ,S j)
jS
We get:
f (2, 3,4) min c23 f (3,4),c24 f (4,3)
= min {15+27,10+25}
= min {42,35} = 35
f (3, 2,4) min c32 f (2,4),c34 f (4,2)
= min {14+19,27+11}
= min {33,38} = 33
f (4, 2,3) min c42 f (2,4),c43 f (3,2)
= min {11+22,17+29}
= min {33,46} = 33
Step 3
By using the equation, f 1,v1 = min c1k f (k ,V 1,k)
2 k n
We get:
7
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
f (1, 2,3,4) min c12 f (2,3,4),c13 f (3,2,4),c14 f (4,2,3)
= min {12+35,11+33,16+33}
= min {47,44,49} = 44
Therefore, the weight of the shortest path that starts and ends at the vertex 1 is 44.
Step 4
To know the path, first note that the value of 44 is a minimum value obtained from 11 + 33.
Means the shortest path is c13 f (3, 2,4) where found that the initial part of the path is 1-3.
Then find out the component of f (3, 2,4) which resulting the minimum value is c32 f (2, 4) .
Therefore, it is known that the edge 3-2 is part of the shortest path. In the same way, then we
trace the f (2, 4) which is found that c24 f (4, ) . The final step is to trace forming of
f (4, ) whichfound is c41 (Thus edge of 4-1 is also part of the shortest path). By assembling
the edges that has been defined (edge 1-3, 3-2, 2-4, 4-1) from front to back. Means that the
shortest path is 1 → 3 → 2 → 4 → 1 with the number of minimum weight 44. Following
figure represent the results (bold edge is the shortest path):
12
1 2
15
8 10
9 16 14 15
11 11
17
4 3
18
Figure 5 Graph with 4 vertices (shortest path)
In general the process of solving TSP by dynamic programming which conducted in Java
programme is represented in the flowchart as follow:
8
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
Start
Draw node i to j
Validation No
of distance
matrix
Yes
Calculate
f (i , ) c , 2 i n
i,1
End
9
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
The design of DPTSP graphical user interface is done using NetBeans IDE 6.9.1. Figure 6 is
example the display of DPTSP applications in jar.file, where the user can choose any nodes
represented in the map of Singapore city. Applications consist of nodes, graph, matrices and
optimum number of city results including computational time (in milliseconds).
As can be seen from Fig 6 a distance or travel time matrix is the form used to enter the travel
time between cities into the matrix. Since the programme is developed by binary (using
bitmask) therefore the value of travel time should be clearly defined in integer numbers and
could not put comma or point in the matrix form. Matrix data thus inputted by the user.
The application of DPTSP was develop by using NetBeans IDE 6.9.1. however to achieve
maximum results of the programme there is need high capacity of hardware requirement. In
this term paper, the programme is develop by following hardware data:
Once again as programming of DPTSP algorithm is solved by using binary number 2^n-1
(which is one binary consumed 4 bytes memory) and bitmaskthe issue of memory capacity is
found as the rest of memory in my laptop is only 400MB out of 1.00 GB. Given example
logic of binary, says I would like to call other rest cities from cities number 10 and 19,
10
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
111111111 0 1 1 1 1 1 1 1 1 0
therefore the logic binary number as follow : in
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
each respective binary would store 4 bytes memory. Since in this programme the bitmask is
use to easily check the state of individual bits regardless of the other bits. Therefore the
requirement the memory needed of programme is easily compute as follow 2^n-
1*4^4=(134,217,728 bytes) or 134 MB of RAM. I use maximum 134 MB as my laptop
memory should be allocate to other devices. Therefore by using binary number the code of
java programming for DPTSP is given below.
import java.util.ArrayList;
/**
*
* @author Aditya Nugroho
*/
public class TSPEngine {
public TSPEngine() {
}
N = n;
npow = (int) Math.pow(2, n);
g = new int[n][npow];
p = new int[n][npow];
d = inputArray;
int i, j, k, l, m, s;
11
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
if (g[start][set] != -1) {
return g[start][set];
} else {
for (int x = 0; x < N; x++) {
mask = npow - 1 - (int) Math.pow(2, x);
masked = set & mask;
if (masked != set) {
temp = d[start][x] + tsp(x, masked);
if (result == -1 || result > temp) {
result = temp;
p[start][set] = x;
}
}
}
g[start][set] = result;
return result;
}
}
int x = p[start][set];
int mask = npow - 1 - (int) Math.pow(2, x);
int masked = set & mask;
outputArray.add(x);
getPath(x, masked);
}
12
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
Suppose there are only one Pizza Hut outlet in Bukit Panjang Singapore, employing about 20
staff. In the face of rapid business growth, Pizza Hut found that their need to optimize their
pizza hut delivery system in order to satisfy their customers, meanwhile minimizing or
reducing time and costs spent on day to day.
Suppose that the manager of the outlet need to seek minimizing time of travel for deliver the
Pizza to the customers. Thus the manager has developed the business information intelligence
system for Pizza Hut Delivery adopted from traffic information provided by Land Transport
Authority traffic.smart. onemotoring.com Given this such condition in assymetric network
problem, thus assume that Pizza Hut outlet has an order for deliver to 15 customers from
Bukit Panjang to other rest city in Singapore, and manager has to decide to send only one
riders, therefore the problem case can be given as follow:
Assume the travel time is fixed between cities Cijwithout considering traffic obstruction such
as accidents occured, traffic control delayed and so on. Therefore the ravel time is input into
matrix form as illustrated in Fig 7.
Figure 7 Travel time (minutes) matrix between cities or customers Cij for pizza hut delivery
In addition when filled up node in the map, the ordered number is number of customer who is
first called the pizza hut delivery center. Say for example, 2nd customer who located in
Woodlands, 3rd customer who located in Yishun, 4th customer who located in Bukit Gombak
and so on. Based on this requirements, the manager has to decide which customer should be
served in order to minimize or reducing the travel time for delivery of pizza while the riders
hould go back to the outlet. Given the initial results from the DP TSP algorithm as illusatrated
in Fig 8. therefore the manager can decide to served customer number 1-2-3-9-8-11-12-13-10-
7-15-14-6-5-4-1 with total travel time 413 minutes or 6.8 hours per day.
13
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
14
Department of Civil Engineering
CE 6001 Operations Management and Infrastructure System
REFERENCES
Dreyfus, Stuart E., Law, Averill M.. 1977. The Art and Theory of Dynamic Programming.
New Jersey San Fransisco London: Academic Press
Evans. J. R. dan Minieka. E. 1992. Optimization Algorithms for Network and Graphs. Marcel
Dekker, Inc
15