图的深度优先遍历(DFS)—递归算法
Posted junjunjun123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图的深度优先遍历(DFS)—递归算法相关的知识,希望对你有一定的参考价值。
实验环境: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)—递归算法的主要内容,如果未能解决你的问题,请参考以下文章