Aashim Lab Report
Aashim Lab Report
LAB REPORT
ON
ARITIFICIAL INTELLIGENCE
By
Aashim Budhathoki
Exam Roll No: 13048
BIM 5th Semester
Mid-Baneshwor, Kathmandu
August 2024
Contents
1 Write a Python program to implement Breadth First Search algorithm...........................3
1.1 Source Code.............................................................................................................3
1.2 Output.....................................................................................................................3
2 Write a Python program to implement Depth First Search algorithm..............................4
2.1 Source Code.............................................................................................................4
2.2 Output.....................................................................................................................4
3 Write a Python program to implement Depth Limit Search algorithm.............................5
3.1 Source Code.............................................................................................................5
3.2 Output.....................................................................................................................5
4 Write a Python program to implement Greedy Best First Search algorithm....................6
4.1 Source Code.............................................................................................................6
4.2 Output.....................................................................................................................7
5 Write a Python program that implements A* Search algorithm......................................8
5.1 Source Code.............................................................................................................8
5.2 Output.....................................................................................................................8
6 Write a Python program to implement Tower of Hanoi problem.....................................9
6.1 Source Code.............................................................................................................9
6.2 Output.....................................................................................................................9
7 Write an example program in PROLOG..........................................................................10
7.1 Source Code...........................................................................................................10
7.2 Output...................................................................................................................10
8 Write an example of relations in PROLOG......................................................................11
8.1 Source Code...........................................................................................................11
8.2 Output...................................................................................................................11
9 Write an example of recursion in PROLOG.....................................................................12
9.1 Source Code...........................................................................................................12
9.2 Output...................................................................................................................12
2
1 Write a Python program to implement Breadth First Search
algorithm
1.1 Source Code
class Node:
def __init__(self, key):
self.key = key
self.children = []
while queue:
node, path = queue.popleft()
if node.key == target:
return path
return None
root = Node(1)
root.children = [Node(2), Node(3), Node(4)]
root.children[0].children = [Node(5), Node(6)]
root.children[2].children = [Node(7)]
target = 7
path = bfs_path(root, target)
if path:
print("Path to the target node:", path)
else:
print("Target node not found in the tree.")
1.2 Output
3
2 Write a Python program to implement Depth First Search
algorithm
2.1 Source Code
class Node:
def __init__(self, key):
self.key = key
self.children = []
path.append(node.key)
if node.key == target:
return True
path.pop()
return False
root = Node(1)
root.children = [Node(2), Node(3), Node(4)]
root.children[0].children = [Node(5), Node(6)]
root.children[2].children = [Node(7)]
target = 7
path = find_path(root, target)
if path:
print("Path to the target node:", path)
else:
print("Target node not found in the tree.")
2.2 Output
4
3 Write a Python program to implement Depth Limit Search
algorithm
3.1 Source Code
class Node:
def __init__(self, key):
self.key = key
self.children = []
if node.key == target:
return True
return False
root = Node(1)
root.children = [Node(2), Node(3), Node(4)]
root.children[0].children = [Node(5), Node(6)]
root.children[2].children = [Node(7)]
target = 7
max_depth = 2
found = dls(root, target, max_depth)
if found:
print(f"Node {target} is reachable within depth
{max_depth}.")
else:
print(f"Node {target} is not reachable within depth
{max_depth}.")
3.2 Output
5
4 Write a Python program to implement Greedy Best First Search
algorithm
4.1 Source Code
from queue import PriorityQueue
class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = {i: [] for i in range(vertices)}
visited[current] = True
for neighbor, weight in self.graph[current]:
if not visited[neighbor]:
pq.put((weight, neighbor))
print(f"Visiting node {neighbor} with
priority {weight}")
g = Graph(5)
g.add_edge(0, 1, 2)
g.add_edge(0, 2, 3)
g.add_edge(1, 3, 4)
g.add_edge(2, 3, 1)
g.add_edge(3, 4, 2)
start = 0
goal = 4
print(f"Greedy Best-First Search from {start} to {goal}:")
g.greedy_best_first_search(start, goal)
6
4.2 Output
7
5 Write a Python program that implements A* Search algorithm
5.1 Source Code
from heapq import heappop, heappush
while open_list:
cost, node = heappop(open_list)
if node == goal:
return cost
if node in closed_list:
continue
closed_list.add(node)
cost -= heuristic_values[node]
for neighbor, edge_cost in graph[node]:
if neighbor in closed_list:
continue
neighbor_cost = cost + edge_cost +
heuristic_values[neighbor]
heappush(open_list, (neighbor_cost, neighbor))
example_graph = {
'S': [('A', 4), ('B', 10), ('C', 11)],
'A': [('B', 8), ('D', 5)],
'B': [('D', 15)],
'C': [('D', 8), ('E', 20), ('F', 2)],
'D': [('F', 1), ('I', 20), ('H', 16)],
'E': [('G', 19)],
'F': [('G', 13)],
'H': [('J', 2), ('I', 1)],
'I': [('K', 13), ('G', 5), ('J', 5)],
'J': [('K', 7)],
'K': [('G', 16)]}
heuristic_values = {'S': 7,'A': 8,'B': 6,'C': 5,'D': 5,'E':
3,'F': 3,'G': 0,'H': 7,'I': 4,'J': 5,'K': 3}
result = a_star_search(example_graph,'S','G',heuristic_values)
print(result)
5.2 Output
8
6 Write a Python program to implement Tower of Hanoi problem.
6.1 Source Code
n = 3 # Number of disks
tower_of_hanoi(n, 'A', 'B', 'C')
6.2 Output
9
7 Write an example program in PROLOG.
7.1 Source Code
valuable(gold).
7.2 Output
10
8 Write an example of relations in PROLOG
8.1 Source Code
female(prakriti).
female(prapti).
female(darshana).
female(menuka).
male(ritesh).
parent(ritesh,prakriti).
parent(ritesh,prapti).
parent(darshana,prakriti).
parent(menuka,prapti).
mother(X,Y):- parent(X,Y),female(X).
father(X,Y):- parent(X,Y),male(X).
sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y.
8.2 Output
11
9 Write an example of recursion in PROLOG
9.1 Source Code
factorial(0, 1).
factorial(N, F) :-
N > 0,
M is N - 1,
factorial(M, F1),
F is N * F1.
9.2 Output
12