Wednesday, October 1, 2008

Dijkstra's algorithm for MatLab

යාන්ත‍ම් පො‍ඩි විවේකයක් ලැබුනා. මොනව හරි ලියන්න කියල හිත හිතා ඉන්නකොට මම ඊයෙ ලියපු Matlab code එකක් මතක් උනා.
පහත දැක්වෙන්නේ Dijkstra Algorithm එකයි. කාට හෝ මේක ප්‍රයෝජනවත් වේ යැයි මම හිතනවා. අවුරුදු ගානකට කලින් මෙම ඇල්ගොරිතම දැනන් හිටියට, මේක කොයි තරම් වෙනස් ආකාරයේ ගැටළු සදහා යොදාගත හැකිද යන්න මට වැටහුනේ මෑතකදී.

Following is an implementation of the Dijkstra's Algorithm for Matlab. This algorithm is a solution for the 'single source shortest path problem', which is useful in many applications.

This code can be pasted in a new .m file in Matlab. Then this can be used as a function.

Format of the Inputs:

src: and integer defining the source node.
ConMat: Connection Matrix, a square matrix. Each element (i,j) of the matrix defines the length of a direct link from node i to node j (if there exists a link). If there is no link from i to j, the element (i,j) must contain -1.

Output format:

The output is given in an array (and) of size N, where N is the number of nodes in the network. Each element of the output array corresponds to a Node with same index. Each element will contain the index of the node which is the closest to the corresponding node, in the shortest path to the source. The element corresponding to source node would contain -1.

Eg. If the source node is 2, and the shortest path from 2 to 7 is 2-->4-->7, then the output array would contain | x |-1| x | 2 | x | x | 4 |.
As seen, the 7th element directs us to 4 th node, which inturn directs us to node 2. Similary, the shortest path to each other node from the source node could be extracted from the output.


%An implementation of the Dijkstra's algorithm for MatLab
%(c)Buddhika Gunawardena

function ans = Dijkstra(src,ConMat)

N = size(ConMat,1);
Visited = -1*ones(N,1); % Here -1 means Not defined
PrevNode = -1*ones(N,1);
Distance = Inf*ones(N,1);

Distance(src) = 0;
CurrentNode = src;
nVisited = 0;
while (nVisited <N)
Visited(CurrentNode) = 1;
for i=1:N
if (ConMat(CurrentNode,i)>0)
temp = ConMat(CurrentNode,i) + Distance(CurrentNode);
if (temp< Distance(i))
Distance(i) = temp;
PrevNode(i) = CurrentNode;
end
end
end
minimum = 0;
for i= 1:N
if (Visited(i)<0)&&(Distance(i)>0)
if (minimum ==0)
minimum = i;
elseif (Distance(i)<Distance(minimum))
minimum = i;
end
end
end
CurrentNode = minimum;

nVisited = nVisited +1;
end

ans = PrevNode;



6 comments:

Unknown said...

Hey Buddhika,

My name is Harsh. I am doing an assignment where I need to use Dijkstra's algorithm. I was trying to program it myself and was going to use your code as my aide as the inputs are exactly as my professor has defined them. The only difference is that in your Conmat, "If there is no link from i to j, the element (i,j) must contain -1." while in the Conmat we will be provided, If there is no link from i to j, the element (i,j) must contain Infinity. If that is my new Conmat, how would your code be changed. I would really appreciate it if you could help me out. My email is harsh_bmw2000@yahoo.com Thanks buddy!

rag said...

Hi Buddika,
i am raghavendra,i doing M-tech,in india, i have node to node distance in excel sheet,i have to import exceel sheet and have to find cost matrix,using cost matrix need to find minum distance from source to destnation....I would really appreciate it if you could help me out.My email is aeroraghava@gmail.com

JosRico said...

Any input example? I really can't make it run.

I am using this matrix, but it's not working...
A = [1 2; 3 4]

JosRico said...

Any input example? I really can't make it run.

I am using this matrix, but it's not working...
A = [1 2; 3 4]

Unknown said...

??? Input argument "ConMat" is undefined.

Error in ==> Dijkstra at 6
N = size(ConMat,1);

kindly help me with this error while running the code, my email id is gauravkhanna88@gmail.com

Buddhika said...

Sorry guys, I am currently not updating this blog. It's been a long time since I wrote some matlab code. This was done during my studies. So I can hardly remember. Anyway, by reading my own writing, I might be able to answer you.

@Harsh: In programming, easiest is to define infinity as -1..
@rag: Sorry mate, I am unable to spend time on it, but there will be plenty of online guides on how to import an excel to matlab.
@JosRico: Your input matrix is not in the correct format.
@Gaurav: Please define input variables and feed it. Use my code just as a function to your main program

Following is the input format:

src: and integer defining the source node.
ConMat: Connection Matrix, a square matrix. Each element (i,j) of the matrix defines the length of a direct link from node i to node j (if there exists a link). If there is no link from i to j, the element (i,j) must contain -1.


Read abt square matrices and connection matrices. I am sure it's a common term.

Hope that helps. And thanks for reading my blog.