0% found this document useful (0 votes)
14 views20 pages

Aiml 1,2

The document contains multiple Python programs demonstrating graph traversal algorithms, including Depth First Search (DFS), Breadth First Search (BFS), and the A* informed search algorithm. Each section outlines the aim, algorithm steps, program code, output, and results of the execution. Additionally, it includes a memory-bounded A* implementation with user input for goal state and depth limit.

Uploaded by

bhuvana19072005
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)
14 views20 pages

Aiml 1,2

The document contains multiple Python programs demonstrating graph traversal algorithms, including Depth First Search (DFS), Breadth First Search (BFS), and the A* informed search algorithm. Each section outlines the aim, algorithm steps, program code, output, and results of the execution. Additionally, it includes a memory-bounded A* implementation with user input for goal state and depth limit.

Uploaded by

bhuvana19072005
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/ 20

EXP NO:1(A)

DATE:
PROGRAM TO APPLY DEPTH FIRST SEARCH

AIM:

To write a python program to calculate depth first search.


ALGORITHM:

Step 1: start the program.

Step 2: create a graph of vertices and edges using a dictionary.

Step 3: Calculate the depth first search in respective function.

Step 4: Display the result.

Step 5: stop the program.


PROGRAM:

graph={'5':['3','7'],'3':['2','4'],'7':['8'],'2':[],'4':['8'],'8':[]}

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("depth first search")


dfs(visited,graph,’5’)
OUTPUT:

DEPTH FIRST SEARCH

RESULT:

Thus,the program was executed and output was verified.

EXP NO:1(B)
DATE:
PROGRAM TO APPLY BREADTH FIRST SEARCH

AIM:
To write a python program to calculate breadth first search.

ALGORITHM:

Step 1: start the program.


Step 2: Create a graph with vertices and edges using dictionary.
Step 3: Calculate the breadth first search using function.
Step 4: Display the result.
Step 5: Stop the program.

PROGRAM:

graph={'5':['3','7'],'3':['2','4'],'7':['8'],'2':[],'4':['8'],'8':[]}
visited=[]
queue=[]
def dfs(visited,graph,node):
visited.append(node)
queue.append(node)
while queue:
m=queue.pop()
print(m)
for neighbor in graph[m]:
if neighbor not in visited:
visited.append(neighbor)
queue.append(neighbor)
print(“following is the breadth first search”)
bfs(visited,graph,’5’)

OUTPUT:

breadth first search


5
3
7
2
4
8

RESULT:
Thus ,the program was executed and output was verified
successfully.
EXP NO:2(A)
DATE:
IMPLEMENTATION OF INFORMED SEARCH ALGORITHM
a)A* ALGORITHM

AIM:
To write a python program for implementation of informed search algorithm
using A* algorithm.

ALGORITHM:

tep1: Place the starting


