BFS (wide search) and DFS (deep search) algorithm principle (easy to understand version)

DFS algorithm
Thought: keep going deep, until you find a solution or you can't go any further
BFS algorithm
DFS: Use the stack to save undetected nodes. The nodes are accessed in depth-first order and pushed into the stack in turn, and popped out of the stack in reverse order for new detection.
BFS: Use a queue to store undetected nodes. Nodes are visited and enqueued in breadth-first order.
frame:

BFS

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 100;
bool vst[maxn][maxn]; // access token
int dir[4][2] = {0,1,0,-1,1,0,-1,0}; // direction vector
struct State { // State data structure in BFS queue
	int x, y; // coordinate position
	int Step_Counter; // search step counter
};
State a[maxn];
bool CheckState(State s) { // constraint check
	if(!vst[sx][sy] && ...) // conditions are met
		return 1;
	else // constraint violation
		return 0;
}
void bfs(State st) {
	queue <State> q; // BFS queue
	State now, next; // define 2 states, current and next
	st.Step_Counter = 0; // the counter is cleared
	q.push(st); // enqueue
	vst[st.x][st.y] = 1; // access token
	while(!q.empty()) {
		now = q.front(); // take the first element of the queue for expansion
		if(now == G) { // The target state appears, this time is the minimum value of Step_Counter, you can exit
			...... // do related processing
			return;
		}
		for(int i = 0; i < 4; i++) {
			next.x = now.x + dir[i][0]; // Generate the next state according to the rules
			next.y = now.y + dir[i][1];
			next.Step_Counter = now.Step_Counter+1; // increment the counter by 1
			if(CheckState(next)) { // enqueue if the state meets the constraints
				q.push(next);
				vst[next.x][next.y] = 1; //Access flag
			}
		}
		q.pop(); // The first element of the team is dequeued
	}
	return;
}
int main() {
	......
	return 0;
}

DFS:

/*
The DFS framework takes the 2D coordinate range as an example to reflect the realization idea of ​​the DFS algorithm.
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=100;
bool vst[maxn][maxn]; // access token
int map[maxn][maxn]; // coordinate range
int dir[4][2]= {0,1,0,-1,1,0,-1,0}; // direction vector, four directions around (x,y)
bool CheckEdge(int x,int y) { // Judgment of boundary conditions and constraints
	if(!vst[x][y] && ...) // conditions are met
		return 1;
	else // conflicts with constraints
		return 0;
}
void dfs(int x,int y) {
	vst[x][y]=1; // mark the node as visited
	if(map[x][y]==G) { // The target state G appears
		...... // do the corresponding processing
		return;
	}
	for(int i=0; i<4; i++) {
		if(CheckEdge(x+dir[i][0],y+dir[i][1])) // Generate the next node according to the rules
			dfs (x + dir [i] [0], y + dir [i] [1]);
	}
	return; // no lower search node, backtracking
}
int main() {
	......
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326039451&siteId=291194637