数据结构与算法-004(C++实现图的邻接矩阵表示以及深度优先和广度优先遍历)

摘要:本文为C++实现图的临街矩阵表示和深度优先与广度优先遍历算法;

graphMatrix.h

#include<iostream>
using namespace std;
#define VERTEX_MAX 26 // 最大顶点数量
#define MAXVALUE 32767 //表示顶点之间不相连
struct MatrixGraph //adjacent matrix
{
	char Vertex[VERTEX_MAX];//顶点信息,序号或字母
	int Edges[VERTEX_MAX][VERTEX_MAX];//边权
	int isVisited[VERTEX_MAX];//遍历标志
	int VertexNum;//顶点数
	int EdgeNum;//边数
	int GraphType;//图类型,0,无向图,1,有向图
};
void createMatrixGraph(MatrixGraph*g)
{
	int i, j, k, weight;
	char start, end;//边的起始顶点
	cout << "输入各个顶点的信息:" << endl;
	for (i = 0; i < g->VertexNum; i++)
	{
		cout << "输入第" << i + 1 << "个顶点:";
		cin >> g->Vertex[i];
	}
	cout << "输入构成各边的两个顶点以及权值:" << endl;
	for (k = 0; k < g->EdgeNum; k++)
	{
		cout << "输入第" << k + 1 << "条边(v1 v2 w):";
		cin >> start >> end >> weight;
		for (i = 0; start != g->Vertex[i]; i++);//找到起始顶点位置
		for (j = 0; end != g->Vertex[j]; j++);
		g->Edges[i][j] = weight;
		if (g->GraphType == 0)
			g->Edges[j][i] = weight;
	}

}
void outputMatrixGraph(MatrixGraph*g)
{
	int i, j;
	cout << '\t';
	for (j = 0; j < g->VertexNum; j++)//首行输出顶点
		cout << g->Vertex[j] << "\t";
	cout << endl;
	for (i = 0; i < g->VertexNum; i++)
	{
		cout << g->Vertex[i];
		for (j = 0; j < g->VertexNum;j++)
		{
			if (g->Edges[i][j] == MAXVALUE)
				cout << "\t∞ ";
			else
				cout << "\t" << g->Edges[i][j];
		}
		cout << endl;
	}

}

void testGraphMatrix()
{
	MatrixGraph* g=new MatrixGraph[1];
	int i, j;
	cout << "输入图的类型(0无向图,1有向图):" << endl;
	cin >> g->GraphType;
	cout << "输入顶点数量和边数量:" << endl;
	cin >> g->VertexNum >> g->EdgeNum;
	for (i = 0; i < g->VertexNum; i++)
		for (j = 0; j < g->VertexNum; j++)
			g->Edges[i][j] = MAXVALUE;
	createMatrixGraph(g);
	cout << "邻接矩阵:" << endl;
	outputMatrixGraph(g);
}

graphTraverse.h

#include<iostream>
#include"graphMatrix.h"
#define QUEUE_MAXSIZE 30
using namespace std;
struct SeqQueue
{
	int data[QUEUE_MAXSIZE];//数据域
	int head;//队头
	int tail;

};

//队列的操作
void queueInit(SeqQueue*q)
{
	q->head = q->tail = 0;
}
int queueIsEmpty(SeqQueue*q)
{
	return q->head == q->tail;
}
int queueIn(SeqQueue*q, int ch)
{
	if ((q->tail + 1) % QUEUE_MAXSIZE == q->head)//队列已满
		return 0;
	q->data[q->tail] = ch;
	q->tail = (q->tail + 1) % QUEUE_MAXSIZE;
	return 1;
}
int queueOut(SeqQueue*q, int *ch)
{
	if (q->head == q->tail)
		return 0;
	*ch = q->data[q->head];
	q->head = (q->head + 1) % QUEUE_MAXSIZE;
	return 1;
}
void DFSM(MatrixGraph*g, int i);
void DFSTraverse(MatrixGraph*g)//深度优先遍历
{
	int i;
	for (i = 0; i < g->VertexNum; i++)
		g->isVisited[i] = 0;
	cout << "深度优先遍历:" << endl;
	for (i = 0; i < g->VertexNum; i++)
		if (!g->isVisited[i])
			DFSM(g, i);
	cout << endl;
}
void DFSM(MatrixGraph*g,int i)//从第i个顶点开始,深度遍历图
{
	int j;
	g->isVisited[i] = 1;
	cout << "->" << g->Vertex[i];
	for (j = 0; j < g->VertexNum; j++)
		if (g->Edges[i][j] != MAXVALUE && !g->isVisited[j])
			DFSM(g, j);

}
void BFSM(MatrixGraph*g, int k);
void BFSTraverse(MatrixGraph*g)
{
	int i;
	for (i = 0; i < g->VertexNum; i++)
		g->isVisited[i] = 0;
	cout << "广度优先遍历:" << endl;
	for (i = 0; i < g->VertexNum; i++)
		if (!g->isVisited[i])
			BFSM(g, i);
	cout << endl;
}
void BFSM(MatrixGraph*g, int k)
{
	int i, j;
	SeqQueue *q=new SeqQueue[1];
	queueInit(q);
	g->isVisited[k] = 1;
	cout << "->" << g->Vertex[k];
	queueIn(q, k);
	while (!queueIsEmpty(q))
	{
		queueOut(q, &i);
		for (j = 0; j < g->VertexNum; j++)
		{
			if (g->Edges[i][j] != MAXVALUE && !g->isVisited[j])
			{
				cout << "->" << g->Vertex[j];
				g->isVisited[j] = 1;
				queueIn(q, j);
			}
		}	
	}
}

void testTraverse()
{
	MatrixGraph* g = new MatrixGraph[1];
	int i, j;
	cout << "输入图的类型(0无向图,1有向图):" << endl;
	cin >> g->GraphType;
	cout << "输入顶点数量和边数量:" << endl;
	cin >> g->VertexNum >> g->EdgeNum;
	for (i = 0; i < g->VertexNum; i++)
		for (j = 0; j < g->VertexNum; j++)
			g->Edges[i][j] = MAXVALUE;
	createMatrixGraph(g);
	cout << "邻接矩阵:" << endl;
	outputMatrixGraph(g);
	DFSTraverse(g);
	BFSTraverse(g);
}

 

 

 

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