Login
Home
Eoin Bailey . com
Tech, Research, Code, Work, and Fun
  • Home
  • Eoin's CV
  • About Eoin
  • Ph.D.
  • Blog
  • Galleries
  • Training
  • Polls
  • Tags
  • Weblinks
  • Site map
  • Contact

Tag Cloud

algorithm antarctica apple banking browser code copyright cycle data centre devel Dijkstra drupal drupalcamp economics escapades facebook firefox galway Google iphone ipod livigno theme training weights
more tags

A Random Image

IMG_3402

Dijkstra's algorithm - An Illustrated Explanation

Submitted by Eoin on Mon, 09/02/2009 - 11:26
in
  • algorithm
  • Dijkstra
  • General-Personal
  • General-Research
  • General-Work

I was recently doing some reading on algorithms, as I was reading up on Dijkstra's algorithm, I noticed that there seemed to be a lack of nice explanations out there. I found lots of applets, and demonstrations, but none of them had a step-by-step explanation, i.e. why certain events were occuring. Wikipedia has an article on Dijkstra's Algorithm, but personally, I didn't find it to clear. I decided to put up my own effort at explaining the algorithm, if any of it is unclear, let me know and I'll add in more detail, or a better explanation.

Dijkstra's Algorithm

Before I start into how Dijkstra's Algorithm does what it does, here is a short description of what Dijkstra's Algorithm does:

In a given graph, and starting node, Dijkstra's Algorithm discovers the shortest path from the starting node to all other nodes.

Assumptions: The cost/length of travelling between nodes is known.

The Explanation: Dijkstra's Algorithm

Figure 1: Dijkstra - Starting PositionFigure 1: Dijkstra - Initial Position

Figure 1 displays the graph: nodes are black circles labelled a-f, a path is a black line connecting two nodes, each path has an associated length beside it (the numbers). The lengths are not to scale.

Node 'a' is our starting node, we want to find the shortest path to all other nodes in the graph. To do this, we generate a table. This table has the distance to all the nodes in the graph, from the perspective of the starting node 'a'.

Table 1: Initial Entries for distances to nodes from Node 'a'
Node Distance to Node from Node 'a'
b INFINTE
c INFINTE
d INFINTE
e INFINTE
f INFINTE
g INFINTE
h INFINTE
i INFINTE

As can be seen from Table 1, the initial entries for the distances are all set to infinity (or some notional maximum value). This ensures that any path found will be shorter than the initial value stored in the table.

The node 'a' is the starting node, as such we examine all the possible paths away from this node first. The options are as follows:

Table 2: Distance to nodes (from 'a') accesible from node 'a'
Node Distance to Node from Node 'a'
b 7
c 4
d 5

These values are used to update the graph table, Table 1, which becomes:

Table 3: Entries for distances to nodes from Node 'a'
Node Distance to Node from Node 'a'
b 7
c 4
d 5
e INFINTE
f INFINTE
g INFINTE
h INFINTE
i INFINTE

Dijkstra's algorithmFigure 2: Dijkstra's algorithm

Figure 2 shows the routes marked in red. We know have three paths from node 'a'. However, these paths are not yet guaranteed to be the shortest path. To be sure we have the shortest path, we have to keep going.

The next move in the algorithm is to move to the nearest node from node 'a'. In this case that is node 'c'.

Figure 3: Dijkstra's AlgorithmFigure 3: Dijkstra's Algorithm

At node 'c' we have paths available to nodes 'b' and 'h'. When calculating the distances we have to calculate the distances from node 'a'. In this case that means the following:

Table 4: Distance to nodes (from 'a') accesible from node 'a'
Node Distance to Node from Node 'a'
b 6
h 13

These values are then compared to the values stored in the Table 3. It can be seen that both of these values are less than the current values stored in the table, as such table 3 becomes:

Table 5: Entries for distances to nodes from Node 'a'
Node Distance to Node from Node 'a'
b 6
c 4
d 5
e INFINTE
f INFINTE
g INFINTE
h 13
i INFINTE

This step has illustrated one of the advantages of dijkstra's algorithm: the route to node 'b' is not the most direct route, but it is the shortest route; Dijkstra's Algorithm can find the shortest route, even when that route is not the most direct route.

Figure 4: Dijkstra's AlgorithmFigure 4: Dijkstra's Algorithm

