View source: R/TSP_5_B_and_B.R
TSP_B_and_B | R Documentation |
This function implements the Branch and Bound algorithm to solve the Traveling Salesman Problem (TSP). It explores all possible tours but efficiently prunes branches that cannot lead to a better solution than the best one found so far by using a lower bound to eliminate suboptimal paths.
The algorithm starts from a root node (an empty path) and recursively explores all possible tours by adding unvisited cities one by one, while applying the bound function to prune branches of the search tree. Once all cities are visited, the function checks if the cost of the tour is better than the current best tour.
TSP_B_and_B(data)
data |
A numeric matrix or data frame where each row represents a city and each column represents its coordinates (for a 2D TSP). |
A list containing the following elements:
A numeric vector representing the order of cities in the optimal tour. The class attribute is set to "TSP".
The total cost of the optimal tour.
The total number of recursive calls made during the algorithm execution.
# Generate a random set of cities
cities <- matrix(runif(10), ncol = 2)
# Solve TSP using Branch and Bound
result <- TSP_B_and_B(cities)
print(result$best_tour)
print(result$best_cost)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.