DFS搜索,用邻接表存储图

本文介绍了一种基于深度优先搜索(DFS)算法遍历有向图的具体实现过程。通过C++代码展示了如何创建图、进行深度优先遍历,并提供了完整的程序示例。读者可以学习到如何使用递归实现DFS,以及如何在实际编程中应用这一算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//DFSTraverse.cpp
 //This function is to traver ALGraph by DFS Algorithm
 # include <iostream>
 # include <malloc.h>
 # include <conio.h>
 
 # define MAX_VERTEX_NUM 20
 # define OK 1
 
 using namespace std;
 
 typedef int VertexType;
 typedef int InfoType;
 
 typedef struct ArcNode        //define structure ALGraph
 {  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 CreateDG(ALGraph &G)    //CreateDG() sub-fuction
 {  int IncInfo,i,j,k,v1,v2,w;
    cout<<endl<<"Please input the number of G.vexnum (eg. G.vexnum=4): ";
    cin>>G.vexnum;        //input the number of vex
    cout<<"Please input the number of G.arcnum (eg. G.arcnum=4): ";
    cin>>G.arcnum;        //input the number of arc
    cout<<"Please input the number of IncInfo (0 for none)     : ";
    cin>>IncInfo;
    for(i=0;i<G.vexnum;++i)    //initial G.vertices
        {  G.vertices[i].data=i;
       G.vertices[i].firstarc=NULL;
        }
    cout<<"Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)...";
    for(k=0;k<G.arcnum;++k)    //input arc(v1,v2)
    {  cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum): ";
       cin>>v1;
       cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
       cin>>v2;
       i=v1;
       j=v2;
       while(i<1||i>G.vexnum||j<1||j>G.vexnum)    //if (v1,v2) illegal,again
        {  cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum) : ";
       cin>>v1;
       cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
       cin>>v2;
       i=v1;
       j=v2;
        } //while end
        i--;
        j--;
        ArcNode *p;
        p=(ArcNode *)malloc(sizeof(ArcNode));    //allocate memory
        if(!p)
        {  cout<<"Overflow!";        //if overflow
       return (0);
        }
        p->adjvex=j;            //assign p
        p->nextarc=G.vertices[i].firstarc;
        p->info=NULL;
        G.vertices[i].firstarc=p;
        if(IncInfo)
       {  cout<<"Please input the info :";
          cin>>*(p->info);        //input information
       } //if end
    } //for end
    return (OK);
 } //CreateDG() end
 
 void DFS(ALGraph G,int v,int *visited)    //DFS() sub-fuction
 {  int w;
    visited[v]=1;
    cout<<v+1<<"->";
    ArcNode *p;
    for(p = G.vertices[v].firstarc;p;p = p ->nextarc) 
 
    {
 
        w = p->adjvex;
        if(!visited[w]) DFS(G,w,visited);
    
    }
         //call DFS()
 } //DFS() end
 
 void DFSTraverse(ALGraph G)    //DFSTraverse() sub-function
 {  int v;
    int visited[MAX_VERTEX_NUM];
    for(v=0;v<G.vexnum;++v)
       visited[v]=0;        //initial visited[v]
    for(v=0;v<G.vexnum;++v)
      if(visited[v]==0)
     DFS(G,v,visited);    //call DFS()
 } //DFSTraverse() end
 
 void main()            //main() function
 {  ALGraph G;
    cout<<endl<<endl<<"DFSTraverse.cpp";
    cout<<endl<<"==============="<<endl;
    CreateDG(G);            //call CreateDG()
    cout<<"DFS Traverse is as follows :";
    cout<<endl<<endl<<"Begin->";
    DFSTraverse(G);        //call DFSTraverse()
    cout<<"End !"<<endl<<endl<<"...OK!...";
    getch();
 } //main() end
http://www.cnblogs.com/uniquews/archive/2012/02/22/2363157.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值