Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32524
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 360
  • 用 户 组: 普通用户
  • 注册时间: 2021-09-17 18:39
文章分类

全部博文(35)

文章存档

2021年(35)

我的朋友

分类: C/C++

2021-11-05 14:58:31

#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) |
给主人留下些什么吧!~~