Take the 2-minute tour ×
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It's 100% free, no registration required.

According to this page, Dijkstra's algorithm is just BFS with a priority queue. Is it really that simple? I think not.

share|improve this question

migrated from cstheory.stackexchange.com Feb 24 '13 at 8:51

This question came from our site for theoretical computer scientists and researchers in related fields.

1  
Why do you think that? –  Raphael Feb 24 '13 at 16:18
    
@Raphael Because it seems too simple, and it is: I studied it again and I see now it doesn't keep track of the distance between nodes, so it's really a BFS, not Dijkstra. –  Barry Fruitman Feb 24 '13 at 18:21
1  
Well, Dijkstra does change the values the queue is "sorted" with (often called "relaxation"); if you forbid that, it's not the same, true. –  Raphael Feb 24 '13 at 23:15

2 Answers 2

up vote 6 down vote accepted

You can implement Dijkstra's algorithm as BFS with a priority queue (though it's not the only implementation).

Dijkstra's algorithm relies on the property that the shortest path from $s$ to $t$ is also the shortest path to any of the vertices along the path. This is exactly what BFS does.

Or in another perspective: how would Dijkstra's algorithm behave if all the weights were 1? Exactly like BFS.

share|improve this answer

First, how can we adapt BFS to more general weighted graph $G = (V, E)$?

Here is an idea from the book "Algorithms (Section 4.4)" by Dasgupta et al:

For any edge $e = (u,v)$ of $E$ (with weight $l_e$), replace it by $l_e$ edges of length $1$, by adding $l_e - 1$ dummy nodes between $u$ and $v$.

As a result, the edges of the result graph $G'$ all have unit length. Therefore, we can compute distances in $G$ by running BFS on $G'$.

Second, how does Dijkstra algorithm on $G$ beat BFS on the transformed graph $G'$?

BFS on $G'$ can be really slow if some $l_e$ are big because it wastes too much time on computing distances to those dummy nodes that we don't care about at all. The Dijkstra algorithm avoids this by setting estimated distances for nodes and relaxing them whenever possible.

Third, how does Dijkstra algorithm behave on unweighted graphs?

It behaves exactly the same as BFS does. We elaborate this from two major points.

  • On "relaxation".

    For Dijkstra algorithm on general, weighted graph, the relaxation is

    for all edges (u,v) in E:
        if dist(v) > dist(u) + w(u,v)
           dist(v) = dist(u) + w(u,v)
    

    For BFS on unweighted graph, we know that $dist(v) = \infty$ and $w(u,v) = 1$, so the relaxation is simpler:

    for all edges (u,v) in E:
        if dist(v) = \infty
           dist(v) = dist(u) + 1
    
  • On "priority queue".

    When Dijkstra algorithm is run on unweighted graph, at any time, the priority queue contains at most two distinct (distance) values. Therefore, a FIFO queue of BFS suffices.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.