图的深度优先遍历(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[].data='a';
G.adjlist[].data='b';
G.adjlist[].data='c';
G.adjlist[].data='d';
G.adjlist[].data='e';
G.adjlist[].data='f';
G.n=;
G.adjlist[].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->adjvex=;
G.adjlist[].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc=NULL;
G.adjlist[].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->adjvex=;
G.adjlist[].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc=NULL;
G.adjlist[].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->adjvex=;
G.adjlist[].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc->nextarc=NULL;
G.adjlist[].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->adjvex=;
G.adjlist[].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc->nextarc=NULL;
G.adjlist[].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->adjvex=;
G.adjlist[].firstarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->nextarc->nextarc->adjvex=;
G.adjlist[].firstarc->nextarc->nextarc->nextarc=NULL;
G.adjlist[].firstarc=(ArcNode *)malloc(sizeof(ArcNode));
G.adjlist[].firstarc->adjvex=;
G.adjlist[].firstarc->nextarc=NULL;
G.e=;
}
int isVisit[maxSize]={};
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]=;
visit(G,v);
p=G.adjlist[v].firstarc;
while(p!=NULL){
if(isVisit[p->adjvex]==)
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,);
getchar();
return ;
}
运行结果:

图的深度优先遍历(DFS)—递归算法的更多相关文章
- 图的深度优先遍历DFS
图的深度优先遍历是树的前序遍历的应用,其实就是一个递归的过程,我们人为的规定一种条件,或者说一种继续遍历下去的判断条件,只要满足我们定义的这种条件,我们就遍历下去,当然,走过的节点必须记录下来,当条件 ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 图的深度优先遍历(DFS) c++ 非递归实现
深搜算法对于程序员来讲是必会的基础,不仅要会,更要熟练.ACM竞赛中,深搜也牢牢占据着很重要的一部分.本文用显式栈(非递归)实现了图的深度优先遍历,希望大家可以相互学习. 栈实现的基本思路是将一个节点 ...
- 图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析
1. 深度优先遍历 深度优先遍历(Depth First Search)的主要思想是: 1.首先以一个未被访问过的顶点作为起始顶点,沿当前顶点的边走到未访问过的顶点: 2.当没有未访问过的顶点时,则回 ...
- 【C++】基于邻接矩阵的图的深度优先遍历(DFS)和广度优先遍历(BFS)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- 数据结构——图的深度优先遍历(邻接矩阵表示+java版本)
1.深度优先遍历(DFS) 图的深度优先遍历本质上是一棵树的前序遍历(即先遍历自身,然后遍历其左子树,再遍历右子树),总之图的深度优先遍历是一个递归的过程. 如下图所示,左图是一个图,右图是图的深度 ...
- PTA 邻接矩阵存储图的深度优先遍历
6-1 邻接矩阵存储图的深度优先遍历(20 分) 试实现邻接矩阵存储图的深度优先遍历. 函数接口定义: void DFS( MGraph Graph, Vertex V, void (*Visit)( ...
- 广度优先遍历-BFS、深度优先遍历-DFS
广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...
- 图论 - 图的深度优先遍历c++实现
图的深度优先遍历c++实现 深度优先搜索 邻接矩阵的创建 int i, j, m, a, b; cin >> n >> m; //初始化二维矩阵 for (i = 1; i & ...
随机推荐
- PROJ.4学习——地图投影
PROJ.4学习——地图投影(坐标系投影) 前言 PROJ是由大量的基础投影库构成.这里主要讨论学习PROJ库的相关参数. 这里大部分是讲如何将3D坐标系投影到2D平面上.投影时,涉及到基准线,单位, ...
- Linux虚机安装配置Tomcat
d第一步:下载Tomcat包,网址http://tomcat.apache.org/ 选择tar.gz包下载,并传到虚机中 第二步:解压下载好的Tomcat包 命令:tar -zxvf apache- ...
- spring(IOC)动态代理
姓名:黄于霞 班级:软件151 1.引入Spring IOC的核心jar包,创建IOC的配置文件beans.xml,内容如下: 1 <?xml version="1.0&qu ...
- maven 项目提示找不到javax.servlet.xxx问题解决
1.https://search.maven.org/search?q=g:javax.servlet%20AND%20a:javax.servlet-api&core=gav 2.下载3.1 ...
- python小总结4(文件)
一.读文件 过程: a.打开文件:open() b.读取文件内容:read() readline() readlines() c.关闭文件:close() open(path,flag,encodin ...
- d3js可视化策略
d3js是数据驱动图形的思路.基本可以这么理解,有什么样的图形,后面基本就有类似结构的数据.大概思路步骤如下: 一.适配数据格式 这一步主要是为第二部服务,第一步的结果作为第二部的入参. 比如,画层级 ...
- fastJson遇到的问题
概述 现在的代码开发中,json这种数据类型使用的是越来越多,因为它的存取速度都比较快,而且,使用起来非常的简单,今天工作的时候,我就遇到了一个关于json的生产问题,这个问题我之前确实还没有注意过, ...
- 手把手教你如何使用Cocos2d Console 进行html5项目发布
手把手教你如何使用Cocos2d Console 进行html5项目发布 1.首先需要先安装Cocos2d Console运行需要的工具. 详情参见 这篇文章 http://www.cocoach ...
- Ajax中,执行成功却依然刷新本页面
网页代码是这个<a href="" id="1">删除</a> 当我使用Ajax的时候 $("a").click(f ...
- Pycharm 远程调试
上传后发现成图片了,很模糊.可以看我在百度盘分享pdf文件. https://pan.baidu.com/s/1bYVcAq40SRFtn8qXCPH6TQ