0% found this document useful (0 votes)
9 views8 pages

Artificial Practical AMAN

The document contains implementations of various algorithms in Python, including Depth First Search (DFS), Breadth First Search (BFS), A* algorithm, Tower of Hanoi, and Hill Climbing. Each section provides a brief description of the algorithm followed by the corresponding code. The document is authored by Aman Prajapati and focuses on artificial intelligence techniques.

Uploaded by

fastflickfusion
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)
9 views8 pages

Artificial Practical AMAN

The document contains implementations of various algorithms in Python, including Depth First Search (DFS), Breadth First Search (BFS), A* algorithm, Tower of Hanoi, and Hill Climbing. Each section provides a brief description of the algorithm followed by the corresponding code. The document is authored by Aman Prajapati and focuses on artificial intelligence techniques.

Uploaded by

fastflickfusion
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/ 8

Name: - AMAN PRAJAPATI Artificial Intelligence

1. Write a program to implement depth first search algorithm.


graph1 = {'A':set(['B', 'C']),
'B':set(['A','D','E']),
'C':set(['A','F']),
'D':set(['B']),
'E':set(['B', 'F']),
'F':set(['C','E'])
}

def dfs(graph, node, visited):


if node not in visited:
visited.append(node)
for n in graph[node]:
dfs(graph, n, visited)
return visited
visited = dfs(graph1, 'A', [])
print(visited)

Output:
Name: - AMAN PRAJAPATI Artificial Intelligence

2. Write a program to implement breadth first search algorithm.


graph = {'A':set(['B', 'C']),
'B':set(['A','D','E']),
'C':set(['A','F']),
'D':set(['B']),
'E':set(['B', 'F']),
'F':set(['C','E'])
}

def bfs(start):
queue = [start]
levels = {}
levels[start] = 0
visited = set(start)
while queue:
node = queue.pop(0)
neighbours = graph[node]
for neighbor in neighbours:
if neighbor not in visited:
queue.append(neighbor)
visited.add(neighbor)
levels[neighbor] = levels[node]+1

print(levels)
return visited
print(str(bfs('A')))

def bfs_paths(graph, start, goal):


queue = [(start, [start])]
while queue:
(vertex, path) = queue.pop(0)
for next in graph[vertex] - set(path):
if next == goal:
yield path +[next]
else:
queue.append((next, path +[next]))

result = list(bfs_paths(graph, 'A', 'F'))


print(result)

def shortest_path(graph, start, goal):


Name: - AMAN PRAJAPATI Artificial Intelligence

try:
return next(bfs_paths(graph, start, goal))
except StopIteration:
return None
result1 = shortest_path(graph, 'A', 'F')
print(result)

Output:
Name: - AMAN PRAJAPATI Artificial Intelligence

3. Write a program to implement A* algorithm.


from simpleai.search import SearchProblem, astar

GOAL = 'HELLO WORLD'


class HelloProblem(SearchProblem):
def actions(self, state):
if len(state) < len(GOAL):
return list(' ABCDEFGHIJKLMNOPQRSTUVWXYZ')
else:
return []

def result(self, state, action):


return state + action

def is_goal(self, state):


return state == GOAL

def heuristic(self, state):


wrong = sum([1 if state[i] != GOAL[i] else 0
for i in range(len(state))])
missing = len(GOAL) - len(state)
return wrong + missing

problem = HelloProblem(initial_state='')
result = astar(problem)
print(result.state)
print(result.path())

Output:
Name: - AMAN PRAJAPATI Artificial Intelligence

4. Write a program to solve tower of Hanoi problem.


def moveTower(height, fromPole, toPole, withPole):
if height >=1:
moveTower(height-1, fromPole, withPole, toPole)
moveDisk(fromPole, toPole)
moveTower(height-1, withPole, toPole, fromPole)

def moveDisk(fp, tp):


print("moving disk from", fp,"to", tp)

moveTower(3, "A", "B", "C")

Output:
Name: - AMAN PRAJAPATI Artificial Intelligence

5. Write a program for Hill climbing problem.

import math

increment = 0.1
startingPoint = [1, 1]
point1 = [1, 5]
point2 = [6, 4]
point3 = [5, 2]
point4 = [2, 1]

def distance(x1, y1, x2, y2):


dist = math.pow(x2-x1, 2) + math.pow(y2-y1, 2)
return dist
def sumOfDistances(x1, y1, px1, py1, px2, py2, px3, py3, px4, py4):
d1 = distance(x1, y1, px1, py1)
d2 = distance(x1, y1, px2, py2)
d3 = distance(x1, y1, px3, py3)
d4 = distance(x1, y1, px4, py4)

return d1 + d2 + d3 + d4

def newDistance(x1, y1, point1, point2, point3, point4):


d1 = [x1, y1]
d1temp = sumOfDistances(x1, y1, point1[0], point1[1], point2[0],
point2[1], point3[0], point3[1], point4[0], point4[1])
d1.append(d1temp)
return d1

minDistance = sumOfDistances(startingPoint[0], startingPoint[1],


point1[0], point1[1], point2[0], point2[1], point3[0], point3[1], point4[0],
point4[1])
flag = True

def newPoints(minimum, d1, d2, d3, d4):


if d1[2] == minimum:
return [d1[0], d1[1]]
elif d2[2] == minimum:
return [d2[0], d2[1]]
elif d3[2] == minimum:
return [d3[0], d3[1]]
Name: - AMAN PRAJAPATI Artificial Intelligence

elif d4[2] == minimum:


return [d4[0], d4[1]]

i=1
while flag:
d1 = newDistance(startingPoint[0] +increment, startingPoint[1],
point1, point2, point3, point4)
d2 = newDistance(startingPoint[0] - increment, startingPoint[1],
point1, point2, point3, point4)
d3 = newDistance(startingPoint[0], startingPoint[1] + increment,
point1, point2, point3, point4)
d4 = newDistance(startingPoint[0], startingPoint[1] - increment,
point1, point2, point3, point4)
print(i,' ', round(startingPoint[0], 2), round(startingPoint[1],2))
minimum = min(d1[2], d2[2], d3[2], d4[2])
if minimum < minDistance:
startingPoint = newPoints(minimum, d1, d2, d3, d4)
minDistance = minimum
i+=1
else:
flag = False
Output:
Name: - AMAN PRAJAPATI Artificial Intelligence

You might also like