DFS - 深度搜索 - 基于邻接列表表示法
2017-07-25 15:38:00
writer:pprp
在前一篇图基于邻接列表表示法的代码加了一小部分,加了一个DFS函数,visited[N]数组
参考书目:张新华的《算法竞赛宝典》
代码如下:
#include <iostream> using namespace std; const int N = ; int visited[N] = {}; //新引入一个数组,用于标记是否访问过 struct node
{
int vertex;
node*next;
}; node head[N];
void create(int node1,int node2)//通过起点和终点的值创建一个邻接表
{
node * point;
node * New = new node();
if(New!=NULL)
{
New->vertex = node2;
New->next = NULL;
point = &(head[node1]);
while(point->next!=NULL)
point = point->next;
point->next = New;
}
} void DFS(int vertex)
{
node*point;
visited[vertex] = ;
cout <<"["<<vertex<<"]->";
point = head[vertex].next;
while(point!=NULL)
{
if(visited[point->vertex]==)
DFS(point->vertex);
point = point->next;
}
} void print()
{
node*point;
for(int i = ; i < N; i++)
{
point = head[i].next;
cout << "Head["<<i<<"]";
while(point!=NULL)
{
cout <<"-> "<<point->vertex;
point = point->next;
}
cout << endl;
}
} int main()
{
int node1,node2; for(int i = ; i < N; i++)
{
head[i].vertex = i;
head[i].next = NULL;
}
while()
{
cout <<"please enter the start point" << endl; cin >> node1;
if(node1 == -)
break;
cout <<"please enter the end point" << endl;
cin >> node2; if(node1 == node2)
cout <<"自身循环"<<endl;
else if(node1>=N||node2>=N)
cout <<"超出范围"<<endl;
else
create(node1,node2);
} cout << "邻接表为:" << endl;
print(); DFS(); cout <<"\n"<<endl; return ;
}
DFS - 深度搜索 - 基于邻接列表表示法的更多相关文章
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Maximum Depth of Binary Tree dfs,深度搜索
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- BFS - 广度优先搜索 - 邻接列表表示法
2017-07-25 21:40:22 writer:pprp 在DFS的基础上加上了一个BFS函数 #include <iostream> #include <queue> ...
- [LeetCode] Unique Binary Search Trees II dfs 深度搜索
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- [LeetCode] Binary Tree Postorder Traversal dfs,深度搜索
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 题目--oil Deposits(油田) 基础DFS(深度搜索)
上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...
- SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...
- Python 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法
图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...
随机推荐
- python中的str()与eval函数
author:headsen chen date:2018-04-09 10:48:22 eval函数是把str转化成list.dict.tuple str函数把list,dict,tuple ...
- Spring中 PROPAGATION_REQUIRED 解释
转自:https://blog.csdn.net/bigtree_3721/article/details/53966617 事务传播行为种类 Spring在TransactionDefinition ...
- IIS 部署WCF时遇到这么个错:
转(http://blog.csdn.net/vic0228/article/details/48806405) 部署WCF时遇到这么个错: "The service cannot be a ...
- Charles抓包工具简单操作
一.界面介绍 1.功能是clear,清理掉所有请求显示信息. 2.功能是搜索关键字,也可以使用ctrl+f实现,可以设置搜索的范围 3.功能是开始或暂停 4.显示所抓取的数据包 5.抓取数据包的请求及 ...
- mysql数据库转移到oracle的经历
简单说明一下情况,系统原本是LAMP的.现在要添加对oracle的支持,原来的mysql也同样支持(通过配置选择数据库类型). 第一步,表结构转移到oracle,并掌握转移的方法(方便给有二开的老客户 ...
- Unity3d依赖于平台的编译
Unity的这一功能被命名为"依赖于平台的编译". 这包括了一些预编译处理指令,让你能够专门的针对不同的平台分开编译和运行一段代码. 此外,你能够在编辑器下运行一些代码用于測试而不 ...
- mysql模糊查询实践总结
%代表任意多个字符 _代表一个字符 在 MySQL中,SQL的模式缺省是忽略大小写的 正则模式使用REGEXP和NOT REGEXP操作符. “.”匹配任何单个的字符.一个字符类 “[...]”匹配在 ...
- 1 TensorFlow入门笔记之基础架构
------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...
- boost atomic
文档: http://www.boost.org/doc/libs/1_53_0/doc/html/atomic.html Presenting Boost.Atomic Boost.Atomic i ...
- mongo常用查询
复杂查询: and: or: lte,gte,=: and+lt: , 逗号表示and, $lt小于写在值当中 查询实例: 找到含有指定数据文档 查找条件spcode有1个字符长度的文档 db.sp ...