tags:

views:

380

answers:

5

I understand what Dijkstra's algorithm is but I don't understand why it works.

When selecting the next vertex to examine, why does Dijkstra's algorithm select the one with the smallest weight? Why not just select a vertex arbitrarily, since the algorithm visits all vertices anyway?

+5  A: 

Dijkstra's algorithm picks the vertex with the least path cost thus far, because a path through any other vertex is at least as costly as a path through the vertex with the least path cost. Visiting any other vertex, therefore, if it is more costly (which is quite possible) would necessitate visiting not only that other vertex, but also the one with the least path cost thus far, so you would have to visit more vertices before finding the shortest path. In fact, you would end up with the Bellman-Ford algorithm if you did that.

I should also add that the vertex doesn't have a weight, it is the edge that has a weight. The key for a given vertex is the cost of the shortest path found thus far to that vertex from the source vertex.

Michael Aaron Safyan
@Michael - I still don't get it. When you say "a path through any other vertex is at least as costly", how are you defining path - the path between two vertices u and v? Or the path between 3 vertices <u,v,w>? I think what I'm having trouble with, is that the path <u,v,w> may have more weight than the path <u,t,w> despite <u,v> being less than <u,t>. So, when Dijkstra is at vertex u, it then chooses to visit v ( let's say <u,v> is the lowest weight ). But then how can we recognise that the shortest path to w from u is in fact <u,t,w> and not <u,v,w>? this is tough without diagrams.
BeeBand
@BeeBand - draw it and play with it by hand. You'll run into that "tada" moment faster.
Donnie
@Donnie - ok good idea. I'm at work at the moment, but I can make it look like I'm working.
BeeBand
@BeeBand, a "path" is different from an "edge". A path is a sequence of vertices (and the edges that connect them), while an edge goes merely between two vertices. Dijkstra's algorithm uses the "path cost" which is to say the total cost along the path from the start vertex to the given vertex (which is the same as the cost of the edge plus the key of the node from which the edge originates). Similar to Dijkstra's algorithm is Prim's algorithm, except in Prim's algorithm, the key is the "edge cost" (which is simply the cost of the edge, not including any previous costs).
Michael Aaron Safyan
+1  A: 

It likes greedy strategy.My English is not good.It translates by google.If you don't understand,I am very sorry.

Dijkstra algorithm, a G from S to all vertices of the shortest path length. We assume that each vertex of G in V have been given a flag L (V), it is either a number, either ∞. Suppose P is the set of vertices of G, P contains S, to satisfy:

A) If V is P, then L (V) from V S to the length of the shortest path, and the existence of such V from S to the shortest path: path on the vertices in P in

B) If V does not belong to P, then L (V) from S to V satisfy the following restrictions on the length of the shortest path: V is the only path P does not belong to the vertex.

We can use induction to prove P Dijkstra algorithm in line with the above definition of the collection:

1) When the number of elements in P = 1, P corresponds to the first step in the algorithm, P = (S), is clearly satisfied.

2) Suppose P is k, the number of elements, P satisfy the above definition, see the algorithm below the third step

3) P in and the first to find out is not marked with the minimum vertex U, marked as L (U), can be proved from S to U of U outside the shortest path, in addition to P does not contain the elements do not belong.

Because if there is outside the other vertices except U, then the shortest path to S, P1, P2 ... Pn, Q1, Q2 ... Qn, U (P1, P2 ... Pn is P; Q1, Q2, ... Qn does not belong to P), from the nature of B) the shortest path length L (Q1) + PATH (Q1, U)> L (U).

Which is greater than S, P1, P2 ... Pn, U of the channel length L (U), is not the shortest path. Therefore, from the S to the U of U outside the shortest path, in addition to P does not contain the elements do not belong to U from S to the length of the shortest path from the L (U) is given.

U is added to P in the form P ', clearly P' to meet the nature of the A).

Take V does not belong to P ', obviously does not belong to V P, then from S to V except the shortest path and meet all the vertices outside V in P' in the path there are two possibilities, i) contains U, ii) does not contain U.

On i) S, P1, P2 ... Pn, U, V = L (U) + W (U, V)

ii) S, P1, P2 ... Pn, V = L (V)

Obviously the two are given in the smallest V from S to meet the minimum access and outside addition to all the vertices are V P 'in length.

The third step to algorithm given in P 'with k +1 elements and meet the A), B).

By the induction proposition may permit.

Here is the source.

WaterCow101
@WaterCow101 - Thanks for the answer, I'm sorry but I don't understand what you meant by "Dijkstra algorithm, a G from S to all vertices of the shortest path length.". I read on further, but it still didn't fill in that blank. Did something get lost in translation there perhaps?
BeeBand
+6  A: 

You can think of Djikstra's algorithm as a water-filling algorithm (i.e. a pruned breadth-first search). At each stage, the goal is to cover more of the whole graph with the lowest-cost path possible. Suppose you have vertices at the edge of the area you've filled in, and you list them in terms of distance:

v0 <= v1 <= v2 <= v3 ...

Could there possibly be a cheaper way to get to vertex v1? If so, the path must go through v0, since no untested vertex could be closer. So you examine vertex v0 to see where you can get to, checking if any path through v0 is cheaper (to any other vertex one step away).

