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 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法
图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...
随机推荐
- 表单验证 靠name获取
表单 靠name获取 <form class="add-form" name="form" action="#" method=&qu ...
- JavaScript学习笔记-Js操控HTML5 <progress> 标签
Js操控----HTML5 <progress> 标签 简单模拟下下载进度跑条 <h4>加载进度</h4> <input type="button& ...
- 巨蟒python全栈开发-第3天
1 今日作业 1.有变量name = "aleX leNb" 完成如下操作: # 1)移除 name 变量对应的值两边的空格,并输出处理结果 '''''' ''' # name = ...
- 【转】va_list 详解
原文出自http://www.cppblog.com/xmoss/archive/2009/07/20/90680.html VA_LIST 是在C语言中解决变参问题的一组宏 他有这么几个成员: 1) ...
- 一篇搞定vue-router
由于Vue常见于前后端分离开发场景下,所以页面跳转工作全部交给了前端,所以基于集中管理的原则,就有了vue-router插件,它给定了url和组件之间的跳转规则 Demo准备 vue init web ...
- 克隆DOM元素 ele.cloneNode();
var nodePrev = p.children[j].cloneNode(true); var nodeNext = p.children[j + 1].cloneNode(true);
- 字符串 (string)与字节数组(byte[])之间的转换
string str = "abc" //字符串转成编码为GB2312的byte[] byte[] pData =System.Text.Encoding.GetEncoding( ...
- flannel相关资料链接
1.DockOne技术分享(十八):一篇文章带你了解Flannel http://dockone.io/article/618 2.理解Kubernetes网络之flannel网络http://ton ...
- django--博客--forms组件-用户注册
---------------------------------------------前端页面简易代码----------------------------------------------- ...
- 简明python教程二-----对象
物理行:是你在编写程序时所看见的. 逻辑行:是Python看见的单个语句. 默认的,python希望每行都只使用一个语句,这样使得代码更加易读. 如果你想要在一个物理行中使用多于一个逻辑行,用分号“: ...