Search Algorithm Assignment !
Search Algorithm Assignment !
Search Algorithm Assignment !
graph = {
'A': ['B', 'C', 'D', 'F'],
'B': ['C', 'D'],
'C': ['F'],
'D': [],
'F': []
# 0:[],
# 1:[0,4],
# 2:[5],
# 3:[1,2,6],
# 4:[3],
# 5:[0],
# 6:[]
}
visited = set()
dfs(graph, v, visited)
return True
visited.add(v)
for neighbor in graph[v]:
if neighbor not in visited:
dfs(graph, neighbor, visited)
mother_vertex = None
for vertex in list(graph.keys()):
if is_mother_vertex(graph, vertex):
mother_vertex = vertex
break
Output -
The mother vertex is A
# Write a code to implement eight puzzle algorithm
import heapq
class PuzzleNode:
def __init__(self, state, parent=None, move=None, cost=0):
self.state = state
self.parent = parent
self.move = move
self.cost = cost
self.heuristic = self.calculate_heuristic()
def calculate_heuristic(self):
total_distance = 0
for i in range(3):
for j in range(3):
if self.state[i][j] != 0:
goal_row, goal_col = divmod(self.state[i][j] - 1, 3)
total_distance += abs(i - goal_row) + abs(j - goal_col)
return total_distance
def get_neighbors(node):
neighbors = []
empty_row, empty_col = find_empty_space(node.state)
return neighbors
def find_empty_space(state):
for i in range(3):
for j in range(3):
if state[i][j] == 0:
return i, j
def is_goal(state):
return state == ((1, 2, 3), (4, 5, 6), (7, 8, 0))
def solve_puzzle(initial_state):
initial_node = PuzzleNode(tuple(map(tuple, initial_state)))
priority_queue = [initial_node]
visited = set()
while priority_queue:
current_node = heapq.heappop(priority_queue)
if current_node.state in visited:
continue
visited.add(current_node.state)
if is_goal(current_node.state):
path = []
while current_node:
path.append(current_node.state)
current_node = current_node.parent
return reversed(path)
neighbors = get_neighbors(current_node)
for neighbor in neighbors:
heapq.heappush(priority_queue, neighbor)
return None
if solution:
print("Solution found:")
for step, state in enumerate(solution):
print(f"Step {step + 1}:")
for row in state:
print(row)
print()
else:
print("No solution found.")
Output -
Solution found:
Step 1:
(1, 2, 3)
(4, 0, 5)
(7, 8, 6)
Step 2:
(1, 2, 3)
(4, 5, 0)
(7, 8, 6)
Step 3:
(1, 2, 3)
(4, 5, 6)
(7, 8, 0)
# Write a code to implement types of hill climbing
import random
for _ in range(max_iterations):
neighbors = get_neighbors(current_state)
best_neighbor = max(neighbors, key=evaluate_fitness)
current_state = best_neighbor
return current_state
for _ in range(max_iterations):
neighbors = get_neighbors(current_state)
best_neighbor = max(neighbors, key=evaluate_fitness)
current_state = best_neighbor
return current_state
def get_neighbors(state):
return [state + 1, state - 1]
def evaluate_fitness(state):
return -abs(state - 5)
Output -
import random
import numpy as np
class TSPGeneticAlgorithm:
def __init__(self, num_cities, population_size, generations, mutation_rate):
self.num_cities = num_cities
self.population_size = population_size
self.generations = generations
self.mutation_rate = mutation_rate
self.distances = np.zeros((num_cities, num_cities))
self.generate_random_distances()
self.population = self.initialize_population()
def generate_random_distances(self):
for i in range(self.num_cities):
for j in range(i + 1, self.num_cities):
self.distances[i][j] = self.distances[j][i] = random.randint(1, 100)
def initialize_population(self):
return [np.random.permutation(self.num_cities) for _ in range(self.population_size)]
def evolve_population(self):
new_population = []
best_route = max(self.population, key=self.evaluate_fitness)
new_population.append(best_route)
while len(new_population) < self.population_size:
parent1, parent2 = random.sample(self.population, 2)
child = self.crossover(parent1, parent2)
self.mutate(child)
new_population.append(child)
self.population = new_population
def find_best_route(self):
for _ in range(self.generations):
self.evolve_population()
return max(self.population, key=self.evaluate_fitness)
num_cities = 10
population_size = 100
generations = 1000
mutation_rate = 0.01
Output -
Best Route: [8 7 5 9 6 2 4 3 1 0]
Best Fitness: 0.004016064257028112
# Web spidering / Scrolling
class Node:
def __init__(self, node_id, data_size):
self.node_id = node_id
self.data = bytearray(data_size)
def process_large_graph(nodes):
for node in nodes:
process_data(node.data)
def process_data(data):
pass
def main():
num_nodes = 5
data_size_per_node = 100 * 1024 * 1024
process_large_graph(nodes)
if __name__ == "__main__":
main()
# Perform Iterative_deepening on a graph
graph = {
1: [2, 3, 4],
2: [5, 6, 7],
3: [8],
4: [9, 10],
5: [11, 12],
6: [13],
7: [14],
8: [15],
9: [15, 16],
10: [17]
}
if depth_limit <= 0:
return False
visited.add(current)
path.append(current)
path.pop()
return False
start_node = 1
goal_node = 15
result = iterative_deepening_dfs(start_node, goal_node)
if result:
print(f"Path from {start_node} to {goal_node}: {result}")
else:
print(f"No path found from {start_node} to {goal_node} within depth limit.")
Output -