dijkstra algorithm function problem

4 views (last 30 days)
yusuf
yusuf on 25 Dec 2022
Commented: Walter Roberson on 27 Dec 2022
the function is defined but I can't understand the problem

Answers (3)

yusuf
yusuf on 25 Dec 2022
  2 Comments
Walter Roberson
Walter Roberson on 25 Dec 2022
In your line
function [araba] = dijkstra1(G,1,7)
what is MATLAB supposed to do with the 1 and the 7? Is there some matching key telling MATLAB that inside the function, you can get to the 7 by naming a particular variable? It looks like you probably intend one to be a start node and the other to be an end node, but how is the function body supposed to know which is which and how is the function body to know how to refer to the values?
For example if the first thing in your function was intended to be something like testing whether G has at least as many rows and columns as the maximum of the start and end nodes, then you would not be able to code something like
maxnode = max(startnode, endnode);
if min(size(G)) < maxnode; error('G too small'); end
because startnode and endnode would be undefined.
yusuf
yusuf on 26 Dec 2022
Thank you very much for your reply. I just wanted to test the example written in the function of the algorithm. I got an error even though I did exactly the same

Sign in to comment.


Torsten
Torsten on 26 Dec 2022
Edited: Torsten on 26 Dec 2022
Works for me.
%---------------------------------------------------
% Dijkstra Algorithm
% author : Dimas Aryo
% email : mr.dimasaryo@gmail.com
%
% usage
% [cost rute] = dijkstra(Graph, source, destination)
%
% example
G = [0 3 9 0 0 0 0;
0 0 0 7 1 0 0;
0 2 0 7 0 0 0;
0 0 0 0 0 2 8;
0 0 4 5 0 9 0;
0 0 0 0 0 0 4;
0 0 0 0 0 0 0;
];
plot(digraph(G))
[e L] = dijkstra(G,1,7)
e = 15
L = 1×6
7 6 4 5 2 1
%---------------------------------------------------
function [e L] = dijkstra(A,s,d)
if s==d
e=0;
L=[s];
else
A = setupgraph(A,inf,1);
if d==1
d=s;
end
A=exchangenode(A,1,s);
lengthA=size(A,1);
W=zeros(lengthA);
for i=2 : lengthA
W(1,i)=i;
W(2,i)=A(1,i);
end
for i=1 : lengthA
D(i,1)=A(1,i);
D(i,2)=i;
end
D2=D(2:length(D),:);
L=2;
while L<=(size(W,1)-1)
L=L+1;
D2=sortrows(D2,1);
k=D2(1,2);
W(L,1)=k;
D2(1,:)=[];
for i=1 : size(D2,1)
if D(D2(i,2),1)>(D(k,1)+A(k,D2(i,2)))
D(D2(i,2),1) = D(k,1)+A(k,D2(i,2));
D2(i,1) = D(D2(i,2),1);
end
end
for i=2 : length(A)
W(L,i)=D(i,1);
end
end
if d==s
L=[1];
else
L=[d];
end
e=W(size(W,1),d);
L = listdijkstra(L,W,s,d);
end
end
function G = exchangenode(G,a,b)
%Exchange element at column a with element at column b;
buffer=G(:,a);
G(:,a)=G(:,b);
G(:,b)=buffer;
%Exchange element at row a with element at row b;
buffer=G(a,:);
G(a,:)=G(b,:);
G(b,:)=buffer;
end
function L = listdijkstra(L,W,s,d)
index=size(W,1);
while index>0
if W(2,d)==W(size(W,1),d)
L=[L s];
index=0;
else
index2=size(W,1);
while index2>0
if W(index2,d)<W(index2-1,d)
L=[L W(index2,1)];
L=listdijkstra(L,W,s,W(index2,1));
index2=0;
else
index2=index2-1;
end
index=0;
end
end
end
end
function G = setupgraph(G,b,s)
if s==1
for i=1 : size(G,1)
for j=1 :size(G,1)
if G(i,j)==0
G(i,j)=b;
end
end
end
end
if s==2
for i=1 : size(G,1)
for j=1 : size(G,1)
if G(i,j)==b
G(i,j)=0;
end
end
end
end
end
  31 Comments
