数据结构-有向图的邻接矩阵表示法(图的建立,深度优先遍历DFS,广度优先遍历BFS,非递归)

本文详细介绍了如何使用深度优先(DFS)和广度优先搜索(BFS)算法在有向图中进行遍历,包括邻接矩阵的创建、队列和栈的数据结构实现以及在实际场景中的应用。通过邻接矩阵的展示和实例演示,读者可以理解这两种搜索算法的工作原理和操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:( 深度优先与广度优先的结果不唯一,本文只提供一种解答)

#include <iostream>
#include <iomanip>
using namespace std;

//队列------------------------------------
typedef int QElemType;
const int QUEUE_INIT_SIZE = 100;
const int QUEUEINCREMENT = 10;
typedef struct {
	QElemType* data;
	int front;
	int rear;
	int queuesize;
	int incresize;
}SqQueue;
bool InitQueue(SqQueue& Q, int = QUEUE_INIT_SIZE, int = QUEUEINCREMENT);//初始化循环队列
int QueueLength(SqQueue Q);//返回队列长度
bool DeQueue(SqQueue& Q, QElemType& e);//将队首元素出队,用e返回
bool EnQueue(SqQueue& Q, QElemType e);//将元素e放入循环队列
bool GetHead(SqQueue Q, QElemType& e);//取队首元素,用e返回
bool incrementQueuesize(SqQueue& Q);//当循环队列空间不足时,动态扩充空间
bool QueueEmpty(SqQueue Q);//判断队列是否为空

bool InitQueue(SqQueue& Q, int maxsize, int incresize) {
	Q.data = new QElemType[maxsize];
	if (!Q.data)return 0;
	Q.front = Q.rear = 0;
	Q.queuesize = maxsize;
	Q.incresize = incresize;
	return 1;
}
int QueueLength(SqQueue Q) {
	return (Q.rear - Q.front + Q.queuesize) % Q.queuesize;
}
bool DeQueue(SqQueue& Q, QElemType& e) {
	if (Q.front == Q.rear)
		return 0;
	e = Q.data[Q.front];
	Q.front = (Q.front + 1) % Q.queuesize;
	return 1;
}
bool EnQueue(SqQueue& Q, QElemType e) {
	if ((Q.rear + 1) % Q.queuesize == Q.front)
		if (!incrementQueuesize(Q))
			return 0;
	Q.data[Q.rear] = e;
	Q.rear = (Q.rear + 1) % Q.queuesize;
	return 1;
}
bool GetHead(SqQueue Q, QElemType& e) {
	if (Q.rear == Q.front)
		return 0;
	e = Q.data[Q.front];
	return 1;
}
bool incrementQueuesize(SqQueue& Q) {
	QElemType* newdata = new QElemType[Q.queuesize + Q.incresize];
	if (!newdata)return 0;
	for (int i = 0; i < Q.queuesize; i++)
		newdata[i] = Q.data[(Q.front + i) % Q.queuesize];
	delete[] Q.data;
	Q.data = newdata;
	Q.front = 0; Q.rear = Q.queuesize - 1;
	Q.queuesize += Q.incresize;
	return 1;
}
bool QueueEmpty(SqQueue Q) {
	if (Q.front == Q.rear)
		return 1;
	return 0;
}

//栈--------------------------------------
typedef int SElemType;
typedef struct StackNode {
	SElemType data;
	struct StackNode* next;
}StackNode, * LinkStack;
bool InitStack(LinkStack& S);//初始化链栈
bool Push(LinkStack& S, SElemType e);//将元素e压入栈中
bool Pop(LinkStack& S, SElemType& e);//将首元素出栈,用元素e返回
bool StackEmpty(LinkStack S);//判断链栈是否为空
bool GetTop(LinkStack S, SElemType& e);//取链栈栈顶元素,用元素e返回

bool InitStack(LinkStack& S) {
	S = NULL;
	return 1;
}
bool Push(LinkStack& S, SElemType e) {
	StackNode* temp = new StackNode;
	if (!temp)return 0;
	temp->data = e;
	temp->next = S;
	S = temp;
	return 1;
}
bool Pop(LinkStack& S, SElemType& e) {
	if (S == NULL)return 0;
	StackNode* temp = S;
	e = S->data;
	S = S->next;
	delete temp;
	return 1;
}
bool StackEmpty(LinkStack S) {
	if (S == NULL)
		return 1;
	return 0;
}
bool GetTop(LinkStack S,SElemType& e){
	if (S == NULL)return 0;
	e = S->data;
	return 1;
}

