0% found this document useful (0 votes)
24 views6 pages

Ai Experiment PDF Final

Breadth First Search (BFS) traversal of a graph is implemented using a queue. The algorithm starts at a root node, marks it visited, and adds it to the queue. It then dequeues nodes from the front of the queue and enqueues their unvisited neighbors, marking the neighbors as visited. This process repeats until the queue is empty, ensuring all nodes at the same level are visited before moving to the next level. The Python code provided performs BFS on a sample graph, printing each visited node.

Uploaded by

AYUSH KUMAR
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)
24 views6 pages

Ai Experiment PDF Final

Breadth First Search (BFS) traversal of a graph is implemented using a queue. The algorithm starts at a root node, marks it visited, and adds it to the queue. It then dequeues nodes from the front of the queue and enqueues their unvisited neighbors, marking the neighbors as visited. This process repeats until the queue is empty, ensuring all nodes at the same level are visited before moving to the next level. The Python code provided performs BFS on a sample graph, printing each visited node.

Uploaded by

AYUSH KUMAR
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/ 6

EXPERIMENT #1

Problem Statement: To implement Breadth First Search Traversal


Objective: To implement Breadth First Search Traversal in Python to visit each
vertex while at the same time avoiding recurring cycles.
Input: Set of vertices of the graph
Algorithm:
create a queue Q
mark v as visited and put v into Q
while Q is non-empty
remove the head u of Q
mark and enqueue all (unvisited) neighbors of u

Code:
def bfs(graph,n):
queue=[n]
visited=[n]
while queue:
x=queue.pop(0)
print(x,end=" ")
for i in graph[x]:
if i not in visited:
queue.append(i)
visited.append(i)

graph = {
'5' : ['3','7'],
'3' : ['2', '4'],
'7' : ['8'],
'2' : [],
'4' : ['8'],
'8' : []
}
bfs(graph,'5')

Output:
EXPERIMENT #2

Problem Statement: Write a python program to implement Water Jug


Problem.

Objective: Given two water jugs with capacities X and Y litres. Initially, both
the jugs are empty. Also given that there is an infinite amount of water available.
The jugs do not have markings to measure smaller quantities.
One can perform the following operations on the jug:

• Fill any of the jugs completely with water.


• Pour water from one jug to the other until one of the jugs is either empty or full,
(X, Y) -> (X – d, Y + d)
• Empty any of the jugs

The task is to determine whether it is possible to measure Z litres of water using


both the jugs. And if true, print any of the possible ways.

Input: X = 5, Y = 7, Z = 4

Algorithm:

1. Def a func” pour” having 2 arguments “jug1, jug2”


2. Set max1, max2 and fill
3. if jug2 is fill, return
elif jug2 is max2, pour(0, jug1)
elif jug1 != 0 and jug2 is 0, pour(0, jug1)
elif jug1 is fill, pour(jug1, 0)
elif jug1 < max, pour(max1, jug2)
elif jug1 < (max2-jug2), pour(0, (jug1+jug2))
else: pour(jug1-(max2-jug2), (max2-jug2)+jug2)
4. pour(0, 0)

Code:

def pour(jug1, jug2):


max1, max2, fill = 5, 7, 4 #Change maximum capacity and final capacity
print("%d\t%d" % (jug1, jug2))
if jug2 is fill:
return
elif jug2 is max2:
pour(0, jug1)
elif jug1 != 0 and jug2 == 0:
pour(0, jug1)
elif jug1 is fill:
pour(jug1, 0)
elif jug1 < max1:
pour(max1, jug2)
elif jug1 < (max2-jug2):
pour(0, (jug1+jug2))
else:
pour(jug1-(max2-jug2), (max2-jug2)+jug2)

print("JUG1\tJUG2")
pour(0, 0)

OUTPUT:
EXPERIMENT 3
Problem Statement: Write a python program to remove punctuations from the
given string.

Objective: Removing punctuations from the given string. Input: A string from
user.

Algorithm:

1 First define a string of punctuations. punctuation = '''''!()-


[]{};:'"\,<>./?@#$%^&*_~'''
2. Then, iterate over the provided string using for loop.
for char in my_str

3. In each iteration, check if the character is a punctuation mark or not using the
membership test.
if char not in punctuation
4. Define an empty string to which add (concatenate) the character if it is not
punctuation.

no_punct = no_punct + cha


5. Display the cleaned up string

CODE:-

punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''

my_str = "Hello!!!, ahfsod f?W;/][][2352534234333."

no_punct = ""

for char in my_str:

if char not in punctuations:

no_punct = no_punct + char

print(no_punct).

OUTPUT:-
EXPERIMENT 4
Problem Statement: Write a python program to sort the sentence in alphabetical
order.

Objective: Sorting the sentence in alphabetical order.


Input: A string a= “hello this is an example with cased letters”. Algorithm:

• • Store the string to be sorted in my_str.


• • Using the split() method the string is converted into a list of words.
• • The split() method splits the string at whitespaces.
• • The list of words is then sorted using the sort() method, and all the
words are

displayed.

CODE:- my_str = "ahuhef khsoe ieh ew kiri weiwwei ssdg"

words = [word.lower() for word in my_str.split()]

words.sort()

print("The sorted words are:")

for word in words:

print(word,end=" ")

OUTPUT:-

You might also like