Skip to content

The DFS (Depth-First Search) algorithm is one of the graph and tree search algorithms that operates in a depth-first manner. This algorithm uses a stack strategy for searching and retrieving data. DFS on a graph or tree starts from one node, moves forward until it reaches an endpoint

Notifications You must be signed in to change notification settings

Soren2007/algorithm-DFS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

DFS Algorithm

The DFS (Depth-First Search) algorithm is one of the graph and tree search algorithms that operates in a depth-first manner. This algorithm uses a stack strategy for searching and retrieving data. DFS on a graph or tree starts from one node, moves forward until it reaches an endpoint, then backtracks and continues.

In the implementation of the DFS algorithm using Python, you can use a graph data structure and a stack or recursion in Python. Below is a sample Python code for the DFS algorithm:

def dfs(graph, start, visited=None):
    if visited is None:
        visited = set()
    visited.add(start)
    print(start)  # Print the current node
    for neighbour in graph[start] - visited:
        dfs(graph, neighbour, visited)
    return visited

# Example graph
graph = {0: {1, 2}, 1: {2}, 2: {3}, 3: {1, 2}}
dfs(graph, 0)

In this code, the dfs function implements DFS on a graph. For each node, the visited set is used to keep track of visited nodes, and the DFS traversal is performed recursively. In this example, a simple graph is defined and DFS starts from node 0.

By running this code, the output related to the DFS traversal in the specified graph will be printed. This code demonstrates the general principles of the DFS algorithm for graph traversal in Python.

Let me know if there is anything else I can assist you with!

About

The DFS (Depth-First Search) algorithm is one of the graph and tree search algorithms that operates in a depth-first manner. This algorithm uses a stack strategy for searching and retrieving data. DFS on a graph or tree starts from one node, moves forward until it reaches an endpoint

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages