Weighted Graph Min Path

Weighted Graph Min Path

Given a directed, weighted graph with non-negative edge weights, return the minimum total weight of a path from start to target, or -1 if unreachable.

minPath(graph, start, target)

Each Edge has a To string and a Weight int. Use Dijkstra's algorithm.

Input format: edges | start | target, edges as a,b,w triples (a → b with weight w).

Examples

edges a,b,1 b,c,2 a,c,5, start a, target c   // → 3   (a→b→c)
edges a,b,1, start a, target z               // → -1

Walkthrough

Why plain BFS no longer works

With unweighted graphs, fewest-edges and cheapest-path are the same thing, so BFS suffices. Here edges have different weights, so fewest hops isn't necessarily cheapest — a two-hop path with small weights can beat a one-hop path with a large weight. You need an algorithm that accounts for accumulated cost, not hop count.

The greedy insight behind Dijkstra

Maintain a best-known cost for every node, 0 for the source, infinity elsewhere. The central claim: among all not-yet-finalized nodes, the one with the smallest tentative distance right now already has its true shortest distance. Any other path to it would pass through some other unfinalized node, but every unfinalized node currently has a distance at least as large, and non-negative weights mean continuing along any path only adds cost, never subtracts. So no detour through a currently-more-expensive node could end up cheaper. This only holds because weights are non-negative — negative edges break the argument, which is why Bellman-Ford exists for those graphs.

Building the approach

  1. Set the source's distance to 0, everything else to infinity.
  2. Repeatedly select the unfinalized node with the smallest tentative distance — guaranteed correct — and finalize it.
  3. Relax every outgoing edge: for each neighbor, check if reaching it via the just-finalized node beats its current distance, and update if so.
  4. Continue until the target is finalized or no unfinalized node has a finite distance.
  5. Return the target's distance, or -1 if never finite.

Why greedily expanding the cheapest frontier node is optimal

Always finalizing the currently-cheapest reachable node ensures you never finalize before discovering a cheaper route — any cheaper alternative would pass through a node that's even cheaper still, which would have been finalized first. This greedy strategy, run to completion, produces exact shortest distances to every reachable node.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.