#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
const Size = 10;
class MGraph{
public:
MGraph(string a[],int n,int e);
~MGraph(){};
void DFSTraverse(){ for(int j=0;j<Size;j++)
{visited[j] = 0;} DFSTraverse(0);}
void BFSTraverse(int v);
void Approximal();
private:
void DFSTraverse(int v);
string vertex[Size];
int arc[Size][Size];
int vertexNum,arcNum;
int visited[Size];
};
MGraph::MGraph(string a[],int n,int e)
{
vertexNum = n;
arcNum = e;
int i;
for(i=0;i<vertexNum;i++)
{
vertex[i] = a[i];
}
for(i=0;i<arcNum;i++)
{
for(int k=0;k<vertexNum;k++)
{
arc[i][k] = 0;
}
}
for(i=0;i<arcNum;i++)
{
int j,k;
cout<<"请输入依附的两个点。"<<endl;
cin>>k>>j;
arc[k][j] = 1;
arc[j][k] = 1;
}
}
void MGraph::DFSTraverse(int v)
{
cout<<vertex[v];
visited[v] = 1;
for(int i=0;i<vertexNum;i++)
{
if(arc[v][i] == 1 && visited[i] == 0)
{
DFSTraverse(i);
}
}
}
void MGraph::BFSTraverse(int v)
{
for(int j=0;j<Size;j++)
{
visited[j] = 0;
}
int front,rear;
front = rear = -1;
cout<<vertex[v];
visited[v] = 1;
int Q[Size];
Q[++rear] = v;
while(front != rear)
{
v = Q[++front];
for(int i=0; i<vertexNum;i++)
{
if(arc[v][i] == 1 && visited[i] == 0)
{
cout<<vertex[i];
visited[i] = 1;
Q[++rear] = i;
}
}
}
}
void MGraph::Approximal()
{
for(int i=0;i<vertexNum;i++)
{
cout<<vertex[i]<<"的邻接点为";
for(int j=0;j<vertexNum;j++)
{
if(arc[i][j] == 1)
{
cout<<setw(8)<<vertex[j];
}
}
cout<<endl;
}
}
void main()
{
int n=0,e=0;
string a[Size];
cout<<"请输入顶点数和边数"<<endl;
cin>>n>>e;
cout<<"请依次输入顶点内容"<<endl;
for(int m=0;m<n;m++)
{
cin>>a[m];
}
MGraph mgraph(a,n,e);
cout<<"广度遍历:";
mgraph.BFSTraverse(0);
cout<<endl;
cout<<"深度遍历:";
mgraph.DFSTraverse();
cout<<endl;
cout<<"各个顶点的邻接点:"<<endl;
mgraph.Approximal();
#include<iomanip>
#include<string>
using namespace std;
const Size = 10;
class MGraph{
public:
MGraph(string a[],int n,int e);
~MGraph(){};
void DFSTraverse(){ for(int j=0;j<Size;j++)
{visited[j] = 0;} DFSTraverse(0);}
void BFSTraverse(int v);
void Approximal();
private:
void DFSTraverse(int v);
string vertex[Size];
int arc[Size][Size];
int vertexNum,arcNum;
int visited[Size];
};
MGraph::MGraph(string a[],int n,int e)
{
vertexNum = n;
arcNum = e;
int i;
for(i=0;i<vertexNum;i++)
{
vertex[i] = a[i];
}
for(i=0;i<arcNum;i++)
{
for(int k=0;k<vertexNum;k++)
{
arc[i][k] = 0;
}
}
for(i=0;i<arcNum;i++)
{
int j,k;
cout<<"请输入依附的两个点。"<<endl;
cin>>k>>j;
arc[k][j] = 1;
arc[j][k] = 1;
}
}
void MGraph::DFSTraverse(int v)
{
cout<<vertex[v];
visited[v] = 1;
for(int i=0;i<vertexNum;i++)
{
if(arc[v][i] == 1 && visited[i] == 0)
{
DFSTraverse(i);
}
}
}
void MGraph::BFSTraverse(int v)
{
for(int j=0;j<Size;j++)
{
visited[j] = 0;
}
int front,rear;
front = rear = -1;
cout<<vertex[v];
visited[v] = 1;
int Q[Size];
Q[++rear] = v;
while(front != rear)
{
v = Q[++front];
for(int i=0; i<vertexNum;i++)
{
if(arc[v][i] == 1 && visited[i] == 0)
{
cout<<vertex[i];
visited[i] = 1;
Q[++rear] = i;
}
}
}
}
void MGraph::Approximal()
{
for(int i=0;i<vertexNum;i++)
{
cout<<vertex[i]<<"的邻接点为";
for(int j=0;j<vertexNum;j++)
{
if(arc[i][j] == 1)
{
cout<<setw(8)<<vertex[j];
}
}
cout<<endl;
}
}
void main()
{
int n=0,e=0;
string a[Size];
cout<<"请输入顶点数和边数"<<endl;
cin>>n>>e;
cout<<"请依次输入顶点内容"<<endl;
for(int m=0;m<n;m++)
{
cin>>a[m];
}
MGraph mgraph(a,n,e);
cout<<"广度遍历:";
mgraph.BFSTraverse(0);
cout<<endl;
cout<<"深度遍历:";
mgraph.DFSTraverse();
cout<<endl;
cout<<"各个顶点的邻接点:"<<endl;
mgraph.Approximal();
}