how to solve Dijkstra algorithm in MATLAB?
6 views (last 30 days)
Show older comments
I used the command “graphshortestpath” to solve “Dijkstra”. I am not getting the correct answer as the output is concentrating on the reduction of nodes alone. I need to follow the path of reduced distance(between 2 nodes) alone.
My coding is for 24 nodes.
clc;
clear all;
close all;
W = [2.10 1.65 2.20 1.50 1.55 2 1.50 1.05 1.75 1.75 1.75 1 1 0.4 1.25 0.75 1.50 1.75 2 2 1.75 0.65 1.05 1.75 0.45 2.75 1.75 1.75 0.5 1.05 0.75 1 1.50 1.25 1 0.75 0.75 0.5 0.5 0.75 0.5];
DG = sparse([1 1 1 1 1 2 2 2 3 3 4 4 4 4 5 5 6 6 7 7 8 8 8 9 9 10 10 11 12 16 16 17 17 18 19 19 20 21 23 24 25],[2 3 4 16 19 5 6 17 6 7 7 8 12 24 9 18 9 10 10 11 7 22 23 10 25 13 14 13 21 2 17 5 18 15 4 20 12 22 11 3 14],W);
g=DG;
names=W;
h = view(biograph(DG,[],'ShowWeights','on'))
s=1;
d=input ('Enter the Destination node : ');
[dist,path,pred]=graphshortestpath(DG,s,d,'Directed',true,'method','Dijkstra');
set(h.Nodes(path),'Color',[1 0.4 0.4]);
edges = getedgesbynodeid(h,get(h.Nodes(path),'ID'));
set(edges,'LineColor',[1 0 0])
set(edges,'LineWidth',1.5)
For the path node 1- node10, manual output is 1-16-2-6-10 But, I got the output as 1-3-6-10.
Please help me.
0 Comments
Answers (2)
Lucio Cetto
on 24 Sep 2012
I get the distance of 1-16-2-6-10 to be 5.8, while 1-3-6-10 is 5.15. HTH
0 Comments
Muhammad awan
on 14 Nov 2013
I am getting the same as previous comment above, there could be some mistake in manual solution that you attempted. Because even by looking up on your network its obvious that SP is 1-3-6-10.
0 Comments
See Also
Categories
Find more on Dijkstra algorithm in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!