邻接矩阵的存储方式实现图的广度和深度优先遍历

在做图的邻接矩阵之前,先做好准备工作,定义存储类型,声明队列的操作(在广度优先遍历中使用)

#include 
#include 
#include 
#define INFINITY INT_MAX
#define MAXSIZE 20

typedef struct queue {
	int* base;
	int front;
	int rear;
}Queue;//定义队列

void Init(Queue* q) {
	q->base = (int*)malloc(sizeof(int) * MAXSIZE);
	if (!q->base) exit(0);
	q->front = q->rear = 0;
}//队列初始化
void EnQueue(Queue* q, int e) {
	q->base[q->rear++] = e;
}//入队
void DeQueue(Queue* q) {
	q->front++;
}//出队
int EmptyQueue(Queue* q) {
	if (q->front == q->rear) return 1;
	return 0;
}//判空

typedef struct ArcCell {
	int adj;
	int* info;
}ArcCell, AdjMatrix[MAXSIZE][MAXSIZE];
typedef struct {
	int vexs[MAXSIZE];
	AdjMatrix arcs;
	int vexnum, arcnum;
	char kind;
}MGraph;

具体图的操作

void CreateGraph(MGraph& G);//选择建立图的类型
void CreateUDN(MGraph& G);//无向网的建立
void CreateUDG(MGraph& G);//无向图的建立
void CreateDN(MGraph& G);//有向网的建立;
void CreateDG(MGraph& G);//有向图的建立
int LocateVex(MGraph& G, int v);//定位函数
void GraphPrint(MGraph& G);//邻接矩阵的打印
void DFSTraverse(MGraph& G);//深度优先遍历算法
void BFSTraverse(MGraph& G);//广度优先遍历算法
void DFS(MGraph& G, int v, int visited[]);

