aodv matlab,aodv matlab

function [path,cost]=hopbyhop(start,stop,admatrix)

%This function performs hop-by-hop routing by selecting the next hop with

%the highest trust value.

%the inputs of this function are:

%start =source node

%stop= destination node

%admatrix = adjacency matrix storing the trust values of the nodes

%the function returns the selected path and the average cost of the path

%the function is used as follows:

%[path,cost]=hopbyhop(start,stop,admatrix)

%NB:the function does not work correctly in some cases,further improvements

%will done in future.

%initialisation

noOfNodes = size(admatrix, 1);

rpath=[];

rpath(1)=start;

pathS=start;

parent=pathS;

cosy = 0;

[route]=hopsdij(pathS,stop,admatrix);%checking if path exists between source and destination node

if (pathS~=stop && route ==1)%main loop

k=1;%counter for neighbours

i=2;%path counter

while(pathS~=stop)

B=[];%vector of neighbours and trust values

jj=1;

for j= 1:noOfNodes

if admatrix(pathS, j)~=inf

B(jj,1)=j;

B(jj,2)=admatrix(pathS,j);

jj=jj+1;

end

end

B =sortrows(B,-2);

nexthop = B(k,1);%nexthop with the highest trust value

chk = searchp(rpath,nexthop);

if chk==1

[route]=hopsdij(nexthop,stop,admatrix);

if(route==0)

k=k+1;

else

rpath(i)=nexthop;

pathS=nexthop;

k =1;

i=i+1;

end

else

k = k+1;

end

end

else

disp('source =dest or cannot reach destination')

end

path =rpath;%selected path

cost=0;

for d=2:length(path)

cost= cost+admatrix(path(d-1),path(d));

end

cost=cost/length(path);%average cost of the path

%searching for visited nodes

function c =searchp(rp,np)

counter =0;

for z=1:length(rp)

if np==rp(z)

counter=counter+1;

end

end

if counter>0

c = 0;

else

c = 1;

end

function [route]=hopsdij(pathS,stop,admatrix)

if admatrix(pathS,stop)~=0 || admatrix(pathS,stop)~=inf

route=1;

else

route=0;

end

return

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值