I Love Merge
I Love Merge
IT-306
Artificial Intelligence and Expert Systems
Department of Information Technology
BREADTH-FIRST SEARCH:
BFS is a graph traversal algorithm that
explores nodes level by level, moving to all
adjacent nodes before proceeding to the
next level. It uses a queue (FIFO - First In,
First Out) to keep track of nodes that need
to be explored.
ALGORITHM:
Initialize a queue and add the starting node.
Mark the starting node as visited.
While the queue is not empty:
Remove the front node from the
queue.
Process the node and add its
unvisited neighbours to the
queue.
Mark those neighbours as visited.
Repeat until all reachable nodes are visited.
DEPTH-FIRST SEARCH:
DFS is a graph traversal algorithm that DEPTH-FIRST SEARCH:
explores as far as possible along each
branch before backtracking. It uses a stack
(either explicit or recursion-based) to keep
track of nodes.
ALGORITHM:
Start from the given node and mark it as
visited.
CODE:
shortest path problems, while DFS goes
deep into one path before backtracking
using recursion or a stack, useful for tasks
like cycle detection and topological sorting.
EXPERIMENT-2
AIM: write a program to implement water
jug problem using BFS.
THEORY:
The Water Jug Problem is a well-known
problem in Artificial Intelligence and Graph
Theory, where the objective is to measure a
specific amount of water using two jugs of
fixed capacities. Given two jugs with
capacities X litters and Y Liters, the goal is
to obtain exactly Z litres, where Z is less
than or equal to the maximum capacity of
the two jugs. The problem can be solved
using a set of allowed operations: filling a
jug completely, emptying a jug completely,
and pouring water from one jug to another
OUTPUT: until either the first jug is empty or the
second jug is full. By systematically
exploring all possible states using the
Breadth-First Search (BFS) algorithm, the
shortest sequence of operations required to
CONCLUSION: achieve the target amount can be
Breadth-First Search (BFS) and Depth-First determined efficiently.
Search (DFS) are essential graph traversal
techniques with wide-ranging applications. ALGORITHM
BFS is suitable for shortest path problems, BREADTH FIRST SEARCH:
whereas DFS is useful in scenarios BFS is a graph traversal algorithm that
requiring backtracking and depth explores nodes level by level, moving to all
exploration. adjacent nodes before proceeding to the
next level. It uses a queue (FIFO - First In,
LEARNING: First Out) to keep track of nodes that need
we learned how to traverse graphs using to be explored.
Breadth-First Search (BFS) and Depth-First Initialize a queue and add the starting node.
Search (DFS). BFS explores nodes level by Mark the starting node as visited.
level using a queue, making it ideal for While the queue is not empty:
Remove the front node from the
queue.
Process the node and add its
unvisited neighbours to the queue.
Mark those neighbours as visited.
Repeat until all reachable nodes are visited.
FLOWCHART:
CODE:
CONCLUSION:
Using the BFS algorithm, we systematically
explore all valid jug operations and
determine the shortest sequence to obtain
the desired amount of water. The use of
BFS ensures that we reach the solution in
the most efficient way.
LEARNING:
we learned how to model problems using
state-space representation and solve them
efficiently using the Breadth-First Search
(BFS) algorithm. BFS ensures the shortest
sequence of operations while avoiding
OUTPUT: redundant calculations using a visited set.
This approach is widely applicable in
pathfinding, AI, and optimization problems,
demonstrating the power of graph traversal
techniques in real-world problem-solving.
DEPTH-FIRST SEARCH:
DFS is a graph traversal algorithm that
explores as far as possible along each
branch before backtracking. It uses a stack
(either explicit or recursion-based) to keep
track of nodes.
Start from the given node and mark it as
visited.
Visit an adjacent unvisited node, mark it as
visited, and recursively call DFS for that
node.
If no unvisited neighbors remain, backtrack
to the previous node.
Repeat until all nodes are visited.
FLOW-CHART:
EXPERIMENT-3
AIM: write a program to solve 8-puzzle
problem using DFS.
THEORY:
The 8-Puzzle Problem is a classic problem
in Artificial Intelligence that involves
arranging tiles in a 3×3 grid to match a
given goal state by sliding a blank tile (0) in
four possible directions: Up, Down, Left,
and Right. This problem is solved using the
Depth-First Search (DFS) algorithm,
which explores paths deeply before
backtracking. DFS uses a stack to store
states and a visited set to prevent cycles.
At each step, possible moves are generated
CODE:
by swapping the blank tile with adjacent
tiles, and the process continues until the
goal state is reached. While DFS can find a
solution, it is not always optimal due to its
depth-first nature.
ALGORITHM:
OUTPUT:
traversal and a visited set to prevent
redundant calculations. However, we also
observed that DFS does not guarantee the
shortest path, highlighting the need for
heuristic-based approaches like A* for more
efficient problem-solving in AI.
CONCLUSION:
The 8-Puzzle Problem was successfully
solved using the Depth-First Search (DFS)
algorithm, which explores possible moves in
a depth-first manner until the goal state is
reached. The use of a stack for state
exploration and a visited set to avoid
redundant calculations ensures efficient
traversal. However, DFS is not guaranteed
to find the shortest path and may explore
unnecessary states, making it less optimal
for larger problems.
LEARNING:
Through this experiment, we learned how to
solve the 8-Puzzle Problem using Depth-
First Search (DFS), which explores possible
moves in a depth-first manner. We
understood the use of a stack for state