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

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

DFS--借助堆栈结构实现

BFS--借助队列结构实现

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

DFS--前序遍历实现

BFS--借助队列实现递归

#include <iostream>
#include<stack>
#include<queue>
#include <set>
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<int> XStack;
	set<int> XSet;//访问过的节点
	XStack.push(1);
	while (!XStack.empty())
	{
		/遍历过的节点不再访问,直接弹出
		int Cur = XStack.top();
		XStack.pop();
		//如果访问过此节点,直接弹出
		if (XSet.find(Cur) != XSet.end())
		{
			continue;
		}
		cout << Cur << ",";
		XSet.insert(Cur);
		//压栈
		Node* head = Head[Cur - 1]->next;
		//临时栈
		stack<int> 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<int> XQueue;
	set<int> 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<int> &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<int> &XQueue, Node** Head, set<int> &Used)
{
	if (XQueue.empty())
	{
		return;
	}
	//从队列中弹出一个元素
	int size = XQueue.size();
	for (int i=0;i<size;i++)
	{
		int cur = XQueue.front();
		XQueue.pop();
		if (Used.find(cur) != Used.end())
		{
			continue;
		}
		Node* curNode = Head[cur - 1]->next;
		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<int> recursiveDFS;
	DFS_graph_recursive(1, Head, recursiveDFS);
	cout << endl;
	BFS_graph(Head);
	set<int> recursiveBFS;
	queue<int> XQueue;
	XQueue.push(1);
	BFS_graph_recursive(XQueue, Head, recursiveBFS);
	cout << endl;
	return 0;
}

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: DFS(深度优先搜索)和BFS(广度优先搜索)算法论中常见的两种算法,用于遍历或树的节点。以下是C++实现DFS算法实现: ```c++ #include <iostream> #include <vector> #include <stack> using namespace std; void dfs(vector<vector<int>>& graph, vector<bool>& visited, int start) { stack<int> s; s.push(start); while (!s.empty()) { int node = s.top(); s.pop(); if (!visited[node]) { visited[node] = true; cout << node << " "; for (int i = graph[node].size() - 1; i >= 0; --i) { int next_node = graph[node][i]; if (!visited[next_node]) { s.push(next_node); } } } } } int main() { int n = 5; vector<vector<int>> graph(n); graph[0].push_back(1); graph[0].push_back(2); graph[1].push_back(3); graph[1].push_back(4); vector<bool> visited(n, false); dfs(graph, visited, 0); return 0; } ``` BFS算法实现: ```c++ #include <iostream> #include <vector> #include <queue> using namespace std; void bfs(vector<vector<int>>& graph, vector<bool>& visited, int start) { queue<int> q; q.push(start); while (!q.empty()) { int node = q.front(); q.pop(); if (!visited[node]) { visited[node] = true; cout << node << " "; for (int i = 0; i < graph[node].size(); ++i) { int next_node = graph[node][i]; if (!visited[next_node]) { q.push(next_node); } } } } } int main() { int n = 5; vector<vector<int>> graph(n); graph[0].push_back(1); graph[0].push_back(2); graph[1].push_back(3); graph[1].push_back(4); vector<bool> visited(n, false); bfs(graph, visited, 0); return 0; } ``` 这里我们以一个简单的无向为例,节点从0到4编号,的邻接表表示为: ``` 0: 1, 2 1: 0, 3, 4 2: 0 3: 1 4: 1 ``` DFS算法BFS算法的输出结果都是:0 2 1 4 3。 ### 回答2: DFS(深度优先搜索)和BFS(广度优先搜索)是两种常用的遍历算法,都可以用来实现C语言。 DFS算法通过递归或者栈的方式实现,可以从的某个顶点开始,沿着一条路径一直到达没有未探索的邻居节点为止,然后返回到前一个顶点继续探索其他未探索的邻居节点。可以用以下C语言代码实现DFS算法: ```c #include <stdio.h> #define SIZE 100 int visited[SIZE]; //用来标记节点是否访问过 int graph[SIZE][SIZE]; //的邻接矩阵表示 void dfs(int node) { printf("%d ", node); visited[node] = 1; for(int i = 0; i < SIZE; i++) { if(graph[node][i] && !visited[i]) { dfs(i); } } } int main() { //初始化visited和graph //调用dfs函数 dfs(0); //从节点0开始深度优先搜索 return 0; } ``` BFS算法通过队列的方式实现,可以从的某个顶点开始,将其加入队列,然后依次将队列中的节点访问并将其邻居节点加入队列,直到队列为空。可以用以下C语言代码实现BFS算法: ```c #include <stdio.h> #define SIZE 100 int visited[SIZE]; //用来标记节点是否访问过 int graph[SIZE][SIZE]; //的邻接矩阵表示 void bfs(int node) { int queue[SIZE]; int front = 0, rear = 0; queue[rear++] = node; visited[node] = 1; while(front < rear) { int curNode = queue[front++]; printf("%d ", curNode); for(int i = 0; i < SIZE; i++) { if(graph[curNode][i] && !visited[i]) { queue[rear++] = i; visited[i] = 1; } } } } int main() { //初始化visited和graph //调用bfs函数 bfs(0); //从节点0开始广度优先搜索 return 0; } ``` 以上就是用C语言实现DFSBFS算法的代码。在实际应用中,可以根据具体场景选择使用DFS还是BFS来进行的遍历。 ### 回答3: DFS(深度优先搜索)和BFS(广度优先搜索)算法都是用于的遍历的常见算法。它们在遍历的顺序、搜索方式和空间复杂度上有所差异。 DFS是一种先深入后回溯的搜索方法。它从起点开始,沿着的一条路径一直遍历到尽头,然后回溯到上一个节点,继续探索其他未遍历的路径,直到整个都被遍历完。DFS常用递归或栈的方式实现BFS是一种逐层扩展的搜索方法。它从起点开始,首先遍历起点的所有邻接节点,然后依次遍历邻接节点的邻接节点,以此类推,直到整个都被遍历完。BFS常用队列的方式实现,每次将待遍历节点加入队列,并在从队列中取出节点时,将其邻接节点加入队列。 在C语言中,实现DFSBFS算法可以借助的表示方式和遍历的数据结构。一种常见的的表示方式是邻接矩阵或邻接表,用于存储的顶点和边的关系。而在遍历过程中,可以借助一个访问标记数组,用于标记节点是否被访问过。 对于DFS算法实现,可以通过递归函数实现递归函数的参数包括当前遍历的节点、访问标记数组等。递归函数的主体部分可以按照DFS的逻辑进行实现。 而对于BFS算法实现,可以通过队列来实现,首先将起点加入队列,然后循环取出队列中的节点,并将其邻接节点依次加入队列,直到队列为空。在每次取出节点时,可以将其标记为已经访问过。 总之,DFSBFS算法在C语言中的实现需要借助的表示方式,以及递归函数或队列等数据结构。具体实现的细节还可以根据具体问题的需求进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值