利用队列实现深度优先搜索 。
考察顶点2作为源点s的深度优先搜索。
输入data.txt 邻接矩阵存储
gaph G
0 1 0 0 0 0 0 0
1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0
0 0 1 0 1 1 0 0
0 0 0 1 0 1 1 1
0 0 0 1 1 0 1 0
0 0 0 0 1 1 0 1
0 0 0 0 1 0 1 0
程序:
/*
* @file BFS.cpp
* @brief implementing BFS
* @author taoxiaoxiao/XMU
* @date 11-20-2013
* @version v1
*/
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 0x3f3f3f3f
#define NIL -1
#define MAXN 101
int graph[MAXN][MAXN], dist[MAXN];
int pre[MAXN], color[MAXN];
int n;
void BFS(int s)
{
int i;
for (i &