Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
About graphshortestpath

Subject: About graphshortestpath

From: Poornima

Date: 22 Jul, 2013 21:19:10

Message: 1 of 1

Hi,
I am a novice to Matlab and I tried finding the shortest path between two random nodes. I am getting an error in line16 where I use the graphshortestpath function. Can anybody help me by telling what is wrong?

The exact error displayed is as follows:
??? Error using ==> graphalgs
Input argument should be a sparse array.

Error in ==> graphshortestpath at 136
        [dist,pred] = graphalgs(algorithmkeys{algorithm},debug_level,directed,G,S);

The code follows:
___________________________________________________
% draw a network graph and find shortest path using dijkstras algo

clear all;
close all;
%generate lower triangular matrix with a max value of 5
G=tril(randi(5,5),-1);

ng=biograph(G); %draw network of graph

%find the shortest path from node s to d

s=1; d=1;

while(s==d)
s=ceil((rand(1)*10)/2); %since the node ranges are 1 to 5, reduce the result to this range
d=ceil((rand(1)*10)/2);
end
    
h = view(biograph(G,[],'ShowArrows','Off','ShowWeights','on'));
    
%%%THE NEXT LINE CAUSES AN ERROR and I am stuck from this point on...:(
    [dist1, path1, pred1] = graphshortestpath(G,s,d);
    
       
 set(h.Nodes(path1),'Color',[1 0.4 0.4])
 fowEdges = getedgesbynodeid(h,get(h.Nodes(path1),'ID'));
 revEdges = getedgesbynodeid(h,get(h.Nodes(fliplr(path1)),'ID'));
 edges = [fowEdges;revEdges];
 set(edges,'LineColor',[1 0 0]);
 set(edges,'LineWidth',1);
    

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us