This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSPDynamicProgramming.scala
72 lines (62 loc) · 2.02 KB
/
TSPDynamicProgramming.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import scala.collection.mutable.ArrayBuffer
object TSPDynamicProgramming {
// Function to calculate the distance between two points
def distance(point1: (Int, Int), point2: (Int, Int)): Double = {
math.sqrt(math.pow(point1._1 - point2._1, 2) + math.pow(point1._2 - point2._2, 2))
}
// Function to solve TSP using Dynamic Programming
def solveTSP(points: Array[(Int, Int)]): (Double, List[Int]) = {
val n = points.length
val dp = Array.ofDim[Double](1 << n, n)
val path = Array.ofDim[Int](1 << n, n)
// Initialize dp table with infinity
for (i <- dp.indices; j <- 0 until n) {
dp(i)(j) = Double.PositiveInfinity
}
// Initialize base case
dp(1)(0) = 0
// Fill the dp table
for (mask <- 1 until (1 << n); last <- 0 until n) {
if ((mask & (1 << last)) != 0) {
for (current <- 0 until n) {
if ((mask & (1 << current)) != 0 && current != last) {
val newMask = mask ^ (1 << last)
val newCost = dp(newMask)(current) + distance(points(current), points(last))
if (newCost < dp(mask)(last)) {
dp(mask)(last) = newCost
path(mask)(last) = current
}
}
}
}
}
// Find the minimum cost tour
var minCost = Double.PositiveInfinity
var lastNode = -1
for (i <- 1 until n) {
val cost = dp((1 << n) - 1)(i) + distance(points(i), points(0))
if (cost < minCost) {
minCost = cost
lastNode = i
}
}
// Reconstruct the path
var mask = (1 << n) - 1
val tour = ArrayBuffer[Int]()
var current = lastNode
while (current != 0) {
tour.prepend(current)
val next = path(mask)(current)
mask ^= (1 << current)
current = next
}
tour.prepend(0)
(minCost, tour.toList)
}
def main(args: Array[String]): Unit = {
val points = Array((0, 0), (1, 2), (3, 1), (4, 3))
val (cost, tour) = solveTSP(points)
println(s"Minimum cost: $cost")
println(s"Tour: ${tour.mkString(" -> ")}")
}
}