Cs3491 - Aiml Lab Record
Cs3491 - Aiml Lab Record
Page MARKS
EX.NO DATE NAME OF THE EXPERIMENT No AWARDED REMARKS
Result:
Thus, the program for the BFS algorithm using python was implemented successfully.
Ex.No: 1b Implementation of Uninformed Search Algorithms – DFS
Date:
Aim :
To implement the DFS algorithm using python.
Algorithm:
1. Start by putting any one of the graph’s vertices on top of a stack.
2. Take the top item of the stack and add it to the visited list.
3. Create a list of that vertex’s adjacent nodes. Add the ones which aren’t in the visited
list to the top of the stack.
4. Keep repeating steps 2 and 3 until the stack is empty.
Program:
graph = { '1':
['2','5','3'],
'2':['6', '4'],
'5':['4'],
'3':['4','7'],
'6':[],
'4':[],
'7':[]
}
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,'1')
Output:
PSNA CET - CSE
PREPAREATION 30
PERORMANCE 30
RECORD 40
TOTAL 100
Result:
Thus, the program for the DFS algorithm using python was implemented successfully.
Ex.No: 2a Implementation of Informed Search Algorithm – A*
Date:
Aim :
To implement A* algorithm in python using search strategy.
Algorithm:
1. Define a Graph class that takes an adjacency list as input.
2. Implement a method called get_neighbors that returns the neighbors of a given node.
3. Implement a heuristic function h that returns the estimated cost from a given node to
the goal node.
4. Implement the A* algorithm by taking the start node and the stop node as input.
5. Once all neighbors of the current node have been examined, move the current node
from the open list to the closed list.
6. If the stop node is not found in the open list, then there is no path between the start and
the stop node, so return none.
Program:
from collections import deque
class Graph:
def init (self, adjacency_list):
self.adjacency_list = adjacency_list
def get_neighbors(self, v):
return self.adjacency_list[v]
def h(self, n):
H={
'A': 1,
'B': 1,
'C': 1,
'D': 1
}
return H[n]
def a_star_algorithm(self, start_node, stop_node):
open_list = set([start_node])
closed_list = set([])
g = {}
g[start_node] = 0
parents = {}
parents[start_node] = start_node
while len(open_list) > 0:
n = None
for v in open_list:
if n == None or g[v] + self.h(v) < g[n] + self.h(n):
n = v;
if n == None:
print('Path does not exist!')
return None
Output:
if n == stop_node:
reconst_path = []
while parents[n] != n:
reconst_path.append(n)
n = parents[n]
reconst_path.append(start_node)
reconst_path.reverse()
print('Path found: {}'.format(reconst_path))
return reconst_path
for (m, weight) in self.get_neighbors(n):
if m not in open_list and m not in closed_list:
open_list.add(m)
parents[m] = n
g[m] = g[n] + weight
else:
if g[m] > g[n] + weight:
g[m] = g[n] + weight
parents[m] = n
if m in closed_list:
closed_list.remove(m)
open_list.add(m)
open_list.remove(n)
closed_list.add(n)
print('Path does not exist!')
return None
adjacency_list = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)]
}
graph1 = Graph(adjacency_list)
graph1.a_star_algorithm('A', 'D')
PSNA CET - CSE
PREPAREATION 30
PERORMANCE 30
RECORD 40
TOTAL 100
Result:
Thus, the program has been implemented successfully.
Ex.No: 2b Implementation of Informed Search Algorithm –
Date: Memory Bounded A*
Aim :
To implement memory bounded A* algorithm in python using search strategy.
Algorithm:
1. Set the initial threshold to ne the heuristic estimate of the distance from the start node to
the goal node.
2. While the threshold has not reached infinity :
2.1. Call the iterative_deepening_a_star_rec with the current threshold value.
2.2. If the return value is negative, print solution was found.
2.3. Else update the threshold to be the return value.
3. If the loop finishes without finding a solution, return -1.
4. Define the function iterative_deepening_a_star_rec.
5. Return the minimum distance.
Program:
V={}
E={}
V=({'A':7,'B':9,'C':6,'D':5,'E':6,'F':4.5,'H':4,'I':2,'J':3,'K':3.5,'G':0})
E=({('B','D'):2,('A','B'):4,('A','C'):4,('A','D'):7,('D','E'):6,('E','F'):5,('D','F'):8,('D','H'):5,('H','I'):3,
('I','J'):3,('J','K'):3,('K','H'):3,('F','G'):5})
INFINITY=10000000
cameFrom={}
def h(node):
return V[node]
def cost(node, succ):
return E[node,succ]
def successors(node):
neighbours=[]
for item in E:
if node==item[0][0]: neighbours.append(item[1]
[0])
return neighbours
def reconstruct_path(cameFrom, current):
total_path = [current]
while current in cameFrom:
current = cameFrom[current]
total_path.append(current)
return total_path
Output:
def ida_star(root,goal):
global cameFrom
def search(node, g, bound):
min_node=None
global cameFrom
f = g + h(node)
if f > bound:return f
if node==goal:return "FOUND"
minn = INFINITY
for succ in successors(node):
t = search(succ, g + cost(node, succ), bound)
if t == "FOUND":return "FOUND"
if t < minn:
minn = t
min_node=succ
cameFrom[min_node]=node
return minn
bound= h(root)
count =1
while 1:
print ("itertion"+str(count))
count+=1
t = search(root, 0, bound)
if t == "FOUND":
print (reconstruct_path(cameFrom, goal))
return bound
if t == INFINITY:return "NOT_FOUND"
bound = t
print (ida_star('A','G'))
Result:
Hence the python program for memory bounded A* algorithm was implemented successfully.
Ex.No: 3 Implementation of Naïve Bayes Model
Date:
Aim :
To build a naïve bayes model using gaussian naïve bayes formula in python.
Algorithm:
1. Import the required math functions from the math module.
2. Define a function named separate_by_class that takes dataset as input.
3. Define a function named mean that takes in a list of numbers as input.
4. Calculate the sum of the numbers in the list using the sum function and get average.
5. Return the average as mean.
6. Define a function named stdev that takes a list of numbers to find mean.
7. Sum the squared difference calculated using list comprehension and divide by the length of
the list minus 1 to get the sample variance.
8. Return the standard deviation as the stdev.
9. Define a function summarize_dataset that takes in a dataset as its input.
10. Calculate the mean, standard deviation and count of each feature using the functions.
11. Return the list of summary tuples.
12. Calculate the prior probability for the class by dividing the count of rows for that class by
the total number of rows in the dataset.
Program:
from math import sqrt
from math import pi
from math import exp
def separate_by_class(dataset):
separated = dict()
for i in range(len(dataset)):
vector = dataset[i]
class_value = vector[-1]
if(class_value not in separated):
separated[class_value] = list()
separated[class_value].append(vector)
return separated
def mean(numbers):
return sum(numbers)/float(len(numbers))
def stdev(numbers):
avg = mean(numbers)
variance = sum([(x-avg)**2 for x in numbers])/float(len(numbers)-1)
return sqrt(variance)
def summarize_dataset(dataset):
summaries = [(mean(column),stdev(column),len(column)) for column in zip(*dataset)]
del(summaries[-1])
return summaries
Output:
def summarize_by_class(dataset):
separated = separate_by_class(dataset)
summaries = dict()
for class_value, rows in separated.items():
summaries[class_value] = summarize_dataset(rows)
return summaries
def calculate_probability(x, mean, stdev):
exponent = exp(-((x-mean)**2/(2*stdev**2)))
return (1/(sqrt(2*pi)*stdev))*exponent
def calculate_class_probabilities(summaries,row):
total_rows = sum([summaries[label][0][2] for label in summaries])
probabilities = dict()
for class_value, class_summaries in summaries.items():
probabilities[class_value] = summaries[class_value][0][2]/float(total_rows)
for i in range(len(class_summaries)):
mean,stdev,_ = class_summaries[i]
probabilities[class_value]*=calculate_probability(row[i], mean, stdev)
return probabilities
dataset = [[3.393533211,2.331273381,0],
[3.110073483,1.781539638,0],
[1.343808831,3.368360954,0],
[3.582294042,4.67917911, 0],
[2.280362439,2.866990263,0],
[7.423436942,4.696522875,1],
[5.745051997,3.533989803,1],
[9.172168622,2.511101045,1],
[7.792783481,3.424088941,1],
[7.939820817,0.791637231,1]]
summaries = summarize_by_class(dataset)
probabilities = calculate_class_probabilities(summaries, dataset[0])
print(probabilities)
Result:
Thus, we have built a naïve bayes model in python program.
Ex.No: 4 Implementation of Bayesian Network
Date:
Aim :
To implement python code for Bayes theorem network.
Algorithm:
1. Import necessary libraries.
2. Create an empty BayesianModel object.
3. Add nodes to the BayesianModel object.
4. Add edges between nodes.
5. Define the conditional probability distributions for each node.
6. Add the defined TabularCPDs to the BayesianModel object.
7. If the model is valid, print Model is correct.
Program:
from pgmpy.models import BayesianModel
bayesNet = BayesianModel()
bayesNet.add_node("M")
bayesNet.add_node("U")
bayesNet.add_node("R")
bayesNet.add_node("B")
bayesNet.add_node("S")
bayesNet.add_edge("M","R")
bayesNet.add_edge("U","R")
bayesNet.add_edge("B","R")
bayesNet.add_edge("B","S")
bayesNet.add_edge("R","S")
Result:
The program for bayes theorem network is implemented successfully.