Skip to content

Commit

Permalink
Fist implementation for TSP using Dynamic Programming(Only returning …
Browse files Browse the repository at this point in the history
…the cost)
  • Loading branch information
Mardiniii committed Apr 6, 2018
1 parent aeefb83 commit 176adc1
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This repository contains some of my first steps working with Go and implementing
### Graph Algoristhms
- Depth first search
- Dijkstra's algorithm
- Travelling Salesman Problem: It is solved using a dynamic programming approach with O(n^2 * 2^n) time complexity. The code is capable to find the minimum cost but not the optimal path yet.

### Advanced Algoristhms
- Hanoi's towers problem
Expand Down
101 changes: 101 additions & 0 deletions graphs/travelling_salesman_problem/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"fmt"
"math"
)

// N is the number of cities
const N = 4

// AllVisited is the mask used to represent when
// all the cities have been visited
const AllVisited = (1 << N) - 1

// memoTable store the results already computed
var memoTable [16][N]int

func buildMatrix() [][]int {
var n int

fmt.Printf("Number of cities: ")
fmt.Scanf("%d", &n)

fmt.Println("Cities", n)

// allocate composed 2d array
matrix := make([][]int, n)
for i := range matrix {
matrix[i] = make([]int, n)
}

for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
fmt.Printf("Position[%d][%d]: ", i+1, i+1)
fmt.Scanf("%d", &matrix[i][j])
}
}

return matrix
}

func tsp(m [][]int, mask int, pos uint) int {
if mask == AllVisited {
return m[pos][0]
}

if memoTable[mask][pos] != -1 {
return memoTable[mask][pos]
}
var city uint
var newCost int
minCost := math.MaxInt64 // Greatest possible value

// Let's go to an unvisited city
for city = 0; city < N; city++ {
cityAsBinary := (1 << city)

if (mask & cityAsBinary) == 0 {
newCost = m[pos][city] + tsp(m, mask|cityAsBinary, city)
minCost = min(minCost, newCost)
memoTable[mask][pos] = minCost
}

}

return minCost
}

func min(a int, b int) int {
if a < b {
return a
}
return b
}

func main() {
// m := buildMatrix()
// m := [][]int{
// []int{0, 20, 42, 25},
// []int{20, 0, 30, 34},
// []int{42, 30, 0, 10},
// []int{25, 34, 10, 0},
// }

m := [][]int{
[]int{0, 10, 15, 20},
[]int{10, 0, 35, 25},
[]int{15, 35, 0, 30},
[]int{20, 25, 30, 0},
}

for i := 0; i < (1 << N); i++ {
for j := 0; j < N; j++ {
memoTable[i][j] = -1
}
}

cost := tsp(m, 1, 0)

fmt.Println("The minimum cost is:", cost)
}

0 comments on commit 176adc1

Please sign in to comment.