邻接表、邻接矩阵的存储结构、及其在他们的基础上进行BFS、DFS

直接上代码,代码中带有详细的测试数据

分别在领接表上实现了DFS和BFS,以及在邻接矩阵上面实现了DFS,BFS。

#include<bits/stdc++.h>
using namespace std;
const int MaxVertexNum=100;
typedef int VexType;//节点类型 
typedef int EdgeType;//边类型 
//邻接矩阵存储图
bool visited[MaxVertexNum];//初始值全为false 
typedef struct{
	VexType Vex[MaxVertexNum];
	EdgeType Edge[MaxVertexNum][MaxVertexNum];
	int vexnum,arcnum;
}MGraph;

//邻接表存储图利用vector实现 
//struct Node{
//	int v,weight;//v表示节点信息,weight表示权重;
//	Node(int _v,int _n){
//		v = _v;weight = _n;
//	}
//};
vector<int> ADJGraph[MaxVertexNum];
 
//邻接表存储图,利用结构体实现
typedef struct ArcNode{//边节点 
	VexType  adjvex;//这条弧所指向的节点
	int weight;//这条弧所对应的权值
	ArcNode *next;//下一条弧 
}ArcNode;
typedef struct VNode{
	VexType data;//这个节点的数据域
	ArcNode *first;//指向该节点的第一条弧 
}VNode;
struct ALGraph{
	VNode AdjList[MaxVertexNum];//构建邻接表 
	int vexnum,arcnum;
};
MGraph MG;
ALGraph AG;

/*
测试数据1: 
5
0 1 0 0 1
1 0 1 0 1
0 1 0 1 1
0 0 1 0 0
1 1 1 0 0

测试数据2:
6
0 1 0 0 0 0
1 0 1 0 1 0
0 1 0 1 0 1
0 0 1 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0 
*/
 //1、通过邻接矩阵来建图 
void Creat_MGraph(){
	cout<<"输入一个整数n表示本图的节点数\n之后再输入n个节点信息表示图的节点数据,类型为char。\n最后输入一个n*n的矩阵\n";
	int n;
	cin>>n;
	MG.vexnum = n; MG.arcnum=0;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++){
			cin>>MG.Edge[i][j];
			if(MG.Edge[i][j]<=0) MG.arcnum++; 
		}
}


/*
测试数据: 
5
0 1 4
1 0 2 4
2 1 3 4
3 2
4 0 1 2
*/
//通过邻接表来创建图 
void Creat_ALGraph1(){ //这种结构体邻接表在编程时不适用,最好还是利用vector来创建邻接表 
	int n;cin>>n;
	AG.vexnum = n;
	for(int i=0;i<n;i++){
		cin>>AG.AdjList[i].data;
		char c = getchar();
		ArcNode *p = AG.AdjList[i].first;
		while(c!='\n'){
			ArcNode *N = (ArcNode *)malloc(sizeof(ArcNode));
			cin>>N->adjvex;c = getchar();
			if(p==NULL)
				p = N;
			else{
				p->next = N;
				p = N;
			} 
		}
		p->next=NULL;
	}
} 
/*
测试数据: 
6
0 1
0 4
1 2
1 4
2 3
2 4
*/ 
void Creat_ALGraph(){
	int n;cin>>n;
	for(int i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		ADJGraph[a].push_back(b);
		ADJGraph[b].push_back(a);
	}
}
//邻接表BFS 
void BFS(int v){
	visited[v] = true;
	printf("%d ",v);
	queue<int> q;
	q.push(v);
	while(!q.empty()){
		int p = q.front();
		q.pop();
		for(int i = 0;i<ADJGraph[p].size();i++){
			if(!visited[ADJGraph[p][i]]){
				visited[ADJGraph[p][i]] = true;
				printf("%d ",ADJGraph[p][i]);
				q.push(ADJGraph[p][i]);
			}
		}
	}
}
//邻接表DFS 
int DFS(int v){
	visited[v] = true;
	printf("%d ",v);
	for(int i=0;i<ADJGraph[v].size();i++){
		int p = ADJGraph[v][i];
		if(!visited[p])
			DFS(p);
	}
}
//邻接矩阵DFS 
int DFS_M(int v){
	printf("%d ",v);
	visited[v] = true;
	for(int i = 0;i<MG.vexnum;i++){
		if(MG.Edge[v][i]==1&&!visited[i])
			DFS_M(i);
	}
}
//邻接矩阵BFS
int BFS_M(int v){
	queue<int> Q;
	printf("%d ",v);visited[v] = true;
	Q.push(v);
	while(!Q.empty()){
		int p = Q.front();Q.pop();
		for(int i=0;i<MG.vexnum;i++){
			if(MG.Edge[p][i]==1&&!visited[i]){
				printf("%d ",i);
				visited[i] = true;
				Q.push(i);
			}
		}
	}
} 
int main(){
	freopen("1.txt","r",stdin);
//	Creat_MGraph();
//	Creat_MGraph();
//	BFS_M(0);
}

猜你喜欢

转载自blog.csdn.net/qq_39072627/article/details/107209233