algorithms-BFS&DFS-start.md
π‘BFS & DFS : Triversal technique
1. BFS, Breadth First Search
| good | shortest path, closer nodes | | ---- | --------------------------- | | bad | more memory |
2. DFS, Depth for Search
| good | less memory, does path exist | | ---- | ---------------------------- | | bad | can get slow |
BFS vs DFS
| | BFS | DFS | | -------------- | --------------------------- | -------------------------- | | μν© | μ΅λ¨ κ²½λ‘, κ°μ₯ κ°κΉμ΄ λ Έλ | κΈΈμ΄ μ‘΄μ¬νλμ§ μ¬λΆ | | ꡬν λ°©μ | ν(queue) | μ€ν(stack) λλ μ¬κ· ν¨μ | | νμ μμ | λλΉ μ°μ | κΉμ΄ μ°μ | | νμ λ°©ν₯ | μν | μμ§ | | μ΅λ¨ κ²½λ‘ νμ | κ°λ₯ | λΆκ°λ₯ | | κ³΅κ° λ³΅μ‘λ | O(V) (V: λ Έλ μ) | O(V) (V: λ Έλ μ) | | μκ° λ³΅μ‘λ | O(V + E) (E: κ°μ μ) | O(V + E) (E: κ°μ μ) |
Quiz
| quiz | reply | answer | explain | | ------------------------------------------------------------ | ----- | ------ | ------------------------------------------- | | If you know a solution is not far from the root of the tree. | BFS | BFS | | | If the tree is very deep and solutions are rare. | DFS | BFS | DFS will take long if the tree is very deep | | If the tree is very wide. | BFS | DFS | BFS will need too much memory | | If solutions are frequent but located deep in the tree. | DFS | DFS | | | determining whether a path exists between two roads. | DFS | DFS | | | Finding the shortest path. | BFS | BFS | |