Again, all paths accesible from node 'c' have been checked, and the table of paths has been updated. Node 'c' is marked as visited.

IMPORTANT:

  1. A Visited node is never re-visited.
  2. Once a node has been marked visited, the path to that node is known to be the shortest route from the initial node.

In that case, we should add another column to our table:

Table 6: Entries for distances to nodes from Node 'a'
Node Distance to Node from Node 'a' Visited
b 6 NO
c 4 YES
d 5 NO
e INFINTE NO
f INFINTE NO
g INFINTE NO
h 13 NO
i INFINTE NO

As these value are being updated, the route that accompanies these distances also needs to be stored.

Once again, the table of paths is consulted, and the shortest path to a node that has not been visited is found. This node becomes the next current node. In this case, that is node 'd'.

Figure 5: Dijkstra's AlgorithmFigure 5: Dijkstra's Algorithm

From node 'd', the following paths are available:

Table 7: Distance to nodes (from 'a') accesible from node 'a'
Node Distance to Node from Node 'a'
f 14

The table of all paths is updated to reflect that, and the node 'd' is marked as visited, this locks in the shortest path to node 'd' also:

Table 8: Entries for distances to nodes from Node 'a'
Node Distance to Node from Node 'a' Visited
b 6 NO
c 4 YES
d 5 YES
e INFINTE NO
f 14 NO
g INFINTE NO
h 13 NO
i INFINTE NO

It can be seen from table 8 above, that the next nearest node to node 'a' is node 'b'. All paths from node 'b' are examined next. In this instance, we have a path to a node that is marked as visited: node 'c', we already know that the path to node 'c' is as short as it can get (the node being marked as visited is the marker for this).

Figure 6 Dijkstra's AlgorithmFigure 6: Dijkstra's Algorithm

As figure 6 shows, we check the path the only other node accesible from node 'b': node 'e'. This updates our paht table as follows:

Table 9: Entries for distances to nodes from Node 'a'
Node Distance to Node from Node 'a' Visited
b 6 YES
c 4 YES
d 5 YES
e 31 NO
f 14 NO
g INFINTE NO
h 13 NO
i INFINTE NO

Table 9 again tells us that the next node for us to visit is node 'h'.

Figure 7: Dijkstra's AlgorithmFigure 7: Dijkstra's Algorithm

We add up the paths, and mark the nodes as visited...

Figure 8: Dijkstra's AlgorithmFigure 8: Dijkstra's Algorithm

We keep on doing this....

Figure 9: Dijkstra's AlgorithmFigure 9: Dijkstra's Algorithm

Until all the nodes have been visited!

Every step in this process can be viewed in this gallery: Dijkstra's Algorithm - Step-by-Step

  • Eoin's blog
  • Printer-friendly version
  • Send to friend

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Nice tutorial, but one question:

Submitted by Visitor (not verified) on Fri, 07/08/2009 - 23:24.

Looking at the final graph, how do you then determine the shortest path from A to F?

  • reply

Route from A to F

Submitted by Eoin on Wed, 12/08/2009 - 14:44.

OK.. so if you are trying to find the shortest route from A to F: The nodes visited would go in this order:

A - C - D - B - H - F. At this point the algorithm would stop, knowing that the shortest route to Node F is A - D - F. Now, the interesting thing in this route to note is this: that route was discovered as soon as node 'D' was visited, this is the third node that was checked. But, it is not guaranteed to be the shortest path until the node in question has been marked as visited also.

The reason for this is because you cannot know for sure that you have travelled every path to a node until it has been marked as visited.

Let us assume that the cost of travelling between C - H is 6, and the cost of travelling between H - F is 1. In this situation the shortest route from A to F is A - C - H - F. But, this route will not be discovered until after A - D - F (because D is checked before H). In this case, the first route found to F will be A - D - F, at a cost of 14. Whereas A - C - H - F is a cost of 11.

The most important point to remember is this: Once a node is visited, we KNOW that there cannot be a shorter path to that node (from the origin). This is because we always visit the next nearest node from the origin. That means that at every step along the way, it has to be longer to go via any other route.

I know this is a little bit circuitous in its reasoning. It's important to remember that algorithm finds the shortest route from a given point, and all distances are relative to that point. As can be seen, the algorithm does not guarantee that it finds the shortest route first, just that it finds the shortest route.

  • reply

