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 图_系列之基于邻接炬阵实现广度、深度优先路径搜索算法
图是一种抽象数据结构,本质和树结构是一样的. 图与树相比较,图具有封闭性,可以把树结构看成是图结构的前生.在树结构中,如果把兄弟节点之间或子节点之间横向连接,便构建成一个图. 树适合描述从上向下的一对 ...
随机推荐
- 同时调整lv分区的大小(减少一个,增加另一个)
author:headsen chen date: 2018-04-20 16:48:06 1.查看分区:/home 为67G,太大了,/ 是50g,太小了. [root@localhost ~]# ...
- 【BZOJ4584】[Apio2016]赛艇 DP
[BZOJ4584][Apio2016]赛艇 Description 在首尔城中,汉江横贯东西.在汉江的北岸,从西向东星星点点地分布着个划艇学校,编号依次为到.每个学校都拥有若干艘划艇.同一所学校的所 ...
- Leetcode-Convert Sorted Array to BST
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. So ...
- SpringBoot专题1----springboot与mybatis的完美融合
springboot大家都知道了,搭建一个spring框架只需要秒秒钟.下面给大家介绍一下springboot与mybatis的完美融合: 首先:创建一个名为springboot-mybatis的ma ...
- Java实现 Hessian接口测试
Hessian接口测试简单单例 Hessian是基于Binary –RPC(二进制远程过程调用协议)进行通讯. Hessian就是把Java对象转变成字节序列,然后通过Http传输到目标服务器上(主机 ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- Audit logon events&Logon type
表一.Logon type 表二.Audit logon events 表三.Logon type details Logon type Logon title Description 2 Inter ...
- 关于python中的查询数据库内容中用到的fetchone()函数和fetchall()函数(转)还有fetchmany()
最近在用python操作mysql数据库时,碰到了下面这两个函数,标记一下: fetchone() : 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None fetchall() ...
- .net:上传图片并将保存至指定目录下(支持PC端和移动端)
页面: <body> <form id="formid" name="myform" action="Upload" me ...
- curl类封装
<?php /** * @author askwei **/ class CURL { private $ch; private $url = "htt ...