Ai - Digital Assignment
Ai - Digital Assignment
Ai - Digital Assignment
Tummala Rohith
21BBS0134
Code:
N=4
def print_solution(board):
for i in range(N):
for j in range(N):
print()
for i in range(col):
if board[row][i] == 1:
return False
if board[i][j] == 1:
return False
if board[i][j] == 1:
return False
return True
if col >= N:
return True
# Consider this column and try placing a queen in all rows one by one
for i in range(N):
if is_safe(board, i, col):
board[i][col] = 1
return True
# If placing the queen in board[i][col] doesn't lead to a solution, backtrack and remove the queen
board[i][col] = 0
# If the queen cannot be placed in any row in this column col, then return False
return False
def solve_n_queens():
if solve_n_queens_util(board, 0) == False:
print_solution(board)
return True
print("Tummala Rohith")
print("21BBS0134")
solve_n_queens()
Code:
def calculate_distance(path):
"""
"""
distance = 0
return distance
cities = list(graph.keys())
cities.remove(start)
shortest_path = None
shortest_distance = float('inf')
while queue:
if len(path) == len(graph):
distance = calculate_distance(path +
[start])
shortest_distance = distance
queue.append((neighbor, path +
[neighbor]))
graph = {
start_city = 'A'
start_city)
print("Rohith 21BBS0134")
import heapq
heuristic = 0
for i in range(3):
for j in range(3):
if state[i][j] != goal_state[i][j]:
x, y = divmod(goal_state[i][j] - 1, 3)
return heuristic
def find_possible_moves(state):
possible_moves = []
for i in range(3):
for j in range(3):
if state[i][j] == 0:
if i > 0:
if i < 2:
if j > 0:
return possible_moves
i, j = move[0], move[1]
x, y = get_zero_position(new_state)
def get_zero_position(state):
for i in range(3):
for j in range(3):
if state[i][j] == 0:
return i, j
# A* algorithm implementation
open_list = []
heapq.heapify(open_list)
visited_states = set()
g_scores = {initial_state: 0}
while open_list:
current_state = heapq.heappop(open_list)[1]
if current_state == goal_state:
path = []
while current_state:
path.append(current_state)
current_state = parents[current_state]
return path[::-1]
visited_states.add(current_state)
possible_moves = find_possible_moves(current_state)
g_score = g_scores[current_state] + 1
g_scores[new_state] = g_score
parents[new_state] = current_state
return None
# Test case
print("Rohith 21BBS0134")
solution = solve_8_puzzle(initial_state, goal_state)
if solution:
print("Solution Found!")
print(state)
else:
Code:
import heapq
def heuristic(node):
# You can modify this function to use a different heuristic for better performance
mst_cost = minimum_spanning_tree(graph)
return mst_cost[node]
mst = {}
queue = []
while queue:
if node in mst:
continue
mst[node] = cost
return mst
# A* algorithm
while open_set:
if len(path) == len(graph):
g = cost + edge_cost
h = heuristic(neighbor)
f=g+h
# Example usage
graph = {
start_node = 'A'
print("Tummala Rohith")
print("21BBS0134")
Code:
#include <iostream>
#include <vector>
#include <climits>
std::cout << "Edge between nodes (n1, n2) & weight: ";
return adj;
int source = 0;
std::vector<int> nodes;
if (i != source) nodes.push_back(i);
// If the node is unvisited and the distance to the node is less than the minimum distance,
minDist = graph[path.back()][nodes[j]];
nearest = nodes[j];
path.push_back(nearest);
int cost = 0;
// Print the path.
std::cout << "The cost of the path is: " << cost << std::endl;
int main() {
printGraph(graph, nodes);
TSP(graph, nodes);
return 0;
}
1.) AIM: A* PROBLEM:
Code: