0% found this document useful (0 votes)
32 views6 pages

Discrete structures-assignment-2352821-DặngDuyNguyên

Uploaded by

nguyenduydang225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

Discrete structures-assignment-2352821-DặngDuyNguyên

Uploaded by

nguyenduydang225
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY

FACULTY OF COMPUTER SCIENCE AND ENGINEERING

ASSIGNMENT

Discrete Structures for Computing

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.

Why is TSP NP-Hard?


Exponential Growth: As the number of cities increases, the number of possi-
ble routes grows factorially. For n cities, there are (n-1)! / 2 possible routes.
This exponential growth makes it impractical to solve the problem using
brute force for large n. No Efficient Algorithm: Although there are approxi-
mation and heuristic methods, there is no known polynomial-time algorithm
to solve the TSP exactly for large instances. Therefore, TSP is classified as
NP-hard.

Dynamic Programming Approach with Bitmask-


ing
To solve TSP more efficiently, we can use dynamic programming (DP) com-
bined with bitmasking. This approach helps reduce the complexity by break-
ing the problem into subproblems and storing the results to avoid redundant
calculations.
Steps of the Dynamic Programming Approach:
1. Initialize the DP table: We use a DP table dp[mask][i], where mask rep-
resents a bitmask indicating which cities have been visited, and i represents
the last city visited. The value of dp[mask][i] stores the minimum cost to
visit all cities in the set represented by mask, ending at city i.
2. Iterate through all subsets: We iterate over all subsets of cities (repre-
sented by the bitmask) and try to find the minimum cost path by extending
the current path to the next city. We update the DP table with the best
possible cost for each subset.
Backtrack to reconstruct the optimal path: After filling the DP table, we
backtrack to reconstruct the optimal path that provides the minimum cost.
This is done by tracing the cities visited in the optimal order using the parent
table.

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

// Try t o extend t h e path by v i s i t i n g a l l o t h e r c i t i e s


v
f o r ( i n t v = 0 ; v < n ; ++v ) {
i f ( ( mask & ( 1 << v ) ) | | graph [ u ] [ v ] == 0 | | graph
[ u ] [ v ] == LIMIT ) c o n t i n u e ; // Skip i f v i s
v i s i t e d o r no edge e x i s t s

i n t nextMask = mask | ( 1 << v ) ; // Mark v as


visited
i n t newCost = dp [ mask ] [ u ] + graph [ u ] [ v ] ; //
C a l c u l a t e new c o s t

// I f t h e new c o s t i s b e t t e r , update DP and p a r e n t


tables
i f ( newCost < dp [ nextMask ] [ v ] ) {
dp [ nextMask ] [ v ] = newCost ;
p a r e n t [ nextMask ] [ v ] = u ;
}
}
}
}

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;

// Try t o f i n d th e minimum c o s t path t h a t ends a t t h e s t a r t i n g


city
f o r ( i n t v = 0 ; v < n ; ++v ) {

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
}

r e v e r s e ( path . b e g i n ( ) , path . end ( ) ) ; // R e v e r s e t h e path t o g e t


t he c o r r e c t o r d e r

// 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.

You might also like