VOOZH about

URL: https://www.analyticsvidhya.com/blog/2024/06/depth-first-search-algorithm-in-python/

⇱ Depth First Search (DFS) Algorithm in Python - Analytics Vidhya


India's Most Futuristic AI Conference Is Back – Bigger, Sharper, Bolder

  • d
  • :
  • h
  • :
  • m
  • :
  • s

Reading list

Implementation of Depth First Search (DFS) Algorithm in Python

NISHANT TIWARI Last Updated : 05 Jun, 2024
5 min read

Introduction

In depth-first search (DFS), all nodes are explored along some branch and backtracked. Think of it as being in a maze: DFS goes down one path until it reaches a dead-end before retracing its steps to take another, right? It is the same as going down, validating the tunnel, and so on for all tunnels.

DFS is useful for:

  • Visiting every node in a graph.
  • Checking how different nodes are connected.

While DFS dives deep, another method called Breadth-First Search (BFS) checks all neighbors at the current level before moving deeper. Both methods are important, but Depth First Search (DFS) helps you go as far as possible down one path before trying another.

πŸ‘ Depth First Search (DFS)

The Overview

  • DFS will exhaustively visit a single path before backtracking to a node with an unvisited path.
  • DFS-Recursive uses a call stack to manage traversal and goes deeper into each path.
  • Uses a separate stack to maintain the exploration; therefore, no recursion depth problem.
  • DFS’s time complexity is O(V+E)O(V + E)O(V+E), and its space complexity is O(V)O(V)O(V).
  • DFS is cool for many things. Some of the most common are pathfinding, cycle detection, topological sorting, and puzzle solving.
  • Difference between DFS and BFS BFS explores each level first and then goes to the next level, whereas DFS goes through one branch and then moves to the current.

How Does Depth First Search (DFS) Work?

The DFS algorithm involves visiting nodes as deeply as possible before backtracking. Here’s a step-by-step explanation:

  1. Starting node: The search will start at the root node, in the case of a tree, or from an arbitrary node, in the case of the graph.
  2. Visit: Mark node as visited.
  3. Explore: For each adjacent node, recursively visit the node if it has not been visited yet.
  4. Backtrack: When a node with no unvisited adjacent nodes is reached, backtrack to the previous node and continue the process.

Also read: A Complete Python Tutorial to Learn Data Science from Scratch

Depth First Search (DFS) Algorithm

DFSβ€”Depth First Search is a recursive algorithm. To implement it for a graph, we can either use recursion or implicit recursion using Stack.

Recursive Implementation

The recursive implementation of DFS leverages the call stack to manage the traversal state. Here is a Python implementation:

Code:

def dfs_recursive(graph, node, visited):

    if node not in visited:

        print(node, end=' ')

        visited.add(node)

        for neighbour in graph[node]:

            dfs_recursive(graph, neighbour, visited)

# Example usage:

graph = {

    'A': ['B', 'C', 'D'],

    'B': ['E'],

    'C': ['G', 'F'],

    'D': ['H'],

    'E': ['I'],

    'F': ['J'],

    'G': ['K']

}

visited = set()

print("DFS traversal using recursive approach:")

dfs_recursive(graph, 'A', visited)

Iterative Implementation

The iterative implementation uses an explicit stack to manage the traversal. This can be useful to avoid potential issues with recursion depth in languages that limit the call stack size.

Code:

def dfs_iterative(graph, start):

    visited = set()

    stack = [start]

    while stack:

        node = stack.pop()

        if node not in visited:

            print(node, end=' ')

            visited.add(node)

            stack.extend(reversed(graph[node]))  # Add in reverse order to maintain the order of traversal

# Example usage:

graph = {

    'A': ['B', 'C', 'D'],

    'B': ['E'],

    'C': ['G', 'F'],

    'D': ['H'],

    'E': ['I'],

    'F': ['J'],

    'G': ['K']

}

print("\nDFS traversal using iterative approach:")

dfs_iterative(graph, 'A')

Explanation

The code examples refer to the graph as an adjacency list. Each node is like a key, listing all the nodes directly connected to it. To avoid revisiting the same node, we have a set named visited, which stores the previous node.

  • Recursive DFS: The dfs_recursive function calls itself for each unvisited neighbor of the current node, diving deep into each path.
  • Iterative DFS: The dfs_iterative function uses a stack (a list where you add and remove items) to manage which nodes to visit next. This stack works like the call stack in the recursive method, helping track the order of visits.