void CreateGraph(MGraph& G) {//选择建立图的类型
	int t;
	printf("-------------\n#输入图类型:\n【1】无向网\n【2】无向图\n【3】有向网\n【4】有向图\n-------------\n");
	scanf_s("%d", &t);
	switch (t) {
	case 1:CreateUDN(G); break;
	case 2:CreateUDG(G); break;
	case 3:CreateDN(G); break;
	case 4:CreateDG(G); break;
	}
}
int LocateVex(MGraph& G, int v) {
	for (int i = 0; i < G.vexnum; i++) {
		if (G.vexs[i] == v) return i;
	}
	return NULL;//没找到
}//定位
void CreateUDN(MGraph& G) {
	G.kind = 'UDN';
	printf("#分别输入:顶点数 弧数\n");
	scanf_s("%d %d", &G.vexnum, &G.arcnum);
	int i, j, k;
	printf("#输入%d个顶点:\n", G.vexnum);
	for (i = 0; i < G.vexnum; i++) {
		scanf_s("%d", &G.vexs[i]);
	}
	for (i = 0; i < G.vexnum; i++) {//用邻接矩阵的存储方式存储边和边之间的关系
		for (j = 0; j < G.vexnum; j++) {
			G.arcs[i][j] = { INFINITY,NULL };
		}
	}
	int v1, v2, w;
	printf("#输入%d对关系和权值:\n", G.arcnum);
	for (k = 0; k < G.arcnum; k++) {
		scanf_s("%d %d %d", &v1, &v2, &w);
		i = LocateVex(G, v1);
		j = LocateVex(G, v2);
		G.arcs[i][j].adj = w;
		G.arcs[j][i] = G.arcs[i][j];//无向网零阶矩阵关于主对角线对称;
	}
	printf("无向网建立成功!\n");
}//无向网创建
void CreateUDG(MGraph& G) {
	G.kind = 'UDG';
	printf("#分别输入:顶点数 弧数\n");
	scanf_s("%d %d", &G.vexnum, &G.arcnum);
	int i, j, k;
	printf("#输入%d个顶点:\n", G.vexnum);
	for (i = 0; i < G.vexnum; i++) {
		scanf_s("%d", &G.vexs[i]);
	}
	for (i = 0; i < G.vexnum; i++) {
		for (j = 0; j < G.vexnum; j++) {
			G.arcs[i][j] = { 0,NULL };
		}
	}
	printf("#输入%d对关系:\n", G.arcnum);
	int v1, v2;
	for (k = 0; k < G.arcnum; k++) {
		scanf_s("%d %d", &v1, &v2);
		i = LocateVex(G, v1);
		j = LocateVex(G, v2);
		G.arcs[i][j].adj = 1;
		G.arcs[j][i] = G.arcs[i][j];
	}
	printf("#无向图创建完成!");
}//无向图创建
void CreateDN(MGraph& G) {
	int totalquan = 0;
	G.kind = 'DN';
	printf("#分别输入:顶点数 弧数\n");
	scanf_s("%d %d", &G.vexnum, &G.arcnum);
	int i, j, k;
	printf("#输入%d个顶点:\n", G.vexnum);
	for (i = 0; i < G.vexnum; i++) {
		scanf_s("%d", &G.vexs[i]);
	}
	for (i = 0; i < G.vexnum; i++) {
		for (j = 0; j < G.vexnum; j++) {
			G.arcs[i][j] = { INFINITY,NULL };
		}
	}
	printf("#输入%d对关系和权值:\n", G.arcnum);
	int v1, v2, w;
	for (k = 0; k < G.arcnum; k++) {
		scanf_s("%d %d %d", &v1, &v2, &w);
		i = LocateVex(G, v1);
		j = LocateVex(G, v2);
		G.arcs[i][j].adj = w;
		totalquan += G.arcs[i][j].adj;
	}
	printf("有向网建立成功!\n");
	printf("有向网的总权值为:%d", totalquan);
}//有向网创建
void CreateDG(MGraph& G) {
	G.kind = 'DG';
	printf("#分别输入:顶点数 弧数\n");
	scanf_s("%d %d", &G.vexnum, &G.arcnum);
	int i, j, k;
	printf("#输入%d个顶点:\n", G.vexnum);
	for (i = 0; i < G.vexnum; i++) {
		scanf_s("%d", &G.vexs[i]);
	}
	for (i = 0; i < G.vexnum; i++) {
		for (j = 0; j < G.vexnum; j++) {
			G.arcs[i][j] = { 0,NULL };
		}
	}
	printf("#输入%d对关系:\n", G.arcnum);
	int v1, v2;
	for (k = 0; k < G.arcnum; k++) {
		scanf_s("%d %d", &v1, &v2);
		i = LocateVex(G, v1);
		j = LocateVex(G, v2);
		G.arcs[i][j].adj = 1;
	}
	printf("#有向图创建完成!\n");
}//有向图创建
void GraphPrint(MGraph& G) {
	int i, j;
	for (i = 0; i < G.vexnum; i++) {
		for (j = 0; j < G.vexnum; j++) {
			if (G.arcs[i][j].adj == INFINITY) printf("# ");
			else printf("%d ", G.arcs[i][j].adj);
		}
		printf("\n");
	}
}//矩阵打印
void DFSTraverse(MGraph& G) {//深度优先遍历
	int i, visited[MAXSIZE];
	for (i = 0; i < G.vexnum; i++) {
		visited[i] = 0;
	}
	printf("#选择初始访问点:\n");
	for (i = 0; i < G.vexnum; i++) {
		printf("【%d 】 ", G.vexs[i]);
		if (i == G.vexnum - 1) printf("\n");
	}
	printf("您选择:");
	scanf_s("%d", &i);
	printf("#深度遍历为:\n");
	DFS(G, i - 1, visited);
	for (i = 0; i < G.vexnum; i++) {
		if (visited[i] == 0) DFS(G, i, visited);
	}
}
void DFS(MGraph& G, int v, int visited[]) {//深度优先遍历
	visited[v] = 1;
	printf("%d ", G.vexs[v]);
	for (int i = 0; i < G.vexnum; i++) {
		if (G.arcs[v][i].adj == 1 && visited[i] == 0) DFS(G, i, visited);
	}
}
void BFSTraverse(MGraph& G)//广度优先遍历,使用队列进行辅助
{
	Queue q;
	Init(&q);
	int i, j, visited[MAXSIZE];
	for (i = 0; i < G.vexnum; i++) {
		visited[i] = 0;
	}
	printf("请选择起始点!\n");//输入起始节点!
	for (i = 0; i < G.vexnum; i++) {
		printf("【%d 】 ", G.vexs[i]);
		if (i == G.vexnum - 1) printf("\n");
		
	}
	printf("请输入:");
	scanf_s("%d", &i);
	i = i - 1;
	printf("#广度度遍历为:\n");
	printf("%d ", G.vexs[i]);
	visited[i] = 1;                  //2.将已访问的结点标志成1
	EnQueue(&q, G.vexs[i]);         //3.将第一个结点入队
	
		while (EmptyQueue(&q) == 0) {
			DeQueue(&q);
			for (j = 0; j < G.vexnum; j++) {
				if (visited[j] == 0) {
					printf("%d ", G.vexs[j]);
					visited[j] = 1;
					EnQueue(&q, G.vexs[j]);
				}
			}
		}
	}

void Meau() {
	printf("------------------------------------\n");
	printf("选择您想要的操作数:\n");
	printf("【1】建立\n");
	printf("【2】打印\n");
	printf("【3】深度遍历\n");
	printf("【4】广度遍历\n");
	printf("------------------------------------\n");
}//交互菜单

 主函数

int main() {
	MGraph G;
	int t;
	Meau();
	scanf_s("%d", &t);
	while (t != 0) {
		switch (t) {
		case 1:CreateGraph(G); break;
		case 2:GraphPrint(G); break;
		case 3:DFSTraverse(G); break;
		case 4:BFSTraverse(G); break;
		}
		printf("\n");
		scanf_s("%d", &t);
	}
}

你可能感兴趣的:(c语言,深度优先,算法,数据结构,广度优先)