Dijkstra's Algorithm - how to calculate the shortest path (having a state matrix)

9 次查看(过去 30 天)
Hello, I am slowly learning Matlab and now have assignment with shortest path using Dijkstra's algorithm.
I got the following code, which is saved as "dijkstra.m"
clear, clf
format short
%
% Number of nodes in network
Nodes=8;
%
% Creating the adjacency matrix
Distance_Matrix=[ 0 20 0 0 0 0 15 0 ; ...
20 0 8 9 0 0 0 0 ; ...
0 8 0 6 15 0 0 10 ; ...
0 9 6 0 7 0 0 0 ; ...
0 0 15 7 0 22 18 0 ; ...
0 0 0 0 22 0 0 0 ; ...
15 0 0 0 18 0 0 0 ; ...
0 0 10 0 0 0 0 0 ];
%
% Create the state matrix
State_Matrix=[ Inf 0 0 ; ...
Inf 0 0 ; ...
Inf 0 0 ; ...
Inf 0 0 ; ...
Inf 0 0 ; ...
Inf 0 0 ; ...
Inf 0 0 ; ...
Inf 0 0 ];
%
% Define the coordinates of the nodes.
Coord_Matrix=[ 0 5 ; 1 0 ; 5 1 ; 2 4 ; 3 6 ; 0 7 ; 4 8 ; 6 2 ];
%
% Drawing the network.
text(Coord_Matrix(1,1), Coord_Matrix(1,2), '1'),hold on
text(Coord_Matrix(2,1), Coord_Matrix(2,2), '2'),hold on
text(Coord_Matrix(3,1), Coord_Matrix(3,2), '3'),hold on
text(Coord_Matrix(4,1), Coord_Matrix(4,2), '4'),hold on
text(Coord_Matrix(5,1), Coord_Matrix(5,2), '5'),hold on
text(Coord_Matrix(6,1), Coord_Matrix(6,2), '6'),hold on
text(Coord_Matrix(7,1), Coord_Matrix(7,2), '7'),hold on
text(Coord_Matrix(8,1), Coord_Matrix(8,2), '8'),hold on
%
for i=1:Nodes
for j=1:(i-1)
if Distance_Matrix(i,j)~=0
Graphx=[Coord_Matrix(i,1) Coord_Matrix(j,1)];
Graphy=[Coord_Matrix(i,2) Coord_Matrix(j,2)];
plot(Graphx,Graphy,':'),hold on
end;
end;
end;
%
% Ask for start and finish point
startPoint=input('Start point is: ');
endPoint=input('End point is: ');
%
axis('equal');
axis('off');
Now I need to compute the shortest path, while also to give the the first point and the end point, so that the program would calculate the shortest route and should display the total distance.
I have seen the video on YouTube and tried to insert it in the same script, however, the program ignores it and only draws the network and initial code and asks for start and end points, but the loops below are ignored:
% Shortest path
function distances = dijkstra1(Distance_Matrix, State_Matrix)
distances = dijkstra1(Distance_Matrix,1);
% Start with a map of distances among Nodes
%Initialize the distance to all points as infinity
distances(1:Nodes) = Inf;
% Mark nodes as UNvisited
visited(1:Nodes) = 0;
% Initialize the distance to the first point as zero.
distances(State_Matrix) = 0;
while sum(visited) < Nodes
% Find all unvisited nodes
possibles(1:Nodes) = Inf;
for index = 1:Nodes
if visited(index) == 0
possibles(index) = distances(index);
end
end
% Then find the one with the smallest distance value
% Make this the current point, and its distance the current
% distance.
[currentDistance, currentPoint] = min(possibles);
% Given the distance to the current point, update the distance to
% all its neighbors if the new distance will be smaller
for index = 1:Nodes
newDistance = currentDistance + Distance_Matrix(currentPoint, index)
if newDistance < distances(index)
distances(index) = newDistance;
end
end
% Mark the current node as visited
visited(currentPoint) = 1;
end
% End
end
Could someone please tell me where is the mistake? Any hints?
Many thanks in advance.
  14 个评论
Dan Kristen
Dan Kristen 2022-10-17
Thanks Torsten!
seems challenging even just to understand this code.
I got the errors though: W - is adjacency matrix?
how it is then expressed in the rest of the code? because later in the code it is expressed as A?
Not enough input arguments.
Error in dijk1 (line 2)
[dist, P] = dijk_(W, start_verts, end_verts);
>> dijk1
Not enough input arguments.
Error in dijk1 (line 5)
[n,cA] = size(A);
Torsten
Torsten 2022-10-17
how it is then expressed in the rest of the code? because later in the code it is expressed as A?
In each function, the variables names can change for the input data from those of the calling function.
I don't know which changes you made to the input matrix A. As you can see above, the code works with the cost matrix you defined.
All information about the code is on the page I gave you the link to:
Syntax
[dist, paths] = DIJK(A, start_verts, end_verts);
Inputs
A : (n,n) node-node weighted adjacency matrix of arc lengths.
A(i,j) = 0 => Arc (i,j) does not exist;
A(i,j) = NaN => Arc (i,j) exists with null weight.
start_verts : FROM node indices; default: s=[], ie. paths from all nodes.
end_verts : TO node indices; default: s=[], ie. paths to all nodes.
Outputs
D : matrix of size (length(s),length(t)) storing the shortest path distances from s to t: D(i,j) is the distance from node i to node j.
P : shortest paths.
Remarks
  • If A is a triangular matrix, then computationally intensive node selection step not needed since graph is acyclic (triangularity is a sufficient, but not a necessary, condition for a graph to be acyclic) and A can have non-negative elements.
  • If length(s)>>length(t), then DIJK is faster if DIJK(A',t,s) used, where D is now transposed and P represents successor indices.
References
[AMO93] R. Ahuja, T. Magnanti and J. Orlin: "Network Flows: Theory, Algorithms, and Applications", Prentice-Hall, 1993.
[DIJK] Source code made available by Michael G. Kay (see copyright) at http://web.mit.edu/cocosci/isomap/code/dijk.m http://cbi.nyu.edu/svn/mrTools/trunk/mrLoadRet/ROI/pred2path.m
Acknowledgment
Copyright (c) 1994-2006 by Michael G. Kay Matlog Version 9 13-Jan-2006 (http://www.ie.ncsu.edu/kay/matlog)

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dijkstra algorithm 的更多信息

产品


版本

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by