Both methods ensure all parts of the graph are explored, but they do it differently.

Time and Space Complexity

Here is the time and space complexity of DFS:

  • Time complexity: The time complexity of DFS is O(V + E), where V and E are the number of vertices and edges, respectively. In the worst case, each vertex and edge will be searched once.
  • Space Complexity: Space complexity would be O(V) since we need to keep track of visited nodes. In recursion, we would run a recursion stack, or we may push nodes into the stack in iterative.

Applications of Depth First Search (DFS)

Depth First Search (DFS) has numerous applications in computer science and real-world problems:

  • Pathfinding: DFS would be useful for finding a path between two nodes in a graph.
  • Cycle Detection: It helps detect cycles in a graph and is useful in various domains, like dependency resolution.
  • Use cases for topological sorting: Scheduling the tasks so that each task depends on the others and must be performed in linear order.
  • Graph Traversal & Connected Components: DFS in an undirected graph to identify all connected components.
  • Maze and Puzzle Solving: Solve complex maze and puzzles by traversing every possible path.

Real-World Example

Suppose you have to find all the paths in a network of computers so that the data will be transmitted correctly. DFS is an algorithm that performs a depth-first search in a graph. It can be used to start from a particular computer and follow connections as far as they go, backtracking when no more connections can be followed.

Code:

def find_paths(graph, start, end, path=[]):

    path = path + [start]

    if start == end:

        return [path]

    if start not in graph:

        return []

    paths = []

    for node in graph[start]:

        if node not in path:

            new_paths = find_paths(graph, node, end, path)

            for p in new_paths:

                paths.append(p)

    return paths

# Example usage:

network = {

    'Computer1': ['Computer2', 'Computer3'],

    'Computer2': ['Computer4'],

    'Computer3': ['Computer4', 'Computer5'],

    'Computer4': ['Computer6'],

    'Computer5': ['Computer6'],

    'Computer6': []

}

start = 'Computer1'

end = 'Computer6'

print(f"All paths from {start} to {end}:")

paths = find_paths(network, start, end)

for path in paths:

    print(" -> ".join(path))

DFS vs BFS

While DFS dives deep into a graph, BFS explores all neighbors of a node before moving to the next level. Each has its advantages:

  • DFS: Uses less memory and can find a path without exploring all nodes.
  • BFS: Guarantees finding the shortest path in an unweighted graph.

Conclusion

DFS, or Depth-First Search, is a powerful utility for traversing graphs and using them in tree problems. DFS is useful when solving puzzles, finding your way in a maze, or organizing tasks. The two ways to use DFS are:

  • Recursive DFS: this uses function calls to track where you are coming from in the graph.
  • Iterative DFS: Using a stack to handle the steps.

The 2 methods guaranteed full coverage of the graph; every node was explored. Here is a list of how DFS can find paths, detect cycles, sort tasks, and connect components in a graph. Gaining knowledge about DFS will help you solve tough problems. After seeing the examples, you can explore DFS in your code.

So, are you looking for Python courses online? If yes, explore – Introduction to Python today!

Frequently Asked Questions

Q1. What is the main purpose of using DFS in graph traversal?

A. The primary goal of DFS is to traverse all the nodes in a graph, visiting each node exactly once (which means no node is visited twice or more), while recursively visiting as deep as possible along a branch before backtracking.

Q2. Why might one prefer the iterative implementation of DFS over the recursive one?

A. DFS can be implemented using recursion, but iterative implementation is desired, especially where the stack is limited, or the recursion depth limit is not high, to prevent stack overflow.

Q3. How does DFS handle cycles in a graph?

A. DFS handles cycles by keeping track of visited nodes, ensuring each node is visited only once to prevent infinite loops.

Q4. Can DFS be used to find the shortest path in a graph?

A. DFS does not guarantee the shortest path in an unweighted graph; Breadth-First Search (BFS) is better suited for this purpose.

Seasoned AI enthusiast with a deep passion for the ever-evolving world of artificial intelligence. With a sharp eye for detail and a knack for translating complex concepts into accessible language, we are at the forefront of AI updates for you. Having covered AI breakthroughs, new LLM model launches, and expert opinions, we deliver insightful and engaging content that keeps readers informed and intrigued. With a finger on the pulse of AI research and innovation, we bring a fresh perspective to the dynamic field, allowing readers to stay up-to-date on the latest developments.

