i cant get the code to give an answer. it just runs in an inf loop i believe
5 views (last 30 days)
Show older comments
% dijkstra code
clc
clear
pause on
% [DIST, PATH] = dijkstrateam(source,dest)
key=('abcdefghijklmnop');
source=14
dest=1
%coord of points (x,y)
a=[132,344];
b=[159,72];
c=[296,72];
d=[296,554];
e=[159,554];
f=[384,76];
g=[567,76];
h=[567,318];
i=[524,371];
j=[524,504];
k=[388,466];
l=[589,298];
m=[508,318];
n=[488,371];
o=[388,504];
p=[384,144]; c
pos=0;
frompos=0;
load('carray.mat')
pointarray=[a;b;c;d;e;f;g;h;i;j;k;l;m;n;o;p];
disarray=zeros(16);
totarray=[1:16]
for hhh=1:16
totarray(hhh,1:16)=inf
end
satisfied=0
%set distance to array to inf
for hhh=1:16
disarray(hhh,1:16)=inf
end
%create closed list
closed=[1:16]
for hhh=1:16
closed(1,hhh)=0
end
% [0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0;1,0,1,0,1,0,0,0,0,0,0,0,1,1,1,0;0,1,0,1,0,1,0,0,1,0,1,0,1,1,1,1;0,0,1,0,1,1,0,1,0,0,1,1,1,1,1,1;1,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0;0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,1;0,0,1,0,0,1,0,1,1,1,0,1,0,0,0,1;0,0,0,1,0,0,1,1,1,1,1,1,1,1,0,0;0,0,1,0,0,0,1,1,0,1,0,1,1,1,0,1;0,0,0,1,1,0,1,1,1,0,0,1,0,0,1,0;0,0,1,1,0,0,0,1,0,0,0,1,1,1,1,1;0,0,0,1,0,0,1,1,1,1,0,0,0,1,0,0;0,0,1,1,0,0,0,1,1,0,1,0,0,1,0,1;0,0,1,1,0,0,0,1,1,0,1,1,1,0,0,1;0,0,1,1,1,0,0,0,0,1,1,0,0,0,0,0;0,0,1,1,0,1,0,0,1,0,1,0,1,1,1,0;];
for kk=1:16
frompos=frompos+1;
for k =1:16
pos =pos+1;
% r= array(11)
% s=array(pos:1)
z=(pointarray(frompos,1)-pointarray(pos,1)).^2;
y=(pointarray(frompos,2)-pointarray(pos,2)).^2;
x=sqrt(z+y);
if carray(frompos,pos)==1
disarray(frompos,pos)=x;
end
end
if pos==16
pos=0;
end
end
%find the amount of nodes
nodes=length(disarray);
% %open list
% open=(1:nodes-1)
% %place source in closed list
% closed(1)=source
%put all other nodes in open list
for vv =1:nodes %CREATE OPEN ARRAY
open(vv)=vv
end
for go = 1:(nodes) %PLACE ALL NONE SOURCE NODES IN OPEN AND SOURCE IN CLOSED
if open(go)~= source
else
close(1)=open(go)
open(go)=0
end
end
pos=source
for counter=1:nodes
row=pos
temparray=disarray(row,1:end) %pull source node from distance array
% for mmm= 1:nodes
% if temparray(mmm)~=inf %dont think this is needed
% holdarray(mmm)=temparray(mmm)
% end
for compcount =1:nodes %add distances to node to current distance
rowToNewNode= temparray(1,compcount)%distance from current node to other nodes
DistToRowNode=totarray(1,pos) %distance to current node from source node
if totarray(1,compcount) > (rowToNewNode +DistToRowNode) %if distance is shorter than current replace
totarray(1,compcount)=(rowToNewNode+DistToRowNode)
end
end
satisfied =0
while satisfied == 0
sortarray=sort(temparray)
[rr,cc]=find(temparray==sortarray(1)) %find the closest node
dd=find(close == cc) %
if cc ~= dd %
satisfied = 1
close(counter) = cc %place new node in the close list
else
temparray(cc)=inf %if already in the close list change to inf and look at the next shortest distance
if find(temparray ~= inf) == false %if all nodes are done then stop
break
end
end
end
end
4 Comments
Geoff Hayes
on 20 Apr 2014
Have you been able to identify where the code is getting stuck? What is the problem to be solved by the above code?
Answers (1)
Walter Roberson
on 20 Apr 2014
That does not appear to be straight Dijkstra code. The reason for marking with "inf" is not obvious. It appears to me that you are perhaps trying to prevent backtracking to where you just came from, but instead you are preventing the search from ever returning to the same column even if in a different row.
Dijkstra's algorithm is usually most easily represented using matrix multiplication. When T is the transition matrix, T^n represents where you can get to in n steps. If you are looking to get from node A to node B,
Tn = T^n;
if Tn(A,B) > 0
%there is a route that is n steps long
end
you would do this from n = 1 to n = number of nodes -- but stopping if Tn ever goes all 0.
2 Comments
See Also
Categories
Find more on Dijkstra algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!