In this article I describe Dijkstra’s algorithm for finding the shortest path from one source to all the other vertexes in a graph. Afterwards, I provide the source code in C of a simple implementation.
To understand this you should know what a graph is, and how to store one in memory. If in doubt check this and this.
Another solution for this problem is the Bellman-Ford algorithm.
The Problem
Given the following graph calculate the length of the shortest path from node 1 to every other node.

Lets take the nodes 1 and 3. There are several paths (1 -> 4 -> 3, 1 -> 2 -> 3, etc.), but the shortest of them is 1 -> 4 -> 2 -> 3 of length 9. Our job is to find it.
The Algorithm
Dijkstra’s algorithm is one of the most common solutions to this problem. Even so, it only works on graphs which have no edges of negative weight, and the actual speed of the algorithm can vary from O(n*lg(lg(n))) to O(n2).
The idea is somewhat simple:
Take the length of the shortest path to all nodes to be infinity. Mark the length of the shortest path to the source as 0.

Now, we already know that the graph has no edges of negative weight so the a path of length 0 is the best we can come up with. The path to the source is 0, so it’s optimal.
This algorithm works by making the paths to one more node optimal at each step. So, at the kth step, you know for sure that there are at least k nodes to which you know the shortest path.
At each step, choose the node, which is not yet optimal, but which is closest to the source; i.e. the node to which the current calculated shortest path is smallest. Then, from it, try to optimise the path to every node connected to it. Finally, mark the said node as optimal (visited, if you prefer). In the previous example, the node which is closest to the source and is not yet optimal is the source. From it, you can optimise the path to nodes 2 and 4.

At this point, the only visited/optimal node is 0. Now we have to redo this step 4 more times (to ensure that all nodes are optimal).
The next node to consider is 4:

It’s worthwhile to note that at this step, we’ve also found a better path to node 2.
Next is node 2:

Finally, we look at nodes 5 and 3 (none of which offer any optimisations):


