实验环境: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)—递归算法的更多相关文章

  1. 图的深度优先遍历DFS

    图的深度优先遍历是树的前序遍历的应用,其实就是一个递归的过程,我们人为的规定一种条件,或者说一种继续遍历下去的判断条件,只要满足我们定义的这种条件,我们就遍历下去,当然,走过的节点必须记录下来,当条件 ...

  2. 图的深度优先遍历(DFS)和广度优先遍历(BFS)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  3. 图的深度优先遍历(DFS) c++ 非递归实现

    深搜算法对于程序员来讲是必会的基础,不仅要会,更要熟练.ACM竞赛中,深搜也牢牢占据着很重要的一部分.本文用显式栈(非递归)实现了图的深度优先遍历,希望大家可以相互学习. 栈实现的基本思路是将一个节点 ...

  4. 图的深度优先遍历(DFS)和广度优先遍历(BFS)算法分析

    1. 深度优先遍历 深度优先遍历(Depth First Search)的主要思想是: 1.首先以一个未被访问过的顶点作为起始顶点,沿当前顶点的边走到未访问过的顶点: 2.当没有未访问过的顶点时,则回 ...

  5. 【C++】基于邻接矩阵的图的深度优先遍历(DFS)和广度优先遍历(BFS)

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  6. 数据结构——图的深度优先遍历(邻接矩阵表示+java版本)

    ​1.深度优先遍历(DFS) 图的深度优先遍历本质上是一棵树的前序遍历(即先遍历自身,然后遍历其左子树,再遍历右子树),总之图的深度优先遍历是一个递归的过程. 如下图所示,左图是一个图,右图是图的深度 ...

  7. PTA 邻接矩阵存储图的深度优先遍历

    6-1 邻接矩阵存储图的深度优先遍历(20 分) 试实现邻接矩阵存储图的深度优先遍历. 函数接口定义: void DFS( MGraph Graph, Vertex V, void (*Visit)( ...

  8. 广度优先遍历-BFS、深度优先遍历-DFS

    广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...

  9. 图论 - 图的深度优先遍历c++实现

    图的深度优先遍历c++实现 深度优先搜索 邻接矩阵的创建 int i, j, m, a, b; cin >> n >> m; //初始化二维矩阵 for (i = 1; i & ...

随机推荐

  1. maven 项目提示找不到javax.servlet.xxx问题解决

    1.https://search.maven.org/search?q=g:javax.servlet%20AND%20a:javax.servlet-api&core=gav 2.下载3.1 ...

  2. elment重置表格行高,hover效果

    来源网络,做个笔记.表头行高.el-table__header tr, .el-table__header th { padding: 0; height: 50px; }表体行高 .el-table ...

  3. mybatis Interceptor拦截器代码详解

    mybatis官方定义:MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  4. python 生成器(generator)的生成方式

    generator包括生成器和带yield的generator函数. 写了一个生成杨辉三角的小例子: # -*- coding:utf-8 -*- def triangles(): l = [1] w ...

  5. centos7初上手3-安装apache服务

    前两篇学习安装了mysql服务器,tomcat服务,这篇文章学习安装apache服务 1.执行yum install httpd,安装完成后查看httpd rpm -qa|grep httpd 2.新 ...

  6. jS弹出新窗口被拦截的解决方法

    使用ajax处理数据,在回调中跳转到或打开新页面,这时就会被浏览器拦截 解决方案:先用window.open打开一个窗口,然后修改该窗口地址 var w = window.open('about:bl ...

  7. Excel--------实用功能(数据对比)

    --excel数据在sql中查询展示出来 SELECT * FROM (SELECT '101001' as code ,'上海宝山站' as name union allSELECT '102083 ...

  8. PostgreSQL 问题总结

    一.postgresql  - server don't listen(服务器未监听) 1)检测是否开启PostgreSQL服务,没开启的话,需要自己手动建立PostgreSQL服务. 2)查看543 ...

  9. hdu4003详解(树形dp+多组背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Find Metal Mineral Time Limit: 2000/1000 MS (Jav ...

  10. python的标准数据类型

    python有5种标准的数据类型 1. number(数字) int(有符号的整形) long(长整[也可以代表八进制和16进制]) float(浮点型) complex(复数类型) 2.string ...