邻接矩阵的深度优先遍历(递归以及非递归),广度优先遍历

#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20

typedef int AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
	int vexnum,arcnum;
	char vexs[MAX_VERTEX_NUM];
	AdjMatrix arc;
}MGraph;
typedef struct{
	int *base;
	int top;
	int stacksize;
}SqStack;
typedef struct{
	int *Qbase;
	int front,rear;
}SqQueue;

int visited[MAX_VERTEX_NUM];

int LocateAdj(MGraph G, char v){
	int  i;
	
	for(i = 0; i < G.vexnum; i ++){
		if(G.vexs[i] == v)
			return i;
	}
	
	return -1;
}

void CreateAdjMatrix(MGraph &G){
	int i,j,k;
	char v1,v2;
	
	printf("please input the vexnum and arcnum:\n");
	scanf("%d%d",&G.vexnum,&G.arcnum);
	
	getchar();
	
	printf("please input the vexs:\n");
	for(i = 0; i < G.vexnum; i ++)
		scanf("%c",&G.vexs[i]);
		
	for(i = 0; i < G.vexnum; i ++)
		for(j = 0; j < G.vexnum; j ++)
			G.arc[i][j] = 0;
	
	printf("please inputb the arc:\n");
	for(k = 0; k < G.arcnum; k ++){
		getchar();
		scanf("%c%c",&v1,&v2);
		
		i = LocateAdj(G,v1);	j = LocateAdj(G,v2);
		
		G.arc[i][j] = G.arc[j][i] = 1;
	}
}

int FirstAdj(MGraph G, int i){
	int j;
	
	for(j = 0; j < G.vexnum; j ++){
		if(G.arc[i][j])
			return j;
	}
	
	return -1;
}

int NextAdj(MGraph G, int i, int j){
	for(j = j + 1; j < G.vexnum; j ++){
		if(G.arc[i][j])
			return j;
	}
	
	return -1;
}

void DFS(MGraph G, int i){
	int j; 
	
	visited[i] = 1; 
	printf("%3c",G.vexs[i]);
	
	for(j = FirstAdj(G,i); j >= 0; j = NextAdj(G,i,j)){
		if(!visited[j])
			DFS(G,j);
	}
}

void DFSTraverse1(MGraph G){
	int i;
	
	for(i = 0; i < G.vexnum; i ++)
		visited[i] = 0;
	
	for(i = 0; i < G.vexnum; i ++){
		if(!visited[i])
			DFS(G,i);
	}
}

void InitStack(SqStack &S){
	S.base = (int *)malloc(sizeof(int) * MAX_VERTEX_NUM);
	
	if(!S.base)
		return ;
	
	S.top = 0;
	S.stacksize = MAX_VERTEX_NUM;
}

void Push(SqStack &S, char ch){
	if(S.top >= S.stacksize){
		S.base = (int *)realloc(S.base,(S.stacksize + MAX_VERTEX_NUM) * sizeof(int));
		
		if(!S.base)
			return ;
		
		S.stacksize += MAX_VERTEX_NUM;
	}
	
	S.base[S.top ++] = ch;
}

int StackEmpty(SqStack S){
	if(!S.top)
		return 1;
		
	return 0;
}

void Pop(SqStack &S, int &i){
	if(!S.top)
		return ;
	
	i = S.base[-- S.top];
}

void GetTop(SqStack S, int &i){
	i = S.base[S.top - 1];
}

void DFS2(MGraph G, int i){
	int j,k;
	SqStack S;
	InitStack(S);
	Push(S,i);
	printf("%3c",G.vexs[i]);
	visited[0] = 1;
	
	while(!StackEmpty(S)){
		
		GetTop(S,i);
	
		for(j = 0; j < G.vexnum; j ++){
			if(G.arc[i][j] && !visited[j]){
				Push(S,j);
				printf("%3c",G.vexs[j]);
				visited[j] = 1;
				i = j;
				j = 0;
			}
		}
		
		if(!StackEmpty(S)){
			Pop(S,k);
		}
	}
}

void DFSTraverse2(MGraph G){
	int i;
	
	for(i = 0; i < G.vexnum; i ++)
		visited[i] = 0;	
	
	for(i = 0; i < G.vexnum; i ++){
		if(!visited[i])
			DFS2(G,i);
	}
}

void InitQueue(SqQueue &Q){
	Q.Qbase = (int *)malloc(sizeof(int) * MAX_VERTEX_NUM);
	
	if(!Q.Qbase)
		return ;
	
	Q.rear = Q.front = 0;
}

void EnQueue(SqQueue &Q, int i){
	Q.Qbase[Q.rear ++] = i;
}

void DeQueue(SqQueue &Q, int &i){
	i = Q.Qbase[Q.front ++];
}

int QueueEmpty(SqQueue Q){
	if(Q.front == Q.rear)
		return 1;
	
	return 0;
}

void BFS(MGraph G, int i){
	int k,j;
	SqQueue Q;
	
	InitQueue(Q);
	printf("%3c",G.vexs[i]);
	visited[i] = 1;
	EnQueue(Q,i);
	
	while(!QueueEmpty(Q)){
		DeQueue(Q,k);
		
		for(j = FirstAdj(G,k); j >= 0; j = NextAdj(G,k,j)){
			if(!visited[j]){
				visited[j] = 1;
				printf("%3c",G.vexs[j]);
				EnQueue(Q,j);
			}
		}
	}
}

void BFSTraverse(MGraph G){
	int i;
	
	for(i = 0; i < G.vexnum; i ++)
		visited[i] = 0;
	
	for(i = 0; i < G.vexnum; i ++){
		if(!visited[i])
			BFS(G,i);
	}
}

int main(){
	MGraph G;
	
	CreateAdjMatrix(G);
	
	DFSTraverse1(G);
	putchar('\n');
	
	DFSTraverse2(G);
	putchar('\n');
	
	BFSTraverse(G);
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值