-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathShortestPath.kt
36 lines (35 loc) · 1.57 KB
/
ShortestPath.kt
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
import java.util.PriorityQueue
fun shortestPath(n: Int, edges: List<List<Int>>, start: Int): List<Int> {
val graph = hashMapOf<Int, MutableList<Pair<Int, Int>>>()
val distances = MutableList(n) { Int.MAX_VALUE }
distances[start] = 0
// Represent the graph as an adjacency list.
for ((u, v, w) in edges) {
graph.computeIfAbsent(u) { mutableListOf() }.add(v to w)
graph.computeIfAbsent(v) { mutableListOf() }.add(u to w)
}
val minHeap = PriorityQueue<Pair<Int, Int>>(compareBy { it.first })
minHeap.add(0 to start) // (distance, node)
// Use Dijkstra's algorithm to find the shortest path between the start node
// and all other nodes.
while (minHeap.isNotEmpty()) {
val (currDist, currNode) = minHeap.poll()
// If the current distance to this node is greater than the recorded
// distance, we've already found the shortest distance to this node.
if (currDist > distances[currNode]) {
continue
}
// Update the distances of the neighboring nodes.
for ((neighbor, weight) in graph[currNode] ?: emptyList()) {
val neighborDist = currDist + weight
// Only update the distance if we find a shorter path to this
// neighbor.
if (neighborDist < distances[neighbor]) {
distances[neighbor] = neighborDist
minHeap.add(neighborDist to neighbor)
}
}
}
// Convert all infinity values to -1, representing unreachable nodes.
return distances.map { if (it == Int.MAX_VALUE) -1 else it }
}