0% found this document useful (0 votes)
10 views12 pages

Aashim Lab Report

Uploaded by

aashimbudhathoki
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)
10 views12 pages

Aashim Lab Report

Uploaded by

aashimbudhathoki
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/ 12

A

LAB REPORT
ON
ARITIFICIAL INTELLIGENCE

By
Aashim Budhathoki
Exam Roll No: 13048
BIM 5th Semester

Submitted to: Pradip Bhochibhoya


Department of Computer Science
Kantipur College of Management and Information Technology

In partial fulfillment of the requirements for the Course


Artificial Intelligence

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

from collections import deque

class Node:
def __init__(self, key):
self.key = key
self.children = []

def bfs_path(root, target):


if not root:
return None

queue = deque([(root, [root.key])])

while queue:
node, path = queue.popleft()

if node.key == target:
return path

for child in node.children:


queue.append((child, path + [child.key]))

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 = []

def dfs_path(node, target, path):


if not node:
return False

path.append(node.key)

if node.key == target:
return True

for child in node.children:


if dfs_path(child, target, path):
return True

path.pop()
return False

def find_path(root, target):


path = []
if dfs_path(root, target, path):
return path
else:
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 = 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 = []

def dls(node, target, depth):


if not node or depth < 0:
return False

if node.key == target:
return True

for child in node.children:


if dls(child, target, depth - 1):
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)}

def add_edge(self, u, v, weight):


self.graph[u].append((v, weight))

def greedy_best_first_search(self, start, goal):


visited = [False] * self.V
pq = PriorityQueue()
pq.put((0, start)) # (priority, vertex)

while not pq.empty():


cost, current = pq.get()
if current == goal:
print(f"Reached goal: {goal} with cost:
{cost}")
return

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}")

print("Goal not reachable")

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

def a_star_search(graph: dict, start: str, goal: str,


heuristic_values: dict) -> int:

open_list, closed_list = [(heuristic_values[start],


start)], set()

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))

return -1 # No path found

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

def tower_of_hanoi(n, source, auxiliary, target):


if n == 1:
print(f"Move disk 1 from {source} to {target}")
return
tower_of_hanoi(n - 1, source, target, auxiliary)
print(f"Move disk {n} from {source} to {target}")
tower_of_hanoi(n - 1, auxiliary, source, target)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy