广度优先搜索

  图论基本算法中广度优先算法,讨论的很多,这里不做过多描述,仅仅贴出代码。

#include<iostream>
#include<queue>
using namespace std;

#define Inf 65535
#define NotAVerter -1

/////////////////////邻接链表的相关定义//////////////////////
typedef struct EdgeNode *position;
typedef struct Led_table* Table;


struct EdgeNode     //边表结点 
{
	int adjvex;    // 邻接点域,存储该顶点对应的下标 
	int weight;     // 对应边的权值,在无权图中可以不使用
	int color;    //此标志表示对应的节点是否已经搜索到,0为白色,1为灰色,2为黑色
	int dis;     //此数据记录从源点到该节点的最短距离
	int precursor;  //此数据记录该节点在广度优先树种的前驱节点
	position next; // 链域,指向下一个邻接点
};

struct Led_table       // 邻接表结构 
{
	int data;                //邻接表的大小
	position *firstedge;       //边表头指针,可以理解为数组
};


//////////////////////////邻接链表相关函数定义///////////////
Table Creat_Lable(int MaxElements)    //MaxElements参数为希望创建的节点数
{

	Table table1 = static_cast<Table> (malloc(sizeof(struct Led_table)));
	table1->data = MaxElements;
	if (table1 == NULL)
	{
		cout << "out of space!!!";
	}

	table1->firstedge = static_cast<position*>(malloc(sizeof(position)*(table1->data)));
	if (table1->firstedge == NULL)
	{
		cout << "out of space!!!";
	}

	//给每个表头赋值,从0开始
	for (int i = 0; i <= table1->data - 1; ++i)
	{
		table1->firstedge[i] = static_cast<position>(malloc(sizeof(EdgeNode)));   //申请一个节点
		if (table1->firstedge[i] == NULL)
		{
			cout << "out of space!!!";
		}
		table1->firstedge[i]->adjvex = 0;   //表头这个参数没有意义
		table1->firstedge[i]->weight = 0;   //表头这个参数没有意义
		table1->firstedge[i]->color = 0;    
		table1->firstedge[i]->dis = Inf;     
		table1->firstedge[i]->precursor = NotAVerter;   
		table1->firstedge[i]->next = NULL;

	}
	return table1;

}


void Insert(Table table1, int v, int w, int weig)   //表示存在一条边为<v,w>
{
	position p = static_cast<position>(malloc(sizeof(EdgeNode)));   //申请一个节点
	if (p == NULL)
	{
		cout << "out of space!!!";
	}
	p->adjvex = w;
	p->weight = weig;    //对于无权图来说,该域可以设置为1
	p->color = 0;    //对于普通节点来说无意义
	p->dis = Inf;    //对于普通节点来说无意义
	p->precursor = NotAVerter;  //对于普通节点来说无意义
	p->next = table1->firstedge[v]->next;
	table1->firstedge[v]->next = p;


	position q = static_cast<position>(malloc(sizeof(EdgeNode)));   //申请一个节点
	if (q == NULL)
	{
		cout << "out of space!!!";
	}
	q->adjvex = v;
	q->weight = weig;    //对于无权图来说,该域可以设置为1
	q->color = 0;    //对于普通节点来说无意义
	q->dis = Inf;    //对于普通节点来说无意义
	q->precursor = NotAVerter;  //对于普通节点来说无意义
	q->next = table1->firstedge[w]->next;
	table1->firstedge[w]->next = q;
}

void init_yuandian(Table table1,int s)  //把s设置为图的源点
{
	table1->firstedge[s]->adjvex = 0;   
	table1->firstedge[s]->weight = 0;   
	table1->firstedge[s]->color = 1;    //设置为灰色
	table1->firstedge[s]->dis = 0;
	table1->firstedge[s]->precursor = NotAVerter;
//	table1->firstedge[s]->next = NULL;
}

/////////////////////////////////广度优先搜索算法/////////////////////////////////

void guangduyouxian(Table table_1, int s)
{
	queue<int> Q;
	Q.push(0);
	while (!Q.empty())
	{
		int u = Q.front();   //返回第一个元素
		Q.pop();   //删除第一个元素

		position p = table_1->firstedge[u]->next;
		while (p != NULL)
		{
			if (table_1->firstedge[p->adjvex]->color == 0)
			{
				table_1->firstedge[p->adjvex]->color = 1;
				table_1->firstedge[p->adjvex]->dis = table_1->firstedge[u]->dis + 1;
				table_1->firstedge[p->adjvex]->precursor = u;
				Q.push(p->adjvex);
			}
			p = p->next;
		}
		table_1->firstedge[u]->color = 2;
	}
}


int main()
{
	Table table_1 = Creat_Lable(8);    //创建一个大小为8的邻接表

									   //根据图来为邻接表插入数据
	Insert(table_1, 1, 2, 1); 
	Insert(table_1, 2, 0, 1); 
	Insert(table_1, 0, 3, 1);
	Insert(table_1, 3, 5, 1); Insert(table_1, 3, 4, 1); 
	Insert(table_1, 4, 5, 1); Insert(table_1, 4, 6, 1); Insert(table_1, 4, 7, 1);
	Insert(table_1, 5, 6, 1); Insert(table_1, 5, 7, 1);

	init_yuandian(table_1, 0);  //把0设置为图的源点

	guangduyouxian(table_1, 0);

	cout << table_1->firstedge[7]->dis << endl;



	return 0;
}

    夜深了,足迹越来越远。

posted on 2017-09-24 15:53  wu_xin  阅读(173)  评论(0编辑  收藏  举报

导航