When Dijkstra Algorithm Should be Use?

A good way to think about Dijkstra is:

Dijkstra works when the first time you remove a node from the priority queue, you already know its optimal value.

This is called the greedy property.

If a node's value can still improve later by taking a different path, Dijkstra is not applicable.


Cases where Dijkstra works

1. Shortest Path (Classic)

Edges represent distances.

 
A --2--> B --3--> D
 \       ^
  \5     |
   v     1
    C ----
 

Suppose we're finding the shortest path from A.

Initially 

A = 0
B = 2
C = 5

We pop B first because 2 < 5.

Could there later be another path to B shorter than 2?

No.

Any other path must go through C, whose distance is already ≥5.

So

2 + positive edge > 2

Impossible to improve.

This is exactly why Dijkstra works.

Requirement:

  • edge weights ≥ 0

2. Network Latency

Router A
 |
10ms
 |
Router B
 |
5ms
 |
Router C

Total latency

10 + 5 = 15ms 

Again,

  • weights are nonnegative
  • costs only increase

Perfect for Dijkstra.


3. GPS Navigation

Road lengths

Road1 = 5 km
Road2 = 8 km
Road3 = 2 km

Distance always accumulates positively.

Works.


4. Cheapest Flight (without discounts)

Edge

A -> B = $100

Cost

100 + 80 + 50

Again additive positive cost.

Works.


5. Maximum Bottleneck Path (modified Dijkstra)

Suppose bandwidths

A --10--> B --8--> D
A --20--> C --5--> D

Path capacity is

min(edge capacities)

So

A-B-D = 8
A-C-D = 5

We maximize the minimum.

A modified Dijkstra works because

capacity(path)
= min(previous_capacity, edge)

The capacity never increases after extending a path.

The greedy property still holds.


Cases where Dijkstra does NOT work

1. Negative Edges

A --2--> B
A --5--> C
C --(-10)--> B

Initially

B = 2
C = 5

Dijkstra pops

B

But later

A -> C -> B
5 + (-10)
= -5

Much better.

Too late.

Greedy fails.

Bellman-Ford is needed.


2. Currency Exchange (your problem)

A -> B = 2
A -> C = 10
C -> B = 0.5

Products 

A->B
2

vs

A->C->B
10 × 0.5
=5

Notice

B

looked optimal initially

2

but later became

5

Dijkstra finalized B too early.


3. Longest Path

Suppose 

A -> B = 2
A -> C = 1
C -> B = 100

Longest path

A->B
2

vs

A->C->B
101

Again

B

looked finished

but wasn't.


4. Maximum Product

Exactly your interview problem.

rate *= edge

Products may increase dramatically later.

Greedy property breaks.


5. Paths with Rewards

Imagine

edge cost = travel time

node reward = money

Objective

 
maximize reward - cost
 

Reaching a node cheaply isn't necessarily best if another path collects much more reward.

No greedy property.


A useful rule of thumb

Suppose your path value is

newValue = combine(oldValue, edge)

Ask:

Can extending a worse path ever make it better than a currently better path?

If the answer is No, Dijkstra usually works.

If Yes, Dijkstra usually fails.


Works

Sum

new = old + edge

with

edge ≥ 0

Cannot decrease.

Works.


Minimum

new = min(old, edge)

Cannot increase.

Works.


Maximum

new = max(old, edge)

Cannot decrease in the relevant direction.

Works.


Doesn't work

Product

new = old * edge

because

 
10 × 0.1 = 1

2 × 100 = 200 

A worse partial product

2
 

can become

200

while the better one

10

becomes

1

Ordering changes.


Sum with negative edges

2 + (-10) = -8

A worse partial sum can become better.

Ordering changes.


Interview heuristic

When solving a graph problem, ask these questions:

  1. Is the objective additive?
    • distance += edge
    • cost += edge
    • time += edge
    • → Think Dijkstra.
  2. Are all edge "increments" non-negative?
    • If not, Dijkstra is unsafe.
  3. Can the ranking of two partial paths flip after extending them?

For example, suppose two paths reach different nodes with current values:

Path 1: value = 10
Path 2: value = 2

If after one more edge you can get:

Path 1 -> 1
Path 2 -> 200 

then the ordering has flipped. Once this can happen, the greedy assumption behind Dijkstra no longer holds, and you should be suspicious of using it.

This "can the ordering flip?" test is one of the quickest ways to judge whether Dijkstra is appropriate in an interview.

posted @ 2026-07-21 00:49  北叶青藤  阅读(3)  评论(0)    收藏  举报