#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;iNv;i++){
if(!Visited[i]&&Graph->G[V][i]!=INFINITY)DFS(Graph,i,Visit);
}
cnt--; //搜索完所有V的边,程序栈里元素个数-1
if(cnt==0) //如果程序栈中没有元素了,即:返回第一层递归,重新找一个没输出过的点来遍历
for(int i=0;iNv;i++){
if(!Visited[i])DFS(Graph,i,Visit);
}
}
阅读(266) | 评论(0) | 转发(0) |