i really appreciate your

Submitted by Abhishek (not verified) on Thu, 07/10/2010 - 02:18.

i really appreciate your work, as i was fed up reading only overall articles. you have really explained it for new bie. Thanks

  • reply

...

Submitted by deedyloaramal (not verified) on Mon, 04/01/2010 - 23:11.

It looks like you are a true expert. Did ya study about the topic? hrhr

  • reply

hey dat was nice

Submitted by siddhi (not verified) on Sun, 24/01/2010 - 07:03.

wat i wish to say is dat in while programming dijkstra how will we back track like in the above example we backtracked for findin path of node e...i,e, from a to c and then to e....i mean for finding the shortest path i will have to try all possible combinations of visited nodes...dats too cumbersome to program...

  • reply

Dijkstra's shortest path

Submitted by John Oke (not verified) on Thu, 23/09/2010 - 19:05.

Can you apply/use Dijkstra's algorithm, (manually), from a table rather than a nertwork. I have tried and failed
Regards
John

  • reply

awesome...i had no clue as to

Submitted by Visitor g (not verified) on Mon, 25/10/2010 - 09:51.

awesome...i had no clue as to how this worked but now i know... so i have a better understanding of 0spf.

  • reply

Thank you!

Submitted by Garth (not verified) on Mon, 13/12/2010 - 02:49.

Taking a CS course for the first time in data structures and algorithms. This article was really helpful because is showed the state changes which I could not really understand at all from my text book authors explanation. Thank you so much for your help in simplifying this for me. I really appreciate it.

Garth

  • reply

thumbs up

Submitted by Visitor (not verified) on Fri, 14/01/2011 - 01:41.

very nice explaination, kindly explain other algos as well

  • reply

can you code it?

Submitted by Visitor (not verified) on Sun, 16/01/2011 - 10:13.

can you code it?

  • reply

Very nice tutorial...I

Submitted by RajuNalini (not verified) on Wed, 16/03/2011 - 04:15.

Very nice tutorial...I understood the concept well..thanks

  • reply

Why dijkstra?

Submitted by Visitor (not verified) on Sun, 15/05/2011 - 05:04.

How is dijkstra better than any other algorithm for a static network?

  • reply

Dijkstra guarantees the

Submitted by Eoin on Sun, 15/05/2011 - 09:15.

Dijkstra guarantees the shortest path between all nodes, that's it's major selling point. There is an overhead in storing the routing information and generating the routes in the first place, but when it's a static network it makes sense to pre-compute all this information since it won't change. In a dynamic network with routes changing all the times there are other approches, a naive example is flooding the network, i.e. send the message to every node, I also wrote a thesis on an approach call parasitic routing that makes smart choices about where to send the message based on a nodes mobility pattern, there are loads more too, but that's a start.

  • reply

like it :) ... thanks :)

Submitted by darkrai (not verified) on Sun, 15/05/2011 - 17:34.

like it :) ... thanks :)

  • reply

Excellent

Submitted by Raghu (not verified) on Thu, 26/05/2011 - 09:53.

Great explanation

  • reply

Algo was really explained

Submitted by sharayu (not verified) on Thu, 04/08/2011 - 05:17.

Algo was really explained nicely.just like u i read so many articles but couldn't clear the concept.this explanation really clear my concept.
thank u so much.

  • reply

thanks alot.......

Submitted by Lanos (not verified) on Fri, 19/08/2011 - 04:51.

thanks alot.......

  • reply

Its not so clear to me.. can

Submitted by Visitor (not verified) on Mon, 03/10/2011 - 11:45.

Its not so clear to me..
can you give ma a code for it?

  • reply

Sorry, no. If you follow the

Submitted by Eoin on Tue, 11/10/2011 - 07:53.

Sorry, no.

If you follow the explanation step-by-step you should be able to put together some pseudo-code pretty easily. Then convert that into whatever language you like best!

  • reply

very nice explanation !

Submitted by mihira (not verified) on Sat, 08/10/2011 - 07:24.

very nice explanation !

  • reply

Thanks!

Submitted by Eoin on Tue, 11/10/2011 - 07:53.

Thanks!

  • reply

Visiting all nodes in Dijkstra?

Submitted by Roan (not verified) on Sun, 09/10/2011 - 12:28.

