Ai Practical-2
Ai Practical-2
class Graph:
def init (self):
# Dictionary to store the graph as an adjacency list
self.graph = {}
while queue:
node = queue.popleft() # Dequeue a node from the front
if node not in visited:
visited.add(node)
bfs_order.append(node)
return bfs_order
while stack:
node = stack.pop() # Pop a node from the stack
if node not in visited:
visited.add(node)
dfs_order.append(node)
return dfs_order
# Example usage
if name == " main ":
g = Graph()
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
class Graph:
def init (self):
# Dictionary to store the graph as an adjacency list
self.graph = {}
bfs_order = []
while priority_queue:
cost, node = heapq.heappop(priority_queue)
bfs_order.append(node)
return bfs_order
# Example usage
if name == " main ":
g = Graph()
A* Algorithm
CODE
import heapq
class Graph:
def init (self):
# Dictionary to store the graph as an adjacency list
self.graph = {}
came_from[start_node] = None
A
Avijit Mohan 08617704423(MCA 3B)
while priority_queue:
current_f_score, current_node = heapq.heappop(priority_queue)
if current_node == target_node:
return self.reconstruct_path(came_from, current_node)
# Example usage
if name == " main ":
A
Avijit Mohan 08617704423(MCA 3B)
g = Graph()
visited = set()
visited.add((0, 0))
while queue:
A
Avijit Mohan 08617704423(MCA 3B)
a, b = queue.popleft()
path.append((a, b))
return True
if solve_n_queen(board, 0, N):
print("Solution for 4-Queen problem:")
print_board(board, N)
else:
print("No solution exists")
A
Avijit Mohan 08617704423(MCA 3B)
class Graph:
def init (self):
# Dictionary to store graph as an adjacency list
# Each node can have AND/OR branches with cost
self.graph = {}
while open_list:
current_node = open_list.pop()
children_info = self.graph.get(current_node)
if children_info is None:
status[current_node] = "SOLVED"
continue
children = children_info['children']
is_and_node = children_info['is_and']
cost = children_info['cost']
solution_graph[current_node] = selected_children
# Add children to open list for further exploration if they are not solved
for child in selected_children:
if status.get(child) != "SOLVED":
open_list.add(child)
return solution_graph
print("Solution Graph:")
for node in solution:
print(f"{node} -> {solution[node]}")
OUTPUT
A
Avijit Mohan 08617704423(MCA 3B)
Q6. Write a program to implement hill climbing & steepest ascent hill climbing algorithm.
Sol.
Hill climbing
CODE
import random
for i in range(max_iterations):
# Generate neighboring solutions
neighbor_solution = current_solution + random.uniform(-step_size, step_size)
neighbor_value = objective_function(neighbor_solution)
neighbors = []
for _ in range(num_neighbors):
# Generate a neighbor by making a small step
neighbor = solution + random.uniform(-step_size, step_size)
neighbors.append(neighbor)
return neighbors
for i in range(max_iterations):
# Generate neighbors of the current solution
neighbors = generate_neighbors(current_solution, step_size)
# If the best neighbor is better than the current solution, move to the best neighbor
if best_value > current_value:
current_solution = best_neighbor
current_value = best_value
A
Avijit Mohan 08617704423(MCA 3B)
OUTPUT
A
Avijit Mohan 08617704423(MCA 3B)
return total_distance
# Update if the current tour is shorter than the best found so far
if current_distance < shortest_distance:
shortest_tour = tour
shortest_distance = current_distance
OUTPUT