All pastes #288958 Raw Edit

shortest path algo

public text v1 · immutable
#288958 ·published 2006-12-22 19:02 UTC
rendered paste body
% Taking number of input nodes from user
n=input('Please enter the number of nodes less than 26 = ');

% Checking for bad input
if n > 26
    error('Plzz follow the instructions ')
end

% Defining node numbers interms of Alphabets
alphabets= char(65:1:65+n);

adj=zeros(n,n);
for p=1:n
    for q=1:n
        adj(p,q)=input(['Enter Weight between [',alphabets(p),']','[',alphabets(q),'] = ']);
    end
end

%Coversting status of all nodes to Tentative
for p=1:n
    a(p).status=0;
end

temp=[];
acc=[];

% Taking input from user regarding source & destination nodes

s=input('Please enter the source node = ');
t=input('Please enter the destination node = ');

% converting source node to status PERMANANT
a(s).status=1; 
path=s;

% if (adj(s,t)==0)
%     disp('Source and Destination are Same');
%     continue
% end


% Main algorithm of Greedy Dijkstra
for k=1:inf
for h=1:n
 if a(h).status==0   % If Unknown 
     temp=[temp adj(s,h)];
 else
     temp=[temp inf];
 end
end
[aa,bb]=min(temp);
acc=[acc aa];
temp=[];
s=bb;
a(s).status=1;
path=[path s];
if s==t
    break
end
end

% Creating dialogue box for Output
helpdlg(['Path : ' alphabets(path) '  Total Cost/Weight : '...
        num2str(sum(acc))],'Greedy Dijkstra Algorithm')