码迷,mamicode.com
首页 > 编程语言 > 详细

图的深度优先遍历(DFS)—递归算法

时间:2019-03-31 15:42:11      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:递归算法   sizeof   define   null   star   png   深度优先   ==   struct   

实验环境:win10, DEV C++5.11 

实验要求:

  实现图的深度优先遍历

技术图片

实验代码:

#include <iostream>
#define maxSize 255
#include "stdlib.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/*the definition of struct*/
typedef struct ArcNode{
    int adjvex;
    struct ArcNode * nextarc;
    int info;
}ArcNode;
typedef struct{
    char data;
    ArcNode *firstarc;    
}VNode;
typedef struct{
    VNode adjlist[maxSize];
    int n,e;
}AGraph;
/*to create the adjoin graph*/
void createGraph(AGraph &G){
    G.adjlist[0].data=a;
    G.adjlist[1].data=b;
    G.adjlist[2].data=c;
    G.adjlist[3].data=d;
    G.adjlist[4].data=e;
    G.adjlist[5].data=f;
    G.n=6;
    G.adjlist[0].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[0].firstarc->adjvex=1;
    G.adjlist[0].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[0].firstarc->nextarc->adjvex=3;
    G.adjlist[0].firstarc->nextarc->nextarc=NULL;    
    G.adjlist[1].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[1].firstarc->adjvex=0;
    G.adjlist[1].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[1].firstarc->nextarc->adjvex=2;
    G.adjlist[1].firstarc->nextarc->nextarc=NULL;    
    G.adjlist[2].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[2].firstarc->adjvex=1;
    G.adjlist[2].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[2].firstarc->nextarc->adjvex=3;
    G.adjlist[2].firstarc->nextarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[2].firstarc->nextarc->nextarc->adjvex=4;
    G.adjlist[2].firstarc->nextarc->nextarc->nextarc=NULL;    
    G.adjlist[3].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[3].firstarc->adjvex=0;
    G.adjlist[3].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[3].firstarc->nextarc->adjvex=2;
    G.adjlist[3].firstarc->nextarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[3].firstarc->nextarc->nextarc->adjvex=4;
    G.adjlist[3].firstarc->nextarc->nextarc->nextarc=NULL;    
    G.adjlist[4].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[4].firstarc->adjvex=2;
    G.adjlist[4].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[4].firstarc->nextarc->adjvex=3;
    G.adjlist[4].firstarc->nextarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[4].firstarc->nextarc->nextarc->adjvex=5;
    G.adjlist[4].firstarc->nextarc->nextarc->nextarc=NULL;    
    G.adjlist[5].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
    G.adjlist[5].firstarc->adjvex=4;
    G.adjlist[5].firstarc->nextarc=NULL;
    G.e=6;    
}
int isVisit[maxSize]={0};
void visit(AGraph &G,int v){
    printf("%c",G.adjlist[v].data);
}
/*deepth first search using recursive*/
void DFS(AGraph &G,int v){
    ArcNode *p;
    isVisit[v]=1;
    visit(G,v);
    p=G.adjlist[v].firstarc;
    while(p!=NULL){
        if(isVisit[p->adjvex]==0)
            DFS(G,p->adjvex);
        p=p->nextarc;
    }        
}
//for finding the adjoin arc of the vertex
//void searchG(AGraph &G,int i){
//    ArcNode * p=G.adjlist[i].firstarc;
//    while(p!=NULL){
//        printf("%c",G.adjlist[p->adjvex].data);
//        p=p->nextarc;
//    }
//}
int main(int argc, char** argv) {
    AGraph G;
    createGraph(G);    
    //searchG(G,1);
    printf("the deepth searth using recursive:\n");
    DFS(G,0);
    getchar();
    return 0;
}

运行结果:

技术图片

 

图的深度优先遍历(DFS)—递归算法

标签:递归算法   sizeof   define   null   star   png   深度优先   ==   struct   

原文地址:https://www.cnblogs.com/junjunjun123/p/10631427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!