Ai Rohit 2
Ai Rohit 2
Ai Rohit 2
# Usage example
graph = Graph()
graph.add_edge('A', 'B')
graph.add_edge('A', 'C')
graph.add_edge('B', 'D')
graph.add_edge('B', 'E')
graph.add_edge('C', 'F')
print("BFS:", graph.bfs('A'))
print("DFS:", graph.dfs('A'))
2. Write a Program for the Best First Search and A* search algorithm.
class Graph:
def __init__(self):
self.graph = {}
if vertex in self.graph:
self.graph[vertex].append((edge, cost))
else:
visited = []
pq = PriorityQueue()
pq.put((0, start_vertex))
if vertex == goal_vertex:
visited.append(vertex)
break
visited.append(vertex)
pq.put((neighbor_cost, neighbor))
return visited
visited = []
pq = PriorityQueue()
pq.put((0 + heuristic[start_vertex], start_vertex))
if vertex == goal_vertex:
visited.append(vertex)
break
visited.append(vertex)
return visited
# Usage example
graph = Graph()
graph.add_edge('A', 'B', 5)
graph.add_edge('A', 'C', 3)
graph.add_edge('B', 'D', 2)
graph.add_edge('B', 'E', 4)
graph.add_edge('C', 'F', 6)
jug1 = 0
jug2 = 0
steps = []
if jug1 == 0:
jug1 = capacity1
steps.append((jug1, jug2))
jug2 = 0
steps.append((jug1, jug2))
else:
jug1 -= amount
jug2 += amount
steps.append((jug1, jug2))
return steps
# Usage example
capacity1 = 5
capacity2 = 3
target = 4
print(step)
4. Write a program to implement 4-Queen Problem.
for i in range(row):
if board[i] == col or \
return False
return True
def solve_n_queens(n):
def backtrack(row):
if row == n:
solutions.append(list(board))
else:
board[row] = col
backtrack(row + 1)
board = [-1] * n
solutions = []
backtrack(0)
return solutions
# Usage example
n=4
solutions = solve_n_queens(n)
print(solution)
5. Write a program for Text Classification for the given sentence using NLTK.
import nltk
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
# Prepare dataset
sentences = [
# Text preprocessing
stop_words = set(stopwords.words("english"))
lemmatizer = WordNetLemmatizer()
processed_sentences = []
# Tokenization
words = word_tokenize(sentence.lower())
# Stop word removal and lemmatization
processed_sentences.append(" ".join(processed_words))
# Feature extraction
vectorizer = TfidfVectorizer()
features = vectorizer.fit_transform(processed_sentences).toarray()
# Train a classifier
classifier = MultinomialNB()
classifier.fit(X_train, y_train)
predictions = classifier.predict(X_test)
print("Accuracy:", accuracy)
6. Write a program to implement hill climbing & steepest ascent hill climbing algorithm.
import random
def objective_function(x):
return x ** 2
current_solution = initial_solution
current_value = objective_function(current_solution)
while True:
best_neighbor_value = min(neighbor_values)
return current_solution
else:
best_neighbor_index = neighbor_values.index(best_neighbor_value)
current_solution = neighbors[best_neighbor_index]
current_value = best_neighbor_value
current_solution = initial_solution
current_value = objective_function(current_solution)
while True:
best_neighbor_value = min(neighbor_values)
return current_solution
best_neighbor_indices = [
best_neighbor_index = random.choice(best_neighbor_indices)
current_solution = neighbors[best_neighbor_index]
current_value = best_neighbor_value
else:
return current_solution
# Usage example
initial_solution = 3
optimal_solution_steepest_ascent = steepest_ascent_hill_climbing(initial_solution,
objective_function)
import sys
num_vertices = len(graph)
visited[start_vertex] = True
path = [start_vertex]
min_cost = sys.maxsize
nonlocal min_cost
if num_visited == num_vertices:
return
if not visited[next_vertex]:
visited[next_vertex] = True
path.append(next_vertex)
visited[next_vertex] = False
path.pop()
backtrack(start_vertex, 0, 1)
graph = [
start_vertex = 0
import numpy as np
class NeuralNetwork:
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.a1 = self.sigmoid(self.z1)
self.a2 = self.sigmoid(self.z2)
return self.a2
m = X.shape[0]
self.dz2 = self.a2 - y
output = self.forward(X)
self.backward(X, y, learning_rate)
if epoch % 1000 == 0:
loss = self.mean_squared_error(output, y)
return self.forward(X)
@staticmethod
def sigmoid(x):
return 1 / (1 + np.exp(-x))
@staticmethod
def sigmoid_derivative(x):
return x * (1 - x)
@staticmethod
input_size = X.shape[1]
hidden_size = 4
output_size = y.shape[1]
predictions = nn.predict(X)
print("Predictions:", predictions)
9. Write a program to implement Artificial Neural Network (ANN) for Classification using a
dataset.
import numpy as np
class NeuralNetwork:
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.a1 = self.sigmoid(self.z1)
self.a2 = self.softmax(self.z2)
return self.a2
m = X.shape[0]
self.dz2 = self.a2 - y
output = self.forward(X)
self.backward(X, y, learning_rate)
@staticmethod
def sigmoid(x):
return 1 / (1 + np.exp(-x))
@staticmethod
def sigmoid_derivative(x):
return x * (1 - x)
@staticmethod
def softmax(x):
exp_scores = np.exp(x)
# Prepare dataset
X = np.array([
[0, 0],
[0, 1],
[1, 0],
[1, 1]
])
y = np.array([
[1, 0],
[0, 1],
[0, 1],
[1, 0]
])
input_size = X.shape[1]
hidden_size = 4
output_size = y.shape[1]
predictions = ann.predict(X_test)
print("Predictions:", predictions)
10. Write a program to implement Genetic Algorithm for different types of gene representation.
We'll consider two types of gene representation: binary and integer.
First, let's define the basic structure and functions for the GA:
Code:
import random
class GeneticAlgorithm:
def __init__(self, population_size, gene_length, gene_representation):
self.population_size = population_size
self.gene_length = gene_length
self.gene_representation = gene_representation
self.population = []
def initialize_population(self):
# Initialize a random population
self.population = [self.generate_random_gene() for _ in
range(self.population_size)]
def generate_random_gene(self):
# Generate a random gene based on the gene representation
if self.gene_representation == "binary":
return [random.choice([0, 1]) for _ in range(self.gene_length)]
elif self.gene_representation == "integer":
return [random.randint(0, 9) for _ in range(self.gene_length)]
def select_parents(self):
# Select parents from the population based on their fitness scores
# Return a pair of selected parents
def evolve_population(self):
new_population = []
self.population = new_population
Now, let's provide an example usage of the GeneticAlgorithm class with binary and
integer gene representations:
Code:
ga_binary.evolve_population()
print()