BFS与DFS常考算法整理

  • Preface

BFS(Breath-First Search,广度优先搜索)与DFS(Depth-First Search,深度优先搜索)是两种针对树与图数据结构的遍历或搜索算法,在树与图相关算法的考察中是非常常见的两种解题思路。

Definition of DFS and BFS

DFS的wikipedia定义:

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

BFS的wikipedia定义:

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a ‘search key’[1]), and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

It uses the opposite strategy as depth-first search, which instead explores the highest-depth nodes first before being forced to backtrack and expand shallower nodes.

So obviously, as their name suggest, DFS focuses on ‘depth’ when searching or traversing while BFS focuses on ‘breath’.

By the way, because of DFS’s feature, it’s easy to relate it with ‘Backtracking’ algorithm as the wiki definition mentions. The relationship between DFS and backtracking is well explained by Reed Copsey on StackOverflow:

Backtracking is a more general purpose algorithm.

Depth-First search is a specific form of backtracking related to searching tree structures. From Wikipedia:

One starts at the root (selecting some node as the root in the graph case) and explores as far as possible along each branch before backtracking.

It uses backtracking as part of its means of working with a tree, but is limited to a tree structure.

Backtracking, though, can be used on any type of structure where portions of the domain can be eliminated - whether or not it is a logical tree. The Wiki example uses a chessboard and a specific problem - you can look at a specific move, and eliminate it, then backtrack to the next possible move, eliminate it, etc.

How to Implement DFS and BFS

DFS

In tree structure, DFS means we always start from a root node and try to reach the leaf node as direct as possible before we have to backtrack.

Order in which the nodes are visited

In graph, DFS means we start from a random assigned node in the graph, and explores as far as possible along the branch before we have to backtrack.

So the key points for DFS are:

- How to explore as far as possible?

- How to backtrack?

How to explore as far as possible

Normally, for tree node, it would have left child or right child, so we would continuously go on exploring current node’s child node until we encounter a null node, then we go back to last node. Repeat above procedures until all nodes have been visited.

for graph node, we do the similar exploration: explore as further as possible according to the representation of graph (adjacency list, adjacency matrix or incidence matrix) until we find no more node that hasn’t been visited and connected with current node, then we go back to last node. Repeat above procedures until all nodes have been visited.

How to backtrack/go back?

‘Go back’ generally can be realized using data structure ——stack—— or by recursion. And if we use stack, it means we would need to push each node we visited in the process of exploring each branch, and pop when we can’t explore further starting from current node.

BFS

In tree structure, BFS means we always start from a root node and try to all the other nodes in the same breath before we further try exploring nodes at next depth level. (The same explanation for graph)

Order in which the nodes are visited

So the key points for BFS are:

  • How to explore all nodes of same depth level?

How to explore all nodes of same depth level?

We can use a queue to do this: Starting from root node of a tree (Or a random node in a graph), we add visit all nodes connected with the starting node and add them to the queue. Then, we poll node from queue one by one and repeat above procedures until all nodes have been visited.

Typical Leetcode Prbolems

DFS

Path Sum II

LC113

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

Return:

[
[5,4,11,2],
[5,8,4,5]
]
  • My Answer
package medium2;

import java.util.ArrayList;
import java.util.List; /**
* @author Tom Qian
* @email tomqianmaple@outlook.com
* @github https://github.com/bluemapleman
* @date 2018年6月7日
*/
public class PathSumII
{
// DFS: make use of recursion to backtrack
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> ans=new ArrayList<List<Integer>>();
if(root==null)
return ans; int goal=sum-root.val;
if(goal==0) {
if(root.left==null && root.right==null) {
List<Integer> tempList=new ArrayList<>();
tempList.add(root.val);
ans.add(tempList);
return ans;
}
} List<List<Integer>> temp;
if((temp=pathSum(root.left, goal)).size()!=0) {
for(List<Integer> list:temp) {
list.add(0, root.val);
ans.add(list);
}
} if((temp=pathSum(root.right, goal)).size()!=0) {
for(List<Integer> list:temp) {
list.add(0,root.val);
ans.add(list);
}
} return ans;
} }

Convert Sorted List to Binary Search Tree

LC109

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
/ \
-3 9
/ /
-10 5
  • My Answer
package medium2;

/**
* @author Tom Qian
* @email tomqianmaple@outlook.com
* @github https://github.com/bluemapleman
* @date 2018年6月11日
*/
public class ConvertSortedListtoBinarySearchTree
{
// DFS: make use of recursion to backtrack
// find the middle node of sorted linked list, and take it as the root node of the BST.
public TreeNode sortedListToBST(ListNode head) {
if(head==null)
return null; ListNode slow=head,fast=head,followSlow=head; boolean moveFlag=false;
while(fast!=null && fast.next!=null) {
if(moveFlag)
followSlow=followSlow.next;
moveFlag=true;
slow=slow.next;
fast=fast.next.next;
}
TreeNode root=new TreeNode(slow.val); if(moveFlag) {
followSlow.next=null;
root.left=sortedListToBST(head);
root.right=sortedListToBST(slow.next);
} return root;
}
}

