数据结构-图的遍历

#include<bits/stdc++.h>

using namespace std;

typedef int VertexType;
typedef int EdgeType;

#define MAXSIZE 20
#define MAXEDGE 20
#define MAXVEX 20
#define INF 0x3f3f3f

typedef struct{
	VertexType vex[MAXVEX];//定点 
	EdgeType edg[MAXVEX][MAXVEX];//矩阵
	int numv, nume;//记录这个图中的边数和顶点数  
	int cul, row;//记录存图的矩阵的行、列数 
}MGraph;

bool vis[MAXSIZE];//记录节点是否被访问过 

void CreateMGraph(MGraph *G){
	G->cul = 6;
	G->row = 6; 
	
	G->nume = 8;
	G->numv = 6;
	
	G->vex[0] = 1;
	G->vex[1] = 2;
	G->vex[2] = 3;
	G->vex[3] = 4;
	G->vex[4] = 5;
	G->vex[5] = 6;
	
	memset(G->edg, 0, sizeof(G->edg));
	
	G->edg[0][1] = 1;
	G->edg[0][5] = 1;
	G->edg[1][0] = 1;
	G->edg[1][2] = 1;
	G->edg[1][3] = 1;
	G->edg[2][1] = 1;
	G->edg[2][3] = 1;
	G->edg[2][4] = 1;
	G->edg[3][1] = 1;
	G->edg[3][2] = 1;
	G->edg[3][4] = 1;
	G->edg[4][2] = 1;
	G->edg[4][3] = 1;
	G->edg[4][5] = 1;
	G->edg[5][0] = 1;
	G->edg[5][4] = 1;
	
	return ;
}


void DFS(MGraph G, int i){
	vis[i] = 1;//遍历这个节点已经被访问过
	cout<<G.vex[i]<<" ";
	 for(int j = 0; j < G.numv; j ++ ){
	 	if(!vis[j] && G.edg[i][j] == 1){//如果j点和i之间有边且j点没有被访问过,就沿着j点继续遍历整个图 
		 	DFS(G, j); 
		 }
	 }
} 

void DFSMGraph(MGraph G){
	memset(vis, 0, sizeof(vis));//将所有节点初始化为没有访问过的状态 
	for(int i = 0; i < G.numv; i ++ ){
		if(!vis[i]){//如果这个节点没有被便利过,就遍历这个节点 
			DFS(G, i); 
		} 
	}
}

void BFS(MGraph G){
	
	memset(vis, 0, sizeof(vis));
	queue<int>q;//定义一个队列 
	
	for(int i = 0; i < G.numv; i ++ ){//遍历网图上的所有结点 
		if(!vis[i]){//如果这个结点没有入队 
			q.push(i);//让这个结点入队 
			vis[i] = 1;//标记这个节点已经入队 
			cout<<G.vex[i]<<" ";//输出这个节点的值 
			while(!q.empty()){//当队列不空时,即刚才入队的节点所连接的连通图还有没遍历到的节点 
				int t = q.front();//取出队头节点 
				q.pop();
				for(int j = 0; j < G.numv; j ++ ){ 
					if(!vis[j] && G.edg[t][j] == 1){//遍历所有节点,如果这个节点和对头结点有边相连,且这个节点没有被访问过,就访问这个节点 
						vis[j] = 1;//标记这个节点已经被访问过 
						q.push(j);//将这个节点入队 
						cout<<G.vex[j]<<" "; 
					}
				}
			}
		}
	}
}

void menu(){
	cout<<"****************************************"<<endl;
	cout<<"**********   1.构建网图       **********"<<endl;
	cout<<"**********   2.输出邻接矩阵   **********"<<endl;
	cout<<"**********   3.深度优先遍历   **********"<<endl;
	cout<<"**********   4.广度优先遍历   **********"<<endl;
	cout<<"**********   5.退出           **********"<<endl;
	cout<<"****************************************"<<endl; 
	cout<<"请选择你需要的功能:"<<endl;
}

void Print(MGraph G){
	for(int i = 0; i < G.cul; i ++ ){
		for(int j = 0; j < G.row; j ++ ){
			cout<<G.edg[i][j]<<" ";
		}
		cout<<endl;
	}
	return ;
}

int main()
{
	MGraph G;
	bool flag = false;//标记矩阵是否被构建 
	while(1){
		menu();
		int a;
		cin>>a;
		if(a == 1){
			CreateMGraph(&G);
			flag = true; 
			cout<<"构建成功!"<<endl;
		}
		else if(a == 2){
			if(!flag){
				cout<<"请先构建网图!"<<endl;
			}
			else{
				cout<<"邻接矩阵内容如下:"<<endl;
				Print(G);
			}
		}
		else if(a == 3){
			if(!flag){
				cout<<"请先构建网图!"<<endl;
			}
			else{
				cout<<"深度优先遍历结果如下:"<<endl;
				DFSMGraph(G);
			}
		}
		else if(a == 4){
			if(!flag){
				cout<<"请先构建网图!"<<endl;
			}
			else{
				cout<<"广度优先遍历结果如下:"<<endl; 
				BFS(G);
			}
		}
		else if(a == 5){
			cout<<"感谢使用!"<<endl;
			break;
		}
		else{
			cout<<"请输入正确的指令"<<endl;
		}
		cout<<endl;
	} 
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值