Skip to content

Commit 7664c01

Browse files
committed
TSP: Use last() and first() instead of indexed access
Since the code is more expressive now, some comments have become obsolete.
1 parent f418dba commit 7664c01

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/main/kotlin/de/ronny_h/aoc/extensions/graphs/TravelingSalesman.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ const val MAX_WEIGHT = 10_000_000
1616
class TravelingSalesman(private val adj: List<List<Int>>) {
1717
private val N: Int = adj.size
1818

19-
// finalPath stores the final solution, i.e. the path of the salesman.
20-
private var finalPath = listOf<Int>()
21-
2219
// visited keeps track of the already visited nodes in a particular path
2320
private var visited = listOf<Int>()
2421

22+
// finalPath stores the final solution, i.e. the path of the salesman.
23+
private var finalPath = listOf<Int>()
24+
2525
// Stores the final minimum weight of shortest tour.
2626
private var finalLength: Int = Int.MAX_VALUE
2727

@@ -72,12 +72,9 @@ class TravelingSalesman(private val adj: List<List<Int>>) {
7272
// base case is when we have reached level N which
7373
// means we have covered all the nodes once
7474
if (level == N) {
75-
// check if there is an edge from last vertex in path back to the first vertex
76-
if (adj[currentPath[level - 1]][currentPath[0]] != MAX_WEIGHT) {
77-
// currentLength has the total weight of the solution we got
78-
val currentLength = currentWeight + adj[currentPath[level - 1]][currentPath[0]]
75+
if (adj[currentPath.last()][currentPath.first()] != MAX_WEIGHT) {
76+
val currentLength = currentWeight + adj[currentPath.last()][currentPath.first()]
7977

80-
// Update final result and final path if current result is better.
8178
if (currentLength < finalLength) {
8279
finalPath = currentPath
8380
finalLength = currentLength
@@ -90,15 +87,14 @@ class TravelingSalesman(private val adj: List<List<Int>>) {
9087
for (node in 0..<N) {
9188
// Consider next vertex if it is not same (diagonal
9289
// entry in adjacency matrix) and not visited already
93-
if (node != currentPath[level - 1] && !visited.contains(node)) {
90+
if (node != currentPath.last() && !visited.contains(node)) {
9491
val tempBound = currentBound
95-
currentWeight += adj[currentPath[level - 1]][node]
92+
currentWeight += adj[currentPath.last()][node]
9693

97-
// different computation of currentBound for level 2 from the other levels
9894
currentBound -= if (level == 1) {
99-
ceil((firstMin(currentPath[0]) + firstMin(node)) / 2.0).toInt()
95+
ceil((firstMin(currentPath.last()) + firstMin(node)) / 2.0).toInt()
10096
} else {
101-
ceil((secondMin(currentPath[level - 1]) + firstMin(node)) / 2.0).toInt()
97+
ceil((secondMin(currentPath.last()) + firstMin(node)) / 2.0).toInt()
10298
}
10399

104100
// currentBound + currentWeight is the actual lower bound for the node that we have arrived on
@@ -110,10 +106,8 @@ class TravelingSalesman(private val adj: List<List<Int>>) {
110106

111107
// Else we have to prune the node by resetting
112108
// all changes to currentWeight and currentBound
113-
currentWeight -= adj[currentPath[level - 1]][node]
109+
currentWeight -= adj[currentPath.last()][node]
114110
currentBound = tempBound
115-
116-
// Also reset the visited list
117111
visited = currentPath
118112
}
119113
}

0 commit comments

Comments
 (0)