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 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法
图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...
随机推荐
- iOS 修改textholder的颜色
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(, , , )]; textField.placeholde ...
- 10013: An attempt was made to access a socket in a way forbidden by its access permissions
nginx的error.log日志报错: 2018/01/25 11:55:22 [emerg] 3380#15488: bind() to 0.0.0.0:20003 failed (10013: ...
- MySQL主从不一致修复
场景: 线上正在服务的库由于紧急主从切换导致主从不一致,报错信息如下: Last_Error: Coordinator stopped because there were error(s) in t ...
- phpstrrchr()函数的问题
strrchr — 查找指定字符在字符串中的最后一次出现 说明 string strrchr ( string $haystack , mixed $needle ) 该函数返回 haystack 字 ...
- 百度 url 当在baidu搜索结果展示页,去点击标头时
Spencer : 百度加一层跳转主要为了监控点击 w 基于dns和用户体验考虑的猜测 0-百度自己的cdn服务器存入各个域名/url的服务器ip(多ip情况下,返回物理空间相对用户最近的服务器ip) ...
- ios cocos2d 使用 sneakyInput 插件
昨晚看了篇使用sneakyInput插件实现模拟手柄的代码,不过我加上后出现了很多问题.最后只看如何实现,没有自己动手去操作.今天终于吧问题都解决了.记录下来.也供别人参考. 首先要先加入libz.d ...
- windows钩子 Hook示例
1.首先编写一个 win32 dll工程. #include "stdafx.h" int WINAPI add(int a,int b) { return a+b; } BOOL ...
- HTTP Keep-Alive是什么?如何工作?(转)
add by zhj: 本篇只是Keep-Alive的第一篇,其它文章参见下面的列表. 原文: HTTP Keep-Alive是什么?如何工作? 1. HTTP Keep-Alive是什么?如何工作? ...
- 同一TextView上内容的不同显示(最新)-SpannableString
上次发了一篇同一TextView内容的不同显示这篇文章. 有关颜色的不同显示,我使用了最简单可是也最复杂的方法.忘记使用SpannableString,现又一次发一下,大家參考下. TextView组 ...
- tomcat访问管理页面出现:403 Access Denied 解决方法
点击红色框框出现以下403错误 打开context.xml文件 vim /usr/local/tomcat/webapps/manager/META-INF/context.xml <Conte ...