Untitled Document-3
Untitled Document-3
Aim : 1A. Implement the Breadth First Search algorithm to solve a given problem.
CODE :
import queue as Q
from RMP import dict_gn
start='Arad'
goal='Bucharest'
result=''
def main():
cityq=Q.Queue()
visitedq=Q.Queue()
BFS(start, cityq, visitedq)
print("BFS Traversal from ",start," to ",goal," is: ")
print(result)
Page 2 of 22
main()
OUTPUT :
Aim : 1B. Implement the Iterative Depth First Search algorithm to solve the same
problem.
CODE :
import queue as Q
from RMP import dict_gn
start='Arad'
goal='Bucharest'
result=''
Page 3 of 22
found=DLS(eachcity, visitedstack, startlimit+1, endlimit)
if found:
return found
def main():
visitedstack=[]
IDDFS(start, visitedstack, 9)
print("IDDFS Traversal from ",start," to ", goal," is: ")
print(result)
main()
OUTPUT :
Page 4 of 22
PRACTICAL NO : 2
Aim : 2A. Implement the A* Search algorithm for solving a pathfinding problem.
CODE :
import queue as Q
from RMP import dict_gn
from RMP import dict_hn
start='Arad'
goal='Bucharest'
result=''
def get_fn(citystr):
cities=citystr.split(" , ")
hn=gn=0
for ctr in range(0, len(cities)-1):
gn=gn+dict_gn[cities[ctr]][cities[ctr+1]]
hn=dict_hn[cities[len(cities)-1]]
return(hn+gn)
def expand(cityq):
global result
tot, citystr, thiscity=cityq.get()
if thiscity==goal:
result=citystr+" : : "+str(tot)
return
for cty in dict_gn[thiscity]:
cityq.put((get_fn(citystr+" , "+cty), citystr+" , "+cty, cty))
expand(cityq)
def main():
cityq=Q.PriorityQueue()
thiscity=start
cityq.put((get_fn(start),start,thiscity))
expand(cityq)
print("The A* path with the total is: ")
print(result)
Page 5 of 22
main()
OUTPUT :
Aim : 2B. Implement the Recursive Best-First Search algorithm for the same
problem.
CODE :
import queue as Q
from RMP import dict_gn
from RMP import dict_hn
start='Arad'
goal='Bucharest'
result=''
def get_fn(citystr):
cities=citystr.split(',')
hn=gn=0
for ctr in range(0,len(cities)-1):
gn=gn+dict_gn[cities[ctr]][cities[ctr+1]]
hn=dict_hn[cities[len(cities)-1]]
return(hn+gn)
def printout(cityq):
for i in range(0,cityq.qsize()):
Page 6 of 22
print(cityq.queue[i])
def expand(cityq):
global result
tot,citystr,thiscity=cityq.get()
nexttot=999
if not cityq.empty():
nexttot,nextcitystr,nextthiscity=cityq.queue[0]
if thiscity==goal and tot<nexttot:
result=citystr+'::'+str(tot)
return
print("Expanded city------------------------------",thiscity)
print("Second best f(n)------------------------------",nexttot)
tempq=Q.PriorityQueue()
def main():
cityq=Q.PriorityQueue()
thiscity=start
cityq.put((999,"NA","NA"))
cityq.put((get_fn(start),start,thiscity))
expand(cityq)
print(result)
main()
Page 7 of 22
OUTPUT :
Page 8 of 22
PRACTICAL NO : 3
Aim : Implement the Decision Tree Learning algorithm to build a decision tree for
a given dataset.
CODE:
Page 9 of 22
OUTPUT :
Page 10 of 22
Page 11 of 22
PRACTICAL NO : 4
CODE :
import numpy as np
class NeuralNetwork():
def __init__(self):
#seeding for random number generation
np.random.seed()
#x is output variable
def sigmoid(self, x):
#applying the sigmoid function
return 1/(1+np.exp(-x))
def sigmoid_derivative(self,x):
#computing derivative to the sigmoid function
return x*(1-x)
def train(self,training_inputs,training_outputs,training_iterations):
error=training_outputs-output
Page 12 of 22
self.synaptic_weights+=adjustments
def think(self,inputs):
#passing the inputs via the neuron to get output
#converting values to floats
inputs=inputs.astype(float)
output=self.sigmoid(np.dot(inputs,self.synaptic_weights))
return output
if __name__=="__main__":
#initializing the neuron class
neural_network=NeuralNetwork()
print("Beginning randomly generated weights: ")
print(neural_network.synaptic_weights)
Page 13 of 22
OUTPUT :
Page 14 of 22
PRACTICAL NO : 5
CODE :
Page 15 of 22
PRACTICAL NO : 6
Page 16 of 22
Page 17 of 22
PRACTICAL NO : 7
Aim : Implement the K-NN algorithm for classification or regression. Apply the
K-NN algorithm to a given dataset and predict the class or value for test data.
CODE :
Page 18 of 22
Page 19 of 22
Page 20 of 22
Page 21 of 22
Page 22 of 22