Discrete structures-assignment-2352821-DặngDuyNguyên
Discrete structures-assignment-2352821-DặngDuyNguyên
ASSIGNMENT
Name ID Email
Đặng Duy Nguyên 2352821 [email protected]
Travelling Salesman Problem (TSP)
The Travelling Salesman Problem (TSP) involves finding the shortest pos-
sible route that visits a given set of cities exactly once and returns to the
starting city. It is a classic problem in the field of optimization and is known
to be NP-hard. This means that there is no known polynomial-time solution
for the problem, and it is computationally expensive to solve as the number
of cities increases.
3
Code Breakdown
1.Initialization:
dp[1 « n][n] is a 2D vector that stores the minimum cost for all possible
subsets of cities. The bitmask mask is used to represent each subset of cities.
parent[1 « n][n] is used for backtracking the path. It stores the previous city
visited for each combination of cities.
2.Dynamic Programming Loop:
We iterate over all possible subsets of cities (using mask), and for each
city u in the subset, we try to extend the path to every other city v. If city v
hasn’t been visited (i.e., the bitmask for v is not set), we calculate the new
cost of visiting v and update the DP table and parent table accordingly.
3. Finding the Minimum Cost Tour:
After processing all subsets, we check all possible cities to find the one with
the minimum cost that can complete the cycle by returning to the starting
city.
4. Backtracking:
Once the minimum cost is found, we backtrack using the parent table to
reconstruct the optimal path.
5. Result:
The path is reconstructed and returned as a string, showing the sequence
of cities in the optimal tour.
4
c o n s t i n t LIMIT = 9 9 9 9 9 9 9 9 ; // A l a r g e v a l u e t o r e p r e s e n t
infinity
s t r i n g T r a v e l i n g ( i n t graph [ 3 0 ] [ 3 0 ] , i n t n , c h a r s t a r t V e r t e x ) {
i n t s t a r t I d x = s t a r t V e r t e x − ’A ’ ; // Convert s t a r t v e r t e x t o
index
v e c t o r <v e c t o r <i n t >> dp ( 1 << n , v e c t o r <i n t >(n , LIMIT ) ) ; // DP
t a b l e f o r s t o r i n g th e minimum c o s t
v e c t o r <v e c t o r <i n t >> p a r e n t ( 1 << n , v e c t o r <i n t >(n , −1) ) ; //
Table f o r path r e c o n s t r u c t i o n
dp [ 1 << s t a r t I d x ] [ s t a r t I d x ] = 0 ; // S t a r t i n g v e r t e x has a
cost of 0
// I t e r a t e through a l l p o s s i b l e v i s i t e d c i t i e s c o m b i n a t i o n s (
bitmask )
f o r ( i n t mask = 0 ; mask < ( 1 << n ) ; ++mask ) {
f o r ( i n t u = 0 ; u < n ; ++u ) {
i f ( ! ( mask & ( 1 << u ) ) | | dp [ mask ] [ u ] == LIMIT )
c o n t i n u e ; // I f u i s not v i s i t e d , s k i p i t
i n t f i n a l M a s k = ( 1 << n ) − 1 ; // A l l c i t i e s v i s i t e d
i n t minCost = LIMIT ;
i n t l a s t V e r t e x = − 1;
5
i f ( graph [ v ] [ s t a r t I d x ] > 0 && dp [ f i n a l M a s k ] [ v ] != LIMIT ) {
i n t c u r r C o s t = dp [ f i n a l M a s k ] [ v ] + graph [ v ] [ s t a r t I d x ] ;
// Add th e c o s t t o r e t u r n t o t h e s t a r t c i t y
i f ( c u r r C o s t < minCost ) {
minCost = c u r r C o s t ;
lastVertex = v ;
}
}
}
// I f no v a l i d c y c l e i s found
i f ( minCost == LIMIT ) {
r e t u r n "No␣ Hamiltonian ␣ c y c l e ␣ found ! " ;
}
// R e c o n s t r u c t th e path
v e c t o r <i n t > path ;
i n t mask = f i n a l M a s k ;
w h i l e ( l a s t V e r t e x != −1) {
path . push_back ( l a s t V e r t e x ) ; // Add t h e c u r r e n t c i t y t o
t he path
i n t temp = l a s t V e r t e x ;
l a s t V e r t e x = p a r e n t [ mask ] [ l a s t V e r t e x ] ; // Get t h e
previous city
mask ^= ( 1 << temp ) ; // Remove c u r r e n t c i t y from mask
}
// Convert th e path t o a s t r i n g
s t r i n g r e s u l t = "" ;
f o r ( i n t i = 0 ; i < path . s i z e ( ) ; ++i ) {
i f ( ! r e s u l t . empty ( ) ) r e s u l t += "␣" ; // Add s p a c e between
cities
r e s u l t += ( ch ar ) ( ’A ’ + path [ i ] ) ; // Convert i n d e x back t o
character
}
r e s u l t += "␣" + s t r i n g ( 1 , s t a r t V e r t e x ) ; // Add t h e s t a r t i n g
c i t y a t t he end o f t h e path
return r e s u l t ;
}
Final result:
The minimum cost of the tour and the sequence of cities visited in the optimal
path are returned as a string.
6
For example, the result might look like:
Shortest tour: A B D C A
This indicates that the shortest path starts at city A, goes to B, then D,
then C, and returns to A, forming a cycle with the minimum cost.
Conclusion
This dynamic programming approach with bitmasking provides a more effi-
cient solution to the TSP compared to brute-force methods. It reduces the
complexity from factorial time to approximately O(n2 x 2n ), which is more
manageable for moderate-sized problem instances.
This method efficiently solves the TSP by leveraging the power of dynamic
programming to store intermediate results and avoid redundant calculations,
and bitmasking helps represent subsets of cities compactly.