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

Aiml Lab Manual Updated Exp1

The document outlines the implementation of uninformed search algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). It provides detailed algorithms for both methods, along with Python code examples demonstrating their execution on a graph. The results confirm the successful implementation and verification of the algorithms.

Uploaded by

c.muthupriya
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)
7 views3 pages

Aiml Lab Manual Updated Exp1

The document outlines the implementation of uninformed search algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). It provides detailed algorithms for both methods, along with Python code examples demonstrating their execution on a graph. The results confirm the successful implementation and verification of the algorithms.

Uploaded by

c.muthupriya
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/ 3

1.

Implementation of Uninformed search algorithms (BFS, DFS)

Aim:
To implement uninformed search algorithms such as BFS and DFS.

Algorithm(BFS):
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS
= 1) and set
their STATUS =
2 (waiting state)
[END OF LOOP]
Step 6: EXIT

Algorithm(DFS):
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbors of N that are in the ready state (whose
STATUS = 1) and set their STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT

Program(BFS):
from collections import defaultdict

class Graph:
def __init__(self):
self.graph = defaultdict(list)

def addEdge(self, u, v):


self.graph[u].append(v)

def BFS(self, s):


visited = [False] * len(self.graph)
queue = []
queue.append(s)
visited[s] = True

while queue:
s = queue.pop(0)
print(s, end=" ")

for i in self.graph[s]:
if not visited[i]:
queue.append(i)
visited[i] = True

# Create a graph and test BFS


g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)

print("Following is Breadth First Traversal (starting from vertex 2):")


g.BFS(2)

Output(BFS):

Following is Breadth First Traversal (starting from vertex 2)


2031

Program(DFS):
from collections import defaultdict
class Graph:
def __init__(self):
self.graph = defaultdict(list)
def addEdge(self, u, v):
self.graph[u].append(v)
def DFSUtil(self, v, visited):
visited.add(v)
print(v, end=' ')
for neighbour in self.graph[v]:
if neighbour not in visited:
self.DFSUtil(neighbour, visited)
def DFS(self, v):
visited = set()
self.DFSUtil(v, visited)
if __name__ == "__main__":
g = Graph()
g.addEdge(0, 1)
g.addEdge(0, 2)
g.addEdge(1, 2)
g.addEdge(2, 0)
g.addEdge(2, 3)
g.addEdge(3, 3)
print("Following is DFS from (starting from vertex 2)")
g.DFS(2)
Output(DFS):

Following is Depth First Traversal (starting from vertex 2) 2


013

Result:

Thus the uninformed search algorithms such as BFS and DFS have been executed
successfully and the output got verified.

You might also like