0% found this document useful (0 votes)
3 views7 pages

Ai 2 - 3

The document contains two practical programming assignments focused on implementing search algorithms for AI problems, specifically BFS for the 8-puzzle problem and DFS for the Water Jug problem. The BFS implementation uses a queue to explore states until it finds the goal configuration, while the DFS implementation recursively explores possible states until it finds a solution or exhausts all options. Both programs include functions for checking goal states, generating next states, and printing the solution path.

Uploaded by

devanshipatel514
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)
3 views7 pages

Ai 2 - 3

The document contains two practical programming assignments focused on implementing search algorithms for AI problems, specifically BFS for the 8-puzzle problem and DFS for the Water Jug problem. The BFS implementation uses a queue to explore states until it finds the goal configuration, while the DFS implementation recursively explores possible states until it finds a solution or exhausts all options. Both programs include functions for checking goal states, generating next states, and printing the solution path.

Uploaded by

devanshipatel514
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/ 7

AI(3161608) E.

R No:- 221240116065

Practical-2

Aim : Write a program to implement BFS (for 8 puzzle problem or Water Jug
problem or any AI search problem).

PROGRAM:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 3
#define MAX_QUEUE 100000

typedef struct {
int board[N][N];
int x, y; // Position of the empty tile
(0) int parent;
} State;

State queue[MAX_QUEUE];
int front = 0, rear = 0;

// Goal state
int goal[N][N] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 0}
};

// Moves: left, right, up, down


int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

// Check if two states are equal


int is_goal(State *s) {
return memcmp(s->board, goal, sizeof(goal)) == 0;
}

void print_board(int board[N][N])


{ for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
{ printf("%d ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
SPCE 1|Page
AI(3161608) E.R No:- 221240116065

// Check if a state has already been visited


int is_visited(State s, int count) {
for (int i = 0; i < count; i++) {
if (memcmp(queue[i].board, s.board, sizeof(s.board)) == 0)
{ return 1; // Already visited
}
}
return 0;
}

// Add state to queue


void enqueue(State s, int parent)
{ s.parent = parent;
queue[rear++] = s;
}

// BFS to solve the puzzle


void solve_puzzle(State initial)
{ enqueue(initial, -1);

int solution_path[MAX_QUEUE];
int path_len = 0;
int found = 0;

while (front < rear) {


State current = queue[front];

// If we reach the goal, backtrack to print the path


if (is_goal(&current)) {
found = 1;
int index = front;
while (index != -1) {
solution_path[path_len++] = index;
index = queue[index].parent;
}
break;
}

// Generate next states


for (int i = 0; i < 4; i++)
{
int new_x = current.x + dx[i];
int new_y = current.y + dy[i];

if (new_x >= 0 && new_x < N && new_y >= 0 && new_y < N)
{ State next = current;
next.x = new_x;
next.y = new_y;
// Swap empty tile with adjacent tile next.board[current.x]
[current.y] = next.board[new_x][new_y];

SPCE 2|Page
AI(3161608) E.R No:- 221240116065

next.board[new_x][new_y] = 0;

// Ensure the new state hasn't been visited before


if (!is_visited(next, rear)) {
enqueue(next, front);
}
}
}

front++;
}

if (found) {
for (int i = path_len - 1; i >= 0; i--) {
print_board(queue[solution_path[i]].board);
}
} else {
printf("No solution found.\n");
}
}

// Main function
int main() {
State initial = {
{{1, 2, 3}, {5, 6, 0}, {7, 8, 4}}, // Initial state
1, 2, // Position of 0
-1
};
solve_puzzle(initial);
return 0;
}

OUTPUT :

SPCE 3|Page
AI(3161608) E.R No:- 221240116065

SPCE 4|Page
AI (3161608) E.R No: - 221240116065

Practical-3
Aim: Write a program to implement DFS (for 8 puzzle problem or Water Jug
problem or any AI search problem).

PROGRAM: -

# Function to check if the current state meets the goal


def is_goal(state, target):
return target in state

# Function to generate all possible next states from the current state
def get_next_states(state, capacities):
next_states = []
jug1, jug2 = state
cap1, cap2 = capacities

# Fill jug1
next_states.append((cap1, jug2))
# Fill jug2
next_states.append((jug1, cap2))
# Empty jug1
next_states.append((0, jug2))
# Empty jug2
next_states.append((jug1, 0))
# Pour from jug1 to jug2
pour_to_jug2 = min(jug1, cap2 - jug2)
next_states.append((jug1 - pour_to_jug2, jug2 + pour_to_jug2))
# Pour from jug2 to jug1
pour_to_jug1 = min(jug2, cap1 - jug1)
next_states.append((jug1 + pour_to_jug1, jug2 - pour_to_jug1))

return next_states

# Depth-First Search (DFS) to solve the Water Jug problem


def dfs(current_state, capacities, target, visited, path):
# Check if the current state is the goal state
if is_goal(current_state, target):
return path + [current_state]

# Mark the state as visited


visited.add(current_state)

SPCE 13 | P a g e
AI (3161608) E.R No: - 221240116065

# Explore next possible states


for next_state in get_next_states(current_state, capacities):
if next_state not in visited:
result = dfs(next_state, capacities, target, visited, path +
[current_state])
if result:
return result
return None # Return None if no solution is found

# Function to start DFS from the initial state


def solve_water_jug(capacities, target):
visited = set() # To track visited states
# Start DFS from the initial state (0, 0)
solution = dfs((0, 0), capacities, target, visited, [])
return solution

# Example Input: Capacities of jugs and target


jug1_capacity = 4
jug2_capacity = 3
target_amount = 2

# Solving the Water Jug problem using DFS


solution = solve_water_jug((jug1_capacity, jug2_capacity), target_amount)

# Printing the solution path


if solution:
print("Solution found!")
for step in solution:
print(f"Jug1: {step[0]}L, Jug2: {step[1]}L")
else:
print("No solution exists!")

SPCE 14 | P a g e
AI (3161608) E.R No: - 221240116065

OUTPUT :

SPCE 15 | P a g e

You might also like