Torsten
Torsten on 26 Dec 2022
And how do you measure the distance between points in the matrix ?
What are the elements of the matrix and what do they mean ?
Finding a shortest path between two points is not the travelling salesman problem.
Walter Roberson
Walter Roberson on 26 Dec 2022
Perhaps you are expecting something like the "cost_path" I added here.
Cost paths are ambiguous. You might be able to follow them to a point, but then you find a node in which there are two or more possible routes with the same cost, and at that point you can only figure out which of the routes was taken by looking further into the route and matching against the available costs, hypothesizing that perhaps you took at particular route until you either reach the goal or encounter a situation in which the hypothesis can no longer be continued. It is much better to have the node route list.
This might answer your question about using this Dijkstra code, but as Torsten points out, Traveling Salesman Problem is a different problem that cannot be solved by Dijkstra Algorithm.
%---------------------------------------------------
% Dijkstra Algorithm
% author : Dimas Aryo
% email : mr.dimasaryo@gmail.com
%
% usage
% [cost rute] = dijkstra(Graph, source, destination)
%
% example
G = [0 3 9 0 0 0 0;
0 0 0 7 1 0 0;
0 2 0 7 0 0 0;
0 0 0 0 0 2 8;
0 0 4 5 0 9 0;
0 0 0 0 0 0 4;
0 0 0 0 0 0 0;
];
plot(digraph(G))
[e, L] = dijkstra(G,1,7)
e = 15
L = 1×6
7 6 4 5 2 1
visited_in_order = fliplr(L)
visited_in_order = 1×6
1 2 5 4 6 7
cost_path = G(sub2ind(size(G), visited_in_order(1:end-1), visited_in_order(2:end)))
cost_path = 1×5
3 1 5 2 4
%---------------------------------------------------
function [e L] = dijkstra(A,s,d)
if s==d
e=0;
L=[s];
else
A = setupgraph(A,inf,1);
if d==1
d=s;
end
A=exchangenode(A,1,s);
lengthA=size(A,1);
W=zeros(lengthA);
for i=2 : lengthA
W(1,i)=i;
W(2,i)=A(1,i);
end
for i=1 : lengthA
D(i,1)=A(1,i);
D(i,2)=i;
end
D2=D(2:length(D),:);
L=2;
while L<=(size(W,1)-1)
L=L+1;
D2=sortrows(D2,1);
k=D2(1,2);
W(L,1)=k;
D2(1,:)=[];
for i=1 : size(D2,1)
if D(D2(i,2),1)>(D(k,1)+A(k,D2(i,2)))
D(D2(i,2),1) = D(k,1)+A(k,D2(i,2));
D2(i,1) = D(D2(i,2),1);
end
end
for i=2 : length(A)
W(L,i)=D(i,1);
end
end
if d==s
L=[1];
else
L=[d];
end
e=W(size(W,1),d);
L = listdijkstra(L,W,s,d);
end
end
function G = exchangenode(G,a,b)
%Exchange element at column a with element at column b;
buffer=G(:,a);
G(:,a)=G(:,b);
G(:,b)=buffer;
%Exchange element at row a with element at row b;
buffer=G(a,:);
G(a,:)=G(b,:);
G(b,:)=buffer;
end
function L = listdijkstra(L,W,s,d)
index=size(W,1);
while index>0
if W(2,d)==W(size(W,1),d)
L=[L s];
index=0;
else
index2=size(W,1);
while index2>0
if W(index2,d)<W(index2-1,d)
L=[L W(index2,1)];
L=listdijkstra(L,W,s,W(index2,1));
index2=0;
else
index2=index2-1;
end
index=0;
end
end
end
end
function G = setupgraph(G,b,s)
if s==1
for i=1 : size(G,1)
for j=1 :size(G,1)
if G(i,j)==0
G(i,j)=b;
end
end
end
end
if s==2
for i=1 : size(G,1)
for j=1 : size(G,1)
if G(i,j)==b
G(i,j)=0;
end
end
end
end
end

Sign in to comment.


Walter Roberson
Walter Roberson on 26 Dec 2022
Suppose you have an N x 2 cell array P, with a row P(K,:) representing information about a potential path to take. P{K,1} is the total cost encountered so far in the K'th proposed route, and P{K,2} is a list of nodes that have been visited already.
Initialize P{1,1} = 0 (you got to the starting node for free) and P{1,2} = starting node -- because traveling salesman must visited every node, this might as well be the first node.
At any time:
  1. Extract the first row in P into separate variables, such as a current cost and a current route
  2. delete the first row from P
  3. if the current route includes all of the nodes ending back at the original location, then save the current cost and the current route as one of the Traveling Salesmen paths. (If you want, you could prune it immediately if the cost is greater than the cost associated with the best path you have found so far.) In this case, after recording the path (or throwing it away), there is no need to queue any further possible steps for this path since you already got t
  4. examine the cost matrix G row associated with the node given by the last entry in the current route
  5. for each non-zero cost you find there, determine whether the target node (column number) already appears in the current route. If so then move on to the next non-zero entry -- with the exception that if the target node is the start node and you have visited all of the other nodes, then this is a solution and you should queue it as described in the next step
  6. for each entry that was not already in the current route, add a new row to P, with the cost being the current cost plus the value from the cost matrix for that row and column, and with the route being the current route followed by the node number of the target (the column number).
  7. By now you have removed this potential path and replaced it (end of P) with all possible cases of taking one further step after that potential path.
  8. If there were no non-zero entries that lead to places you have not already visited, then you reached a dead end and the potential path does not need to be considered further. Which you do not need to do anything special for, as it just means that you did not queue any further potential paths from here. At any given time, P contains only paths that have worked so far, as far as they have gone
  9. loop back to step 1 if P is not empty
  10. you now have either saved all possible solutions, or at least the ones that are lowest cost. Output them.
  2 Comments
yusuf
yusuf on 27 Dec 2022
the operations you mentioned are valid only on the distance matrix, right?
Walter Roberson
Walter Roberson on 27 Dec 2022
Do you have some other kind of matrix that you need the code to work with?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!