0% found this document useful (0 votes)
80 views

Experiment-2: AIM: WAP To Implement Depth First Search and Breadth Search Algorithms. Theory

The document describes depth first search (DFS) and breadth first search (BFS) graph traversal algorithms. DFS uses a stack and recursively explores nodes along each branch as deep as possible before backtracking. BFS uses a queue and explores all neighbor nodes first before moving to the next level. The document provides pseudocode for implementing DFS and BFS in Python on a sample graph. The program outputs the node traversal order using both algorithms on the graph.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Experiment-2: AIM: WAP To Implement Depth First Search and Breadth Search Algorithms. Theory

The document describes depth first search (DFS) and breadth first search (BFS) graph traversal algorithms. DFS uses a stack and recursively explores nodes along each branch as deep as possible before backtracking. BFS uses a queue and explores all neighbor nodes first before moving to the next level. The document provides pseudocode for implementing DFS and BFS in Python on a sample graph. The program outputs the node traversal order using both algorithms on the graph.

Uploaded by

akshat sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT- 2

AIM: WAP to implement depth first search and breadth search algorithms.
Theory:
The Depth-First Search is a recursive algorithm that uses the concept of backtracking. It
involves thorough searches of all the nodes by going ahead if potential, else by backtracking.
Here, the word backtrack means once you are moving forward and there are not any more nodes
along the present path, you progress backward on an equivalent path to seek out nodes to
traverse. All the nodes are progressing to be visited on the current path until all the unvisited
nodes are traversed after which subsequent paths are going to be selected. The DSF algorithm
follows as:
1. We will start by putting any one of the graph's vertex on top of the stack.
2. After that take the top item of the stack and add it to the visited list of the vertex.
3. Next, create a list of that adjacent node of the vertex. Add the ones which aren't in the
visited list of vertexes to the top of the stack.
4. Lastly, keep repeating steps 2 and 3 until the stack is empty.
Breadth-First Search (BFS) is an algorithm used for traversing graphs or trees. Traversing
means visiting each node of the graph. Breadth-First Search is a recursive algorithm to search all
the vertices of a graph or a tree. BFS in python can be implemented by using data structures like
a dictionary and lists. Breadth-First Search in tree and graph is almost the same. The only
difference is that the graph may contain cycles, so we may traverse to the same node again.The
steps of the algorithm work as follow:
1. Start by putting any one of the graph’s vertices at the back of the queue.
2. Now take the front item of the queue and add it to the visited list.
3. Create a list of that vertex's adjacent nodes. Add those which are not within the visited
list to the rear of the queue.
4. Keep continuing steps two and three till the queue is empty.

Coding:
Depth first search:
graph = {

'5' : ['3','7'],

'3' : ['2', '4'],

'7' : ['8'],

'2' : [],
'4' : ['8'],

'8' : []

visited = set()

def dfs(visited, graph, node):

if node not in visited:

print (node)

visited.add(node)

for neighbour in graph[node]:

dfs(visited, graph, neighbour)

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

dfs(visited, graph, '5')

OUTPUT:

Breadth first Search:


graph = {

'5' : ['3','7'],

'3' : ['2', '4'],

'7' : ['8'],

'2' : [],

'4' : ['8'],

'8' : []

visited = []
queue = []

def bfs(visited, graph, node): #function for BFS

visited.append(node)

queue.append(node)

while queue:

m = queue.pop(0)

print (m, end = " ")

for neighbour in graph[m]:

if neighbour not in visited:

visited.append(neighbour)

queue.append(neighbour)

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

bfs(visited, graph, '5')

OUTPUT:

Result:
The program is successfully written and compiled in python.

You might also like