Depth First Search in Python (with Code) | DFS Algorithm

Depth First Search in Python

Traversing or navigating through nodes is a fundamental operation in the field of computer science, especially in the study of graphs. This traversal can be compared to visiting cities on a map, where each city is a node, and the roads connecting them are edges. There are many methods for traversing a graph, and the most prominent of them is depth-first search (DFS). In this tutorial, we will understand the intricacies of graph traversal and understand the essence of DFS algorithms.

Graph Traversal: A Brief Overview

Before diving into DFS, it is important to understand what graph traversal means.

What is a graph?

A graph is a collection of nodes (or vertices) and the edges that connect these nodes. Think of it as a network where each point (node) can have one or more connections (edges) to other points.

Why traverse the graph?

Traversing a graph means systematically visiting each node. This is essential for many applications, such as finding the shortest path between two nodes, checking connectivity, or searching for specific data within a graph.

Traversal methods:

There are mainly two ways to traverse a graph:

  1. Breadth-First Search (BFS): This method involves visiting all the direct neighbors of a node before visiting the neighbors of the next level.
  2. Depth-First Search (DFS): In this method, we dive as deep as possible on a path before going back.

Diving Deep with Depth-First Search (DFS)

What is Depth First Search in Python?

Depth-first search, as the name suggests, delves into the depths of a graph. Starting from a source node, DFS scans as far as possible along each branch before backtracking. It’s like navigating a maze: you take one path and follow it until you reach a dead end, then turn back and try another path.

Why is Depth First Search in Python important?

DFS is not just a theoretical concept; It has practical applications:

  1. Path Search: DFS can help determine a path between two nodes.
  2. Topological sorting: For directed acyclic graphs, DFS can provide a linear order of vertices.
  3. Cycle Detection: DFS can check whether a given graph has cycles.
  4. Connected Components: In an undirected graph, DFS can identify different connected components.

The Essence of Depth First Search in Python:

The core idea behind DFS is recursion. When you visit a node:

  1. Mark it as visited.
  2. Visit all your unvisited neighbors.
  3. If there are no unvisited neighbors, move back.

This recursive nature allows DFS to drill down into a graph, exploring each branch completely before continuing.

Understanding Depth First Search in Python

In the vast landscape of algorithms, Depth First Search (DFS) stands out as a beacon for traversing and searching tree or graph data structures. Like an explorer heading deep into the wilderness, undeterred by the challenges that await, DFS dives deep into each lane of a chart before retracing its steps. 

This comprehensive guide aims to illuminate the complexities of DFS, drawing parallels with real-world scenarios, unraveling its underlying principles, and distinguishing it from other cross-cutting techniques. Whether you are an experienced programmer or a curious newbie, embark on this journey to understand the essence of DFS and its vital role in the field of computing.

Navigating the Labyrinth: DFS and the Maze Analogy

Imagine being at the entrance to a vast, intricate labyrinth. Your goal is to find a way out, but there are countless paths stretching out in front of you, leading to various destinations, some to dead ends and others to the exit. How would you approach this challenge? 

One strategy could be to choose a path and follow it as far as you can. If it encounters a dead end, it will backtrack to the last junction and try a different route. This exploration method, in which one path is drilled down before considering others, reflects the principles of the Depth First Search (DFS) algorithm in graph theory.

The Essence of Backtracking in Depth First Search in Python

What is recoil?

Backtracking is a general algorithmic technique that involves exploring all possible solutions to a problem incrementally. When it becomes clear that the current path will not lead to a solution, you go back (or “backtrack”) to a previous step and try a different path.

Backtracking in DFS:

In the context of DFS, backtracking occurs when you venture deep into a branch of the graph and reach a node with no unvisited neighbors. 

At this point, it would backtrack to the previous node and explore other unvisited neighbors. This process continues until all nodes on the current path are visited, after which it will backtrack further to explore other branches of the graph.

Depth First Search in Python vs. Other Traversal Methods

While DFS is a powerful traversal method, it is essential to understand how it compares to other traversal techniques:

DFS (depth-first search):

Principle: Dive as deep down a path as possible before turning back.

Method: Use a stack (either explicitly or recursively) to remember which vertex to visit next.

Ideal for situations where you want to visit all nodes of a connected component, topological sorting, or when you have a tree-like structure.

BFS (breadth first search):

Principle: Visit all neighbors of a node before moving to the neighbors of the next level.

Method: Use a queue to keep track of which vertex to visit next.

Ideal for Situations where you want to find the shortest path from a source node to all other nodes, or when you have a more uniformly structured graph.


Depth vs. Breadth: While DFS drills down into a graph before moving back, BFS scans the graph in layers, visiting all nodes at the current depth before moving to nodes at the next depth level.

Data Structures: DFS typically uses a stack, while BFS uses a queue.

Applications: DFS is more suitable for tasks such as topological classification, while BFS is often used for shortest-path problems.

