DFS以及BFS

一、用邻接矩阵实现DFS和BFS

DFS:深度优先

BFS:广度优先

#include<bits/stdc++.h>
using namespace std;
int visited[100]={0};
int visit[100]={0};
typedef struct
{
	char vexs[100];
	int arr[100][100];
	int vexnum,arcnum;
}AMGraph;
typedef struct
{
	int *base;
	int front;
	int rear;
}sqQueue;
int initGraph(AMGraph& G);			//初始化邻接矩阵
void showGraph(AMGraph G);			//打印邻接矩阵
int locatvex(AMGraph G, char u);		//定位顶点在邻接矩阵的下标
int createGraph(AMGraph& G);			//建立邻接矩阵
void DfsAM(AMGraph G,int i);			//深度优先搜索遍历
void BFSAM(AMGraph G, int i);			//广度优先搜索遍历
int initQueue(sqQueue& Q);			//初始化队列
int enQueue(sqQueue& Q, int i);			//入队
int firstvex(AMGraph G, int u);			//第一个邻接顶点		
int nextvex(AMGraph G,int w ,int u);	//下一个邻接顶点
 
int initGraph(AMGraph &G)
{
	cout<<"请输入你想要的顶点数和边数:"<<endl;
	cin>>G.vexnum>>G.arcnum;
	cout<<"请输入顶点信息:"<<endl;
	for(int i=1;i<=G.vexnum;i++)
	{
		cin>>G.vexs[i];
	}
	for(int i=1;i<=G.vexnum;i++)
	{
		for(int j=1;j<=G.vexnum;j++)
		{
			G.arr[i][j]=32767;
		}
	}
	return 1;
}
int locatevex(AMGraph G,char u)
{
	for(int i=1;i<=G.vexnum;i++)
	{
		if(u==G.vexs[i])
		{
			return i;
		}
	}
	return -1;
}
void showGraph(AMGraph G)
{
	for(int i=1;i<=G.vexnum;i++)
	{
		for(int j=1;j<=G.vexnum;j++)
		{
			if(G.arr[i][j]==32767)
			cout<<"∞"<<" ";
			else
			cout<<" "<<G.arr[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}
int createGraph(AMGraph& G)
{
	int i=0;int j=0;int w=0;
	char v1=0;char v2=0;
	cout<<"请输入两个顶点以及权值:"<<endl;
	for(int k=1;k<=G.arcnum;k++)
	{
		cin>>v1>>v2>>w;
		i=locatevex(G,v1);
		j=locatevex(G,v2);
		G.arr[i][j]=w;
	} 
	cout<<endl;
	return 1;
}

void DFSAM(AMGraph G,int i)
{
	cout<<G.vexs[i]<<" ";
	visited[i]=1;
	for(int j=1;j<=G.vexnum;j++)
	{
		if(G.arr[i][j]!=32767&&(!visited[j]))
		{
			DFSAM(G,j);
		}
	}
}
int initQueue(sqQueue& Q)
{
	Q.base=(int *)malloc(sizeof(int)*100);
	Q.front=Q.rear=0;
	return 1;
}
int enQueue(sqQueue& Q,int i)
{
	if((Q.rear+1)%100==Q.front)
		return 0;
	Q.base[Q.rear]=i;
	Q.rear=(Q.rear+1)%100;
	return 1;
}
int deQueue(sqQueue& Q,int &u)
{
	if(Q.rear==Q.front) 
		return 0;
	u=Q.base[Q.front];
	Q.front=(Q.front+1)%100;
	return 1;
}
int firstvex(AMGraph G,int u)
{
	for(int i=1;i<=G.vexnum;i++)
	{
		if(G.arr[u][i]!=32767&&visit[i]==0)
		{
			return i;
		}
	}
	return 32767;
}
int nextvex(AMGraph G,int w,int u)
{
	for(int i=w+1;i<=G.vexnum;i++)
	{
		if(G.arr[u][i]!=32767&&visit[i]==0)
		{
			return i;
		}
	}
	return 32767;
}
void BFSAM(AMGraph G,int i)
{
	cout<<G.vexs[i]<<" ";
	visit[i]=1;
	sqQueue Q;
	initQueue(Q);
	enQueue(Q,i);
	while(Q.rear!=Q.front)
	{
		int u=0;
		deQueue(Q,u);
		for(int w=firstvex(G,u);w!=32767;w=nextvex(G,w,u))
		{
			if(!visit[w])
			{
				cout<<G.vexs[w]<<" ";
				visit[w]=1;
				enQueue(Q,w);
			}
		}
	}
}
int main()
{
	AMGraph G;
	initGraph(G);
	createGraph(G);
	showGraph(G);
	cout<<"DFS结果为:"<<endl;
	DFSAM(G,1);
	cout<<endl;
	cout<<"BFS结果为:"<<endl;
	BFSAM(G,1);
	cout<<endl;	
	return 0;
}

二、用邻接表表示法实现DFS以及BFS

用邻接表存储图可以解决邻接矩阵的缺点问题

#include<bits/stdc++.h>
using namespace std;
struct ArcNode
{
	int adjvex;               //该边所指向的顶点的位置
	ArcNode * next;           //指向下一条边的指针
	                          //int weight;边上是否有权
};
typedef struct VNode
{
	char vertex;              //顶点信息
	ArcNode * firstarc;       //指向第一条依附该顶点的弧的指针 
}AdjList[20];
struct ALGraph
{
	AdjList adjList;
	int vexNum;               //图的顶点数
	int arcNum;               //图的边数
};
bool visited[20];//设置标志数组
void CreateGraph(ALGraph & graph);
void PrintGraph(ALGraph & graph);
void DFSTraverse(ALGraph & graph);
void BFSTraverse(ALGraph & graph);

void CreateGraph(ALGraph & graph)
{
	//1.输入顶点数和边数
	cout << "请输入图的顶点数: ";
	cin >> graph.vexNum;
	cout << "请输入图的边数: ";
	cin >> graph.arcNum;
 
	///2.输入顶点信息
	cout << "请输入" << graph.vexNum << "个顶点信息: ";
	for (int i = 0; i < graph.vexNum; i++)
	{
		cin >> graph.adjList[i].vertex;
		graph.adjList[i].firstarc=NULL;
	}
	///3.根据输入的边的信息构造邻接表
	cout << "请输入" << graph.arcNum << "个边的信息: \n";
	int h1, h2;
	ArcNode * temp;
	for (int i = 0; i < graph.arcNum; i++)
	{
		cin >> h1 >> h2;
		temp = new ArcNode;
		temp->adjvex = h2;
		temp->next = graph.adjList[h1].firstarc;
		graph.adjList[h1].firstarc = temp;
 
		temp = new ArcNode;
		temp->adjvex = h1;
		temp->next = graph.adjList[h2].firstarc;
		graph.adjList[h2].firstarc = temp;
	}
}
void PrintGraph(ALGraph & graph)
{
	for (int i = 0; i < graph.vexNum; i++)
	{
		cout << graph.adjList[i].vertex << "------>";
		ArcNode * p = graph.adjList[i].firstarc;
		while (p)
		{
			cout << graph.adjList[p->adjvex].vertex << "  ";
			p = p->next;
		}
 
		cout << endl;
	}
}
void DFS(ALGraph & graph, int v)
{
	visited[v] = true;
	cout << graph.adjList[v].vertex << " ";
 
	ArcNode * p = graph.adjList[v].firstarc;
 
	while (p)
	{
		if (!visited[p->adjvex])
			DFS(graph, p->adjvex);
 
		p = p->next;
	}
}
 
void DFSTraverse(ALGraph & graph)
{
	for (int i = 0; i < graph.vexNum; i++)//初始化访问标志数组
		visited[i] = false;
 
	for (int i = 0; i < graph.vexNum; i++)
	{
		if (!visited[i])//如果没有访问
			DFS(graph, i);
	}
}
void BFSTraverse(ALGraph & graph)
{
	for (int i = 0; i < graph.vexNum; i++)//初始化访问标志数组 
		visited[i] = false;
 
	queue<int> q;
 
	for (int i = 0; i < graph.vexNum; i++)
	{
		if (!visited[i])//如果没有访问过
		{
			visited[i] = true;
			q.push(i);//访问过的入队列
			cout << graph.adjList[i].vertex << " ";
 
			while (!q.empty())//队列不为空时
			{
				int x = q.front();
				q.pop();//先取出队首第一个元素,然后将第一个元素删除
				ArcNode * p = graph.adjList[x].firstarc;
				while (p)//访问未被访问过的邻接顶点
				{
					if (!visited[p->adjvex])
					{
						visited[p->adjvex] = true;
						cout << graph.adjList[p->adjvex].vertex << " ";
						q.push(p->adjvex);
					}
 
					p = p->next;
				}
			}
		}
	}
} 
int main()
{
	ALGraph graph;
	//1.创建邻接表
	CreateGraph(graph);
	//2.打印邻接表
	cout << "\n邻接表打印为: \n";
	PrintGraph(graph);
	//3.深度优先搜索DFS
	cout << "\n深度优先搜索DFS: ";
	DFSTraverse(graph);
	cout << endl;
 
	//4.广度优先搜索BFS
	cout << "\n广度优先搜索BFS: ";
	BFSTraverse(graph);
	cout << endl<<endl;
 
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值