If you peel away the problem this way, you're guaranteed that your distances are all minimums, because you always check exactly that vertex that could lead to a shortest path. Either you find that shortest path, or you rule it out, and move on to the next vertex. Thus, you're guaranteed to consume one vertex per step.

And you stop without doing any more work than you need to, because you stop when your destination vertex occupies the "I am smallest" v0 slot.

Let's look at a brief example. Suppose we're trying to get from 1 to 12 by multiplication, and the cost between nodes is the number you have to multiply by. (We'll restrict the vertices to the numbers from 1 to 12.)

We start with 1, and we can get to any other node by multiplying by that value. So node 2 has cost 2, 3 has cost 3, ... 12 has cost 12 if you go in one step.

Now, a path through 2 could (without knowing about the structure) get to 12 fastest if there was a free link from 2 to 12. There isn't, but if there was, it would be fastest. So we check 2. And we find that we can get to 4 for cost 2, to 6 for 3, and so on. We thus have a table of costs like so:

3  4  5  6  7  8  9 10 11 12 // Vertex
3  4  5  5  7  6  9  7 11  8 // Best cost to get there so far.

Okay, now maybe we can get to 12 from 3 for free! Better check. And we find that 3*2==6 so the cost to 6 is the cost to 3 plus 2, and to 9 is plus 3 and so on.

4  5  6  7  8  9 10 11 12
4  5  5  7  6  8  7 11  8

Fair enough. Now we test 4, and we see we can get to 8 for an extra 2, and to 12 for an extra 3. The cost to get to 12 is thus no more than 4+3 = 7:

5  6  7  8  9 10 11 12
5  5  7  6  8  7 11  7

Now we try 5 and 6--no improvements so far. This leaves us with

7  8  9 10 11 12
7  6  8  7 11  7

Now, for the first time, we see that the cost of getting to 8 is less than the cost of getting to 7, so we had better check that there isn't some free way to get to 12 from 8. There isn't--there's no way to get there at all with integers--so we throw it away.

7  9 10 11 12
7  8  7 11  7

And now we see that 12 is as cheap as any path left, so the cost to reach 12 must be 7. If we'd kept track of the cheapest path so far (only replacing the path when it's strictly better), we'd find that 4*3 is the first cheapest way to hit 12.

Rex Kerr
I get it now! Ok - I now realise that this is what all the answers were trying to tell me, but this is the clearest explanation and the one that caused the penny to drop. Thank you!
BeeBand
Incidentally, doesn't the best cost to 12 from 3, make it 7? (3*4=12, but you still have the best cost listed as 8...)
BeeBand
@Bee: Glad this helped. But I did say that the best cost is 7 to get to 12 (it was 8 along the way, but switches down to 7 when we test 4).
Rex Kerr
@Rex - I thought about this some more. It still doesn't explain why, after visiting vertex 1, we visit 2. We could just as easily say, ok let's visit 4, there might be a cheaper way to get to 12 via 4 than our current cost of 12. We don't know about structure at this point, isn't there a high probability of there being an edge <4,12> with cost 1? I know that later on we choose to examine 4, but why not choose it straight away after examining 1? Is it simply that the cost to 2 is so low, we run a high chance of really improving the cost to 12 via this vertex?
BeeBand
@Rex - Ok, I see the mistake I'm making. What I missed was, that this algorithm is examining paths from one source vertex to *all other* vertices (even though ultimately it is trying to find the least cost to some destination vertex). So we choose 2 because we might be able to reduce our current cost to 3, 4, 5 ... etc via this node. Eg. if we can reduce the cost to 3, via 2, then we have definitely found the least cost path from source vertex to 3. Any path that needs to go via vertex 3 is now guaranteed to have the least cost (up to vertex 3 anyway). Ok I'm done now. Thanks!
BeeBand
+1  A: 

Very good question - the reason why Dijsktra's algorithm works the way it does is, in part, because it exploits the fact that the shortest path between point u and w that includes point v also contains the shortest path from u to v and from v to w. If there existed something shorter between u to v, then it wouldn't be the shortest path.

To really understand why Dijkstra's algorithm works, look into the basics of dynamic programming - sounds hard but it's really pretty easy to understand the principles.

Grembo
@Grembo - thank you - I now completely understand exactly what you are telling me here. The shortest path from u to w via v has to ensure that it includes the shortest path from u to v also. I really don't know why I was having such a problem before :-)
BeeBand
No prob - it's one of those areas of significant overlap between the CS and OR community. Just teaching the "what" doesn't give the insight necessarily to know the "how" and the "why".
Grembo
+1  A: 

It checks the path with the lowest weight first because this is most likely (without additional information) to reduce the number of paths checked. For example:

a->b->c     cost is 20
a->b->d     cost is 10
a->b->d->e  cost is 12

If goal is to get from a to e, we don't need to even check the cost of:

a->b->c->e

Because we know it's at least 20 so we know it's not optimal because there is already another path with a cost of 12. You can maximize this effect by checking the lowest weights first. This is similar (same?) to how minimax works in chess and other games to reduce the branching factor of the game tree.

phkahler