Bellman–Ford and negative edges

What distance should replace 8?

Examples

Worked example

Input
Distance to A is 5. A → B has weight -2. Current distance to B is 8.
Expected output
3

Hints and solution

Hint 1

Write down the starting state and walk through one example by hand.

Reveal worked answer

Traveling via A costs 5 + (-2) = 3, which is smaller than 8.

Back to the lesson