图的深度优先搜索与宽度优先搜索
宽度优先搜索算法(又称广度优先搜索)
图的深度优先搜索dfs—c++版
void DFS_graph(GraphNode *node, int visit[]){
visit[node->label] = 1; //标记已访问的节点
printf("%d ", node->label);
for (int i = 0; i < node->neighbors.size(); i++){ //访问相邻且没有被访问过的节点
if (visit[node->neighbors[i]->label] == 0){
DFS_graph(node->neighbors[i], visit);
}
}
}
图的宽度优先搜索bfs—c++版
void BFS_graph(GraphNode *node, int visit[]){
std::queue<GraphNode *> Q;
Q.push(node);
visit[node->label] = 1;
while(!Q.empty()){
GraphNode *node = Q.front();
Q.pop();
printf("%d ", node->label);
for (int i = 0; i < node->neighbors.size(); i++){
if (visit[node->neighbors[i]->label] == 0){
Q.push(node->neighbors[i]);
visit[node->neighbors[i]->label] = 1;
}
}
}
}
图的深度优先搜索–java版
import java.util.*;
/*定义图中的节点的数据结构*/
class Node {
int val; // 节点数据
List<Node> neighbours; // 相邻其他节点
public Node(int x) {
val = x;
neighbours = new ArrayList<Node>();
}
}
public class Code1 {
public static void main(String[] args) {
Node begin = createGraph();
Set<Node> visited = new HashSet<>();
System.out.println("DFS:");
DFS(begin, visited);
System.out.println("BFS:");
BFS(begin);
}
/* 创建图,返回一个节点 */
public static Node createGraph() {
Node v1 = new Node(1);
Node v2 = new Node(2);
Node v3 = new Node(3);
Node v4 = new Node(4);
Node v5 = new Node(5);
Node v6 = new Node(6);
Node v7 = new Node(7);
Node v8 = new Node(8);
v1.neighbours.add(v2);
v1.neighbours.add(v3);
v1.neighbours.add(v4);
v1.neighbours.add(v5);
v2.neighbours.add(v6);
v2.neighbours.add(v7);
v4.neighbours.add(v8);
return v1;
}
/**
* 深度优先
*
* @param v 源顶点
* @param visited 集合类型,记录所有已访问顶点
*/
public static void DFS(Node v, Set<Node> visited) {
if (visited.contains(v))
return;
else {
System.out.println("Visit node:" + v.val);
visited.add(v);
for (Node next : v.neighbours) {
DFS(next, visited);
}
}
}
}
图的宽度优先搜索–java版
/**
* 宽度优先
*
* @param v 源顶点,即出发顶点
*/
public static void BFS(Node v) {
Set<Node> visited = new HashSet<>();
Queue<Node> queue = new LinkedList<>();
queue.offer(v); // 入队列
while (!queue.isEmpty()) {
Node node = queue.poll(); // 出队列并删除
if (!visited.contains(node)) {
System.out.println("Visit node:" + node.val);
visited.add(node);
for (Node next : node.neighbours) {
queue.offer(next);
}
}
}
}
图的深度优先搜索和–完整代码–c++版
#include <stdio.h>
#include <vector>
struct GraphNode{
int label;
std::vector<GraphNode *> neighbors;
GraphNode(int x) : label(x) {};
};
void DFS_graph(GraphNode *node, int visit[]){
visit[node->label] = 1;
printf("%d ", node->label);
for (int i = 0; i < node->neighbors.size(); i++){
if (visit[node->neighbors[i]->label] == 0){
DFS_graph(node->neighbors[i], visit);
}
}
}
int main(){
const int MAX_N = 5;
GraphNode *Graph[MAX_N];
for (int i = 0; i < MAX_N; i++){
Graph[i] = new GraphNode(i);
}
Graph[0]->neighbors.push_back(Graph[4]);
Graph[0]->neighbors.push_back(Graph[2]);
Graph[1]->neighbors.push_back(Graph[0]);
Graph[1]->neighbors.push_back(Graph[2]);
Graph[2]->neighbors.push_back(Graph[3]);
Graph[3]->neighbors.push_back(Graph[4]);
Graph[4]->neighbors.push_back(Graph[3]);
int visit[MAX_N] = {0};
for (int i = 0; i < MAX_N; i++){
if (visit[i] == 0){
printf("From label(%d) : ", Graph[i]->label);
DFS_graph(Graph[i], visit);
printf("\n");
}
}
for (int i = 0; i < MAX_N; i++){
delete Graph[i];
}
return 0;
}
图的宽度优先搜索–完整代码–c++版
#include <stdio.h>
#include <vector>
#include <queue>
struct GraphNode{
int label;
std::vector<GraphNode *> neighbors;
GraphNode(int x) : label(x) {};
};
void BFS_graph(GraphNode *node, int visit[]){
std::queue<GraphNode *> Q;
Q.push(node);
visit[node->label] = 1;
while(!Q.empty()){
GraphNode *node = Q.front();
Q.pop();
printf("%d ", node->label);
for (int i = 0; i < node->neighbors.size(); i++){
if (visit[node->neighbors[i]->label] == 0){
Q.push(node->neighbors[i]);
visit[node->neighbors[i]->label] = 1;
}
}
}
}
int main(){
const int MAX_N = 5;
GraphNode *Graph[MAX_N];
for (int i = 0; i < MAX_N; i++){
Graph[i] = new GraphNode(i);
}
Graph[0]->neighbors.push_back(Graph[4]);
Graph[0]->neighbors.push_back(Graph[2]);
Graph[1]->neighbors.push_back(Graph[0]);
Graph[1]->neighbors.push_back(Graph[2]);
Graph[2]->neighbors.push_back(Graph[3]);
Graph[3]->neighbors.push_back(Graph[4]);
Graph[4]->neighbors.push_back(Graph[3]);
int visit[MAX_N] = {0};
for (int i = 0; i < MAX_N; i++){
if (visit[i] == 0){
printf("From label(%d) : ", Graph[i]->label);
BFS_graph(Graph[i], visit);
printf("\n");
}
}
for (int i = 0; i < MAX_N; i++){
delete Graph[i];
}
return 0;
}