Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Traveling Salesman Problem algorithm and tests #717

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions dynamic/travelingsalesman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 MAX = math.MaxInt32 / 2

func TravelingSalesman(cost [][]int) int {
n := len(cost)
allVisited := (1 << n) - 1
dp := make([][]int, 1<<n)
for i := range dp {
dp[i] = make([]int, n)
for j := range dp[i] {
dp[i][j] = MAX
}
}
dp[1][0] = 0

for mask := 1; mask < (1 << n); mask += 2 {
for i := 0; i < n; i++ {
if (mask & (1 << i)) != 0 {
for j := 0; j < n; j++ {
if (mask & (1 << j)) == 0 {
newMask := mask | (1 << j)
if dp[mask][i]+cost[i][j] < dp[newMask][j] {
dp[newMask][j] = dp[mask][i] + cost[i][j]
}
}
}
}
}
}

res := MAX
for i := 1; i < n; i++ {
if dp[allVisited][i]+cost[i][0] < res {
res = dp[allVisited][i] + cost[i][0]
}
}

return res
}
harshithsaiv marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 46 additions & 0 deletions dynamic/travelingsalesman_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dynamic_test

import (
"fmt"
"testing"

"github.com/TheAlgorithms/Go/dynamic"
)

func TestTravelingSalesman(t *testing.T) {
costTables := [][][]int{
{
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0},
},
{
{0, 29, 20, 21},
{29, 0, 15, 17},
{20, 15, 0, 28},
{21, 17, 28, 0},
},
{
{0, 12, 10},
{12, 0, 15},
{10, 15, 0},
},
}

expectedResults := []int{
80,
73,
37,
}

for i, costTable := range costTables {
expected := expectedResults[i]
t.Run(fmt.Sprintf("TSP Case %d", i+1), func(t *testing.T) {
result := dynamic.TravelingSalesman(costTable)
if result != expected {
t.Errorf("Expected %d, but got %d", expected, result)
}
})
}
}