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));
}

最新文章

  1. [WPF]带下拉列表的文本框
  2. jenkins配置邮件
  3. hdu 5229 找规律
  4. mysqli连接数据库的模板
  5. Hadoop MapReduce编程 API入门系列之压缩和计数器(三十)
  6. sessionId在fragment里无法保存的问题
  7. web测试中,各类web控件测试点总结
  8. double精度的坑与BigDecimal
  9. 深入理解this对象
  10. TexturePacker 介绍
  11. iOS 之 runtime --- 集百家之言
  12. nginx+多个tomcat
  13. Linux 系统设置sh文件开机自启动
  14. xhtml和html的区别 html5和xhtml的区别
  15. Spring控制反转(依赖注入)的最简单说明
  16. nvl 与 nvl2
  17. Spring学习笔记1——IOC: 尽量使用注解以及java代码
  18. AdvStringGrid 单元格字体颜色、背景颜色
  19. Windows 安装配置memcached+php的教程,以及相关资源下载
  20. 「日常训练」Magic Stones(CodeForces-1110E)

热门文章

  1. 解析java实体类
  2. java 的HashMap底层数据结构
  3. MyBatis从入门到精通(第2章):MyBatis XML方式的基本用法【insert用法、update用法、delete用法】
  4. tensorflow从训练自定义CNN网络模型到Android端部署tflite
  5. cJSON api的使用教程
  6. GCC与gcc,g++区别
  7. Django框架(六):模型(二) 字段查询、查询集
  8. SQL Link Oracle
  9. 吴裕雄--天生自然 PYTHON3开发学习:字典
  10. Java任务调度框架之分布式调度框架XXL-Job介绍