An interactive visualization of Dijkstra's algorithm for finding shortest paths in a weighted directed graph. Watch how the priority queue greedily selects the closest unvisited node and relaxes its neighbors.
Shortest Path LeetCode 743 β Mediumn nodes, labeled from 1 to n.
You are also given times, a list of travel times as directed edges
times[i] = (ui, vi, wi), where ui is the source node,
vi is the target node, and wi is the time it takes for a signal to travel from source to target.k. Return the minimum time it takes for all n nodes to receive the signal.
If it is impossible for all n nodes to receive the signal, return -1.
fun networkDelayTime(times: Array<IntArray>, n: Int, k: Int): Int { val graph = HashMap<Int, MutableList<Pair<Int, Int>>>() for ((u, v, w) in times) graph.getOrPut(u) { mutableListOf() }.add(Pair(v, w)) val dist = IntArray(n + 1) { Int.MAX_VALUE } dist[k] = 0 val pq = PriorityQueue<Pair<Int, Int>>(compareBy { it.first }) pq.add(Pair(0, k)) while (pq.isNotEmpty()) { val (d, u) = pq.poll() if (d > dist[u]) continue // stale entry for ((v, w) in graph[u] ?: emptyList()) { if (dist[u] + w < dist[v]) { dist[v] = dist[u] + w pq.add(Pair(dist[v], v)) } } } val mx = dist.drop(1).max() return if (mx < Int.MAX_VALUE) mx else -1}
Dijkstra's algorithm is a greedy algorithm that always processes the node with the smallest known distance first (via a priority queue / min-heap). Once a node is popped from the priority queue, its shortest distance is finalized because all remaining entries in the queue have equal or greater distance. This greedy property only works when all edge weights are non-negative.
k, which gets distance 0. Push (0, k) into the priority queue.(d, u) from the priority queued > dist[u], this is an outdated entry β skip itv with weight w, if dist[u] + w < dist[v], update dist[v] and push (dist[v], v) into the queue-1 (unreachable)
Each edge is relaxed at most once, and each heap operation costs O(log V).
The adjacency list and distance map take O(V + E) space.