Login to continue reading and enjoy expert-curated content.

Free Courses

Generative AI - A Way of Life

Explore Generative AI for beginners: create text and images, use top AI tools, learn practical skills, and ethics.

Getting Started with Large Language Models

Master Large Language Models (LLMs) with this course, offering clear guidance in NLP and model training made simple.

Building LLM Applications using Prompt Engineering

This free course guides you on building LLM apps, mastering prompt engineering, and developing chatbots with enterprise data.

Improving Real World RAG Systems: Key Challenges & Practical Solutions

Explore practical solutions, advanced retrieval strategies, and agentic RAG systems to improve context, relevance, and accuracy in AI-driven applications.

Microsoft Excel: Formulas & Functions

Master MS Excel for data analysis with key formulas, functions, and LookUp tools in this comprehensive course.

Responses From Readers

Flagship Programs

GenAI Pinnacle Program| GenAI Pinnacle Plus Program| AI/ML BlackBelt Program| Agentic AI Pioneer Program

Free Courses

Generative AI| DeepSeek| OpenAI Agent SDK| LLM Applications using Prompt Engineering| DeepSeek from Scratch| Stability.AI| SSM & MAMBA| RAG Systems using LlamaIndex| Building LLMs for Code| Python| Microsoft Excel| Machine Learning| Deep Learning| Mastering Multimodal RAG| Introduction to Transformer Model| Bagging & Boosting| Loan Prediction| Time Series Forecasting| Tableau| Business Analytics| Vibe Coding in Windsurf| Model Deployment using FastAPI| Building Data Analyst AI Agent| Getting started with OpenAI o3-mini| Introduction to Transformers and Attention Mechanisms

Popular Categories

AI Agents| Generative AI| Prompt Engineering| Generative AI Application| News| Technical Guides| AI Tools| Interview Preparation| Research Papers| Success Stories| Quiz| Use Cases| Listicles

Generative AI Tools and Techniques

GANs| VAEs| Transformers| StyleGAN| Pix2Pix| Autoencoders| GPT| BERT| Word2Vec| LSTM| Attention Mechanisms| Diffusion Models| LLMs| SLMs| Encoder Decoder Models| Prompt Engineering| LangChain| LlamaIndex| RAG| Fine-tuning| LangChain AI Agent| Multimodal Models| RNNs| DCGAN| ProGAN| Text-to-Image Models| DDPM| Document Question Answering| Imagen| T5 (Text-to-Text Transfer Transformer)| Seq2seq Models| WaveNet| Attention Is All You Need (Transformer Architecture) | WindSurf| Cursor

Popular GenAI Models

Llama 4| Llama 3.1| GPT 4.5| GPT 4.1| GPT 4o| o3-mini| Sora| DeepSeek R1| DeepSeek V3| Janus Pro| Veo 2| Gemini 2.5 Pro| Gemini 2.0| Gemma 3| Claude Sonnet 3.7| Claude 3.5 Sonnet| Phi 4| Phi 3.5| Mistral Small 3.1| Mistral NeMo| Mistral-7b| Bedrock| Vertex AI| Qwen QwQ 32B| Qwen 2| Qwen 2.5 VL| Qwen Chat| Grok 3

AI Development Frameworks

n8n| LangChain| Agent SDK| A2A by Google| SmolAgents| LangGraph| CrewAI| Agno| LangFlow| AutoGen| LlamaIndex| Swarm| AutoGPT

Data Science Tools and Techniques

Python| R| SQL| Jupyter Notebooks| TensorFlow| Scikit-learn| PyTorch| Tableau| Apache Spark| Matplotlib| Seaborn| Pandas| Hadoop| Docker| Git| Keras| Apache Kafka| AWS| NLP| Random Forest| Computer Vision| Data Visualization| Data Exploration| Big Data| Common Machine Learning Algorithms| Machine Learning| Google Data Science Agent
πŸ‘ Av Logo White

Continue your learning for FREE

Forgot your password?
πŸ‘ Av Logo White

Enter OTP sent to

Edit

Wrong OTP.

Enter the OTP

Resend OTP

Resend OTP in 45s

πŸ‘ Popup Banner
πŸ‘ AI Popup Banner