数据结构——图的DFS和BFS算法递归和非递归实现

数据结构——图的DFS和BFS算法递归和非递归实现,第1张

 1.使用非递归算法实现DFS和BFS遍历

DFS--借助堆栈结构实现

BFS--借助队列结构实现

2.使用递归算法实现DFS和BFS遍历

DFS--前序遍历实现

BFS--借助队列实现递归

#include 
#include
#include
#include 
using namespace std;
struct Node
{
	int vertex;
	Node* next;
};
//打印图结构
void printLinkList(const Node* Head)
{
	if (Head==nullptr)
	{
		return;
	}
	while (Head!=nullptr)
	{
		cout << Head->vertex << ",";
		Head = Head->next;
	}
	cout << endl;
}
//DFS非递归实现
void DFS_graph(Node **Head)
{
	//深度优先遍历
	//借助栈结构实现
	stack XStack;
	set XSet;//访问过的节点
	XStack.push(1);
	while (!XStack.empty())
	{
		/遍历过的节点不再访问,直接d出
		int Cur = XStack.top();
		XStack.pop();
		//如果访问过此节点,直接d出
		if (XSet.find(Cur) != XSet.end())
		{
			continue;
		}
		cout << Cur << ",";
		XSet.insert(Cur);
		//压栈
		Node* head = Head[Cur - 1]->next;
		//临时栈
		stack temp;
		while (head != nullptr)
		{
			temp.push(head->vertex);
			head = head->next;
		}
		//放入Stack
		while (!temp.empty())
		{
			if (XSet.find(temp.top()) != XSet.end())
			{
				temp.pop();
				continue;
			}
			XStack.push(temp.top());
			temp.pop();
		}
	}
	cout << endl;
}
//BFS非递归实现
void BFS_graph(Node**Head)
{
	//广度优先遍历
	queue XQueue;
	set BFSset;
	XQueue.push(1);
	while (!XQueue.empty())
	{
		int cur = XQueue.front();
		XQueue.pop();
		if (BFSset.find(cur) != BFSset.end())
		{
			continue;
		}
		BFSset.insert(cur);
		cout << cur << ",";
		Node* head = Head[cur - 1]->next;
		while (head != nullptr)
		{
			//向队列中添加元素
			if (BFSset.find(head->vertex) != BFSset.end())
			{
				head = head->next;
				continue;
			}
			XQueue.push(head->vertex);
			head = head->next;
		}
	}
	cout << endl;
}
//DFS递归实现
void DFS_graph_recursive(int CurNode, Node** Head, set &Used)
{
	//遍历过不再遍历
	if (Used.find(CurNode)!=Used.end())
	{
		return;
	}
	cout << CurNode << ",";
	Used.insert(CurNode);
	Node* tmp = Head[CurNode - 1]->next;
	while (tmp!=nullptr)
	{
		int cur = tmp->vertex;
		if (Used.find(cur)!=Used.end())
		{
			tmp = tmp->next;
			continue;
		}
		DFS_graph_recursive(cur, Head, Used);
		tmp = tmp->next;
	}
}
//BFS非递归实现
void BFS_graph_recursive(queue &XQueue, Node** Head, set &Used)
{
	if (XQueue.empty())
	{
		return;
	}
	//从队列中d出一个元素
	int size = XQueue.size();
	for (int i=0;inext;
		while (curNode!=nullptr)
		{
			int tmp = curNode->vertex;
			if (Used.find(tmp)!=Used.end())
			{
				curNode = curNode->next;
				continue;
			}
			XQueue.push(tmp);
			curNode = curNode->next;
		}
		cout << cur << ",";
		Used.insert(cur);
	}
	BFS_graph_recursive(XQueue, Head, Used);
}
int main()
{
	int data[20][2] = { {1,2},{2,1},{1,3},{3,1},
								{2,4},{4,2},{2,5},{5,2},
								{3,6},{6,3},{3,7},{7,3},
								{4,5},{5,4},{6,7},{7,6},
								{5,8},{8,5},{6,8},{8,6} };
	//邻接链表法存储图结构
	//申请8个头指针
	Node *Head[8];
	for (int i = 0; i < 8; i++)
	{
		Head[i] = new Node;
		Head[i]->vertex = i + 1;
		Node* tail = Head[i];
		for (int j = 0; j < 20; j++)
		{
			//有公共的点
			if (data[j][0]==i+1)
			{
				Node* temp = new Node;
				temp->vertex = data[j][1];
				temp->next = nullptr;
				tail->next = temp;
				tail = temp;
			}
		}
	}
	//输出图结构
	for (int i = 0; i < 8; i++)
	{
		printLinkList(Head[i]);
	}
	DFS_graph(Head);
	set recursiveDFS;
	DFS_graph_recursive(1, Head, recursiveDFS);
	cout << endl;
	BFS_graph(Head);
	set recursiveBFS;
	queue XQueue;
	XQueue.push(1);
	BFS_graph_recursive(XQueue, Head, recursiveBFS);
	cout << endl;
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/662309.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-18
下一篇2022-04-18

发表评论

登录后才能评论

评论列表(0条)

    保存