├── README.md ├── bfs.py ├── nq.py └── jug.py /README.md: -------------------------------------------------------------------------------- 1 | # AI-LAB -------------------------------------------------------------------------------- /bfs.py: -------------------------------------------------------------------------------- 1 | graph={ 2 | 'P':['Q','R','S'], 3 | 'Q':['P','R'], 4 | 'R':['P','Q','T'], 5 | 'T':['R'], 6 | 'S':['P'], 7 | } 8 | 9 | visited=[] 10 | queue=[] 11 | def bfs(visited,graph,node): 12 | visited.append(node) 13 | queue.append(node) 14 | while queue: 15 | m=queue.pop(0) 16 | print(m,end=" ") 17 | for neighbour in graph[m]: 18 | if neighbour not in visited: 19 | visited.append(neighbour) 20 | queue.append(neighbour) 21 | print("following is the breadth first search") 22 | bfs(visited,graph,'P') 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /nq.py: -------------------------------------------------------------------------------- 1 | class solution: 2 | def __init__(self): 3 | self.MAX = 20 4 | self.A = [0]*self.MAX 5 | 6 | def placment(self,i,j): 7 | for k in range(1,i): 8 | if(self.A[k] == j) or abs(self.A[k] - j) == abs(k - i): 9 | return False 10 | print(self.A) 11 | return True 12 | def printplacedqueen(self,N): 13 | print('Arrangment---->') 14 | print() 15 | 16 | for i in range(1,N+1): 17 | for j in range(1,N+1): 18 | if self.A[i] != j: 19 | print('\t_',end =' ') 20 | else: 21 | print('\tQ',end=' ') 22 | 23 | print() 24 | print() 25 | 26 | def N_Queens(self,i,j): 27 | for k in range(1,N+1): 28 | if self .placment(i,k): 29 | self.A[i] = k 30 | if i == N: 31 | self.printplacedqueen(N) 32 | else: 33 | self.N_Queens(i+1,N) 34 | 35 | 36 | 37 | 38 | N =int(input("enter the queens value:")) 39 | obj = solution() 40 | obj.N_Queens(1,N) 41 | -------------------------------------------------------------------------------- /jug.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import deque 3 | 4 | a = int(input("enter jug A capacity:")) 5 | b = int(input("enter jug B capacity:")) 6 | ai = int(input("intailly water in jug A:")) 7 | bi = int(input("intailly water in jug B:")) 8 | af = int(input("final state of jug A:")) 9 | bf = int(input("final state of jug B:")) 10 | 11 | if a<=0 or b<=0: 12 | print("jug capacities must be positive.") 13 | exit(1) 14 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 15 | print("negative values are not allowed.") 16 | exit(1) 17 | if ai==af and bi==bf: 18 | print(f"initial state is already the final state: juga{ai} and jugb={bi}") 19 | exit() 20 | 21 | def bfs_wjug(a, b, ai, bi, af, bf): 22 | visited = set() 23 | queue =deque([(ai, bi, [])]) 24 | 25 | while queue: 26 | curr_ai, curr_bi,operations = queue.popleft() 27 | 28 | if(curr_ai, curr_bi) in visited: 29 | continue 30 | visited.add((curr_ai, curr_bi)) 31 | 32 | if curr_ai == af and curr_bi == bf: 33 | for i,op in enumerate(operations): 34 | print(f"step{i+1}:{op}") 35 | print(f"final state reached: jug A = {curr_ai},jug B = {curr_bi}") 36 | return 37 | possible_operations = [ 38 | (a, curr_bi, "fill jug A"), 39 | (curr_ai, b, "fill jug B"), 40 | (0, curr_bi, "empty jug A"), 41 | (curr_ai, 0, "empty jug B"), 42 | (curr_ai - min(curr_ai, b - curr_bi),curr_bi + min(curr_ai, b - curr_bi),"pour from A to B"), 43 | (curr_ai + min(curr_bi, a - curr_ai),curr_bi - min(curr_bi, a - curr_ai),"pour from B to A"), 44 | ] 45 | for next_ai, next_bi,op in possible_operations: 46 | if (next_ai,next_bi) not in visited: 47 | queue.append((next_ai, next_bi, operations + [op])) 48 | 49 | print("no solution found.") 50 | return 51 | gcd = math.gcd(a,b) 52 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 53 | bfs_wjug(a, b, ai, bi, af, bf) 54 | else: 55 | print("the final state is not achievable with the given capacities.") 56 | exit() 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | --------------------------------------------------------------------------------