You are on page 1of 4

1.

Implement A* Search algorithm

def aStarAlgo(start_node, stop_node):

open_set = set(start_node)
closed_set = set()
g = {} #store distance from starting node
parents = {}# parents contains an adjacency map of all nodes

#ditance of starting node from itself is zero


g[start_node] = 0
#start_node is root node i.e it has no parent nodes
#so start_node is set to its own parent node
parents[start_node] = start_node

while len(open_set) > 0:


n = None

#node with lowest f() is found


for v in open_set:
if n == None or g[v] + heuristic(v) < g[n] + heuristic(n):
n = v

if n == stop_node or Graph_nodes[n] == None:


pass
else:
for (m, weight) in get_neighbors(n):
#nodes 'm' not in first and last set are added to first
#n is set its parent
if m not in open_set and m not in closed_set:
open_set.add(m)
parents[m] = n
g[m] = g[n] + weight

#for each node m,compare its distance from start i.e g(m) to
the
#from start through n node
else:
if g[m] > g[n] + weight:
#update g(m)
g[m] = g[n] + weight
#change parent of m to n
parents[m] = n

#if m in closed set,remove and add to open


if m in closed_set:
closed_set.remove(m)
open_set.add(m)

if n == None:
print('Path does not exist!')
return None

# if the current node is the stop_node


# then we begin reconstructin the path from it to the start_node
if n == stop_node:
path = []

while parents[n] != n:
path.append(n)
n = parents[n]

path.append(start_node)

path.reverse()

print('Path found: {}'.format(path))


return path

# remove n from the open_list, and add it to closed_list


# because all of his neighbors were inspected
open_set.remove(n)
closed_set.add(n)

print('Path does not exist!')


return None

#define fuction to return neighbor and its distance


#from the passed node
def get_neighbors(v):
if v in Graph_nodes:
return Graph_nodes[v]
else:
return None
#for simplicity we ll consider heuristic distances given
#and this function returns heuristic distance for all nodes
def heuristic(n):
H_dist = {
'A': 10,
'B': 8,
'C': 5,
'D': 7,
'E': 3,
'F': 6,
'G': 5,
'H': 3,
'I': 1,
'J': 0
}

return H_dist[n]

#Describe your graph here


Graph_nodes = {
'A': [('B', 6), ('F', 3)],
'B': [('C', 3), ('D', 2)],
'C': [('D', 1), ('E', 5)],
'D': [('C', 1), ('E', 8)],
'E': [('I', 5), ('J', 5)],
'F': [('G', 1),('H', 7)] ,
'G': [('I', 3)],
'H': [('I', 2)],
'I': [('E', 5), ('J', 3)],
}
aStarAlgo('A', 'J')

Output:
Path found: ['A', 'F', 'G', 'I', 'J']

Functions:
1.set()- set() method is used to convert any of the iterable to sequence of
iterable elements with distinct elements
2.heuristic()-Heuristic function estimates how close a state is to the
goal.Admissibility of the heuristic function is given as: h(n) <= h*(n)
3.add()-The set add() method adds a given element to a set if the element is not
present in the set.
4.remove()-The remove() method takes a single element as an argument and removes it
from the set.

Algorithm:
We create two lists – Open List and close node (just like Dijkstra Algorithm)

// A* Search Algorithm
1. Initialize the open node
2. Initialize the close node
put the starting node on the open
list (you can leave its f at zero)

3. while the open node is not empty


a) find the node with the least f on
the open node, call it "q"

b) pop q off the open node

c) generate q's 8 successors and set their


parents to q

d) for each successor


i) if successor is the goal, stop search

ii) else, compute both g and h for successor


successor.g = q.g + distance between
successor and q
successor.h = distance from goal to
successor (This can be done using many
ways, we will discuss three heuristics-
Manhattan, Diagonal and Euclidean
Heuristics)

successor.f = successor.g + successor.h

iii) if a node with the same position as


successor is in the OPEN NODE which has a
lower f than successor, skip this successor

iV) if a node with the same position as


successor is in the close node which has
a lower f than successor, skip this successor
otherwise, add the node to the open node
end (for loop)
e) push q on the close node
end (while loop)

Theory:
A * algorithm is a searching algorithm that searches for the shortest path between
the initial and the final state. It is used in various applications,
such as maps.
In maps the A* algorithm is used to calculate the shortest distance between the
source (initial state) and the destination (final state).
A* algorithm has 3 parameters:
g : the cost of moving from the initial cell to the current cell. Basically, it is
the sum of all the cells that have been visited since leaving the first cell.
h : also known as the heuristic value, it is the estimated cost of moving from the
current cell to the final cell. The actual cost cannot be calculated until the
final cell is reached. Hence, h is the estimated cost. We must make sure that there
is never an over estimation of the cost.
f : it is the sum of g and h. So, f = g + h
The way that the algorithm makes its decisions is by taking the f-value into
account. The algorithm selects the smallest f-valued cell and moves to that cell.
This process continues until the algorithm reaches its goal cell.

You might also like