mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Update README.md
This commit is contained in:
parent
c1949996e4
commit
465e840600
@ -7,8 +7,13 @@
|
|||||||
|
|
||||||
### breath-first search
|
### breath-first search
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
* similar to pre-order, but work with queue (level order problem)
|
- similar to pre-order, but work with queue (level order problem)
|
||||||
|
- one common application of breadth-first search (BFS) is to find the shortest path from the root node to the target node.
|
||||||
|
- in the first round, we process the root node, in the second round, we process the nodes next to the root node, in the third round, we process the nodes which are two steps from the root node, etc. newly-added nodes will not be traversed immediately but will be processed in the next round.
|
||||||
|
- if node X is added to the kth round queue, the shortest path between the root node and X is exactly k.
|
||||||
|
- the processing order of the nodes in the exact same order as how they were added to the queue, which is FIFO.
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
@ -16,8 +21,16 @@
|
|||||||
|
|
||||||
### depth-first search
|
### depth-first search
|
||||||
|
|
||||||
* if the depth of the tree is too large, stack overflow might happen, therefore iterative solutions might be better.
|
<br>
|
||||||
* work with stacks
|
|
||||||
|
- similar to BFS, deep-first search (DFS) can also be used to find the path from the root node to the target node.
|
||||||
|
- overall, we only trace back and try another path after we reach the deepest node. as a result, the first path you find in DFS is not always the shortest path.
|
||||||
|
- we first push the root node to the stack, then we try the first neighbor and push its node to the stack, etc.
|
||||||
|
- when we reach the deepest node, we need to trace back. when we track back, we pop the deepest node from the stack, which is actually the last node pushed to the stack.
|
||||||
|
- the processing order of the nodes is exactly the opposite order of how they are added to the stack.
|
||||||
|
- recursion solutions are easier to implement; however, if the recursion depth is too high, stack overflow might occur. in that case, you might use BFS instead or implement DFS using an explicit stack (i.e., use a while loop and a stack structure to simulate the system call stack).
|
||||||
|
- if the depth of the tree is too large, stack overflow might happen, therefore iterative solutions might be better.
|
||||||
|
- work with stacks
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user