0% found this document useful (0 votes)
19 views3 pages

Aiml Exp 2 Adarsh Pandey

The document discusses implementing and analyzing the depth-first search algorithm. It includes pseudocode and an example of DFS to find a path from node A to F in a sample graph. The code implements DFS recursively to return the path if found.

Uploaded by

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

Aiml Exp 2 Adarsh Pandey

The document discusses implementing and analyzing the depth-first search algorithm. It includes pseudocode and an example of DFS to find a path from node A to F in a sample graph. The code implements DFS recursively to return the path if found.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment-1.2
Student Name: ADARSH PANDEY UID: 21BCS2027
Branch: CSE Section/Group: 906-A
Semester: 5TH Date of Performance: 17/8/23
Subject Name: AIML Subject Code: 21CSH-316

1. Aim: Implement the DFS algorithm and analyze its performance and
characteristics.

2. Objective: To understand the concept of DFS algorithm.

3. Pseudo Code:
function DFS(graph, current, goal, visited): if current equals
goal: return [current] # Path found, return the single-node
path

if current not in visited:


add current to visited

for each neighbor in graph[current]:


if neighbor not in visited:
path = DFS(graph, neighbor, goal, visited)
if path is not null:
add current to path
return path # Path found, return the complete path

return null # No path found

function DFS_Search(graph, start, goal):


visited = empty set
path = DFS(graph, start, goal, visited)
return path
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

# Example graph representation (dictionary adjacency list) graph


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

start_node = 'A' goal_node


= 'F'

path = DFS_Search(graph, start_node, goal_node)


print("DFS Path:", path)

4. Code:
def dfs(graph, start, goal):
stack = [(start, [start])]
visited = set()
while stack:
current, path = stack.pop()
if current == goal:
return path
if current not in visited:
visited.add(current)
for neighbor in graph[current]:
if neighbor not in visited:
stack.append((neighbor,
path + [neighbor]))

return None
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

'C': ['A', 'F'],


'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
start_node = 'A'
goal_node = 'F'
path = dfs(graph, start_node,
goal_node)
print("DFS Path:", path)
print("NAME: ADARSH PANDEY")
print("UID: 21BCS2027")

5. Output:

6. Learning Outcomes:
The learning outcomes of this experiments are:
• Understood how DFS can be used to find paths between nodes in a
graph.
• Learnt how DFS backtracks when exploring paths.
• Understood the concept of DFS.
• Ability to analyze algorithmic performance and optimize graph
traversal strategies.

You might also like