AlgoFrames

Why no negative edges

Warm-up · Multiple choice · about 3 min

Dijkstra runs from node 0 on this directed graph, which has one negative edge. It settles node 1 at distance 2. Why is that answer unsafe?

Example 1

Input:

{"edges":[[0,1,2],[0,2,3],[2,1,-2]],"source":0}

Output:

Node 1 is really 1 away

The route 0, 2, 1 costs 3 - 2 = 1.

Learn the idea first: Dijkstra's algorithm, a visual lesson.

More Dijkstra's algorithm problems