0% found this document useful (0 votes)
10 views2 pages

AIML - 1.2 Yash

Uploaded by

Pranay Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

AIML - 1.2 Yash

Uploaded by

Pranay Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

DEPARTMENT OF

COMPUTER SCIENCE &


ENGINEERING

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.

3. Tools Used: IDLE

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:

print (node, end=' ')

Name:Yash Mandan UID:21BCS3996


DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
visited.add(node)
for neighbour in graph[node]:
dfs(visited, graph, neighbour)

print("Following is the Depth-First Search")


dfs(visited, graph, 'S')

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.

Name:Yash Mandan UID:21BCS3996

You might also like