Tiefensuche (DFS) ist ein grundlegender algorithm used in Informatik for traversing or searching tree or graph Datenstrukturen. The algorithm starts at the root node (or an arbitrary node in the case of a graph) and explores as far as possible along each branch before backtracking. This approach results in a depthward exploration of the structure.
DFS kann mit einer Stapeldatenstruktur implementiert werden, entweder explizit mit einem Stack oder implizit durch Rekursion. Die Schritte des Algorithmus umfassen:
- Beginne beim Wurzelknoten (oder einem beliebigen Knoten).
- Markiere den Knoten als besucht.
- Erkunde jeden unbesuchten Nachbarknoten und gehe tiefer in die Struktur.
- Wenn ein Knoten keine unbesuchten Nachbarn hat, kehre zum zuletzt besuchten Knoten mit unbesuchten Nachbarn zurück.
DFS is particularly useful for problems where a solution might be found deep within the structure, such as solving mazes, puzzle-solving, or conducting searches in künstliche Intelligenz applications. It can also be leveraged to find connected components in graphs or to perform topological sorting.
However, DFS has its limitations, such as the potential for deep recursion leading to stack overflow and the inability to find the shortest path in unweighted graphs. Due to its nature, it may explore paths that do not lead to a solution, making it less efficient in some scenarios compared to other algorithms wie die Breitensuche (BFS).