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 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法
图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...
随机推荐
- 【BZOJ4631】踩气球 链表+线段树+堆
[BZOJ4631]踩气球 Description 六一儿童节到了, SHUXK 被迫陪着M个熊孩子玩一个无聊的游戏:有N个盒子从左到右排成一排,第i个盒子里装着Ai个气球. SHUXK 要进行Q次操 ...
- 【BZOJ3190】[JLOI2013]赛车 单调栈+几何
[BZOJ3190][JLOI2013]赛车 Description 这里有一辆赛车比赛正在进行,赛场上一共有N辆车,分别称为个g1,g2……gn.赛道是一条无限长的直线.最初,gi位于距离起跑线前进 ...
- Java散列和散列码的实现
转自:https://blog.csdn.net/al_assad/article/details/52989525 散列和散列码 ※正确的equals方法应该满足的的条件: ①自反性:x.equ ...
- [Algorithms] KMP
KMP is a classic and yet notoriously hard-to-understand algorithm. However, I think the following tw ...
- jquery 轮播图实例
实现效果:1.图片每2秒钟切换1次. 2.当鼠标停留在整个页面上时,图片不进行轮播. 3.当点击右下角的小球时,出现该选项的对应图片,而且切换页选项的背景颜色发生相应的变化. 4.当图片发生轮播切换时 ...
- 关于jQuery中nth-child和nth-of-type的详解
首先贴出来HTML的代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 如何编译部署 UIKit 离线文档?
如何编译部署 UIKit 离线文档? Whis is UIKit? Note:部署在 Windows 系统会出现路径错误情况,这里采用在 Debian 系统上进行编译和部署. 1. 安装一些前置工具 ...
- CentOS7保留默认Python版本并安装更新Python2和Python3共存
CentOS 7 默认的python版本是python2.7.5.因为yum依赖于默认的python版本的缘由,所以要先保留默认版本,并修改yum文件头部后,才能开始安装更新python2和pytho ...
- windows7上使用docker容器
1.安装 下载DockerToolbox,并安装. 下载地址:https://dn-dao-github-irror.daocloud.io/docker/toolbox/releases/downl ...
- .net ASPxTreeList 使用手记
ASPxTreeList在使用ASPxGridViewExporter控件做导出时,如果指定文件名是中文时会乱码可以用以下方法解决: grvExporter为ASPxGridViewExporter控 ...