MyWorldGo All About Depth First Search(DFS) in Data Structure

Blog Information

  • Posted By : sandesh sandy
  • Posted On : Apr 12, 2023
  • Views : 97
  • Category : Education
  • Description : The depth-first traversal (DFS) algorithm in data structures is a recursive method generally used to examine every vertex in a graph or tree data structure.

Overview

  • The depth-first traversal (DFS) algorithm in data structures is a recursive method generally used to examine every vertex in a graph or tree data structure. Every node in a graph is visited during traversal. The algorithm starts at the root node, which can be any node in the network. It then proceeds to travel along each branch as far as possible before turning around. 


    The goal is to start at the root or any other node and to maintain the marked node. After that, you must move to the unmarked neighboring node and repeat this loop till there is no more unmarked nearby node. After that, go back and inspect and navigate the remaining unmarked nodes. Printing the nodes along the path is the last step. Explore the top Data structure course, to upgrade your skills and fast track your career. 

    Algorithm

    Create a recursive function that takes a visited array and the node's index.

    • Keep the current node's visited status and print it afterward.
    • Navigate all neighboring and unmarked notes, then call the recursive procedure using the index of the adjacent node.

    Properties 

    Depending on the application, several temporal and space analyses are performed on the DFS in the data structure. According to theory, DFS is primarily used to traverse a full graph and requires a time O(|V|+|E|), where |V| represents the number of vertices and |E| represents the number of edges. This graph has a linear slope. 


    The stack of vertices stored on the search path and the set of vertices that have already been visited are kept in these applications as a last resort using space O(|V|). As a result, the breadth-first search is comparable to establishing time and space limitations. In this case, the two methods are more affected by the different traits of the vertex ordering that each algorithm generates than by their level of complexity. 


    The graph that needs traversing could be too large to completely explore when it comes to DFS in Data Structure applications pertaining to particular areas, such as solving problems in web crawling or AI. Due to limited resources, such as memory or disc space, the search is only carried out to a certain depth in these situations. Normally, the set of all previously visited vertices is not tracked by data structures. In terms of the unit of extended edges and vertices, a search performed to a limited depth nevertheless results in linear time. 

    Keep in mind, though, that this number is not the same size as the complete graph because certain vertices may be searched more than once while others may not be.

    For the purpose of picking a more promising branch in such circumstances, DFS also uses heuristic methods. Finally, a priori, iterative deepening DFS is repeatedly implemented via a series of rising limits when the suitable depth limit cannot be identified. 

    Depth First Search Algorithm

    In a typical DFS implementation, each graph vertex falls into one of two categories:


    1. Visited 
    2. Not Visited

    Each vertex is located using the technique, marking them as visited while preventing cycles.


    What the DFS algorithm does is as follows:

    • Any specific graph vertex can be placed on a stack.
    • The item at the top of the pile has to be put on the visited list.
    • Create a list of the nodes next to that vertex and add any that aren't on the visited list to the top of the stack.
    • Till the stack is empty, keep performing the previous steps.

    Depth First search Ordering

    Vertex orderings: In DFS, there are four different techniques to arrange the vertices of a network or tree linearly:


    1. Pre-ordering refers to the list of vertices organized according to the ones the DFS algorithm visited first. It provides a brief summary of the search's development.
    2. Post-ordering refers to the list of vertices in the order in which the algorithm last visited them.
    3. A reverse pre-ordering is demonstrated by listing the vertices in the direction of their first visit. As a result, post-ordering should not be confused with it.
    4. A reverse post-ordering is when the vertices are listed in the opposite order from when they were last visited. It shouldn't be confused with pre-ordering.

    Depth First Search or DFS for a Graph

    A graph's DFS is nearly identical to a tree's DFS. The sole distinction is that graphs may include cycles in contrast to trees. A Boolean visited array is used in cases where a node is visited more than once to avoid processing the node each time. 

    Results From A Depth-First Search

    It is simpler to understand the depth-first search in terms of a spanning tree of the previously found vertexes. Based on this spanning tree, the original graph's edges can be categorized into three groups: forward edges, where a tree node is pointed towards one of its offspring; back edges, where a node is pointed towards one of its ancestors; and cross edges, which serve neither of the purposes mentioned above.

    Applications Of Depth First Traversal (DFS)

    The following algorithms employ depth-first search as a foundation:


    •         Finding linked parts through searching.
    •         Identifying 2-connected (vertex or edge) elements.
    •         Finding the bridges in the graph.
    •         Locating 3 connected (vertex or edge) elements.
    •         Sorting using topology.
    •         Identifying elements with a strong connection.
    •         Determining a species' relationship to other species by looking at a phylogenetic tree.
    •         Evaluating planarity.
    •         Creating words to establish any group's upper limit.
    •         Figuring out puzzles with a single solution, like mazes.
    •         Making a maze.
    •         Graphs are being examined for bi-connectivity.

    Last Words 

    The DFS algorithm's primary objective is to visit every vertex in the target graph while avoiding cycles. Consider taking a comprehensive and high-quality data structures and algorithm course if you want to work with sophisticated searching or ordering operations implementations.