无向图的深度优先搜索
邻接矩阵存储结构
连通图的深度优先遍历类似于树的先根遍历。
注意:要习惯于把分支和有进退的思想和递归联系起来
#include <iostream>
#include<cmath>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100
//定义无穷大
#define MAXInt 32767
typedef int Status;
typedef char TElemType;
using namespace std;
int visited[MAXSIZE];
//定义顶点数据类型
typedef char VerTexType;
//定义边的数据类型
typedef int ArcType;
//定义图的数据结构
typedef struct {
//顶点表
VerTexType vexs[MAXSIZE];
//邻接矩阵
ArcType arcs[MAXSIZE][MAXSIZE];
//当前结点数和当期边数
int vexnum, arcnum;
}AMGraph; //Adjacency(邻接) Matrix Graph
//函数输入一个顶点的值,返回该顶点在顶点表中的下标,不存在则返回-1
int LocateVex(VerTexType x, AMGraph& G) {
for (int i = 0; i < G.vexnum; i++)
{
if (x == G.vexs[i])
{
return i;
}
}
return -1;
}
//创建无向图
void createUDN(AMGraph& G) {
//先输入图的顶点数和边数
cout << "请输入要创建的无向图的顶点数和边数:" << "\n";
scanf_s("%d%d", &G.vexnum, &G.arcnum);
//依次输入各个顶点的值
cout << "依次输入各个顶点的值" << "\n";
for (int i = 0; i < G.vexnum; i++) {
cin >> G.vexs[i];
}
//对邻接矩阵进行初始化
for (int i = 0; i < G.vexnum; i++) {
for (int j = 0; j < G.vexnum; j++) {
G.arcs[i][j] = 0;
}
}
cout << "请输入每条边的顶点和权值:输入方式:顶点1 顶点2" << "\n";
//输入依次输入每一条边的顶点和权值
for (int i = 0; i < G.arcnum; i++)
{
VerTexType a, b;
cin >> a >> b ;
//找到输入的顶点a,b在顶点表中的下标
int x = LocateVex(a, G);
int y = LocateVex(b, G);
if (x != -1 && y != -1) {
G.arcs[x][y] = 1;
G.arcs[y][x] = 1;
}
}
}
//输出顶点表以及邻接矩阵
void outPut(AMGraph G) {
cout << "顶点表如下:" << "\n";
for (int i = 0; i < G.vexnum; i++) {
printf("%c ", G.vexs[i]);
}
printf("\n输出邻接矩阵如下\n");
for (int i = 0; i < G.vexnum; i++)
{
for (int j = 0; j < G.vexnum; j++)
{
printf("%6d", G.arcs[i][j]);
}
printf("\n");
}
}
//深度优先搜索遍历
/*
根据老师说的写倒是挺容易的,
就是需要自己花点时间理解这个思路
*/
void DFS(AMGraph G, int v) {
cout << G.vexs[v] << " ";
visited[v] = 1;
for (int i = 0; i < G.vexnum; i++)
{
//(G.arcs[v][i]!=0)说明有该边,visited[i]==0说明未被访问
if ((G.arcs[v][i]!=0)&&visited[i]==0)
{
DFS(G, i);
}
}
}
int main() {
AMGraph G;
createUDN(G);
outPut(G);
for (int i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
}
cout << "\n请输入深度优先搜索开始的结点的值:\n" << "";
VerTexType x;
cin >> x;
cout << "\n深度优先搜索遍历的顺序为:" << "\n";
DFS(G, LocateVex(x,G));
return 0;
}
测试样例:
算法效率分析:
采用邻接矩阵来表示图,遍历图中每个顶点都要从头开始扫描该顶点所在行,时间复杂度为O(n2)
邻接表表示法
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define MAXNum 100
//定义无穷大
#define MAXInt 32767
typedef int Status;
//定义顶点数据类型
typedef char VerTexType;
//定义节点权重的类型
typedef int OtherInfo;
//定义边的数据类型
typedef struct ArcNode {
int adjvex;//该边指向的顶点的位置
ArcNode* nextarc;//指向下一条边的指针
OtherInfo info;//与边相关的信息
}ArcNode;
//定义表结点的类型
typedef struct VNode {
VerTexType data;//顶点信息
ArcNode* firstarc;//指向第一条依附该顶点的边的指针
}VNode, AdjList[MAXNum];//AdjList是邻接表类型
//图结构的类型
typedef struct {
AdjList vertices;//邻接表
int vexnum, arcnum;//图当前顶点数和弧数
}ALGraph;//图的定义
//该函数查找顶点x在图的顶点表中的下标
int LocateVex(ALGraph G, VerTexType x) {
for (int i = 0; i < G.vexnum; i++) {
if (G.vertices[i].data == x) {
return i;
}
}
return -1;
}
//邻接表的创建——无向图
Status createUDN(ALGraph& G) {
cout << "请输入无向图的总结点的数目和总的边数" << "\n";
cin >> G.vexnum >> G.arcnum;
cout << "请输入无向图中的各个顶点" << "\n";
for (int i = 0; i < G.vexnum; i++)
{
cin >> G.vertices[i].data;
//初始化表头结点的指针域
G.vertices[i].firstarc = NULL;
}
cout << "请输入每条边的信息:(顶点1 顶点2)" << "\n";
for (int i = 0; i < G.arcnum; i++) {
VerTexType x, y;
cin >> x >> y;
//查找两个顶点的下标
int xIndex = LocateVex(G, x);
int yIndex = LocateVex(G, y);
//找到这两个顶点的下标之后就可以往firstarc域插入表结点了
ArcNode* xArcNode = new ArcNode;
ArcNode* yArcNode = new ArcNode;
if (xIndex != -1 && yIndex != -1) {
//在x结点后面插入邻接y结点的信息
xArcNode->info = 1;
xArcNode->adjvex = yIndex;
xArcNode->nextarc = G.vertices[xIndex].firstarc;
G.vertices[xIndex].firstarc = xArcNode;
//在y结点后面插入邻接x结点的信息
yArcNode->info = 1;
yArcNode->adjvex = xIndex;
yArcNode->nextarc = G.vertices[yIndex].firstarc;
G.vertices[yIndex].firstarc = yArcNode;
}
else
{
return ERROR;
}
}
return OK;
}
void outPut(ALGraph G) {
cout << "输出顶点表如下:" << "\n";
for (int i = 0; i < G.vexnum; i++)
{
cout << G.vertices[i].data << " ";
}
cout << "\n输入邻接表如下:" << "\n";
for (int i = 0; i < G.vexnum; i++)
{
printf("%c顶点相邻接的顶点为:\n", G.vertices[i].data);
ArcNode* p = G.vertices[i].firstarc;
while (p) {
printf("%c ", G.vertices[p->adjvex].data);
p = p->nextarc;
}
printf("\n");
}
}
//进行深度优先搜索
int visited[MAXNum];
void DFS(ALGraph G, int v) {
visited[v] = 1;
cout << G.vertices[v].data << " ";
ArcNode* p = G.vertices[v].firstarc;//这个结点的第一条边的信息
while (p) {
if (visited[p->adjvex]==0)
{
DFS(G, p->adjvex);
}
p = p->nextarc;
}
}
int main() {
ALGraph G;
createUDN(G);
//输出该图进行查看,是否正确
outPut(G);
//初始化访问数组
for (int i = 0; i < G.vexnum; i++)
{
visited[i] = 0;
}
cout << "\n请输入深度优先搜索开始的结点的值:\n" << "";
VerTexType x;
cin >> x;
cout << "\n深度优先搜索遍历的顺序为:" << "\n";
DFS(G, LocateVex(G,x));
return 0;
}
测试样例自己用上那个图测试就可以了!
算法效率分析:
采用邻接表来表示图,虽然有2e个表结点,但是只需要扫描e个结点即可完成遍历,再加上n个头结点的时间,时间复杂度为O(n+e)
故:
- 稠密图适于在邻接矩阵上进行深度遍历
- 稀疏图适于在邻接表上进行深度遍历