一个关于用邻接矩阵表示的无向图的深度遍历问题
程序代码:#define MaxVertexNum 10 /* 最大顶点数设为10 */
#define INFINITY 65535 /* ∞设为双字节无符号整数的最大值65535*/
typedef int Vertex; /* 用顶点下标表示顶点,为整型 */
typedef int WeightType; /* 边的权值设为整型 */
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv; /* 顶点数 */
int Ne; /* 边数 */
WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
};
typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
bool Visited[MaxVertexNum]; /* 顶点的访问标记 */
void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) ){
static cnt=0; //cnt模拟程序栈中元素个数
Visit(V);
Visited[V]=true;
cnt++; //程序栈里元素个数+1
for(int i=0;i<Graph->Nv;i++){
if(!Visited[i]&&Graph->G[V][i]!=INFINITY)DFS(Graph,i,Visit);
}
cnt--; //搜索完所有V的边,程序栈里元素个数-1
if(cnt==0) //如果程序栈中没有元素了,即:返回第一层递归,重新找一个没输出过的点来遍历
for(int i=0;i<Graph->Nv;i++){
if(!Visited[i])DFS(Graph,i,Visit);
}
}函数接口是题目给的,为了实现多个连通分量的遍历,用静态本地变量模拟了一个程序栈的进栈出栈的过程,但提交后还是有:改变MaxVertexNum的定义,处理较大规模的不连通的图-----这一点是错误的,感觉写的没问题,大家帮忙看看







深夜发帖,自顶