As much as I know that Dijkstra algo is used to find the shortest path to the destination from the source. Visiting each node is not neccessary

  • reply

In what cases is it not? To

Submitted by Eoin on Tue, 11/10/2011 - 07:55.

In what cases is it not? To know how to get to every path in a network you need to visit every node once (assuming you have no prior knowledge of the network topology). If you only need the route to a single node, then no, you don't necessarily need to visit every node to find the shortest route to that node.

  • reply

Assumptions on cost/length

Submitted by Srinivasan Ramachandran (not verified) on Tue, 11/10/2011 - 07:36.

Dear Eoin,

You have done a great explanation, but i am struck at one point initially. How to make the assumptions about the cost/length between the nodes? Are they assumed based on hypotenuse values between two nodes using Pythagoras since the solution involves a graph? or is there any other logic to give cost/length between the nodes?
Your timely answer will help me a lot.

Regards,

Ashok Srinivasan.

  • reply

I'm open to correction on

Submitted by Eoin on Tue, 11/10/2011 - 07:57.

I'm open to correction on this, but to the best of my recollection the algorithm gives no advice on how to calculate the cost of transmission between two nodes. Calculating that cost could be achieved via the power required to transmit to a given node in a wireless network of nodes, it may come down to a simple distance calculation. In short, it depends on the network and it depends what you want to optimise against.

- Eoin

  • reply

Thank you for the insight.

Submitted by Srinivasan Ramachandran (not verified) on Tue, 11/10/2011 - 08:31.

Dear Eoin,

Hmmm, now i come to see some point. So what you mean to say is that it is subjective when it comes to cost/length calculation. So if i want to calculate rail routes on a map which is looked as a Graph, is it fine to go ahead with distance calculation as a base to calculate cost/length?

Regards,

Ashok Srinivasan.

  • reply

Thank you for the insight.

Submitted by Srinivasan Ramachandran (not verified) on Tue, 11/10/2011 - 08:19.

Dear Eoin,

Hmmm, now i come to see some point. So what you mean to say is that it is subjective when it comes to cost/length calculation. So if i want to calculate rail routes on a map which is looked as a Graph, is it fine to go ahead with distance calculation as a base to calculate cost/length?

Regards,

Ashok Srinivasan.

  • reply

Exactly.

Submitted by Eoin on Tue, 11/10/2011 - 08:26.

Exactly.

  • reply

Thanks for the direction.

Submitted by Srinivasan Ramachandran (not verified) on Tue, 11/10/2011 - 09:14.

Dear Eoin,

Thank you for the confirmation. I can now proceed with this direction.

Regards,

Ashok Srinivasan.

  • reply

Thank you..

Submitted by Visitor (not verified) on Mon, 23/01/2012 - 08:15.

Ur explanation does makes the funda very clear

  • reply

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.

More information about formatting options

Current Poll

What is your current phone OS?:

Freelance Work

A sample of websites I have developed:

  • Studio Richards
  • Medilex
  • Design21C
  • The Spine Clinic
  • Abrivia - Careers and Outplacement
  • Emilie Conway

Twitter

Follow @eoinbailey

Some Links

  • James Bond Opening Scenes
  • Polls
  • Chess Module-Drupal
  • Ski Trip Jan 2009

Training

  • Spinning
  • Hodson Bay Hotel Training
  • Spinning Class - Not too Shabby!
  • Bike Time Trial
  • Spinning Class of Anti-Doom!

Recent Comments

  • Happy New Year 2012!
  • Thank you..
  • Simple way
  • Yep, CSS is an option - there
  • Maybe CSS is an easy solution?
  • Thanks for the direction.
  • Thank you for the insight.
  • Exactly.
  • Thank you for the insight.
  • I'm open to correction on

Powered by

Powered by Drupal, an open source content management system

Recent blog posts

  • Copyright Issues
  • Leap Card
  • A new College term - Dijkstra's Algorithm
  • Dublin GTUG - June 2011 - Over 100 attendees
  • City of a Thousand Welcomes
  • Groupon et al. - "Bet on the Future"
  • Dublin Web Summit - DWS6 - Roundup
  • Anti-Spam - Using a Catch-All to identify bad companies
  • Hide a Note Title - Drupal 7
  • The Election on Hit The Road
more
Copyright © 2012 Eoin Bailey . com.

I'm trying out: Web Analytics