See Bellman-Ford relax every edge again and again to find shortest paths with negative weights, then check for a negative cycle. Worked example from A to H.
About this algorithm
Bellman-Ford finds the shortest path from one source to every node of a directed graph, and it still works when some edges have negative weights. It relaxes every edge, over and over, for up to V - 1 passes, and one more pass tells you when no shortest path exists, because a cycle whose weights add up to less than zero lets the cost drop forever.
Use Bellman-Ford when edge weights can be negative: a refund along a route, a trade that gains value, a time credit. It is also the textbook way to detect a negative cycle, which is how a profitable loop of currency exchanges is spotted. When every weight is zero or more, Dijkstra gives the same answers faster. When every edge costs the same, BFS is simpler still.
Set the source's distance to 0 and every other node's distance to infinity.
One pass walks the whole edge list. For an edge u → v with weight w, if dist[u] + w is smaller than dist[v], set dist[v] to that sum and record u as v's parent. An edge whose start is still at infinity changes nothing.
A shortest path uses at most V - 1 edges, and after pass k every shortest path of k edges or fewer is correct, so V - 1 passes are always enough. If a whole pass changes nothing, every distance is already final and the run can stop early.
If all V - 1 passes made changes, run one more pass. If any edge can still lower a distance, the graph has a cycle with a negative total, and shortest paths through it are undefined. A pass that changed nothing already rules this out.
The page loads eight nodes, A to H, and 11 directed edges, checked in this order: A→B 6, A→C 7, B→D 5, B→E 8, C→E -3, C→G 9, E→D -2, D→F 4, E→G 2, F→H 3, G→H 2. With 8 nodes there are at most 7 passes. In pass 1, B becomes 6 and C becomes 7. D becomes 11 and E becomes 14 through B, then C→E brings E down to 7 - 3 = 4. G becomes 16 through C, then E→D brings D down to 4 - 2 = 2. F becomes 6, G drops to 4 + 2 = 6, H becomes 9 through F, and G→H brings H to 8. Pass 2 checks all 11 edges again and changes nothing, so the run stops after 2 of the 7 possible passes. The cheapest route to H is A → C → E → G → H, costing 7 - 3 + 2 + 2 = 8. Both negative edges help here: without C→E, reaching E would cost 14.
Within a pass, an update can feed the very next edge. Here C→E sets E to 4 just before E→D reads it, so D reaches its final value in pass 1. With the edges listed in a worse order the same answer would take more passes, but never more than V - 1.
Time: each pass checks all E edges and there are at most V - 1 passes, so O(V · E); a pass that changes nothing ends the run, so when the first pass already gets everything right the cost is two passes, O(E). Space: one distance and one parent per node; the edge list is the input itself.