/************************ author's email:wardseptember@gmail.com date:2017.12.15 图的深度优先搜索遍历 ************************/
/* 图的深度优先搜索遍历类似于二叉树的先序遍历。它的基本思想是:首先访问出发点v,并将其标 记为已访问过;然后选取与v邻接的未被访问的任意一个顶点w,并访问它;再选取与w邻接的为被 访问的任一顶点并访问,以此重复进行。当一个顶点所有的邻接顶点都被访问过时,则依次退回 到最近被访问过的顶点,若该顶点还有其他邻接顶点未被访问,则从这些未被访问的顶点中去一 个并重复上述访问过程,直至图中所有顶点都被访问过为止。 */
#include<iostream>
#define maxSize 8
using namespace std;
typedef struct Node { //定义一个结点
int vertex; //该边指向其他结点
struct Node *pNext; //指向下一条边的指针
}Node;
typedef struct head {//定义一个顶点
char data; //顶点的数据
Node *first; //指向第一条边的指针
}head, *Graph;
int visit[maxSize]; //定义一个全局变量,用来判断某一结点是否被访问过
Graph create_graph(); //创建一个邻接表
void DFS(Graph graph, int v); //深度遍历连通图
void dfs(Graph graph); //深度遍历非连通图
void DFSTrave(Graph graph, int i, int j);//判断顶点i和顶点j(i!=j)之间是否有路径
int main() {
Graph graph = create_graph();//接收一个邻接表
cout << "深度遍历连通图结果为:";
DFS(graph, 7); //dfs(graph);深度遍历非连通图
cout << endl;
int i, j;
cout << "请输入要判断的两个顶点(0-7):";
cin >> i >> j;
DFSTrave(graph, i, j); //判断顶点i和顶点j(i!=j)之间是否有路径
return 0;
return 0;
}
Graph create_graph()
{
//为保存顶点相关信息的数组分配空间,并对数据域赋值
Graph graph = (Graph)malloc(maxSize * sizeof(head));
int i;
//顶点的序号按照输入顺序从0依次向后
for (i = 0; i < maxSize; i++)
graph[i].data = 'A' + i;
//为每个节点对应的的单链表中的节点分配空间
Node *p00 = (Node *)malloc(sizeof(Node));
Node *p01 = (Node *)malloc(sizeof(Node));
Node *p10 = (Node *)malloc(sizeof(Node));
Node *p11 = (Node *)malloc(sizeof(Node));
Node *p12 = (Node *)malloc(sizeof(Node));
Node *p20 = (Node *)malloc(sizeof(Node));
Node *p21 = (Node *)malloc(sizeof(Node));
Node *p22 = (Node *)malloc(sizeof(Node));
Node *p30 = (Node *)malloc(sizeof(Node));
Node *p31 = (Node *)malloc(sizeof(Node));
Node *p40 = (Node *)malloc(sizeof(Node));
Node *p41 = (Node *)malloc(sizeof(Node));
Node *p50 = (Node *)malloc(sizeof(Node));
Node *p51 = (Node *)malloc(sizeof(Node));
Node *p60 = (Node *)malloc(sizeof(Node));
Node *p61 = (Node *)malloc(sizeof(Node));
Node *p70 = (Node *)malloc(sizeof(Node));
Node *p71 = (Node *)malloc(sizeof(Node));
//为各单链表中的节点的相关属性赋值
p00->vertex = 1;
p00->pNext = p01;
p01->vertex = 2;
p01->pNext = NULL;
p10->vertex = 0;
p10->pNext = p11;
p11->vertex = 3;
p11->pNext = p12;
p12->vertex = 4;
p12->pNext = NULL;
p20->vertex = 0;
p20->pNext = p21;
p21->vertex = 5;
p21->pNext = p22;
p22->vertex = 6;
p22->pNext = NULL;
p30->vertex = 1;
p30->pNext = p31;
p31->vertex = 7;
p31->pNext = NULL;
p40->vertex = 1;
p40->pNext = p41;
p41->vertex = 7;
p41->pNext = NULL;
p50->vertex = 2;
p50->pNext = p51;
p51->vertex = 6;
p51->pNext = NULL;
p60->vertex = 2;
p60->pNext = p61;
p61->vertex = 5;
p61->pNext = NULL;
p70->vertex = 3;
p70->pNext = p71;
p71->vertex = 4;
p71->pNext = NULL;
//将顶点与每个单链表连接起来
graph[0].first = p00;
graph[1].first = p10;
graph[2].first = p20;
graph[3].first = p30;
graph[4].first = p40;
graph[5].first = p50;
graph[6].first = p60;
graph[7].first = p70;
return graph;
}
void DFS(Graph graph, int v) {//深度遍历连通图
Node *p;
visit[v] = 1; //置此结点已访问
cout << graph[v].data<<' '; //输出结点信息
p = graph[v].first; //p指向顶点v的第一条边
while (p!=NULL) { //p=NULL遍历结束
if (visit[p->vertex] == 0) //若此顶点未被访问,则递归访问他
DFS(graph, p->vertex);
p = p->pNext; //p指向顶点v的下一条边的终点
}
}
void dfs(Graph graph) { //深度遍历非连通图
int i;
for (i = 0; i < maxSize; ++i)//循环使每个结点都访问到
if (visit[i] == 0)
DFS(graph, i);
}
void DFSTrave(Graph graph, int i, int j) {//判断顶点i和顶点j(i!=j)之间是否有路径
int k;
for (k = 0; k < maxSize; ++k)
visit[k] = 0;
DFS(graph, i);
cout << endl;
if (visit[j] == 1)//visit[j]=1则证明访问过程遇到了j
cout << "两顶点间有路径" << endl;
else
cout << "两顶点间无路径" << endl;
}
以上如有错误,请指出,大家共同学习进步。