C++ 图的邻接表存储与搜索遍历(DFS、BFS)

#include <bits/stdc++.h>
#define MaxSize 100

/*
* Created by HarvestWu on 2018/06/22.
*/
using namespace std;
typedef int ElemType;

//边表
typedef struct ArcNode
{
	int adjvex;                 //该边所指向的结点的位置
	struct ArcNode *nextarc;    //指向下一条边的指针
	int info;                   //
}ArcNode;

//顶点表
typedef struct
{
	char data;                  //顶点信息
	ArcNode *firstarc;          //指向第一条边的指针
}VNode;

//邻接表
typedef struct
{
	VNode adjlist[MaxSize];
	int n, e;                    //顶点数、边数
}AGraph;                        //图的邻接表类型

void Visit(int v)
{
	cout << v << " ";
}

//图的深度优先搜索遍历(DFS)
//假设用邻接表作为图的存储结构

int visit[MaxSize];
void DFS(AGraph *G, int v)
{
	ArcNode *p;
	visit[v] = 1;					//置标志位1代表已访问
	Visit(v);
	p = G->adjlist[v].firstarc;		//p指向顶点v的第一条边
	while (p != NULL)
	{
		if (visit[p->adjvex] == 0)	//未访问则递归访问
			DFS(G, p->adjvex);		
		p = p->nextarc;				
	}

}

//图的广度优先搜索遍历(BFS)
//假设用邻接表作为图的存储结构

void BFS(AGraph *G, int v, int visit[MaxSize])
{
	ArcNode *p;
	int que[MaxSize], front = 0, rear = 0;
	int j;
	Visit(v);
	visit[v] = 1;				//置标志位1代表已访问
	rear = (rear + 1) % MaxSize;
	que[rear] = v;				//当前顶点入队,便于此层扫描完后,继续下一层
	while (front != rear)		//队空则遍历结束
	{
		front = (front + 1) % MaxSize;
		j = que[front];
		p = G->adjlist[j].firstarc;
		while (p != NULL)		
		{
			if (visit[p->adjvex] == 0)
			{
				Visit(p->adjvex);
				visit[p->adjvex] = 1;
				rear = (rear + 1) % MaxSize;
				que[rear] = p->adjvex;
			}
			p = p->nextarc;
		}
	}
}

//非连通图深度优先搜索遍历

void dfs(AGraph *g)
{
	int i;
	for (i = 0; i<g->n; ++i)
	{
		if (visit[i] == 0)
			DFS(g, i);
	}
}

//非连通图广度优先搜索遍历

void bfs(AGraph *g)
{
	int i;
	for (i = 0; i<g->n; ++i)
	{
		if (visit[i] == 0)
			BFS(g, i, visit);
	}
}

//创建有向图的邻接表
void createAGraph1(AGraph *&AG)
{
	
	int i, j, k;
	ArcNode *q;
	cout << "输入顶点数、边数:" << endl;
	cin >> AG->n >> AG->e;
	for (i = 0; i<AG->n; i++)
	{
		AG->adjlist[i].data = i;
		AG->adjlist[i].firstarc = NULL;
	}
	cout << "输入边(vi,vj)的顶点序号i,j:" << endl;
	for (k = 0; k<AG->e; ++k)
	{
		cin >> i >> j;
		//头插法
		q = (ArcNode*)malloc(sizeof(ArcNode));
		q->adjvex = j;
		q->nextarc = AG->adjlist[i].firstarc;
		AG->adjlist[i].firstarc = q;

	}
}

//创建无向图的邻接表
void createAGraph2(AGraph *&AG)
{
	int i, j, k;
	ArcNode *q;
	cout << "输入顶点数、边数:" << endl;
	cin >> AG->n >> AG->e;
	for (i = 0; i<AG->n; i++)
	{
		AG->adjlist[i].data = i;
		AG->adjlist[i].firstarc = NULL;
	}
	cout << "输入边(vi,vj)的顶点序号i,j:" << endl;
	for (k = 0; k<AG->e; ++k)
	{
		cin >> i >> j;
		//头插法
		q = (ArcNode*)malloc(sizeof(ArcNode));
		q->adjvex = j;
		q->nextarc = AG->adjlist[i].firstarc;
		AG->adjlist[i].firstarc = q;

		q = (ArcNode*)malloc(sizeof(ArcNode));
		q->adjvex = i;
		q->nextarc = AG->adjlist[j].firstarc;
		AG->adjlist[j].firstarc = q;

	}
}
AGraph *AG;
int main()
{
	AG = (AGraph*)malloc(sizeof(AGraph));
	createAGraph2(AG);
	//深度优先搜索遍历
	cout << "深度优先搜索遍历:" << endl;
	DFS(AG, 0);
	//广度优先搜索遍历
	cout << "\n广度优先搜索遍历:" << endl;
	int v[MaxSize] = {0};
	BFS(AG, 0, v);
	return 0;
}

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/HarvestWu/article/details/80807438
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