mh.cpp

来自「传感器网络的可靠路由算法」· C++ 代码 · 共 104 行

CPP
104
字号
#include "stdafx.h"
#include "Network.h"

/* global variables */
extern NODE_TYPE nodeT[4000];
extern NETWORK_TYPE net;

void BuildMH(int root){

int top,i;
queue <int> flood;


SetE2ECost(root, root, 0,root);
nodeT[root].floodsend =1;
flood.push(root);

while (!flood.empty()){

	top = flood.front();

	nodeT[top].inqueue = 0;
	flood.pop();

	for (i=0;i<nodeT[top].neighbornum;i++) 
	  { 
		  //get-rid of asymetric links
		if (unidirectional(top, nodeT[top].neighbor[i])==true){
			//printf("No connection between %d and  %d\n",top,nodeT[top].neighbor[i] );
			continue;
        }
		
		if (nodeT[nodeT[top].neighbor[i]].floodsend == 0){
			  
			 /* nodeT[top] send out control message and its neighbors receive it */
			 flood.push(nodeT[top].neighbor[i]);
			 nodeT[nodeT[top].neighbor[i]].inqueue = 1;
         					   
			 
			 SetNextHop(nodeT[top].neighbor[i], root, top);
			 nodeT[nodeT[top].neighbor[i]].floodsend =1;
		  }

	}
}
assert(flood.empty());

}


void BuildMH_A(int root){

int top,i;
queue <int> flood;


SetE2ECost(root, root, 0,root);
nodeT[root].floodsend =1;
flood.push(root);

while (!flood.empty()){

	top = flood.front();

	nodeT[top].inqueue = 0;
	flood.pop();

	for (i=0;i<nodeT[top].neighbornum;i++) 
	  { 
		  //get-rid of asymmetric links
		if (unidirectional(top, nodeT[top].neighbor[i])==true){
			//printf("No connection between %d and  %d\n",top,nodeT[top].neighbor[i] );
			continue;
        }
		
		if (nodeT[nodeT[top].neighbor[i]].floodsend == 0){
			  
			 /* nodeT[top] send out control message and its neighbors receive it */
			 flood.push(nodeT[top].neighbor[i]);
			 nodeT[nodeT[top].neighbor[i]].inqueue = 1;
         					   
			 double cost = GetE2ECost(top,root) + linkCost(nodeT[top].neighbor[i],top,root); 			 
			 SetE2ECost(nodeT[top].neighbor[i], root, cost,top);
			 nodeT[nodeT[top].neighbor[i]].floodsend =1;
		  }else{
			  
			  if(GetE2ECost(nodeT[top].neighbor[i],root) > GetE2ECost(top,root) + linkCost(nodeT[top].neighbor[i],top,root)){
         		 double cost = GetE2ECost(top,root)+ linkCost(nodeT[top].neighbor[i],top,root);         
				 SetE2ECost(nodeT[top].neighbor[i], root, cost,top);		 		 
			
				 if (nodeT[nodeT[top].neighbor[i]].inqueue==0){
					flood.push(nodeT[top].neighbor[i]);
					nodeT[nodeT[top].neighbor[i]].inqueue=1;
				 }   
			  }
		  }//else

	}
}
assert(flood.empty());

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?