****1.DFS 2.BFS 3.退出****
输入你的选择
1
输入图的点数和边数
3 3
输入图的点
1 2 3
依次输入边的两个端点
0 1
0 2
1 2
输入想要遍历的起点
0
广度优先遍历为:1 2 3
输入你的选择
2
输入图的点数和边数
3 3
输入图的点
1 2 3
依次输入边的两个端点
0 1
0 2
1 2
输入想要遍历的起点
1
深度优先遍历为:2 1 3
输入你的选择
3
退出程序
*/
#include<iostream>
#include<algorithm>
using namespace std;
const int maxsize=100;
bool visited[maxsize];
template <class Datatype>
class grahf
{
public:
grahf(Datatype a[],int n,int e);
~grahf();
void BFS(int v);
void DFS(int v);
private:
int bianshu,dianshu;
int bian[maxsize][maxsize];
Datatype dian[maxsize];
};
template <class Datatype>
grahf <Datatype>::~grahf()
{
dianshu=0;
bianshu=0;
}
template <class Datatype>
grahf <Datatype>::grahf(Datatype a[],int n,int e)
{
int i,j,k;
dianshu=n;
bianshu=e;
for(i=0;i<dianshu;i++)
{
dian[i]=a[i];
visited[i]=
无向图的遍历
最新推荐文章于 2021-01-28 23:36:05 发布
本文详细探讨了无向图的深度优先搜索(DFS)和广度优先搜索(BFS)算法,包括它们的基本思想、实现步骤及在实际问题中的应用。通过实例解析,帮助读者理解如何遍历无向图并解决相关问题。
摘要由CSDN通过智能技术生成