惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
腾讯CDC
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
A
About on SuperTechFans
博客园 - 叶小钗

博客园 - 北叶青藤

2096. Step-By-Step Directions From a Binary Tree Node to Another Find path from root to a target node in Binary Tree 1188. Design Bounded Blocking Queue 1115. Print FooBar Alternately 1114. Print in Order 1242. Web Crawler Multithreaded Python Multi-threading bot ip Log Rate Limiter Same Word of HTML Labels Most Frequent Call Chain remove prefix in a words list 1102. Path With Maximum Minimum Value Property Booking Optimizer minimum number 755. Pour Water Keyword Tagging in Reviews with Overlapping Matches Retryer Function Implementation 1125. Smallest Sufficient Team Print the terrain Split stay Task scheduling problem 滑雪问题 845. Longest Mountain in Array 723. Candy Crush 1539. Kth Missing Positive Number 1650. Lowest Common Ancestor of a Binary Tree III 424. Longest Repeating Character Replacement 843. Guess the Word 551. Student Attendance Record I
When Dijkstra Algorithm Should be Use?
北叶青藤 · 2026-07-21 · via 博客园 - 北叶青藤

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.

Suppose we're finding the shortest path from A.

Initially 

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

Impossible to improve.

This is exactly why Dijkstra works.

Requirement:

  • edge weights ≥ 0

2. Network Latency

Total latency

Again,

  • weights are nonnegative
  • costs only increase

Perfect for Dijkstra.


3. GPS Navigation

Road lengths

Distance always accumulates positively.

Works.


4. Cheapest Flight (without discounts)

Edge

Cost

Again additive positive cost.

Works.


5. Maximum Bottleneck Path (modified Dijkstra)

Suppose bandwidths

Path capacity is

So

We maximize the minimum.

A modified Dijkstra works because

The capacity never increases after extending a path.

The greedy property still holds.


Cases where Dijkstra does NOT work

1. Negative Edges

Initially

Dijkstra pops

But later

Much better.

Too late.

Greedy fails.

Bellman-Ford is needed.


2. Currency Exchange (your problem)

Products 

vs

Notice

looked optimal initially

but later became

Dijkstra finalized B too early.


3. Longest Path

Suppose 

Longest path

vs

Again

looked finished

but wasn't.


4. Maximum Product

Exactly your interview problem.

Products may increase dramatically later.

Greedy property breaks.


5. Paths with Rewards

Imagine

Objective

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

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

with

Cannot decrease.

Works.


Minimum

Cannot increase.

Works.


Maximum

Cannot decrease in the relevant direction.

Works.


Doesn't work

Product

because

A worse partial product

can become

while the better one

becomes

Ordering changes.


Sum with negative edges

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:

If after one more edge you can get:

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.