DFS Algorithm Explained: A Deep Dive into Depth First Search

Navigating the world of algorithms can sometimes feel like traversing a dense forest. Among the many routes available, Depth First Search (DFS) stands out as a unique and powerful method for exploring and understanding graph structures. In this tutorial, we will embark on a journey to discover the intricacies of the DFS algorithm, step by step.

Overview of DFS Algorithm

At its core, DFS is a traversal or search algorithm that explores as far as possible along each branch of a graph before backtracking. This is similar to a spelunker exploring a cave system: they will venture deeper into a tunnel, and only when they reach a dead end will they retreat to explore another passage.

Main features of DFS:

Depth search: DFS is depth-prioritized, meaning it searches a node’s descendants before its siblings.

Recursive nature: The algorithm naturally uses a recursive approach, diving deep into nodes and then backtracking when necessary.

Versatility: Despite being commonly associated with graphs, DFS can also be applied to tree structures.

Steps involved in the DFS tour

Step 1: Start at the source. Start your journey from an origin node or starting point.
Step 2: Explore unvisited neighbors. From the current node, move to an adjacent unvisited node. This becomes the new current node.
Step 3: Mark as Visited. Once a node is explored, mark it as “Visited”. This ensures that we don’t revisit nodes and get stuck in an infinite loop, especially in cyclic graphs.
Step 4: Dive Deeper. Continue the process, moving to the next unvisited neighbor of the current node, marking it as visited, and drilling down further.
Step 5: Go back when necessary. If the current node has no unvisited neighbors, roll back to the previous node and explore its other neighbors. This is where the algorithm gets its name, as it dives “deep” before “searching” for other branches.
Step 6: Complete the tour. The process continues until all nodes connected to the source node have been visited. If the graph has multiple disconnected components, it will select a new unvisited node as the source and repeat the process.

The crucial role of “visited” and “unvisited” categories

In the world of DFS, the distinction between “visited” and “unvisited” nodes is paramount. This is why:

  1. Avoid infinite loops: Especially in graphs with loops, without a “Visited” marker, the algorithm could continue traversing circles indefinitely.
  2. Efficiency: By marking nodes as “visited,” the algorithm avoids unnecessary exploration, making the traversal process faster and more efficient.
  3. Rollback Decisions: The “Not Visited” category helps in making decisions during rollback. By backtracking to a node, the algorithm can quickly identify which neighbors have not yet been explored.
  4. Ensure comprehensive exploration: By differentiating between visited and unvisited nodes, DFS ensures that all nodes in the graph are explored, ensuring a thorough and complete traversal.

DFS Pseudocode Unveiled: A Detailed Guide to Depth First Search Algorithm

Pseudocode, the bridge between human thought and machine operation, provides a high-level representation of an algorithm. When it comes to the Depth First Search (DFS) algorithm, understanding its pseudocode is crucial to understanding its essence and implementation. In this tutorial, we’ll delve deeper into DFS pseudocode, breaking down each component for a comprehensive understanding.

Pseudocode Representation of DFS

					FUNCTION DFS(graph, startNode):
    DECLARE Stack S
    DECLARE Set Visited

    PUSH startNode INTO S

    WHILE S is not empty:
        currentNode = POP S

        IF currentNode is not in Visited:
            ADD currentNode to Visited

            FOR each neighbor in neighbors of currentNode:
                IF neighbor is not in Visited:
                    PUSH neighbor INTO S

    RETURN Visited


Explanation of the Pseudocode and its Components FUNCTION DFS(graph, startNode):

This initializes the DFS function, which takes two parameters:

  • Graph: The graph you want to traverse.
  • StartNode: The node from which you want to start the traversal.
  • Declare stack s: A stack, S, is declared to keep track of nodes. The choice of stack is important for the depth-first nature of DFS, ensuring that the most recently discovered nodes are explored first.
  • Declare set visited: A set, visited, is declared to keep track of all the nodes visited. This ensures that we don’t revisit nodes and get stuck in an infinite loop.
  • Push start node to S: The starting node is pushed onto the stack, marking the beginning of the traversal.
  • While S is not empty: The main loop of the algorithm runs as long as there are nodes in the stack. This ensures that all nodes connected to the start node have been explored.
  • CurrentNode = POP S: The top node is popped from the stack and set as the current node for exploration.
  • If the current node is not in visit: Before exploring a node, we check whether it has been visited before. If this has not happened, we proceed with the exploration.
  • Add the current node to visited: The current node is added to the visited set, marking it as discovered.
  • For each neighbor in currentNode’s neighbors: This loop allows us to find all the neighbors of the current node.
  • If the neighbor is not in the visit: Before pushing a neighbor onto the stack, we make sure that it has never been there before.
  • Push neighbor into S: Undiscovered neighbors are pushed onto the pile for future exploration.
  • Visited Returns: Once traversal is complete, the visited set is returned, providing a record of all nodes explored.