Course Schedule

There are a total of n courses you have to take, labeled from 0 to n-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

Example 1:

Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should
also have finished course 1. So it is impossible.

Note:

1.The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.

2.You may assume that there are no duplicate edges in the input prerequisites.

  • My Answer
// DFS
public boolean canFinish(int numCourses, int[][] prerequisites) {
Map<Integer, List<Integer>> map=new HashMap<>();
for(int i=0;i<numCourses;i++)
map.put(i, new ArrayList<>());
for(int i=0;i<prerequisites.length;i++) {
map.get(prerequisites[i][0]).add(prerequisites[i][1]);
} // start DFS: detect if there is any circle in course graph, i.e. whether DFS starting from certain start point i would lead to the start point again.
for(int i=0;i<numCourses;i++) {
// Use a set to avoid infinite loop: when met same node twice, ignore it.
Set<Integer> set=new HashSet<>();
// Use a stack to backtrack
ArrayDeque<Integer> stack=new ArrayDeque<>();
List<Integer> preCourseList=map.get(i);
for(Integer preCourse:preCourseList)
stack.push(preCourse);
while(!stack.isEmpty()) {
int preCourse=stack.pop(); if(set.contains(preCourse))
continue;
else
set.add(preCourse); if(preCourse==i)
return false;
else {
preCourseList=map.get(preCourse);
for(Integer tempPreCourse:preCourseList) {
stack.push(tempPreCourse);
}
}
}
} return true;
}

BFS

Course Schedule

  • My Answer
// BFS
public boolean canFinish(int numCourses, int[][] prerequisites) {
Map<Integer, List<Integer>> map=new HashMap<>();
for(int i=0;i<numCourses;i++)
map.put(i, new ArrayList<>());
for(int i=0;i<prerequisites.length;i++) {
map.get(prerequisites[i][0]).add(prerequisites[i][1]);
} // start DFS: detect if there is any circle in course graph, i.e. whether BFS starting from certain start point i would lead to the start point again.
for(int i=0;i<numCourses;i++) {
// Use a set to avoid infinite loop: when met same node twice, ignore it.
Set<Integer> set=new HashSet<>();
// Use a queue to remember nodes of same depth level
ArrayDeque<Integer> queue=new ArrayDeque<>();
List<Integer> preCourseList=map.get(i);
for(Integer preCourse:preCourseList)
queue.add(preCourse);
while(!queue.isEmpty()) {
int preCourse=queue.poll(); if(set.contains(preCourse))
continue;
else
set.add(preCourse); if(preCourse==i)
return false;
else {
preCourseList=map.get(preCourse);
for(Integer tempPreCourse:preCourseList) {
queue.add(tempPreCourse);
}
}
}
} return true;
}

Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation: 1 <---
/ \
2 3 <---
\ \
5 4 <---
  • My Answer
package medium2;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List; /**
* @author Tom Qian
* @email tomqianmaple@outlook.com
* @github https://github.com/bluemapleman
* @date 2018年6月12日
*/
public class BinaryTreeRightSideView
{
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans=new ArrayList<>();
if(root==null)
return ans; ans.add(root.val); ArrayDeque<TreeNode> queue1=new ArrayDeque<>(),queue2=new ArrayDeque<>();;
queue1.add(root); while(!queue1.isEmpty() || !queue2.isEmpty()){
TreeNode rightestNode=null; if(!queue1.isEmpty()) {
while(!queue1.isEmpty()) {
TreeNode fatherNode=queue1.poll(); if(fatherNode.right!=null) {
queue2.add(fatherNode.right);
if(rightestNode==null)
rightestNode=fatherNode.right;
} if(fatherNode.left!=null) {
queue2.add(fatherNode.left);
if(rightestNode==null)
rightestNode=fatherNode.left;
}
}
}else{
while(!queue2.isEmpty()) {
TreeNode fatherNode=queue2.poll();
if(fatherNode.right!=null) {
queue1.add(fatherNode.right);
if(rightestNode==null)
rightestNode=fatherNode.right;
}
if(fatherNode.left!=null) {
queue1.add(fatherNode.left);
if(rightestNode==null)
rightestNode=fatherNode.left;
}
}
} if(rightestNode!=null)
ans.add(rightestNode.val); } return ans;
}
}

.

