Professional Documents
Culture Documents
AIM: Write a program to implement best rst search for 8-puzzle problem.
THEORY:
Best First Search (BFS) is an informed search algorithm used in ar cial intelligence
to nd the shortest path from a star ng node to a goal node. It makes use of a
priority queue and heuris c search to e ciently explore the search space.
Two lists for tracking traversal: BFS maintains two lists during the search process -
the Open list and the CLOSED list. The Open list contains the nodes that are available
for traversal at the current stage, while the CLOSED list contains nodes that have
already been traversed.
Priori za on of nodes: BFS priori zes nodes in the Open list based on their
evalua on func on values. Nodes with lower evalua on func on values, indica ng
higher promise or desirability, are explored rst.
Overall, Best First Search is a powerful algorithm for solving various problems in
ar cial intelligence, including path nding, route op miza on, and decision making
in complex environments.
PSEUDO CODE:
Best-First-Search(Graph g, Node start) 1) Create an empty PriorityQueue
pq.insert(start)
3) Un l PriorityQueue is empty
Else
Foreach neighbor v of u
SOURCE CODE:
from queue import PriorityQueue v = 14
graph = [[] for i in range(v)]
break
for v, c in graph[u]:
print()
source = 0 target = 9
OUTPUT
fi
fi
EXPERIMENT 8
AIM: Write a program to implement A* Algorithm for 8- puzzle problem.
THEORY:
The A* algorithm is a popular path nding algorithm used in ar cial intelligence and
computer science. Its key feature is its ability to e ciently nd the shortest path
from a star ng node to a goal node by intelligently selec ng nodes to explore based
on a combina on of two scores: the heuris c (h-score) and the actual cost of
reaching the node from the start (g-score).
E cient node selec on: A* selects the next node to explore from the open list
based on its f-score, which is the sum of its h-score and g-score. The node with the
lowest f-score is chosen for explora on, ensuring that the algorithm priori zes nodes
that are likely to lead to the shortest path.
Tracking visited nodes: A* keeps track of visited nodes to avoid revisi ng them,
saving computa onal me and resources. Once a node is explored, it is added to the
closed list to prevent redundant explora on.
Path retracing: Each node in A* maintains a pointer to its parent node, allowing the
algorithm to retrace the shortest path from the goal node back to the start node
once the goal is reached. This allows for e cient path reconstruc on.
Overall, the A* algorithm is a powerful and widely used method for solving
path nding problems, o ering a balance between completeness and op mality by
e ciently exploring the search space while priori zing the most promising paths.
ALGORITHM:
1. Ini alize the open list 2. Ini alize the closed list
list (you can leave its f at zero) 3. while the open list is not empty
a) nd the node with the least f on the open list, call it "q"
ffi
ffi
fi
fi
ti
ti
ti
ti
ti
ti
ti
ti
ff
ti
fi
ti
ti
ffi
ti
ti
ffi
ti
fi
ti
fi
ti
ti
ti
ti
b) pop q o the open list
successor.h = distance from goal to successor (This can be done using many ways, we
will discuss three heuris cs- Manha an, Diagonal and Euclidean Heuris cs)
successor is in the OPEN list which has a lower f than successor, skip this successor
iV) if a node with the same posi on as successor is in the CLOSED list which has a
lower f than successor, skip this successor otherwise, add the node to the open list
SOURCE CODE:
class Node:
def __init__(self, data, level, fval):
def generate_child(self):
x, y = self. nd(self.data, '_')
val_list = [[x, y - 1], [x, y + 1], [x - 1, y], [x + 1, y]] children = []
for i in val_list:
temp_puz = []
temp_puz = self.copy(puz)
ffl
fi
ff
ffl
ti
ti
tt
ti
ti
temp = temp_puz[x2][y2] temp_puz[x2][y2] = temp_puz[x1][y1] temp_puz[x1][y1] =
temp
return temp_puz
else:
return None
for i in root: t = []
for j in i: t.append(j)
return puz
def f(self, start, goal):
temp = 0
for i in range(0, self.n):
def process(self):
start = self.accept()
goal = self.accept()
fi
start = Node(start, 0, 0) start.fval = self.f(start, goal) self.open.append(start) while
True:
for j in i:
print(j, end=" ")
print("")
if self.h(cur.data, goal) == 0:
break
for i in cur.generate_child():
self.open.append(i)
self.closed.append(cur)
del self.open[0]
self.open.sort(key=lambda x: x.fval, reverse=False)
OUTPUT:
EXPERIMENT 9
AIM : Compare the di erent Search Algorithm.
THEORY:-
A search problem consists of:
• A State Space. Set of all possible states where you can be.
• A Goal State. A func on that looks at the current state returns whether or
not it is the goal state.
Types of search algorithms: There are far too many powerful search algorithms out
there to t in a single ar cle. Instead, this ar cle will discuss six of the fundamental
search algorithms, divided into two categories, as shown below.
UNINFORMED/BLIND SEARCH:-
The uninformed search does not contain any domain knowledge such as closeness,
the loca on of the goal. It operates in a brute-force way as it only includes
informa on about how to traverse the tree and how to iden fy leaf and goal nodes.
Uninformed search applies a way in which search tree is searched without any
informa on about the search space like ini al state operators and test for the goal,
so it is also called blind search.It examines each node of the tree un l it achieves the
goal node.
ti
ti
ti
fi
ti
ti
ff
ti
ti
ti
ti
ti
ti
It can be divided into ve main types:
1. Breadth- rst search
• 3) A fringe, which is a data structure used to store all the possible states
(nodes) that you can go from the current states.
INFORMED SEARCH:-
Informed search algorithms use domain knowledge. In an informed search, problem
informa on is available which can guide the search. Informed search strategies can
nd a solu on more e ciently than an uninformed search strategy. Informed search
is also called a Heuris c search.
A heuris c is a way which might not always be guaranteed for best solu ons but
guaranteed to nd a good solu on in reasonable me.
Informed search can solve much complex problem which could not be solved in
another way. An example of informed search algorithms is a traveling salesman
problem.
1. Greedy Search
2. A* Search
(Lesser the distance, closer the goal.) Different heuristics are used in
different informed
algorithms discussed below.
BREADTH-FIRST SEARCH:-
• 1) Breadth- rst search is the most common search strategy for traversing a
tree or graph.
This algorithm searches breadthwise in a tree or graph, so it is called breadth-
rst search.
• 2) BFS algorithm starts searching from the root node of the tree and expands
all successor node at the current level before moving to nodes of next level.
Advantages:
1. BFS will provide a solu on if any solu on exists.
2. If there are more than one solu ons for a given problem, then BFS will provide
the minimal solu on which requires the least number of steps.
Disadvantages:
1.It requires lots of memory since each level of the tree must be saved into memory
to expand the next level.
2.BFS needs lots of me if the solu on is far away from the root node.
Time Complexity: Time Complexity of BFS algorithm can be obtained by the number
of nodes traversed in BFS un l the shallowest Node. Where the d= depth of
shallowest solu on
Space Complexity: Space complexity of BFS algorithm is given by the Memory size of
fron er which is O(bd).
Completeness: BFS is complete, which means if the shallowest goal node is at some
nite depth, then BFS will nd a solu on.
Depth- rst search is a recursive algorithm for traversing a tree or graph data
structure.It is called the depth- rst search because it starts from the root node and
follows each path to its greatest depth node before moving to the next path.
DFS uses a stack data structure for its implementa on.The process of the DFS
algorithm is similar to the BFS algorithm.
2. It takes less me to reach to the goal node than BFS algorithm (if it traverses
in the right path).
Disadvantage:
1. There is the possibility that many states keep re-occurring, and there is no
guarantee of nding the solu on.
2. DFS algorithm goes for deep down searching and some me it may go to the
in nite loop.
Completeness: DFS search algorithm is complete within nite state space as it will
expand Time Complexity: Time complexity of DFS will be equivalent to the node
traversed by the Where, m= maximum depth of any node and this can be much
larger than d (Shallowest Space Complexity: DFS algorithm needs to store only single
path from the root node, hence Op mal: DFS search algorithm is non-op mal, as it
may generate a large number of steps or
• Hill climbing algorithm is a local search algorithm which con nuously moves in
the direc on of increasing eleva on/value to nd the peak of the mountain or
best solu on to the problem. It terminates when it reaches a peak value
where no neighbor has a higher value.
• It is also called greedy local search as it only looks to its good immediate
neighbour state
• In this algorithm, we don't need to maintain and handle the search tree or
graph as it only keeps a single current state.
fi
ti
ti
ti
ti
fi
ti
ti
ti
fi
fi
ti
ti
ti
ti
HILL CLIMBING ALGORITHM
• E ciency: Best First Search can be e cient in nding solu ons, especially in
problems with well-de ned heuris c func ons and rela vely small search
spaces.
• Op mality: When the heuris c func on is admissible (never overes mates
the actual cost), BFS guarantees nding an op mal solu on.
• Heuris c Guidance: It u lizes heuris c informa on to focus the search on
promising areas of the search space, reducing the explora on of unpromising
paths.
• Memory E ciency: BFS typically requires less memory compared to
uninformed search algorithms like breadth- rst search or depth- rst search.
•
Disadvantages of Best First Search Algorithm:
• Completeness: Best First Search is not guaranteed to nd a solu on if the
heuris c func on is not admissible or if the search space is in nite or too large.
• Greedy Nature: It may get stuck in local op ma or fail to explore the en re
search space thoroughly, especially in problems with mul ple peaks or
plateaus.
• Heuris c Quality: The quality of the solu on found by BFS heavily depends on
the quality of the heuris c func on, which may not always be easy to de ne
accurately.
• Complexity: In some cases, de ning an e ec ve heuris c func on that
balances between guiding the search and avoiding local op ma can be
challenging.
ffi
ti
ti
ti
ti
ti
ti
ti
ti
ffi
ti
ti
ti
fi
ti
ti
ti
ti
fi
fi
fi
ti
ti
ti
ti
ffi
ti
ti
ff
ti
ti
fi
ti
ti
ti
fi
ffi
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
fi
ti
fi
ti
fi
ti
A* ALGORITHM
Features of A* Algorithm: