diff --git a/dynamic/travelingsalesman.go b/dynamic/travelingsalesman.go new file mode 100644 index 000000000..5b8dd43aa --- /dev/null +++ b/dynamic/travelingsalesman.go @@ -0,0 +1,69 @@ +// filename: travelingsalesman.go +// description: The TravelingSalesman function calculates the minimum cost to complete a round-trip through all cities, starting and ending at the first city, using dynamic programming with bitmasking to track visited cities. +// details: +// The code defines a Go function tsp that solves the Traveling Salesman Problem using dynamic programming and bitmasking to track the subsets of visited cities. +// It initializes a DP table where dp[mask][i] holds the minimum cost to visit cities represented by mask and end at city i. +// The function iterates through all possible city subsets to update the DP table. +// Finally determining the least costly route to visit all cities and return to the starting point. +// author [Harshith Sai (SAI)](https://github.com/harshithsaiv) + +package dynamic + +import "math" + +const maxCost = math.MaxInt32 / 2 + +// initializeCostTable creates and initializes the DP table with maximum costs. +func initializeCostTable(numCities int) [][]int { + costTable := make([][]int, 1<