//图-----------------------------------
const int MAX = 32767; //最大值∞设为MAX
const int MAX_VERTEX_NUM = 20;    //最大顶点个数
typedef char VexType; //顶点类型
typedef int VRType; //边的类型,无权图,用1或0表示是否相邻;带权图则为权值类型
typedef struct {
	VexType vexs[MAX_VERTEX_NUM]; //描述顶点的数组
	VRType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; //邻接矩阵	
	int vexnum, arcnum;  //图的当前顶点数和弧(边)数
}MGraph;
bool visited[MAX_VERTEX_NUM];//记录结点是否被访问过

bool CreateDG(MGraph& G);//建立有向图
int LocateVex(MGraph G, char v);//将顶点信息转为位次,便于程序计算
void PrintMGraph(MGraph G);
void DFSTraverse(MGraph G);//深度优先遍历
void DFS(MGraph G, int i);//从结点i开始进行深度优先遍历
void BFSTraverse(MGraph G);//广度优先遍历

bool CreateDG(MGraph& G) {
	char v1, v2;
	cout << "请依次输入有向图G的顶点数、弧数:" << endl;
	cin >> G.vexnum >> G.arcnum;
	cout << "请输入各顶点信息:" << endl;
	for (int i = 0; i < G.vexnum; i++)
		cin >> G.vexs[i];
	for (int i = 0; i < G.vexnum; i++)//将各个顶点之间边的值初始化为0
		for (int j = 0; j < G.vexnum; j++)
			G.arcs[i][j] = 0;
	cout << "输入有向弧:" << endl;
	for (int k = 0; k < G.arcnum; k++) {
		cin >> v1 >> v2;//输入两个点的信息
		int i = LocateVex(G, v1);//通过LocateVex函数将顶点转为位次,以供程序计算
		int j = LocateVex(G, v2);
		if (i == -1 || j == -1)//若顶点不存在则建立结束
			return 0;
		G.arcs[i][j] = 1;//将两点所连的边值变为1
	}
	return 1;
}
int LocateVex(MGraph G, char v) {
	for (int i = 0; i < G.vexnum; i++)//遍历所有顶点,当与v相等时返回位次值
		if (G.vexs[i] == v)
			return i;
	return -1;
}
void PrintMGraph(MGraph G) {
	for (int i = 0; i < G.vexnum; i++) {
		for (int j = 0; j < G.vexnum; j++) {
			cout << setw(3) << G.arcs[i][j];
		}
		cout << endl;
	}
}
void DFSTraverse(MGraph G) {
	int i;
	for (i = 0; i < G.vexnum; i++)//将所有结点初始化为false,即未访问
		visited[i] = false;

	for (i = 0; i < G.vexnum; i++)
	{
		if (!visited[i])//从一未被访问的结点开始进行深度优先遍历
			DFS(G, i);
	}
}
void DFS(MGraph G, int i) {
	LinkStack S; int temp;//用于接收临时数据
	InitStack(S); Push(S, i); //布置初始任务
	visited[i] = true;
	while (!StackEmpty(S)) { //每次处理一项任务
		Pop(S, temp);
		cout << setw(3) << G.vexs[temp];
		for (int j = 0; j <G.vexnum; j++)
			if (G.arcs[temp][j] && visited[j] == false)
			{
				visited[j] = true;
				Push(S, j);
			}
	}//while
}
void BFSTraverse(MGraph G) {
	SqQueue Q; InitQueue(Q, G.vexnum); //建立循环队列Q
	int temp;//用于接收临时数据
	for (int i = 0; i < G.vexnum; i++) //将所有结点初始化为false,即未访问
		visited[i] = false;
	for (int i = 0; i < G.vexnum; i++)//从第一个结点开始依位次执行
		if (!visited[i]) {//若已被访问过,则不执行
			visited[i] = true;
			cout << setw(3) << G.vexs[i];//访问第i个顶点
			EnQueue(Q, i);  //i入队列
			while (!QueueEmpty(Q)) {
				DeQueue(Q, temp); //队头元素出队并用temp来接收
				for (int j = 0; j < G.vexnum; j++)
					if (G.arcs[temp][j] && !visited[j]) {
						visited[j] = true;
						cout << setw(3) << G.vexs[j];//访问顶点w
						EnQueue(Q, j); //当前访问的顶点w入队列Q
					}//if
			}//while
		}//if
}

int main()
{
	MGraph G;
	cout << "*****构造有向图(邻接矩阵)*****" << endl;
	CreateDG(G);
	cout << "*******该图的邻接矩阵为:*******" << endl;
	PrintMGraph(G);
	cout << "*******深度优先搜索*******" << endl;
	DFSTraverse(G);
	cout << endl;
	cout << "*******广度优先搜索*******" << endl;
	BFSTraverse(G);
	cout << endl;
	return 0;
}

实验样例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜的大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值