zoukankan      html  css  js  c++  java
  • [Algorithm] BFS vs DFS

    //If you know a solution is not far from the root of the tree:
    BFS, because it is faster to get closer node
    
    //If the tree is very deep and solutions are rare: 
    BFS, DFS will take a longer time because of the deepth of the tree
    
    //If the tree is very wide:
    DFS, for the worse cases, both BFS and DFS time complexity is O(N).
    But for the space complexity, DFS is O(H), where H is the height of the tree
    BFS space complexity is O(W), where W is the width of the tree
    As we know tree is very wide, W > H, so we choose DFS
    
    //If solutions are frequent but located deep in the tree:
    DFS, because we can find the node quickly
    
    //Determining whether a path exists between two nodes:
    DFS, it is good to check a path exists
    
    //Finding the shortest path:
    BFS, it is good to find shortest path
  • 相关阅读:
    wxpython自定义按钮
    wxPython修改文本框颜色
    strcat函数
    fopen作用
    Less 语法快速入门
    Echarts构建图表
    伪数组
    MVC,MVP,MVVM基本原理
    VUE常见指令
    Apply,Call,bind对比
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11958668.html
Copyright © 2011-2022 走看看