BFS与DFS常考算法整理的更多相关文章

  1. Leetcode——二叉树常考算法整理

    二叉树常考算法整理 希望通过写下来自己学习历程的方式帮助自己加深对知识的理解,也帮助其他人更好地学习,少走弯路.也欢迎大家来给我的Github的Leetcode算法项目点star呀~~ 二叉树常考算法 ...

  2. Leetcode——回溯法常考算法整理

    Leetcode--回溯法常考算法整理 Preface Leetcode--回溯法常考算法整理 Definition Why & When to Use Backtrakcing How to ...

  3. C++常考算法

    1 strcpy, char * strcpy(char* target, char* source){  // 不返回const char*, 因为如果用strlen(strcpy(xx,xxx)) ...

  4. c++常考算法知识点汇总

    前言:写这篇博客完全是给自己当做笔记用的,考虑到自己的c++基础不是很踏实,只在大一学了一学期,c++的面向对象等更深的知识也一直没去学.就是想当遇到一些比较小的知识,切不值得用一整篇 博客去记述的时 ...

  5. .NET面试常考算法

    1.求质数    质数也成为素数,质数就是这个数除了1和他本身两个因数以外,没有其他因数的数,叫做质数,和他相反的是合数,    就是除了1和他本身两个因数以外,还友其他因数的数叫做合数. 1 nam ...

  6. JS-常考算法题解析

    常考算法题解析 这一章节依托于上一章节的内容,毕竟了解了数据结构我们才能写出更好的算法. 对于大部分公司的面试来说,排序的内容已经足以应付了,由此为了更好的符合大众需求,排序的内容是最多的.当然如果你 ...

  7. 近5年常考Java面试题及答案整理(三)

    上一篇:近5年常考Java面试题及答案整理(二) 68.Java中如何实现序列化,有什么意义? 答:序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内容进行流化.可以对流化后的对象进行读写 ...

  8. 近5年常考Java面试题及答案整理(二)

    上一篇:近5年常考Java面试题及答案整理(一) 31.String s = new String("xyz");创建了几个字符串对象? 答:两个对象,一个是静态区的"x ...

  9. 面试常考的常用数据结构与算法(zz)

    数据结构与算法,这个部分的内容其实是十分的庞大,要想都覆盖到不太容易.在校学习阶段我们可能需要对每种结构,每种算法都学习,但是找工作笔试或者面试的时候,要在很短的时间内考察一个人这方面的能力,把每种结 ...

随机推荐

  1. 关于运算符的那些坑—自增x++&&++y

    题目 比较常见的问题,因为比较细,看书的时候一不注意可能就过去啦,但是遇到的时候就会容易出问题.先看下面程序,考虑一下运行结果是什么呢? int x = 1, y = 1; if(x++ == 2 & ...

  2. C++扬帆远航——17(递归函数求阶乘)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:阶乘.cpp * 作者:常轩 * 微信公众号:Worldhell ...

  3. 名企6年Java程序员的工作感悟,送给迷茫的你

    程序员从开始选择到坚持下去,工作了六年对一个程序员意味什么?在职位上:高级开发工程师?架构师?技术经理?or ... ?在能力上:各种编码无压力?核心代码无压力?平台架构无压力? or ... fuc ...

  4. c语言之单向链表

    0x00 什么是链表 链表可以说是一种最为基础的数据结构了,而单向链表更是基础中的基础.链表是由一组元素以特定的顺序组合或链接在一起的,不同元素之间在逻辑上相邻,但是在物理上并不一定相邻.在维护一组数 ...

  5. javascript常用工具函数总结(不定期补充)未指定标题的文章

    前言 以下代码来自:自己写的.工作项目框架上用到的.其他框架源码上的.网上看到的. 主要是作为工具函数,服务于框架业务,自身不依赖于其他框架类库,部分使用到es6/es7的语法使用时要注意转码 虽然尽 ...

  6. 沪江iKcamp出品微信小程序教学共5章16小节汇总(含视频)

  7. Feign 在 SpringCloud 中的使用 四

    此处就单纯写一个消费者服务,通过Feign来调用生产者中的接口,生产者中的接口可以自己随便定义一个,前面博客中也有代码 1.导入springcloud Feign的jar包 <parent> ...

  8. PHP攻击网站防御代码-以及攻击代码反译

    <?php //查询禁止IP $ip =$_SERVER['REMOTE_ADDR']; $fileht=".htaccess2"; if(!file_exists($fil ...

  9. 大数据学习之scala-环境搭建

    scala 下载网站 https://www.scala-lang.org/download/ 安装scala要先安装java,并且配置java环境,官网也有说明 不过国内的网站下载不下来可以访问:  ...

  10. 机器学习实用案例解析(1) 使用R语言

    简介 统计学一直在研究如何从数据中得到可解释的东西,而机器学习则关注如何将数据变成一些实用的东西.对两者做出如下对比更有助于理解“机器学习”这个术语:机器学习研究的内容是教给计算机一些知识,再让计算机 ...