图的DFS。。类似树的DFS
Depth-First Search (DFS)
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.
判断连通性代码:
const allowTraversalCallback = (
() => {
const seen = {};
return ({ nextVertex }) => {//如果没有访问过邻近结点,则返回true
if (!seen[nextVertex.getKey()]) {
seen[nextVertex.getKey()] = true;
return true;
}
return false;
};
}
)();
完整代码:
/**
* @typedef {Object} Callbacks
*
* @property {function(vertices: Object): boolean} [allowTraversal] -
* Determines whether DFS should traverse from the vertex to its neighbor
* (along the edge). By default prohibits visiting the same vertex again.
*
* @property {function(vertices: Object)} [enterVertex] - Called when DFS enters the vertex.
*
* @property {function(vertices: Object)} [leaveVertex] - Called when DFS leaves the vertex.
*/ /**
* @param {Callbacks} [callbacks]
* @returns {Callbacks}
*/
function initCallbacks(callbacks = {}) {//对回调函数进行初始化
const initiatedCallback = callbacks; const stubCallback = () => {}; const allowTraversalCallback = (
() => {
const seen = {};
return ({ nextVertex }) => {//如果没有访问过邻近结点,则返回true
if (!seen[nextVertex.getKey()]) {
seen[nextVertex.getKey()] = true;
return true;
}
return false;
};
}
)(); initiatedCallback.allowTraversal = callbacks.allowTraversal || allowTraversalCallback;
initiatedCallback.enterVertex = callbacks.enterVertex || stubCallback;
initiatedCallback.leaveVertex = callbacks.leaveVertex || stubCallback; return initiatedCallback;
} /**
* @param {Graph} graph
* @param {GraphVertex} currentVertex
* @param {GraphVertex} previousVertex
* @param {Callbacks} callbacks
*/
function depthFirstSearchRecursive(graph, currentVertex, previousVertex, callbacks) {
callbacks.enterVertex({ currentVertex, previousVertex }); graph.getNeighbors(currentVertex).forEach((nextVertex) => {
if (callbacks.allowTraversal({ previousVertex, currentVertex, nextVertex })) {
depthFirstSearchRecursive(graph, nextVertex, currentVertex, callbacks);
}
}); callbacks.leaveVertex({ currentVertex, previousVertex });
} /**
* @param {Graph} graph
* @param {GraphVertex} startVertex
* @param {Callbacks} [callbacks]
*/
export default function depthFirstSearch(graph, startVertex, callbacks) {
const previousVertex = null;
depthFirstSearchRecursive(graph, startVertex, previousVertex, initCallbacks(callbacks));
}
图的DFS。。类似树的DFS的更多相关文章
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 图的创建和遍历(BFS/DFS)
图的表示方法主要有邻接矩阵和邻接表.其中邻接表最为常用,因此这里便以邻接表为例介绍一下图的创建及遍历方法. 创建图用到的结构有两种:顶点及弧 struct ArcNode { int vertexIn ...
- 图的基本算法(BFS和DFS)(转载)
图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...
- 2018.08.30 NOIP模拟 graph(dfs序/树剖+线段树)
[描述] 给你一个图,一共有 N 个点,2*N-2 条有向边. 边目录按两部分给出 1. 开始的 n-1 条边描述了一颗以 1 号点为根的生成树,即每个点都可以由 1 号点 到达. 2. 接下来的 N ...
- poj3321-Apple Tree(DFS序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36442 Accepted: 10894 Desc ...
- 图的基本算法(BFS和DFS)
图是一种灵活的数据结构,一般作为一种模型用来定义对象之间的关系或联系.对象由顶点(V)表示,而对象之间的关系或者关联则通过图的边(E)来表示. 图可以分为有向图和无向图,一般用G=(V,E)来表示图. ...
- 【bzoj2819】Nim DFS序+树状数组+倍增LCA
题目描述 著名游戏设计师vfleaking,最近迷上了Nim.普通的Nim游戏为:两个人进行游戏,N堆石子,每回合可以取其中某一堆的任意多个,可以取完,但不可以不取.谁不能取谁输.这个游戏是有必胜策略 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- HDU4117 GRE WORDS(AC自动机+线段树维护fail树的dfs序)
Recently George is preparing for the Graduate Record Examinations (GRE for short). Obviously the mos ...
随机推荐
- 单机版solr的搭建
1.1. Solr的环境 Solr是java开发. 需要安装jdk. 安装环境Linux. 需要安装Tomcat. 1.2. 搭建步骤 第一步:把solr 的压缩包上传到Linux系统 第二步:解压s ...
- uni-app: 如何实现增量更新功能?
都知道,很多APP都有增量更新功能,Uni APP也是在今年初,推出了增量更新功能,今天我们就来学习一波. 当然,很多应用市场为了防止开发者不经市场审核许可,给用户提供违法内容,对增量更新大多持排斥态 ...
- 专业程序设计part2
05tue 乘以1.0使得int*int!=0 today:缩放 和计算机图形学关联 已知:currentdataset ask for:两个方向的缩放比例.保存路径.重采样方法(necessary) ...
- python os.path.dirname() abspath()
测试文件的名称 path_test.py 先确定文件目录 (my_flask3) python@ubuntu:~/Desktop/flask_news_pro$ python path_test.py ...
- 【C#并发】00概述
摘自<C#并发编程经典实例>[美]Stephen Cleary 并发:同时做多件事情.终端用户利用并发功能,在输入数据库的同时相应用户输入.服务器应用并发,在处理第一个请求的同时响应第二个 ...
- ubuntu下Django的搭建
工具:Window下的pycharm .VirtualBox下的Ubuntu系统.非必需的Xshell(远程连接工具) 现在针对各种包或python版本不能共存或包不能很好下载的问题,开始流行使用虚拟 ...
- [HAOI2018]苹果树(组合数学)
首先有个很奇妙而且很有用的性质:每个二叉树对应唯一的中序遍历,然后每个二叉树出现概率相同.所以n个节点的二叉树形态是n!种(题目中说了*n!已经是提示了),对每种方案求和即可得到期望.令f[i]表示i ...
- 吴裕雄--天生自然 PYTHON3开发学习:MySQL - mysql-connector 驱动
import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user=&q ...
- springMVC常用知识点的整理
[spring boot]第3篇:spring boot 进行 web 开发 forward和redirect的区别是什么 Spring MVC中redirect重定向3种方式 =========== ...
- adaptation|domestication|genome evolution|convergent evolution|whole-genome shotgun sequencing|IHGSC
Dissecting evolution and disease using comparative vertebrate genomics-online 因为基因组不是独一无二的,同时人类基因组可以 ...