Use Dijkstra's algorithm with a priority queue to compute shortest distances from S, then check if T is reachable. Since edge weights are non-negative, Dijkstra's is optimal; alternatively, BFS works only if all weights are equal. Return the distance to T or -1 if unreachable.
Pro tip: Mention that you can early-exit when T is popped from the priority queue, and discuss handling large graphs with lazy deletion to avoid stale entries. This shows awareness of practical optimizations and memory management.
Confirm that edge weights are non-negative, the graph may be directed or undirected, and nodes are labeled 0 to N-1. Discuss edge cases: S equals T, disconnected graph, and large input size.
Select Dijkstra's algorithm for non-negative weights. If all weights are equal, BFS is simpler and more efficient. Explain why Dijkstra's is correct and its time complexity O((V+E) log V).
Initialize distances to infinity, set dist[S]=0, and push S into a min-heap. Repeatedly pop the node with the smallest distance, relax its neighbors, and update distances if a shorter path is found.
Stop when T is popped (early exit) or when the heap is empty. If T was never reached, return -1. Otherwise, return dist[T].
State time complexity O((V+E) log V) and space O(V+E). Mention optimizations like using a Fibonacci heap (theoretical) or lazy deletion with a visited set to skip stale entries.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Recognize that the shortest path from S to T through K is the concatenation of the shortest path from S to K and the shortest path from K to T. Run Dijkstra's algorithm twice: once from S to compute distances to all nodes, and once from K (or from T on the reversed graph) to compute distances from K to all nodes. Then sum the distances at K and check for reachability.
Pro tip: Mention that if the graph is undirected, you can run Dijkstra from K once and reuse distances for both segments, but if directed, you need two runs (or one from S and one from K). Also, clarify that the path must be simple? Actually, shortest path with positive weights is automatically simple, but if negative weights exist, use Bellman-Ford and watch for negative cycles.
Ask about directed vs undirected, weighted vs unweighted, and whether edge weights are non-negative. This determines the algorithm choice (Dijkstra vs Bellman-Ford vs BFS).
Explain that the shortest S→T path through K is the sum of the shortest S→K path and the shortest K→T path. This holds because any path through K can be split at K, and minimizing each segment independently minimizes the total.
For non-negative weights, run Dijkstra from S to get dist(S, K), and run Dijkstra from K to get dist(K, T). For negative weights, use Bellman-Ford from S and from K (or from T on reversed graph).
If either dist(S, K) or dist(K, T) is infinity, return -1. Otherwise, return the sum. Mention that if S == K or K == T, one segment is zero.
State time complexity: O((V+E) log V) for two Dijkstra runs. Discuss edge cases: K unreachable from S or T, graph with negative cycles, and multiple edges.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.