Skip to main content

Depth first Search (DFS)


 

Depth-first search (DFS) is a method for traversing a graph or tree data structure. It involves exploring as far as possible along each branch before backtracking. The algorithm starts at the root node (or a selected node) and explores as far as possible along each branch before backtracking.

The basic idea behind DFS is to go deep in each branch before exploring siblings. This is done by using a stack data structure to keep track of the current vertex and the vertices to be visited next. The algorithm explores a vertex, marks it as visited, and adds it to the stack. It then selects the next vertex to visit by popping the top element from the stack. If the popped vertex has unvisited vertices, it pushes them onto the stack and continues exploring.

One of the main advantages of DFS is its simplicity. It is easy to implement and requires minimal memory. It can also be used to solve problems such as finding the connected components of a graph and testing if a graph is bipartite.

In addition to traversing a graph, DFS can also be used to find the path between two nodes in a graph, known as the DFS traversal. The algorithm can also be used to find the shortest path between two nodes in an unweighted graph.

Another application of DFS is in Topological sorting, where it is used to arrange the vertices of a directed acyclic graph in a linear order such that for every directed edge uv, vertex u comes before vertex v in the order.

DFS can also be used to solve the problem of finding cycles in a graph. A cycle is found if we backtrack from a vertex to an already visited vertex.

In summary, Depth-first search is a simple and efficient algorithm for traversing and searching a graph or tree. It is used in many applications such as graph traversal, path finding, topological sorting and finding cycles.

ALGORITHM:


The DFS algorithm can be implemented using a stack data structure. The basic steps for the algorithm are:

1. Initialize an empty stack and push the starting vertex onto it. Mark the starting vertex as visited.

2. While the stack is not empty, pop the top vertex from the stack.

3. For each unvisited neighbor of the popped vertex, mark it as visited and push it onto the stack.

4. Repeat step 2 and 3 until the stack is empty, at which point the algorithm has completed its traversal.

PSEUDO CODE:


Here's an example of pseudocode for the DFS algorithm using a stack:



And here's an example of pseudocode for the DFS algorithm using recursion:



And here is an example of pseudocode for the DFS algorithm that store the path of traversal:


It's important to note that the above pseudocode is a general representation of DFS algorithm and it may vary depending on the specific problem or the language you're implementing it in.

PYTHON CODE:

Here's an example of the DFS algorithm implemented in Python using a stack:




Here's an example of the DFS algorithm implemented in Python using recursion:




Here's an example of the DFS algorithm implemented in Python that store the path of traversal:




It's important to note that the above code assumes that the input graph is represented as an adjacency list, where each vertex is a key in a dictionary and its value is a set of its neighbors. It can be easily modified to work with adjacency matrix or any other data structure.

JAVA CODE:

Here's an example of the DFS algorithm implemented in Java using a stack:




Here's an example of the DFS algorithm implemented in Java using recursion:



And here's an example of the DFS algorithm implemented in Java that store the path of traversal:




It's important to note that the above code assumes that the input graph is represented as an adjacency list, where each vertex is a key in a List and its value is a List of its neighbors. It can be easily modified to work with adjacency matrix or any other data structure.
It's also important to note that the DFS algorithm can be implemented in many ways and the above examples are just a representation.

C CODE:

Here's an example of the DFS algorithm implemented in C using a stack:




Here's an example of the DFS algorithm implemented in C using recursion:




And here's an example of the DFS algorithm implemented in C that store the path of traversal:



It's important to note that the above code assumes that the input graph is represented as an adjacency matrix, where each vertex is a key in a 2D array and its value is an int indicating if it is connected to the other vertex or not. The code can be modified to work with adjacency list or any other data structure. It's also important to note that the DFS algorithm can be implemented in many ways and the above examples are just a representation.

C++ CODE:

Here's an example of the DFS algorithm implemented in C++ using a stack:



Here's an example of the DFS algorithm implemented in C++ using recursion:



And here's an example of the DFS algorithm implemented in C++ that store the path of traversal:



It's important to note that the above code assumes that the input graph is represented as an adjacency matrix, where each vertex is a key in a 2D array and its value is a boolean indicating if it is connected to the other vertex or not. The code can be modified to work with adjacency list or any other data structure. It's also important to note that the DFS algorithm can be implemented in many ways and the above examples are just a representation.

JAVASCRIPT CODE:

Here's an example of the DFS algorithm implemented in JavaScript using a stack:


Here's an example of the DFS algorithm implemented in JavaScript using recursion:



And here's an example of the DFS algorithm implemented in JavaScript that store the path of traversal:



It's important to note that the above code assumes that the input graph is represented as an adjacency list, where each vertex is a key in a object and its value is an array of its neighbors. It can be easily modified to work with any other data structure.
It's also important to note that the DFS algorithm can be implemented in many ways and the above examples are just a representation.




Comments

Popular posts from this blog

Selection Sort

Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array. The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays. One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations. Another advantage of selection sort is that it is a stable so

Counting Sort

Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output. The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array. Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times. The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k

Pigeonhole Sort

Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm. The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole. Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence. Pigeonhole sort has a time complexity