Question

Write a Python Program to implement Depth First Search.

17 Feb 2026
Answer :
Word Count : 909
Depth First Search (DFS) is a fundamental graph traversal algorithm widely used in Artificial Intelligence and Machine Learning laboratories for exploring state spaces, searching solution paths, and analyzing graph structures. It follows the principle of exploring as far as possible along each branch before backtracking. In AI applications such as problem solving, puzzle solving, path finding, and tree-based model exploration, DFS provides a systematic way to traverse nodes either recursively or using an explicit stack data structure. In Python, DFS can be implemented efficiently using an adjacency list representation of a graph. An adjacency list stores each vertex and a list of its neighboring vertices. This representation is memory-efficient and suitable for sparse graphs, which are common in real-world AI problems. There are two main approaches to implementing DFS in Python: recursive implementation and iterative implementation using a stack. The recursive approach is simpler and leverages the system call stack, while the iterative approach uses an explicit stack data structure and provides more control over traversal. Below is a Python program that implements Depth First Search using recursion: ```python # Depth First Search using recursion def dfs_recursive(graph, node, visited): if node not in visited: print(node, end=" ") visited.add(node) for neighbor in graph[node]: dfs_recursive(graph, neighbor, visited) # Example graph represented as adjacency list graph = { 'A': ['B', 'C'], 'B': ['D', 'E'], 'C': ['F'], 'D': [], 'E': ['F'], 'F': [] } visited = set() print("DFS Traversal (Recursive):") dfs_recursive(graph, 'A', visited) ``` In this program, the graph is represented as a dictionary where each key represents a
_________ ____ ______ _____ ________ ______ _______ ____ ______ _________ __________.
____ _________ ____ __________ _________ ____ _____.
____ __________ _____ __________ ______ _____ ____ ___.
_____ ______ _____ __________ _________ ________ ___.
___ ____ ______ _________ _____.
___ ____ __________ _______ __________.
________ ____ ________ _________ ____ ____ __________ ______ ______ ________.
_____ ___ __________ ___ ___ __________.
__________ _______ ____ ____ ____ _____ ____ _____ __________ ____ ___.
_______ _____ ________ ____ ___ ____ ___ ________ _____.
________ ________ __________ ______ ___ __________.
___ ___ ____ __________ _______ __________ ___ _________ _________ _____.
________ _____ ____ _____ ____ ______ _________.
______ __________ ______ _____ _______ ________ ____ ______ __________ ___.
__________ ___ _____ ___ __________.
__________ ________ ______ _______ _________ ____.
_________ ______ _________ ____ ________ _____.
________ ______ __________ ________ __________ _______ ____ ___ ___ ___.
____ __________ ________ ___ ________ ______ _____ _______ ___ ________ _____ ______.
____ _______ _____ ___ ________.
__________ _____ ______ ____ ___ _________ _______ ________ ____ ______ _____.
________ ___ ___ ___ _______ _____ __________ ______ ____ ____ __________ ____.
________ ______ ____ ___ ___ ____.
__________ ___ _____ ______ __________ _____ ___.
___ __________ ________ ______ _____ __________.
____ _______ ______ ______ _________ ________ ______ _____ _________ ______ ___ __________.
____ _________ ___ _____ ___ ___.
_____ _____ ________ _____ __________ __________ ___.
______ _______ ___ _______ ____ ________ ____ ______ __________.
___ ____ _______ ___ ____ _______ _________.
_____ __________ _____ _______ _____ _______ _________.
____ _______ _________ _______ _________ _______.
________ __________ _________ _____ ______ ________ ______ _______.
____ ____ _______ __________ ___ _________ _____.
______ _______ ___ __________ ________ ________ _______.
_____ _____ _______ _______ ______ ________ _________ ______ _______ ________.
__________ ________ __________ ________ ___ ______.
_____ _____ __________ _________ ________ ____ ______.
________ ______ ________ ____ ________ ________.
__________ ____ _____ ____ ______ _________ _______ ______ ___ ___ _______.
_____ _________ ____ ____ _______ ______ _______ ____ __________.
________ ___ ______ _______ ____ ___ _________ ________ ____ _________ _________ ______.
______ _____ ___ _____ _____ ____ ______ ______.
______ _____ ________ _________ ________.
___ ____ __________ ________ ______ ________ ____ _______ _____.
__________ ______ ________ _____ ________ __________ ________ ________ ____ ______.
___ _______ _________ __________ _____ _______ ____ ___ _________ _____.
____ __________ _____ _________ _______ ____ __________ _____.
____ ___ _________ _________ ________ _____ _______ _____ _____ _________.
_____ ____ __________ _______ __________ _______ __________ ____ ____ _____ _______ ____.
____ ______ _______ ____ _____ _______ _____.
______ _________ ______ __________ ___ ______ ______ ________ _______ _________ __________.
__________ __________ _________ ________ ________ _______ ___ ________ ____.
________ ________ ________ ______ _______.
___ ____ ______ ____ ______ ___ ______ _______ ___ _______ _________ _________.
__________ ___ _______ __________ _______ ____ ____ ________ _________ ________.
____ ___ ________ __________ ________.
_______ _____ ___ ______ ________.
______ _________ __________ ____ ________ _____ _______.
_________ _______ ___ __________ ___ _______ ______ _______ __________ __________ _____ ______.
_________ ______ _________ __________ __________ _______ _______ ________ _________ __________ _______.
________ ___ _________ ______ _______ ____.
_______ _________ ____ ____ _______ ___ _______ ___ _____ ____ ______ ____.
_____ ________ ______ ___ ____ _______ _______ __________ _______ ________ _______.
_________ __________ ___ ________ ___ ____ ___ _________ ______ _____ ________ _______.
_____ ______ _________ ___ _____ ________ ________ ___ _______ ______.
_______ _____ _________ ___ _____ _________ ___.
____ __________ ________ ___ _____ ____ ___ ______ ________.
____ ________ ______ _________ _______ ___ __________ ___ ______.
______ __________ __________ _______ ___ _____ _____ ____ _________.
________ __________ ________ ___ __________ _________ ___ _______ _______ _________.
___ _________ ____ ________ ___.
__________ ____ __________ ________ _________ _________ ______ _______ ___.
________ _____ _______ __________ ____.
______ _________ _____ _____ ________.
_________ ___ ____ __________ _________ _____ _______ ______ ___ ______ ____ _____.
____ ______ ____ ______ ____ ____ ________ ______ __________.
______ _______ _________ ___ ___ ____.
________ ____ _______ _______ ____ __________ ___.
IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
Top
📞
Call Support Instant phone assistance IGNOU MCSL 228 2026 - Write a Python Program to implement Depth First Search. | IGNOU Academy
→
🟢
WhatsApp Chat Fast live messaging
→
Email Us Business enquiries & support
→