//vertex []存放顶点
//edge [][]存放边的数组,图中的边以邻接矩阵形式存放
//visted []存放已经访问的顶点,1代表已经访问
void shendu(int v)
{
cout<<vertex[v];
visted[v]=1;
for(int i=0;i<vertexnum;i++)
{
if(edge[v][i]==1&&visted[i]==0)
shendu(i);
}
}
//q []队列,存储已被访问的顶点
void guangdu(int v)
{
int q[Max],w,j;
int Front=-1,rear=-1;
cout<<vertex[v];
visted[v]=1;
q[++rear]=v;//被访问顶点入队
while(Front!=rear)
{
w=q[++Front];//队首元素出队
for(j=0;j<vertexnum;j++)
{
if(edge[w][j]==1&&visted[j]==0)
{
cout<<vertex[j];
visted[j]=1;
q[++rear]=j;
}
}
}
}
图的深度遍历和广度遍历
最新推荐文章于 2024-05-09 11:45:55 发布