Too many input arguments

I'm doing a Dijkstra' Algorithm.
And I have 2 scripts:
-In this one I have the following code:
%---------------------------------------------------
% 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;
% ];
% [e L] = dijkstra(G,1,7)
%---------------------------------------------------
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
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;
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
And in the other script, I have the following code:
datos = xlsread('map.xlsx');%Reads the data from a file
[e, L] = dijkstra(datos, 1, 12);
Which have the following data (matrix):
But when I run it (in the second script), it appears the following error:
Can you please help me on what should I do?

16 comentarios

dpb
dpb el 13 de Nov. de 2021
I put your function in a file named dijkstra.m and ran it with some random data --
> dijkstra(randi(10,7,7),1,7)
Unrecognized function or variable 'setupgraph'.
Error in dijkstra (line 25)
A = setupgraph(A,inf,1);
>>
which did not produce the error you have above.
Hence, one can conclude the above code didn't produce the error; perhaps you've got another test copy around somewhere first in the path (like the local working directory while the real one is somewhere on the path and is thus aliased).
What does
which -all dijkstra
return?
lol
lol el 13 de Nov. de 2021
Oh yes, now I have the same error and where do I put
which -all dijkstra
this?
Star Strider
Star Strider el 13 de Nov. de 2021
My hypothesis is that xlsread is producing a cell array rather than a numeric array, and that having the file (or a representative sample) could provide the reason and lead to a solution. I believe the file is the problem.
.
lol
lol el 13 de Nov. de 2021
I put it there and gives this error:
lol
lol el 13 de Nov. de 2021
I change "xlsread" for readmatrix but it stills has an error, which is the above one
Star Strider
Star Strider el 13 de Nov. de 2021
Find and download all the required functions to run the ‘dijkstra’ function.
lol
lol el 14 de Nov. de 2021
Sorry, how do I do that?
Star Strider
Star Strider el 14 de Nov. de 2021
Probably find it the same place the other file was.
However the real problem still appears to be the file. We can’t help with it if we don’t have it to work with.
Please upload it.
lol
lol el 14 de Nov. de 2021
yes! thanks, here it is
lol
lol el 14 de Nov. de 2021
and here is the code and the scripts
The file is not the problem.
Please find ‘setupgraph.m’ and any others, and post them here.
datos = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/799369/map.xlsx')
datos = 12×12
0 2 11 0 16 0 0 0 0 0 0 0 0 0 0 11 8 0 0 0 0 0 0 0 11 0 0 0 0 3 0 0 12 0 0 0 0 0 0 0 2 0 5 0 0 7 0 0 0 0 0 0 0 0 4 2 0 0 0 0 0 0 3 0 7 0 0 0 9 0 0 0 0 0 0 0 0 0 0 3 0 1 6 0 0 0 0 0 0 0 3 0 0 0 17 9 0 0 0 0 0 0 0 14 0 0 0 7 0 0 0 0 0 0 0 0 0 0 12 0
Graph = datos;
source = 1;
destination = 12;
[cost rute] = dijkstra(Graph, source, destination)
Unrecognized function or variable 'setupgraph'.

Error in solution>dijkstra (line 32)
A = setupgraph(A,inf,1);
%---------------------------------------------------
% 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;
% ];
% [e L] = dijkstra(G,1,7)
%---------------------------------------------------
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
.
dpb
dpb el 14 de Nov. de 2021
My conclusions are the same as before -- I downloaded and ran -- the problem is missing other functions required, but nothing wrong with the input to the function itself. The error you're getting is NOT from this incarnation of the function.
>> whos datos
Name Size Bytes Class Attributes
datos 12x12 1152 double
>>
datos is returned as double array whether use xlsread or readmatrix
>> untitled5
Unrecognized function or variable 'setupgraph'.
Error in dijkstra (line 25)
A = setupgraph(A,inf,1);
Error in untitled5 (line 3)
[e, L] = dijkstra(datos, 1, 12);
>>
lol
lol el 14 de Nov. de 2021
@Star Strider and where do I put this?
lol
lol el 14 de Nov. de 2021
And the only setupgraph I found, is in this part of the code:
function [e L] = dijkstra(A,s,d)
if s==d
e=0;
L=[s];
else
A = setupgraph(A,inf,1); %HERE IS ONE setupgraph
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
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;
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
which -all dijkstra
And then what do I have to do? please @Star Strider
lol
lol el 14 de Nov. de 2021
@dpb and how do I fix it?
dpb
dpb el 14 de Nov. de 2021
Editada: dpb el 15 de Nov. de 2021
As @Star Strider said, go to whomever/wherever it was you got these codes and ask/look for the missing routines there; we don't have any way to know anything about that.
The line
A = setupgraph(A,inf,1); %HERE IS ONE setupgraph
is trying to call the function called setupgraph; you need that function as well (plus any others that may be referenced later on in the code or called by it, of course).
We can't help with that...you got this code from somewhere else, there's the place to go for help with it.

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 14 de Nov. de 2021

0 votos

Do you need to implement Dijkstra yourself (as part of a homework assignment or school project) or do you just need to call some implementation of it? If the latter build a graph or digraph object from your data then call the shortestpath function on it. One of the methods available for use by the shortestpath function is the 'positive' algorithm whose description states it is "Dijkstra algorithm that requires all edge weights to be nonnegative."

Etiquetas

Preguntada:

lol
el 13 de Nov. de 2021

Editada:

dpb
el 15 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by