Python Implementation of DFS: A Step-by-Step Guide

Depth First Search (DFS) is a powerful algorithm for traversing and searching tree or graph data structures. Implementing it in Python, a language known for its clarity and readability, allows us to better see and understand its complexities. In this tutorial, we will learn about the Python implementation of DFS, from setting up a graph to executing a function.

Setting up the Graph Representation using Python Dictionaries

Python dictionaries provide an intuitive way to represent graphs. Each key-value pair corresponds to a node and its adjacent nodes.


					graph = {
    'A': ['B', 'C'],
    'B': ['A', 'D', 'E'],
    'C': ['A', 'F'],
    'D': ['B'],
    'E': ['B', 'F'],
    'F': ['C', 'E']


In this representation:

  • Each key (e.g., ‘A’, ‘B’, ‘C’) represents a node.
  • The associated list (e.g., ['B', 'C']) represents the neighboring nodes.

Writing the DFS Function in Python

					def DFS(graph, start, visited=None):
    if visited is None:
        visited = set()

    for neighbor in graph[start]:
        if neighbor not in visited:
            DFS(graph, neighbor, visited)

    return visited


Explanation of the Code and its Components

def DFS(graph, start, visits=None):
It initializes the DFS function with three parameters:

Graph: The graph to be traversed.
Start: The starting node for traversal.
Visited: A set to keep track of visited nodes (initially defaults to none).
If visited there is none:
Checks whether the visited set is initialized or not. If not, it starts it.

The initial node is added to the visited set.

For neighbors in the graph[start]:
This loop iterates over the neighbors of the current node (start).

If the neighbor has not come:
Before diving deeper into a neighbor node, we check if it has been visited before.

DFS(graph, neighbors, visited):
A recursive call to the DFS function, with the neighbor as the new starting node. This ensures depth-first traversal.

Visited Return:
Once all nodes connected to the initial start node have been explored, the visited set is returned.

Driver Code to Execute the DFS Function

To see the DFS function in action, we’ll use the previously defined graph and start the traversal from node ‘A’.

					if __name__ == "__main__":
    graph = {
        'A': ['B', 'C'],
        'B': ['A', 'D', 'E'],
        'C': ['A', 'F'],
        'D': ['B'],
        'E': ['B', 'F'],
        'F': ['C', 'E']

    result = DFS(graph, 'A')



					{'A', 'B', 'C', 'D', 'E', 'F'}

This output shows the nodes visited during the DFS traversal starting from node ‘A’.

Depth First Search (DFS) Unveiled: A Visual and Analytical Guide

Visual representation & Graphical representation of the graph used in the code:

To truly understand the DFS algorithm, visualizing the graph is paramount. Imagine a network of interconnected nodes, where each node represents a data point and the connections (or edges) signify their relationships.

For our tutorial, let’s consider this graph:

					A - B - D
|   |
C   E


Step-by-step visual traversal using DFS:

Starting from node A:

  1. go to a
  2. Visit an unknown neighbor: B
  3. From B, move towards its unknown neighbor 😀
  4. D has no unknown neighbors, so go back to B
  5. B: Go to next undiscovered neighbor of E
  6. E has no unknown neighbors, so go back to B, then to A
  7. Go to next unseen neighbor of A:C
    output explanation
  8. Displaying the output of DFS code:

For the above graph, a DFS traversal starting from A would yield: A, B, D, E, C

Analyzing output and understanding traversal order: The output reflects the depth-first nature of DFS. It dives deep from B to D before backtracking and exploring other branches.

Consider this graph:

					F - G
|   |
H - I - J


Visual representation of DFS traversal on an example graph:

Starting from node F:

  1. go to f
  2. go to g
  3. move from G to I
  4. From I, move to H
  5. Go back to I and then go to J

Explanation of traversal process:

The traversal shows how DFS explores as much as possible along a branch before backtracking.

Time and space complexity of DFS

Time Complexity Analysis: O(V + E)

  • V represents the number of vertices.
  • E represents the number of edges.
  • In the worst case, DFS may visit all vertices and edges.

Understanding Space Complexity: O(V)

The free space is mainly used by the stack during recursive function calls, and in the worst case, all the vertices may be stored in the stack.

Applications of depth-first search Real-world applications and use-cases of DFS:

Path finding: Identifying paths in a maze or network.
Topological sorting: Useful in scheduling tasks.

Cycle detection: identifying loops in a network or graph.
Connected components: Finding isolated clusters in networks.

Importance of DFS in various domains: DFS is important in computer networking, data mining, and even social network analysis, where it helps identify clusters or groups.


This comprehensive guide aims to provide a structured and in-depth understanding of the depth-first search algorithm in Python. With practical examples, you’re ready to leverage the power of DFS in your computational efforts. Dive in, explore and let the world of algorithms reveal its treasures to you!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *