深度优先遍历

有向邻接表的深度优先遍历

#include<iostream>               ///深度优先搜索,通过有向图的邻接表存储
#include<stdio.h>
#include<string.h>
#include<malloc.h>               ///有向图的邻接表中,第i个链表的的结点数只是顶点vi的出度,为求入度,必须遍历整个邻接表或是建立有向图的逆邻接表

#define MAX_VERTEX_NUM 20
#define MAX_NAME 5
#define OK 1
#define TRUE 1
#define FALSE -1
typedef char VertexType[MAX_NAME];    
typedef int InfoType;       
using namespace std;
typedef struct ArcNode                           //单链表中结点的类型
{
    int adjvex;                              //该边指向的顶点在顺序表中的位置
    struct ArcNode *nextarc;  
    InfoType *info;                          //网的权值指针
}ArcNode;

typedef struct VNode                             //定义顶点类型
{
    VertexType data;
    ArcNode *firstarc; 
}VNode, AdjList[MAX_VERTEX_NUM];

typedef struct      
{   AdjList vertices;                        //顺序表
    int vexnum, arcnum;    
    int kind;
}ALGraph;
int LocateVex(ALGraph &G, VertexType &u)         //返回顶点u在vertices中的位置
{
   for(int i = 0;i < G.vexnum; ++i)
   {
       if(strcmp(G.vertices[i].data,u) == 0)
           return i;
   }
   return FALSE ;
}

int CreateDG( ALGraph &G )                       //有向图的邻接表
{
   int  i,j,k,w;
  VertexType v1,v2;
   cout <<"开始构造有向图:\n请输入图的顶点的个数:";
    cin >> G.vexnum;
    cout << "请输入图的边的数目:";
    cin >> G.arcnum;
  cout <<"请输入所有的顶点:\n";
  for(i = 0; i < G.vexnum; ++i)
    {
            cout << "请输入第"<< i+1 << "个顶点:";
            scanf("%s",G.vertices[i].data);
        G.vertices[i].firstarc = NULL;
    }
  cout <<"请输入弧,例如 v1-->v2\n";
  for(k = 0; k < G.arcnum; ++k)
    {
           cout << "请输入第" << k+1 << "条弧的弧尾";
       cin >> v1;
       cout << "请输入第" << k+1 << "条弧的弧头";
       cin >> v2;
           i = LocateVex(G,v1);
       j = LocateVex(G,v2);
       ArcNode *p;
       p = (ArcNode *)malloc(sizeof(ArcNode));
       if(!p)
       {
           cout << "Overflow";
           return 0;
       }
       p->adjvex = j;
       p->nextarc = G.vertices[i].firstarc;       //头插法
       G.vertices[i].firstarc  = p;
       p->info = NULL;
    }   // for end



   return OK;
}  // CreateDG() end

void DFS(ALGraph G, int v, int *visited)              //如果该图是连通图,只需从G.vertices[0]出发便可以遍历图中的所有节点
{                                                     
        ArcNode *p;
    int w;
    visited[v] = TRUE;
    cout << G.vertices[v].data << "->";
    for(p = G.vertices[v].firstarc; p != NULL; p = p->nextarc)
    {
        if(visited[p->adjvex] == 0)
        {
            visited[p->adjvex] = 0;
            DFS(G, p->adjvex,visited);   //不断的访问节点的邻接点
        }
    }


}  // DFS() end
void DFSTraverse( ALGraph &G )
{
    int v;
    int visited[MAX_VERTEX_NUM];
    for( v = 0; v < G.vexnum; ++v )
        visited[v] = 0;                       //visited 为辅助数组用于标记顶点是否被访问
    for( v = 0; v < G.vexnum; ++v )
        if(visited[v] == 0 )
            DFS(G,v,visited); 
}//DFSTraverse() end
void DestroyALGraph(ALGraph &G)                      //销毁分配的空间
{
    ArcNode *q;
    for(int i = 0; i < G.vexnum; ++i)
        for(ArcNode *p = G.vertices[i].firstarc; p != NULL;)
        {
          G.vertices[i].firstarc = NULL;
          q = p;
              p = p->nextarc;
              free(q);

        }
}
void main()
{
    ALGraph G;
    cout << endl << "DFSTraverse.cpp";
    cout << endl << "===================" << endl;
    CreateDG(G);   
    cout <<"深度优先搜索:\n";
    DFSTraverse(G);
        DestroyALGraph(G);

}// main end

猜你喜欢

转载自blog.csdn.net/weixin_40472158/article/details/78906992
今日推荐