Skip to content

Commit 80daf7b

Browse files
committed
TSP: use filter() and min() to find the first minimum edge weight
Performance impact on "All in a single night" (day 9 of 2015, both parts together, approximated measuremnts): - from 797us to 1.2ms
1 parent 7664c01 commit 80daf7b

File tree

1 file changed

+1
-9
lines changed

1 file changed

+1
-9
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ class TravelingSalesman(private val adj: List<List<Int>>) {
2828
/**
2929
* Find the minimum edge cost having an end at the vertex [node]
3030
*/
31-
private fun firstMin(node: Int): Int {
32-
var min = MAX_WEIGHT
33-
for (otherNode in 0..<N) {
34-
if (adj[node][otherNode] < min && node != otherNode) {
35-
min = adj[node][otherNode]
36-
}
37-
}
38-
return min
39-
}
31+
private fun firstMin(node: Int) = adj[node].filterIndexed { i, _ -> i != node }.min()
4032

4133
/**
4234
* Find the second minimum edge cost having an end at the vertex [node]

0 commit comments

Comments
 (0)