AI Dynamic
AI Dynamic
BFS
while queue:
node = queue.popleft()
if node not in visited:
print(node)
visited.add(node)
if node == goal:
return True
for neighbour in graph[node]:
if neighbour not in visited:
queue.append(neighbour)
return False
def create_graph():
graph = {}
num_edges = int(input("Enter the number of edges: "))
for _ in range(num_edges):
src, dest = input("Enter source and destination node separated by space:
").split()
if src not in graph:
graph[src] = []
if dest not in graph:
graph[dest] = []
graph[src].append(dest)
return graph
def get_goal():
return input("Enter the goal node: ")
def main():
graph = create_graph()
start = 'A' # Assuming start node is 'A'
goal = get_goal()
print("Following is the Breadth First Search:")
bfs(graph, start, goal)
if __name__ == "__main__":
main()
DFS
def create_graph():
graph = {}
num_edges = int(input("Enter the number of edges: "))
for _ in range(num_edges):
src, dest = input("Enter source and destination node separated by space:
").split()
if src not in graph:
graph[src] = []
if dest not in graph:
graph[dest] = []
graph[src].append(dest)
return graph
def get_goal():
return input("Enter the goal node: ")
def main():
graph = create_graph()
goal = get_goal()
visited = set()
print("Following is the Depth First Search:")
dfs(visited, graph, 'A', goal) # Pass 'goal' here
if __name__ == "__main__":
main()
2. A-star
import heapq
class Node:
def __init__(self, state, parent=None, g=0, h=0):
self.state = state
self.parent = parent
self.g = g # Cost from start node to current node
self.h = h # Heuristic estimate from current node to goal node
def f(self):
return self.g + self.h # Total estimated cost of the cheapest solution
through this node
if current_node.state == goal:
path = []
while current_node:
path.append(current_node.state)
current_node = current_node.parent
return path[::-1] # Return reversed path
closed_set.add(current_node.state)
def get_neighbors(state):
# Function to get neighbors of a state in the game
pass # Implement according to the game's rules
# Example usage:
start_state = ...
goal_state = ...
start_node = Node(state=start_state)
path = astar(start_node, goal_state, heuristic)
if path:
print("Path found:", path)
else:
print("No path found")
3.prims
INF = 99999999
V = 6
G = [
[0, 1, 9, 0, 0, 0],
[1, 0, 8, 2, 7, 0],
[9, 8, 0, 3, 0, 0],
[0, 2, 3, 0, 4, 6],
[0, 7, 0, 4, 0, 5],
[0, 0, 0, 6, 5, 0]
]
selected = [0, 0, 0, 0, 0, 0]
no_edge = 0
selected[0]=True
4.(a) n-queen
class NQueens:
def __init__(self, n):
self.n = n
self.board = [-1] * n
def print_solution(self):
for row in range(self.n):
line = ['Q' if col == self.board[row] else '.' for col in
range(self.n)]
print(' '.join(line))
# Example usage:
n = 8
queens = NQueens(n)
print("Backtracking Solution:")
if queens.solve_backtracking():
queens.print_solution()
else:
print("No solution found")
class GraphColoring:
def __init__(self, graph, num_colors):
self.graph = graph
self.num_colors = num_colors
self.colors = [0] * len(graph)
def print_solution(self):
for i, color in enumerate(self.colors):
print(f"Vertex {i}: Color {color}")
# Example usage:
graph = {
0: [1, 2, 3],
1: [0, 2],
2: [0, 1, 3],
3: [0, 2]
}
num_colors = 3
import random
# Predefined responses
greetings = ["Hello!", "Hi there!", "Hey!", "Greetings!"]
options = ["How can I assist you today?", "What can I do for you?", "How may I help
you?"]
farewells = ["Goodbye!", "See you later!", "Until next time!", "Have a great day!"]
cc 3.oldaccount
update oldAccounts;
}
}
OlderAccountsUtility.updateolderAccounts();