AIML - 1.2 Yash
AIML - 1.2 Yash
Experiment 1.2
Student Name: Yash Mandan UID: 21BCS3996
Branch: CSE Section/Group: 21BCS_IOT-621A
Semester: 5th Date of Performance: 24/08/23
Subject Name: AIML Subject Code: 21CSH-316
1. Aim: Implement the DFS algorithm and analyze its performance and characteristics.
2. Objective: The objective of this experiment is to implement the Depth-First Search (DFS)
algorithm and analyze its performance and characteristics.
4. Source Code:
graph={
'S':['A', 'B'],
'A':['S', 'C', 'D'],
'C':['A'],
'D':['A'],
'B':['S','E', 'F'],
'E': ['B', 'H'],
'F':['B','I','G'],
'H':['E'],
'I':['F'],
'G':['F'] } visited = set() # Set to keep track of visited nodes
of graph.
def dfs(visited, graph, node): #function for dfs
if node not in visited:
5. Output:
6. Observations:
The depth-first search (DFS) is used to search all the vertices of a tree data structure or a
graph. The DFS algorithm starts with the initial node of graph G and goes deeper until we
find the goal node or the node with no children.
DFS is recursive in nature, so stack data structure is used to implement the DFS
algorithm.
The time complexity of DFS algorithm is O(V + E), where V is the number of vertices
and E is the number of edges in the graph.
The Space complexity of DFS algorithm is O(V + E).
DFS is complete if the search tree is finite, meaning DFS will come up with a
solution if it exists.