0
点赞
收藏
分享

微信扫一扫

DFS与BFS伪代码

晴儿成长记 2022-03-20 阅读 57
  1. DFS(深度优先搜索)类似于树的先序遍历
void DFS(Vertex V){
	visited[V] = true;
	for(V的每个邻接点W)
		if(!Visited[W])
			DFS(W);
}
  1. BFS(广度优先搜索)类似于树的层序遍历
void BFS(Vertex V){
	visited[V]=true;
	Enqueue(V,Q);
	while(IsEmpty(Q)){
	V = Dequeue(Q);
	for(V的每个邻接点W)
		if(!visited[W]){
		visited[W] = true;
		Enqueue(W,Q);
		}
	}
}
举报

相关推荐

0 条评论