The actual code in C looks something like this:
void dijkstra(int s) {
int i, k, mini;
int visited[GRAPHSIZE];
for (i = 1; i <= n; ++i) {
d[i] = INFINITY;
visited[i] = 0; /* the i-th element has not yet been visited */
}
d[s] = 0;
for (k = 1; k <= n; ++k) {
mini = -1;
for (i = 1; i <= n; ++i)
if (!visited[i] && ((mini == -1) || (d[i] < d[mini])))
mini = i;
visited[mini] = 1;
for (i = 1; i <= n; ++i)
if (dist[mini][i])
if (d[mini] + dist[mini][i] < d[i])
d[i] = d[mini] + dist[mini][i];
}
}
The Programme
Putting the above into context, we get the O(n2) implementation. This works well for most graphs (it will not work for graphs with negative weight edges), and it’s quite fast.
Here’s the source code in C (dijkstra.c):
#include <stdio.h>
#define GRAPHSIZE 2048
#define INFINITY GRAPHSIZE*GRAPHSIZE
#define MAX(a, b) ((a > b) ? (a) : (b))
int e; /* The number of nonzero edges in the graph */
int n; /* The number of nodes in the graph */
long dist[GRAPHSIZE][GRAPHSIZE]; /* dist[i][j] is the distance between node i and j; or 0 if there is no direct connection */
long d[GRAPHSIZE]; /* d[i] is the length of the shortest path between the source (s) and node i */
void printD() {
int i;
for (i = 1; i <= n; ++i)
printf("%10d", i);
printf("\n");
for (i = 1; i <= n; ++i) {
printf("%10ld", d[i]);
}
printf("\n");
}
void dijkstra(int s) {
int i, k, mini;
int visited[GRAPHSIZE];
for (i = 1; i <= n; ++i) {
d[i] = INFINITY;
visited[i] = 0; /* the i-th element has not yet been visited */
}
d[s] = 0;
for (k = 1; k <= n; ++k) {
mini = -1;
for (i = 1; i <= n; ++i)
if (!visited[i] && ((mini == -1) || (d[i] < d[mini])))
mini = i;
visited[mini] = 1;
for (i = 1; i <= n; ++i)
if (dist[mini][i])
if (d[mini] + dist[mini][i] < d[i])
d[i] = d[mini] + dist[mini][i];
}
}
int main(int argc, char *argv[]) {
int i, j;
int u, v, w;
FILE *fin = fopen("dist.txt", "r");
fscanf(fin, "%d", &e);
for (i = 0; i < e; ++i)
for (j = 0; j < e; ++j)
dist[i][j] = 0;
n = -1;
for (i = 0; i < e; ++i) {
fscanf(fin, "%d%d%d", &u, &v, &w);
dist[u][v] = w;
n = MAX(u, MAX(v, n));
}
fclose(fin);
dijkstra(1);
printD();
return 0;
}
And here’s a sample input file(dist.txt):
10
1 2 10
1 4 5
2 3 1
2 4 3
3 5 6
4 2 2
4 3 9
4 5 2
5 1 7
5 3 4
The graph is given as an edge list:
- the first line contains e, the number of edges
- the following e lines contain 3 numbers: u, v and w signifying that there’s an edge from u to v of weight w
That’s it. Good luck and have fun. Always open to comments.
Finding the shortest path
UPDATE In response to campOs‘ comment.
Now we know the distance between the source node and any other node (the distance to the ith node is remembered in d[i]). But suppose we also need the path (which nodes make up the path).
Look at the above code. Where is d modified? Where is the recorded distance between the source and a node modified? In two places:
Firstly, d[s] is initialised to be 0.
d[s] = 0;
And then, when a new shortest path is found, d[i] is updated accordingly:
for (i = 1; i <= n; ++i) if (dist[mini][i]) if (d[mini] + dist[mini][i] < d[i]) d[i] = d[mini] + dist[mini][i];
The important thing to notice here is that when you update the shortest distance to node i, you know the previous node in the path to i. This is, of course, mini. This suggests the solution to our problem.
For every node i other than the source, remember not only the distance to it, but also the previous node in the path to it. Thus we have a new array, prev.
Now, we need to make to modifications.
First, we initialise the value of prev[i] to something impossible (say -1) at the start of dijkstra().
for (i = 1; i <= n; ++i) {
d[i] = INFINITY;
prev[i] = -1; /* no path has yet been found to i */
visited[i] = 0; /* the i-th element has not yet been visited */
}
Secondly, we update the value of prev[i] every time a new shortest path is found to i.
for (i = 1; i <= n; ++i)
if (dist[mini][i])
if (d[mini] + dist[mini][i] < d[i]) {
d[i] = d[mini] + dist[mini][i];
prev[i] = mini;
}
Good. For every node reachable from the source we know which node is just before it in the shortest path. For the above example, we would have the following array:
i - prev[i]
1 - -1
2 - 4
3 - 2
4 - 1
5 - 4
Using this, how do you get the path? Let’s say you want to get to 3. Which node comes right before 3? Node 2. Which node comes right before node 2? Node 4. Which node comes before 4? Node 1. We’ve reached the source, so we’re done. Go through this list backwards and you get the path: 1 -> 4 -> 2 -> 3. This is easily implemented with recursion.
void printPath(int dest) {
if (prev[dest] != -1)
printPath(prev[dest]);
printf("%d ", dest);
}
Here is the updated source: dijkstraWithPath.c.
Good luck.
2008-01-07 at 19:43
Hi, i have a direct question: how to include also the actual node path (from the specified source) along with the weight to the specific node?
tnx :)
2008-01-11 at 1:17
Nice work with this blog. The algorithms are well explained, with lots of examples. It’s quite useful for programmers. I wonder if you could also post a code which obtains the k-th shortest path in a weighted graph. I’m trying to work it out using Dijkstra algorithm, but to no avail so far. Keep up the good work. Cheers. :-h
2008-01-17 at 17:12
@campOs
Updated article. The function dijkstra() now also calculates the path, which can be printed with printPath().
Good luck.
2008-02-25 at 9:41
Can some one have the graphical implementation of dijkstra algo,Its quite difficult to implement it In C.
2008-02-29 at 12:28
hi…
thank you
:)
2008-03-09 at 5:14
Hi,
Can u send me a C program to find all possible routes
2008-03-20 at 13:10
Hi:
I’m interested in the performance.
therefore i need to implement it
using priority queues, do you have
the source code in c.
Regars,
Nathan
2008-04-27 at 12:34
Hello,
Logic and the code is very useful and easy to understand. I is helping me to greater extend. I have more than 100 nodes and I have start node and destination node, how do I specify the start node and destination node, how do I extract these are all the possible nodes for this process. Can you help me out.
Thanks
Bala
2008-08-05 at 9:52
@Bala
The start node is s, the first parameter to void dijkstra(int s).
In Dijkstra’s algorithm, there is no destination node, per se. The algorithm generates the shortest path to all nodes.
If you want the path from the source to a destination, use void printPath(int dest), where dest is the destination node.
2008-11-01 at 19:57
thx a lot.. You explain it very well
2008-11-10 at 17:46
hii……..nice work…..u help me through this in my sessionals…………
thnks alot……
2009-01-12 at 20:18
hi all….
How can i randomly generate cost matrices of size 10×10, 100×100, 1000×1000, and 10000×10000 and display
the run times ,to make input.txt file for input
if you give me some examples about it, will be great for me….
thanks……
2009-03-21 at 6:47
Hi the above code was really helpful.
I need a help from you can u send me the code for the following procedures
(1) compute the shortest paths from a given node to all other nodes
(2) print routing table at a given node, and
(3) print the shortest path between given src and dest in C.
i need to understand more in detail as my reasearch paper is mostly based on this.
thanks
2009-06-04 at 20:06
Hi!
I have one question. Is there any simply way to frezee out shortest path? Because i must find backup way for it and they must be completly diffrend. thanks
2009-06-11 at 16:24
Great work! Never before have I read such crystal clear descriptions of algorithms..not even from the classics like Sedgewick or Cormen!
2009-06-25 at 20:18
Hey thanks man. I’m curious though are you sure your algorithm works every time? I implemented it and it works for MOST cases but there are some that choose the incorrect path.
2009-08-01 at 13:42
If you are looking to further optimize the code, if you are, for example, looking to only fint the way from a single source to only 1 destination, you can stop execution at the moment your destination node gets marked as visited :)
to be precise, after this line: visited[mini] = 1;
i hope that was useful :) definetly is for me :)
PS: great algorithms man.
2009-09-23 at 19:26
to find a shortest path from a source node to a destination node in a random network:-
Read n,d and t. Create an output file (say output.txt)
Create n different nodes within the grid of dimension d. Store them in output.txt
Create the adjacency or link matrix. Store them in output.txt
Read a source and destination node from the user. Find the shortest path and output to output.txt.
Repeat the last step, until the user wishes to exit (here the option must be given to the user, either to give a source-destination pair or to exit the program)
can anyone solve tis one???
2009-10-07 at 22:14
hi
is there a way to write the code without using the file statements..or without using the .txt…like asking the user to give inputs…if yes cud u explain it ?
thanks
2009-10-22 at 10:53
scvalex,
Just a huge thanks for your work here. I used your guidance to help me fast track the porting of a PHP Dijkstra implementation of mine into C. You’ve saved me and I’m sure a lot of others a lot headaches.
Cheers,
JC
2009-11-02 at 21:02
Well explained algo.
A small bug perhaps, if we call dijkstra(2); instead of dijkstra(1); then there is a problem. It gives shortest path between 1 & 2 as 12 instead of correct 10. If someone can please check that.
2009-11-03 at 5:00
simply explained,but very effective :)
thx