node in the OPEN list.
Step 2: Check if the OPEN
list is empty or not, if the
list is empty then return
failure and stops.
Step 3: Select the node
from the OPEN list which
has the smallest value of
evaluation function (g+h),
if node n is goal
node then return success
and stop, otherwise
Step 4: Expand node n
and generate all of its
successors, and put n
into the closed list. For
each successor n', check
whether n' is already in the
OPEN or CLOSED list, if not
then compute evaluation
function for n' and place
into Open list.
Step 5: Else if node n' is
already in OPEN and
CLOSED, then it should be
attached to the back
pointer which reflects the
lowest g(n') value
tep1: Place the starting
node in the OPEN list.
Step 2: Check if the OPEN
list is empty or not, if the
list is empty then return
failure and stops.
Step 3: Select the node
from the OPEN list which
has the smallest value of
evaluation function (g+h),
if node n is goal
node then return success
and stop, otherwise
Step 4: Expand node n
and generate all of its
successors, and put n
into the closed list. For
each successor n', check
whether n' is already in the
OPEN or CLOSED list, if not
then compute evaluation
function for n' and place
into Open list.
Step 5: Else if node n' is
already in OPEN and
CLOSED, then it should be
attached to the back
pointer which reflects the
lowest g(n') value
tep1: Place the starting
node in the OPEN list.
Step 2: Check if the OPEN
list is empty or not, if the
list is empty then return
failure and stops.
Step 3: Select the node
from the OPEN list which
has the smallest value of
evaluation function (g+h),
if node n is goal
node then return success
and stop, otherwise
Step 4: Expand node n
and generate all of its
successors, and put n
into the closed list. For
each successor n', check
whether n' is already in the
OPEN or CLOSED list, if not
then compute evaluation
function for n' and place
into Open list.
Step 5: Else if node n' is
already in OPEN and
CLOSED, then it should be
attached to the back
pointer which reflects the
lowest g(n') value
tep1: Place the starting
node in the OPEN list.
Step 2: Check if the OPEN
list is empty or not, if the
list is empty then return
failure and stops.
Step 3: Select the node
from the OPEN list which
has the smallest value of
evaluation function (g+h),
if node n is goal
node then return success
and stop, otherwise
Step 4: Expand node n
and generate all of its
successors, and put n
into the closed list. For
each successor n', check
whether n' is already in the
OPEN or CLOSED list, if not
then compute evaluation
function for n' and place
into Open list.
Step 5: Else if node n' is
already in OPEN and
CLOSED, then it should be
attached to the back
pointer which reflects the
lowest g(n') value
Step1: Place the starting
node in the OPEN list.
Step 2: Check if the OPEN
list is empty or not, if the
list is empty then return
failure and stops.
Step 3: Select the node
from the OPEN list which
has the smallest value of
evaluation function (g+h),
if node n is goal
node then return success
and stop, otherwise
Step 4: Expand node n
and generate all of its
successors, and put n
into the closed list. For
each successor n', check
whether n' is already in the
OPEN or CLOSED list, if not
then compute evaluation
function for n' and place
into Open list.
Step 5: Else if node n' is
already in OPEN and
CLOSED, then it should be
attached to the back
pointer which reflects the
lowest g(n') value.
Step 6: Return to Step 2
step 1: Firstly, Place the starting node into OPEN and find its f (n) value.
step 2: Then remove the node from OPEN, having the smallest f (n) value. If it
is a goal node, then stop and return to success.
step 3: Else remove the node from OPEN, and find all its successors.
step 4: Find the f (n) value of all the successors, place them into OPEN, and
place the removed node into CLOSE.
step 5: Goto Step-2.
step 6: Exit.

PROGRAM:

from collections import deque

class Graph:

def __init__(self, adjac_lis):

self.adjac_lis = adjac_lis

def get_neighbors(self, v):

return self.adjac_lis[v]

def h(self, n):

H = {'A': 1, 'B': 1,'C': 1,'D': 1 }

return H[n]

def a_star_algorithm(self, start, stop):

open_lst = set([start])

closed_lst = set([])

poo = {}

poo[start] = 0

par = {}

par[start] = start

while len(open_lst) > 0:

n = None

for v in open_lst:

if n == None or poo[v] + self.h(v) < poo[n] + self.h(n):


n = v;

if n == None:

print('Path does not exist!')

return None

if n == stop:

reconst_path = []

while par[n] != n:

reconst_path.append(n)

n = par[n]

reconst_path.append(start)

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_lst and m not in closed_lst:

open_lst.add(m)

par[m] = n

poo[m] = poo[n] + weight

else:

if poo[m] > poo[n] + weight:

poo[m] = poo[n] + weight

par[m] = n
if m in closed_lst:

closed_lst.remove(m)

open_lst.add(m)

open_lst.remove(n)

closed_lst.add(n)

print('Path does not exist!')


return None
adjac_lis = {
'A': [('B', 1), ('C', 3), ('D', 7)],
'B': [('D', 5)],
'C': [('D', 12)]
}
graph1 = Graph(adjac_lis)
graph1.a_star_algorithm('A', 'D')

OUTPUT:

Path found: ['A', 'B', 'D']


RESULT:

Thus ,the program was executed and output was verified


successfully.

EXP NO:2(B)
DATE:
MEMORY BOUNDED – A*

AIM:

To write a python program to implement the memory bounded A*.


ALGORITHM:

Step 1: Start the program.

Step 2: get the graph as user input

Step 3: Create a function A with parameters(start,goal,path,level,maxD)

Step 4: get the depth limit as usert input.

Step 5: Check for the goal node amd return present or not.

Step 6: Call the function.


step 7: stop the program.
PROGRAM:

graph={‘A’:[‘B’,’C’},’B’:[‘D’,’E’],’C’:[‘F’,’G’],’D’:[‘H’,’J’],’E’:[‘J’,’K’],’F’:[‘L’,’M’],’G’:
[‘N’,’O’],’H’:[],’I’:[],’J’:[],’K’:[],’L’:[],’M’:[],’N’:[],’O’:[]}

def ma(start,goal,path,level,maxD):

print(“\n Current level”,level)

print(“goal node testing for”,start)

path.append(start)

if(start==goal)

print(“goal test successfully”).

return path

else:

print(“goal node test failed”)

if(level==maxD):

return False

print(“\n expanding the current node”,start)

for child in graph[start]:

if ma(child,goal,path,level+1,maxD):

return path

path.pop()

return False

start=’A’

goal=input(“enter the goal state:”)

maxD=int(input(“enter the max depth limit:”)


print()

path=list()

res=ma(start,goal,path,1,maxD)

if(res):

print(“path to goal node available”)

print(“path”,path)

else:

print(“No path available for the goal node in the given depth limit”)

OUTPUT:

Enter goal state:D


enter the max depth limit:4

current level test 1


goal node for testing:A
goal node failed

Expanding current node A


current level test 2
goal node for testing:B
goal node failed

Expanding current node B


current level test 3
goal node for testing:D
goal node test successful

path to goal node


path[‘A’,’B’,’D’]

RESULT:
Thus,the program was executed and output was verified successfully.

You might also like