广度优先搜索(BFS) C语言实现

伪代码


#define Black 2
#define Gray 1
#define White 0
#define Infinite -1
#define NIL -1

struct map{
	int d;
	int color;
	int parent;
} vertix[100];

void BFS(int a[100][100],int n,int s){
	int i,j;
	int tmp;
	Queue q;
	for(i = 0;i<n;i++){
		vertix[i].d = Infinite;
		vertix[i].color = White;
		vertix[i].parent = NIL;
	}
	vertix[s].color = Gray;
	vertix[s].d = 0;
	q = CreateQueue(n); 
	Enqueue(s,q);
	while(!IsEmpty(q)){
		tmp = FrontAndDequeue(q);
		for(i = 0;i<n;i++)
			if(a[tmp][i])
				if(vertix[i].color == White){
					vertix[i].d = vertix[tmp].d + 1;
					vertix[i].color = Gray;
					vertix[i].parent = tmp;
					Enqueue(i,q);
				}
		vertix[tmp].color = Black;
	}
	DisposeQueue(q);
}



版权声明:本文为andyyang0212原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。