Question
Write a Python Program to implement Depth First Search.
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'], ____ _______ __________ _______ ______ ____ ___ ______ ______.
____ __________ _______ ________ _____.
___ ________ _______ ________ __________ _____ _______ _________ ________ _________ _________.
________ _____ ____ __________ ____.
________ _________ _____ _______ ____ ___ __________ _______ ________ ____ ________.
__________ _______ ____ ________ ___ _______.
_______ _________ __________ ___ ___ _______ _______ _________ __________.
______ _________ _____ _______ ______ ______ _______.
______ ______ _________ ________ _______ _____ ____.
_________ ______ __________ _______ ___ _____ _________ _________ __________ _______.
___ _________ __________ ________ ______ _____ ________ _____ _______ __________ ________.
_________ __________ ___ _________ ______.
_________ ______ _____ __________ ____ ____ ______ ________ ______ ____.
_______ _______ ____ _____ _____ _______ ______.
__________ _________ __________ ________ _________ ______ _________ ______.
____ _______ ___ _____ __________ _________.
_________ ___ ________ _____ ______.
____ __________ ___ ____ ________ _____ _________ _______ _______ ________ ______ ___.
_____ ______ __________ _________ _________ ___.
________ _______ ___ ___ _______ _________ __________ ________ __________ ________.
_________ __________ _______ ______ ______ _______ __________ ____.
__________ ___ __________ ____ _________ ____ ______ ____ ____ _________ _________.
______ _________ ______ __________ _________ ___.
__________ _________ ____ _______ _______ ______ _________ ____ ___ ____ ____ ________.
_________ _____ ______ ______ ________ ____ _________ _________ ______ ________ _____ _____.
___ ___ ________ _____ ___ _______ ________ _________ _______ _________ __________ _______.
______ __________ ________ ______ ___ _________ _________ _________ ____ ___ ________ ______.
___ ___ ______ ________ ____ ________ __________ ________ _______.
_______ ______ ____ ________ _________ _______ ______.
_________ ________ ____ ____ ______ _____ ______ ____ _________ ______ __________ ____.
____ ________ _______ _____ _________ _________ _______ __________.
___ _________ ______ _______ _________ ____ _______.
________ ___ __________ _____ ______ __________ ___ _________ __________ _______ _______.
__________ _________ ________ _________ _______ ______.
_____ ___ _______ _____ ___ ______ _______.
_____ ____ ______ ________ __________ ____ ___ _____ _______ __________ _____.
_____ _____ ______ _____ ___.
__________ ___ ______ ________ _____ __________ ______ ______.
_________ _________ ____ ____ ________ ____ _________ ______ _______ _______ ______.
______ ___ _________ ____ _____ ___ _____ _________ ___.
___ _______ ________ __________ _______.
________ ___ ________ ______ _____ ______ __________ _________ _______.
__________ __________ _________ _________ _________ ____.
__________ _____ ____ __________ _________ _______ __________ ____ _______ ____.
_____ ____ __________ _________ _____ _________ __________ ____ _________ ________ _________.
_________ __________ _______ ________ ________ _____ ____ _____.
_______ ____ __________ ___ _____ ______ _______ _______ _______ _______.
_______ ____ ______ ________ _____ ____ _____.
___ ______ _____ ___ _____ __________ ________ ______ _____ ___ __________.
_______ ___ __________ __________ _________ _______ _______ __________ ______ ________ _________.
___ ___ _______ _____ ___ _____ __________ ___ ___ _________ __________.
________ _____ ________ _______ _____.
__________ _____ _________ __________ _____ ________ ____ __________ _____ ________ ____ ______.
______ _____ ______ _________ _________ ________ _______ _________.
________ __________ _____ _____ _______.
_______ _____ ____ ________ ________ ________ _______ __________ ____ __________ ____.
___ ____ _________ _______ ___ ________ ________ __________ ________ _______.
______ _______ _____ ____ _______ ________ _________ ______ ____ ______.
___ _____ __________ _________ _________ _________ _________.
________ ______ ________ ____ ___ _______ ______.
___ __________ __________ ____ __________ _________ ______ ____ ______ __________ _____.
_____ ______ _______ ___ _________ _______ ____ ______ __________.
_____ ______ _________ ______ ____ ________ _______ _____ _________ _______.
_____ ______ _________ ______ ______ ___ ______ __________ ___ _______ ________.
__________ ________ ____ __________ ______ _____ __________ ____ __________ ________ ______ __________.
________ ___ _________ __________ __________ ___ ______ _____ _________ ________ __________ ______.
______ _________ ________ ________ __________ ____.
______ _______ __________ ___ _______ _________ _____ _____.
_______ ________ _____ ________ ____ _______ ________ _________ _______.
_____ ________ ______ _____ ______ ____ ___ __________ _____ ______.
__________ _______ _________ ______ __________ _________ ________ ________ _____ ______ __________.
_____ _____ ____ ______ _________ ____ _________ ______ _________ ____ ___.
___ _________ _________ ____ ___ _________ _______ __________ ____ ______ ______.
___ ________ __________ ____ _______ _________ ______ ____ ___.
_________ _____ _______ ________ ______ _______.
___ ______ _____ ______ ____.
__________ _______ _________ _________ ____ _______ ___ ______.
__________ ______ _____ ______ _______.
Get Full Answer on WhatsApp
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'], ____ _______ __________ _______ ______ ____ ___ ______ ______.
____ __________ _______ ________ _____.
___ ________ _______ ________ __________ _____ _______ _________ ________ _________ _________.
________ _____ ____ __________ ____.
________ _________ _____ _______ ____ ___ __________ _______ ________ ____ ________.
__________ _______ ____ ________ ___ _______.
_______ _________ __________ ___ ___ _______ _______ _________ __________.
______ _________ _____ _______ ______ ______ _______.
______ ______ _________ ________ _______ _____ ____.
_________ ______ __________ _______ ___ _____ _________ _________ __________ _______.
___ _________ __________ ________ ______ _____ ________ _____ _______ __________ ________.
_________ __________ ___ _________ ______.
_________ ______ _____ __________ ____ ____ ______ ________ ______ ____.
_______ _______ ____ _____ _____ _______ ______.
__________ _________ __________ ________ _________ ______ _________ ______.
____ _______ ___ _____ __________ _________.
_________ ___ ________ _____ ______.
____ __________ ___ ____ ________ _____ _________ _______ _______ ________ ______ ___.
_____ ______ __________ _________ _________ ___.
________ _______ ___ ___ _______ _________ __________ ________ __________ ________.
_________ __________ _______ ______ ______ _______ __________ ____.
__________ ___ __________ ____ _________ ____ ______ ____ ____ _________ _________.
______ _________ ______ __________ _________ ___.
__________ _________ ____ _______ _______ ______ _________ ____ ___ ____ ____ ________.
_________ _____ ______ ______ ________ ____ _________ _________ ______ ________ _____ _____.
___ ___ ________ _____ ___ _______ ________ _________ _______ _________ __________ _______.
______ __________ ________ ______ ___ _________ _________ _________ ____ ___ ________ ______.
___ ___ ______ ________ ____ ________ __________ ________ _______.
_______ ______ ____ ________ _________ _______ ______.
_________ ________ ____ ____ ______ _____ ______ ____ _________ ______ __________ ____.
____ ________ _______ _____ _________ _________ _______ __________.
___ _________ ______ _______ _________ ____ _______.
________ ___ __________ _____ ______ __________ ___ _________ __________ _______ _______.
__________ _________ ________ _________ _______ ______.
_____ ___ _______ _____ ___ ______ _______.
_____ ____ ______ ________ __________ ____ ___ _____ _______ __________ _____.
_____ _____ ______ _____ ___.
__________ ___ ______ ________ _____ __________ ______ ______.
_________ _________ ____ ____ ________ ____ _________ ______ _______ _______ ______.
______ ___ _________ ____ _____ ___ _____ _________ ___.
___ _______ ________ __________ _______.
________ ___ ________ ______ _____ ______ __________ _________ _______.
__________ __________ _________ _________ _________ ____.
__________ _____ ____ __________ _________ _______ __________ ____ _______ ____.
_____ ____ __________ _________ _____ _________ __________ ____ _________ ________ _________.
_________ __________ _______ ________ ________ _____ ____ _____.
_______ ____ __________ ___ _____ ______ _______ _______ _______ _______.
_______ ____ ______ ________ _____ ____ _____.
___ ______ _____ ___ _____ __________ ________ ______ _____ ___ __________.
_______ ___ __________ __________ _________ _______ _______ __________ ______ ________ _________.
___ ___ _______ _____ ___ _____ __________ ___ ___ _________ __________.
________ _____ ________ _______ _____.
__________ _____ _________ __________ _____ ________ ____ __________ _____ ________ ____ ______.
______ _____ ______ _________ _________ ________ _______ _________.
________ __________ _____ _____ _______.
_______ _____ ____ ________ ________ ________ _______ __________ ____ __________ ____.
___ ____ _________ _______ ___ ________ ________ __________ ________ _______.
______ _______ _____ ____ _______ ________ _________ ______ ____ ______.
___ _____ __________ _________ _________ _________ _________.
________ ______ ________ ____ ___ _______ ______.
___ __________ __________ ____ __________ _________ ______ ____ ______ __________ _____.
_____ ______ _______ ___ _________ _______ ____ ______ __________.
_____ ______ _________ ______ ____ ________ _______ _____ _________ _______.
_____ ______ _________ ______ ______ ___ ______ __________ ___ _______ ________.
__________ ________ ____ __________ ______ _____ __________ ____ __________ ________ ______ __________.
________ ___ _________ __________ __________ ___ ______ _____ _________ ________ __________ ______.
______ _________ ________ ________ __________ ____.
______ _______ __________ ___ _______ _________ _____ _____.
_______ ________ _____ ________ ____ _______ ________ _________ _______.
_____ ________ ______ _____ ______ ____ ___ __________ _____ ______.
__________ _______ _________ ______ __________ _________ ________ ________ _____ ______ __________.
_____ _____ ____ ______ _________ ____ _________ ______ _________ ____ ___.
___ _________ _________ ____ ___ _________ _______ __________ ____ ______ ______.
___ ________ __________ ____ _______ _________ ______ ____ ___.
_________ _____ _______ ________ ______ _______.
___ ______ _____ ______ ____.
__________ _______ _________ _________ ____ _______ ___ ______.
__________ ______ _____ ______ _______.
Get Full Answer on WhatsApp
IGNOU NEWS
Assignment Submission Last Date Extended Till 30 June 2026 Click Here★★★IGNOU June 2026 TEE Date Sheet Released Click Here★★★