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
  • 相关阅读:
    高二下期末考试
    LG月赛.7
    CF1187E
    P5440 【XR-2】奇迹
    P1084 疫情控制
    P1083 借教室
    P2680 运输计划
    P3128 [USACO15DEC]最大流Max Flow
    博弈入门
    HDU 1907
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11958668.html
Copyright © 2011-2022 走看看