Saturday, June 25, 2011

Shortest Path Routing [Matlab]


clc;
clear all;
n=input('enter the number of nodes: ');
conn=input('enter the connected nodes: ');
delarr=ones(n);
delarr=10000*delarr;
len=length(conn);
delay2=randint(1,(len/2),[10 50]);
k=1;
for i=1:2:len;
    delarr(conn(i),conn(i+1))=delay2(k);
    delarr(conn(i+1),conn(i))=delay2(k);
    k=k+1;
end
for i=1:n
    delarr(i,i)=0;
end
disp('....Delay time.....');
disp(delarr);
gr=delarr;
v=[1:n];
p=perms(v);
start=input('enter the source node: ');
dest=input('enter the destination node: ');

%finding all possible paths and thier delays
paths=zeros(factorial(n-1),n);
index=0;
for i=1:factorial(n);
    if p(i,1)~=start;
        continue;
    end
    de=0;
    temp=p(i,1);
    index=index+1;
    paths(index,1)=temp;
    for j=2:n;
        if(gr(temp,p(i,j))>0 && gr(temp,p(i,j))<1000);
            flag=1;
            de=de+gr(temp,p(i,j));
            temp=p(i,j);
            paths(index,j)=temp;
            if temp == dest
                delay(index)=de;
                break;
            end
        else
            flag=0;
            index=index-1;
            break;
        end;
    end;
end;
%removing the repeated paths
j=1;
delay1(j)= delay(1);
paths1(j,:) = paths(1,:);
for i=2:length(delay);
    if delay(i-1)~=delay(i);
        j=j+1;
        delay1(j)=delay(i);
        paths1(j,:)=paths(i,:);
        shortind=j;
    end;
end;

%displaying all paths and also the delay
for i=1:length(delay1);
    for j=1:n;
        if paths1(1,j)~=0;
            fprintf('%d->',paths1(i,j));
        else
            break;
        end;
    end;
    fprintf('\b\b:delay=%d\n',delay1(i));
end;



temp=min(delay1(1));

for i=1:length(delay1)
    if delay1(i)==temp
        break;
    end
end

disp('');
for j=1:n
    if paths1(i,j)~=0
       
    fprintf('%d->',paths1(i,j));
    end
end
fprintf('\b\b\t\t delay=%d\n',delay1(i));

Post a Comment

Google : The top most search engine

Google : MAGIC BOX

nRelate - Posts and Homepage

LinkWithin

Related Posts Plugin for WordPress, Blogger...

Which is the toughest subject ?