Depth-First Search, or DFS for short, is a way to explore all parts of a tree or graph. You can do it in two main ways: using recursion or iteration.
How long does it take? The time it takes is . Here, stands for the number of vertices (or points), and is the number of edges (or connections).
How much space does it use? The space needed is , where is how tall the tree is.
How long does it take? Just like the recursive method, it takes .
How much space does it use? In the worst case, it could use space.
And that’s how Depth-First Search works! Whether you choose to do it recursively or iteratively, it’s a helpful way to explore trees and graphs.
Depth-First Search, or DFS for short, is a way to explore all parts of a tree or graph. You can do it in two main ways: using recursion or iteration.
How long does it take? The time it takes is . Here, stands for the number of vertices (or points), and is the number of edges (or connections).
How much space does it use? The space needed is , where is how tall the tree is.
How long does it take? Just like the recursive method, it takes .
How much space does it use? In the worst case, it could use space.
And that’s how Depth-First Search works! Whether you choose to do it recursively or iteratively, it’s a helpful way to explore trees and graphs.