图的BFS算法和DFS的递归非递归算法

#include<iostream>
#include<fstream>
#include<queue>
#include<stack>
using namespace std;
#define MAX 10
typedef struct graph
{
	int n;   //顶点数
	int e;  //边数
	int edge[MAX][MAX];  //标识边,0为没有该边,不为0则有边,且标识边的权值
}Graph;
int visit[MAX] = { 0 };    //表示该顶点有没有访问过,没有为0,有为1

void InitGraph(Graph *G)
{
	for (int i = 0; i < MAX;i++)
	for (int j = 0; j < MAX; j++)
		(*G).edge[i][j] = 0;
}

//广度优先搜索算法
void BFS(Graph G, int num)  //num为从该点开始进行搜索
{
	queue<int>Queue;
	cout << num <<" ";   
	visit[num] = 1;
	Queue.push(num);  //访问完该点后入队列
	while (!Queue.empty())  //队列不为空时
	{
		num = Queue.front();  //出队
		Queue.pop();
		for (int i = 0; i < G.n; i++)
		{
			if (G.edge[num][i] != 0 && visit[i] == 0)   //找到与之相连的顶点入队
			{
				cout << i << " ";
				Queue.push(i);
				visit[i] = 1;
			}
		}
	}
	cout << endl;
}

void DFS1(Graph G, int num) //深度优先搜索递归算法
{
	int i;
	cout << num << " ";
	visit[num] = 1;
	for (i = 0; i < G.n; i++)
	{
		if (G.edge[num][i] != 0 && visit[i] == 0)
			DFS1(G, i);
	}
}
void DFS2(Graph G, int num) //深度优先搜索非递归算法
{
	stack<int> Stack;
	visit[num] = 1;
	Stack.push(num);
	while (!Stack.empty())
	{
		num = Stack.top();
		Stack.pop();
		cout << num << " ";
		for (int i = G.n - 1; i >= 0; i--)
		{
			if (G.edge[num][i] != 0 && visit[i] == 0)
			{
				Stack.push(i);
				visit[i] = 1;
			}
		}
	}
	cout << endl;
}
int main()
{
	int a, b, v, i;
	Graph G;
	ifstream cin("data.txt");
	cin >> G.n >> G.e;   //n,e为顶点个数,边个数
	InitGraph(&G);   //对G进行初始化,整个MAX范围初始化
	for (i = 0; i < G.e; i++)   //建图
	{
		cin >> a >> b >> v;  //a,b为顶点,v为权值
		G.edge[a][b] = v;
		G.edge[b][a] = v;
	}
	BFS(G, 0); //0为开始搜索的顶点序号
	for (i = 0; i < MAX; i++)
		visit[i] = 0;
	DFS1(G, 0);
	cout << endl;
	for (i = 0; i < MAX; i++)
		visit[i] = 0;
	DFS2(G, 0);
	system("pause");
	return 0;
}



展开阅读全文

没有更多推荐了,返回首页